You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Orange Pi Debian11驱动1.4英寸LCD显示屏报错求助

问题描述

我在Orange Pi上安装了Debian 11(bullseye)系统,尝试连接1.44寸LCD HAT显示屏。显示屏能点亮但显示全白,运行代码时出现如下报错:

const sclk = new Gpio(11, 'out'); // SPI clock pin
^
TypeError: Gpio is not a constructor

at file:///home/orangepi/app/lcd.js:4:14

我之前没有显示屏驱动相关经验,不知道从何入手,请问下一步该尝试什么?

运行的代码

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);
      }
    }
  }
};

解决步骤

1. 修复orange-pi-gpio的导入问题

报错TypeError: Gpio is not a constructor核心是包的导入或安装有问题:

  • 先确认包已安装:终端执行npm list orange-pi-gpio,无输出则运行npm install orange-pi-gpio重新安装。
  • 调整导入方式:部分GPIO库的导出逻辑并非解构{ Gpio },改成以下两种方式测试:
    // ES模块方式
    import Gpio from 'orange-pi-gpio';
    // 或CommonJS方式(若Node.js用CommonJS模块)
    const Gpio = require('orange-pi-gpio');
    

2. 开启Orange Pi的SPI功能

这款LCD依赖SPI通信,默认可能未启用:

  • 安装armbian-config:sudo apt install armbian-config,运行后进入Hardware菜单,找到SPI0等SPI选项设置为启用,重启系统。
  • 验证SPI状态:重启后执行ls /dev/spi*,能看到/dev/spidev0.0类文件说明SPI已开启。

3. 先用官方Python驱动验证硬件

手动模拟SPI容易出时序问题,先拿官方驱动确认硬件正常:

  • 安装依赖:sudo apt install python3-pip python3-spidev python3-rpi.gpio
  • 克隆官方驱动库:git clone https://github.com/waveshare/1.44inch-LCD-HAT.git
  • 运行示例:cd 1.44inch-LCD-HAT/python/examples && python3 main.py
    如果官方代码能正常显示,说明硬件连接没问题,再回头调试Node.js代码。

4. 检查硬件接线

显示屏全白可能是初始化命令未发送成功,先确认接线:

  • 对照显示屏引脚定义,核对Orange Pi的SCLK、MOSI、CS、DC、RST、BL引脚是否对应正确。
  • 用万用表检测电源和背光引脚的电压,确保供电正常。

5. 改用专业SPI库替代手动模拟

手动位操作SPI时序容易出错,换成spi-device库优化代码:

  • 安装库:npm install spi-device
  • 改写核心通信逻辑示例:
    import spi from 'spi-device';
    import Gpio from 'orange-pi-gpio';
    
    const dc = new Gpio(25, 'out');
    const rst = new Gpio(27, 'out');
    const bl = new Gpio(24, 'out');
    
    // 打开SPI设备
    const display = spi.open(0, 0, (err) => {
      if (err) throw err;
      // 后续初始化和显示逻辑改用spi-device提供的方法发送数据/命令
    });
    

内容的提问来源于stack exchange,提问作者Justin Young

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 09:32:13