I’ve setup my orangepi with XXX and now I’m trying to connect this display
The display turns on, but is all white and when I run the code shown below I see this
const sclk = new Gpio(11, ‘out’); // SPI clock pin
^TypeError: Gpio is not a constructor
at file:///home/orangepi/app/lcd.js:4:14
I haven’t run any displays or anything before, so I’m even sure really where to start. Can idea of what I should try next?
import pkg from 'orange-pi-gpio';
const { Gpio } = pkg;
const sclk = new Gpio(11, 'out'); // SPI clock pin
const mosi = new Gpio(10, 'out'); // SPI data pin
const cs = new Gpio(8, 'out'); // Chip select
const dc = new Gpio(25, 'out'); // Data/Command control
const rst = new Gpio(27, 'out'); // Reset
const bl = new Gpio(24, 'out'); // Backlight
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
(async () => {
try {
// Initialize the display
await rst.write(0);
await delay(100);
await rst.write(1);
await delay(100);
// Send initialization commands to the display
await sendCommand(0x01); // Software reset
await delay(150);
await sendCommand(0x11); // Sleep out
await delay(500);
await sendCommand(0x29); // Display on
// Display "Hello World"
await displayText("Hello World");
} catch (error) {
console.error('Error initializing display:', error);
}
})();
const sendCommand = async (command) => {
await dc.write(0); // Command mode
await cs.write(0); // Select the display
await spiWrite(command);
await cs.write(1); // Deselect the display
};
const sendData = async (data) => {
await dc.write(1); // Data mode
await cs.write(0); // Select the display
await spiWrite(data);
await cs.write(1); // Deselect the display
};
const spiWrite = async (data) => {
for (let i = 0; i < 8; i++) {
await sclk.write(0);
await mosi.write((data & 0x80) ? 1 : 0);
data <<= 1;
await sclk.write(1);
}
};
const displayText = async (text) => {
const font = {
'H': [0x7C, 0x12, 0x12, 0x7C], // Example font data for 'H'
'e': [0x38, 0x54, 0x54, 0x08], // Example font data for 'e'
'l': [0x00, 0x7E, 0x00, 0x00], // Example font data for 'l'
'o': [0x38, 0x44, 0x44, 0x38], // Example font data for 'o'
'W': [0x7C, 0x20, 0x10, 0x7C], // Example font data for 'W'
'r': [0x7C, 0x08, 0x04, 0x04], // Example font data for 'r'
'd': [0x38, 0x44, 0x44, 0x7C], // Example font data for 'd'
' ': [0x00, 0x00, 0x00, 0x00] // Example font data for ' '
};
for (const char of text) {
const charData = font[char];
if (charData) {
for (const byte of charData) {
await sendData(byte);
}
}
}
};