我最近想在树莓派 3 上使用 pi4j v2 库的 Grove lcd rgb 显示器,操作系统:dietpi。 我遵循了所有 pi4j 文档(https://www.pi4j.com/about/new-in-v2/),我还遵循了 Grove 库的文档(有点过时)(https://dexterind) .github.io/GrovePi/)。尽管我进行了所有尝试,但我无法使用 pi4j 在 i2c 端口上写入。但是,当我启动 jar 时,控制台上没有出现任何错误,并且我的测试脚本告诉我正在写入它。 i2c 端口在 Dietpi 上启用,奇怪的是,我的测试在 python 中运行得很好。 你有遇到过类似的问题吗?
这是我的java示例测试: (注意:GrovePi4j 类是 Grove 库提供的预配置类。i2c 输入/输出是预配置的,与 python 示例中的相同。)
import java.io.IOException;
import java.util.Random;
import com.github.yafna.raspberry.grovepi.GroveDigitalOut;
import com.github.yafna.raspberry.grovepi.GrovePi;
import com.github.yafna.raspberry.grovepi.devices.GroveRgbLcd;
import com.github.yafna.raspberry.grovepi.pi4j.GrovePi4J;
import com.pi4j.Pi4J;
import com.pi4j.context.Context;
public class LcdService {
public void exec() throws IOException, InterruptedException {
Context pi4j = Pi4J.newAutoContext();
GrovePi grovePi = new GrovePi4J(pi4j);
GroveRgbLcd lcd = grovePi.getLCD();
String[] phrases = new String[]{
"Hi! ALL!",
"This should work just fine",
"A message\nA response",
"More data that you can handle for sure!",
"Welcome to the internet of things with java",
"Short\nMessage"
};
int[][] colors = new int[][]{
{50, 255, 30},
{15, 88, 245},
{248, 52, 100},
{48, 56, 190},
{178, 25, 180},
{210, 210, 210}
};
while (true) {
try {
String text = phrases[new Random().nextInt(phrases.length)];
int[] color = colors[new Random().nextInt(colors.length)];
lcd.setRGB(color[0], color[1], color[2]);
Thread.sleep(100);
lcd.setText(text);
Thread.sleep(2000);
} catch (IOException io) {
}
}
}
}
这是我的Python示例测试:
import time,sys
if sys.platform == 'uwp':
import winrt_smbus as smbus
bus = smbus.SMBus(1)
else:
import smbus
import RPi.GPIO as GPIO
rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
bus = smbus.SMBus(1)
else:
bus = smbus.SMBus(0)
# this device has two I2C addresses
DISPLAY_RGB_ADDR = 0x62
DISPLAY_TEXT_ADDR = 0x3e
# set backlight to (R,G,B) (values from 0..255 for each)
def setRGB(r,g,b):
bus.write_byte_data(DISPLAY_RGB_ADDR,0,0)
bus.write_byte_data(DISPLAY_RGB_ADDR,1,0)
bus.write_byte_data(DISPLAY_RGB_ADDR,0x08,0xaa)
bus.write_byte_data(DISPLAY_RGB_ADDR,4,r)
bus.write_byte_data(DISPLAY_RGB_ADDR,3,g)
bus.write_byte_data(DISPLAY_RGB_ADDR,2,b)
# send command to display (no need for external use)
def textCommand(cmd):
bus.write_byte_data(DISPLAY_TEXT_ADDR,0x80,cmd)
# set display text \n for second line(or auto wrap)
def setText(text):
textCommand(0x01) # clear display
time.sleep(.05)
textCommand(0x08 | 0x04) # display on, no cursor
textCommand(0x28) # 2 lines
time.sleep(.05)
count = 0
row = 0
for c in text:
if c == '\n' or count == 16:
count = 0
row += 1
if row == 2:
break
textCommand(0xc0)
if c == '\n':
continue
count += 1
bus.write_byte_data(DISPLAY_TEXT_ADDR,0x40,ord(c))
#Update the display without erasing the display
def setText_norefresh(text):
textCommand(0x02) # return home
time.sleep(.05)
textCommand(0x08 | 0x04) # display on, no cursor
textCommand(0x28) # 2 lines
time.sleep(.05)
count = 0
row = 0
while len(text) < 32: #clears the rest of the screen
text += ' '
for c in text:
if c == '\n' or count == 16:
count = 0
row += 1
if row == 2:
break
textCommand(0xc0)
if c == '\n':
continue
count += 1
bus.write_byte_data(DISPLAY_TEXT_ADDR,0x40,ord(c))
# Create a custom character (from array of row patterns)
def create_char(location, pattern):
"""
Writes a bit pattern to LCD CGRAM
Arguments:
location -- integer, one of 8 slots (0-7)
pattern -- byte array containing the bit pattern, like as found at
https://omerk.github.io/lcdchargen/
"""
location &= 0x07 # Make sure location is 0-7
textCommand(0x40 | (location << 3))
bus.write_i2c_block_data(DISPLAY_TEXT_ADDR, 0x40, pattern)
# example code
if __name__=="__main__":
setText("Hello world\nThis is an LCD test")
setRGB(0,128,64)
time.sleep(2)
for c in range(0,255):
setText_norefresh("Going to sleep in {}...".format(str(c)))
setRGB(c,255-c,0)
time.sleep(0.1)
setRGB(0,255,0)
setText("Bye bye, this should wrap onto next line")
使用 Linux i2cdetect 工具确认在 I2C 总线上正确检测到 Grove LCD RGB 显示器
sudo i2cDetect -y 1
地址 0x62 和 0x3E 应该出现(分别是背光和文本的地址)。如果没有,请检查接线和连接。