用 microbit 驱动 I2C 接口的 Four Letter pHAT(4字符显示板)。
接线
Raspberry Pi | [/b]micro:bit | |
---|---|---|
Power LED | 5V | 5V |
Power IC | 3V3 | 3V |
Ground | GND | GND |
Data | Pin 2 | Pin 20 |
Clock | Pin 3 | Pin 19 |
代码
代码: 全选
from microbit import i2c, sleep
class FourLetter:
ADDRESS = 0x70
BLINK_CMD = 0x80
CMD_BRIGHTNESS = 0xE0
DIGITS = {
' ': 0b0000000000000000,
'!': 0b0000000000000110,
'"': 0b0000001000100000,
'#': 0b0001001011001110,
'
: 0b0001001011101101,
'%': 0b0000110000100100,
'&': 0b0010001101011101,
'\'': 0b0000010000000000,
'(': 0b0010010000000000,
')': 0b0000100100000000,
'*': 0b0011111111000000,
'+': 0b0001001011000000,
',': 0b0000100000000000,
'-': 0b0000000011000000,
'.': 0b0000000000000000,
'/': 0b0000110000000000,
'0': 0b0000110000111111,
'1': 0b0000000000000110,
'2': 0b0000000011011011,
'3': 0b0000000010001111,
'4': 0b0000000011100110,
'5': 0b0010000001101001,
'6': 0b0000000011111101,
'7': 0b0000000000000111,
'8': 0b0000000011111111,
'9': 0b0000000011101111,
':': 0b0001001000000000,
';': 0b0000101000000000,
'<': 0b0010010000000000,
'=': 0b0000000011001000,
'>': 0b0000100100000000,
'?': 0b0001000010000011,
'@': 0b0000001010111011,
'A': 0b0000000011110111,
'B': 0b0001001010001111,
'C': 0b0000000000111001,
'D': 0b0001001000001111,
'E': 0b0000000011111001,
'F': 0b0000000001110001,
'G': 0b0000000010111101,
'H': 0b0000000011110110,
'I': 0b0001001000000000,
'J': 0b0000000000011110,
'K': 0b0010010001110000,
'L': 0b0000000000111000,
'M': 0b0000010100110110,
'N': 0b0010000100110110,
'O': 0b0000000000111111,
'P': 0b0000000011110011,
'Q': 0b0010000000111111,
'R': 0b0010000011110011,
'S': 0b0000000011101101,
'T': 0b0001001000000001,
'U': 0b0000000000111110,
'V': 0b0000110000110000,
'W': 0b0010100000110110,
'X': 0b0010110100000000,
'Y': 0b0001010100000000,
'Z': 0b0000110000001001,
'a': 0b0001000001011000,
'b': 0b0010000001111000,
'c': 0b0000000011011000,
'd': 0b0000100010001110,
'e': 0b0000100001011000,
'f': 0b0000000001110001,
'g': 0b0000010010001110,
'h': 0b0001000001110000,
'i': 0b0001000000000000,
'j': 0b0000000000001110,
'k': 0b0011011000000000,
'l': 0b0000000000110000,
'm': 0b0001000011010100,
'n': 0b0001000001010000,
'o': 0b0000000011011100,
'p': 0b0000000101110000,
'q': 0b0000010010000110,
'r': 0b0000000001010000,
's': 0b0010000010001000,
't': 0b0000000001111000,
'u': 0b0000000000011100,
'v': 0b0010000000000100,
'w': 0b0010100000010100,
'x': 0b0010100011000000,
'y': 0b0010000000001100,
'z': 0b0000100001001000
}
def __init__(self):
self.buffer = bytearray([0]*16)
i2c.write(self.ADDRESS,b'\x21',repeat=False)
self.blink_rate(0)
self.set_brightness(15)
self.update_display()
def write_reg(self,reg,value):
i2c.write(self.ADDRESS, bytes([reg,value]), repeat=False)
def blink_rate(self, b):
if b>3:
b=0
i2c.write(self.ADDRESS,bytes([self.BLINK_CMD | 1 | (b << 1)]), repeat=False)
def set_brightness(self,b):
if b>15:
b=15
i2c.write(self.ADDRESS,bytes([self.CMD_BRIGHTNESS | b]), repeat=False)
def update_display(self):
data = bytearray([0]) + self.buffer
i2c.write(self.ADDRESS,data,repeat=False)
def clear(self):
self.buffer = bytearray([0]*16)
def raw_digit(self, value, position):
self.buffer[position*2] = value & 0xFF
self.buffer[position*2+1] = (value >> 8) & 0xFF
def set_digit(self, value, position):
self.raw_digit(self.DIGITS.get(str(value), 0x00), position)
def print_str(self, value, ralign=True):
# Calculcate starting position of digits
pos = (4-len(value)) if ralign else 0
for i, ch in enumerate(value):
self.set_digit(ch,i+pos)
def set_decimal(self, decimal, pos):
if pos < 0 or pos > 3:
return
# Set bit 14
if decimal:
self.buffer[pos*2+1] |= (1 << 6)
else:
self.buffer[pos*2+1] &= ~(1 << 6)
flh = FourLetter()
flh.print_str("Byte")
flh.update_display()
sleep(5000)
# test decimals
for i in range(4):
flh.set_decimal(True,i)
flh.update_display()
sleep(500)
flh.clear()
# test numbers
for i in range(10000):
flh.print_str(str(i),ralign=True)
flh.update_display()
sleep(50)
# some strings
words = [" I", "HOPE", "THAT", "THIS", "WILL", "WORK", " NOW"]
for w in words:
flh.print_str(w)
flh.update_display()
sleep(1000)
for w in words:
flh.print_str(w.lower())
flh.update_display()
sleep(1000)