-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathFourDigitDisplay.py
82 lines (67 loc) · 2.01 KB
/
FourDigitDisplay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'''
microbit drive for Four Digit LED Display (TM1650)
Author: shaoziyang
Date: 2018.3
http://www.micropython.org.cn
'''
from microbit import *
COMMAND_I2C_ADDRESS = 0x24
DISPLAY_I2C_ADDRESS = 0x34
buf = (0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71)
class FourDigitDisplay():
def __init__(self):
self._intensity = 3
self.dbuf = [0, 0, 0, 0]
self.tbuf = bytearray(1)
self.on()
def intensity(self, dat = -1):
if dat < 0 or dat > 8:
return self._intensity
if dat == 0:
self.off()
else:
self._intensity = dat
self.cmd((dat<<4)|0x01)
def cmd(self, c):
self.tbuf[0] = c
i2c.write(COMMAND_I2C_ADDRESS, self.tbuf)
def dat(self, bit, d):
self.tbuf[0] = d
i2c.write(DISPLAY_I2C_ADDRESS + (bit%4), self.tbuf)
def on(self):
self.cmd((self._intensity<<4)|0x01)
def off(self):
self._intensity = 0
self.cmd(0)
def clear(self):
self.dat(0, 0)
self.dat(1, 0)
self.dat(2, 0)
self.dat(3, 0)
self.dbuf = [0, 0, 0, 0]
def showbit(self, num, bit = 0):
self.dbuf[bit%4] = buf[num%16]
self.dat(bit, buf[num%16])
def shownum(self, num):
if num < 0:
self.dat(0, 0x40) # '-'
num = -num
else:
self.showbit((num // 1000) % 10)
self.showbit(num % 10, 3)
self.showbit((num // 10) % 10, 2)
self.showbit((num // 100) % 10, 1)
def showhex(self, num):
if num < 0:
self.dat(0, 0x40) # '-'
num = -num
else:
self.showbit((num >> 12) % 16)
self.showbit(num % 16, 3)
self.showbit((num >> 4) % 16, 2)
self.showbit((num >> 8) % 16, 1)
def showDP(self, bit = 1, show = True):
if show:
self.dat(bit, self.dbuf[bit] | 0x80)
else:
self.dat(bit, self.dbuf[bit] & 0x7F)