const { GPIO } = require('gpio');
const { SPI } = require('spi');
const ssr = new GPIO(0, OUTPUT);
const button = new GPIO(1, INPUT_PULLUP);
// pinMode(26, INPUT) // analogRead(26)
const csn = new GPIO(17, OUTPUT);
const dc = new GPIO(20, OUTPUT);
const rst = new GPIO(21, OUTPUT);
const spi0 = new SPI(0, {
[0x01, [], 150], // Software reset
// .[ 0xEF, [ 0x03, 0x80, 0x02 ] },
[0xCF, [0x00, 0xC1, 0x30]], // Pwr ctrl B
[0xED, [0x64, 0x03, 0x12, 0x81]], // # Pwr on seq. ctrl
[0xE8, [0x85, 0x00, 0x78]], // Driver timing ctrl A
[0xCB, [0x39, 0x2C, 0x00, 0x34, 0x02]], // Pwr ctrl A
[0xF7, [0x20]], // Pump ratio control
[0xEA, [0x00, 0x00]], // Driver timing ctrl B
[0xC0, [0x23]], // Pwr ctrl 1
[0xC1, [0x10]], // Pwr ctrl 2
[0xC5, [0x3E, 0x28]], // VCM control1
[0xC7, [0x86]], // VCM control2
[0x36, [0x80]], // Memory Access Control
[0x37, [0x00]], // Vertical scroll zero
[0x3A, [0x55]], // COLMOD: Pixel Format Set
[0xB1, [0x00, 0x18]], // Frame Rate Control (In Normal Mode/Full Colors)
[0xB6, [0x08, 0x82, 0x27]], // Display Function Control
[0xF2, [0x00]], // 3Gamma Function Disable
[0x26, [0x01]], // Gamma curve selected
// .[ 0xE0, [ 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00 ] ], // Set Gamma Positive Gamma Correction
// .[ 0xE1, [ 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F ] ], // Set Gamma Negative Gamma Correction
[0x11, [], 150], // Exit Sleep/Sleep Out
[0x29, [], 150], // Display
function sendCommand(cmd, data) {
spi0.send(new Uint8Array([cmd]));
function block(x0, y0, x1, y1, data) {
sendCommand(0x2A, [x0 >> 8, x0 & 0xFF, x1 >> 8, x1 & 0xFF]); // Column address set
sendCommand(0x2B, [y0 >> 8, y0 & 0xFF, y1 >> 8, y1 & 0xFF]); // Page address set
sendCommand(0x2C, data); // Memory write
function hardware_reset() {
for (var y = 0; y < height; y += 8) {
block(0, y, width, y + 8 - 1, new Uint8Array(width * 2 * 8).fill(color));
for (const init of initCommands) {
sendCommand(init[0], init[1]);
button.irq((pin, mode) => {
if (button.read() === LOW) {