2
2
3
3
from micropython import const
4
4
from machine import Pin
5
- from time import sleep_us
5
+ from time import sleep_us , sleep_ms
6
6
7
7
TM1637_CMD1 = const (64 ) # 0x40 data command
8
8
TM1637_CMD2 = const (192 ) # 0xC0 address command
9
9
TM1637_CMD3 = const (128 ) # 0x80 display control command
10
10
TM1637_DSP_ON = const (8 ) # 0x08 display on
11
11
TM1637_DELAY = const (10 ) # 10us delay between clk/dio pulses
12
12
13
- # 0-9, a-f , blank, dash
14
- _SEGMENTS = [ 63 , 6 , 91 , 79 , 102 , 109 , 125 , 7 , 127 , 111 , 119 , 124 , 57 , 94 , 121 , 113 , 0 , 64 ]
13
+ # 0-9, a-z , blank, dash, star
14
+ _SEGMENTS = bytearray ( b' \x3F \x06 \x5B \x4F \x66 \x6D \x7D \x07 \x7F \x6F \x77 \x7C \x39 \x5E \x79 \x71 \x3D \x76 \x06 \x1E \x76 \x38 \x55 \x54 \x3F \x73 \x67 \x50 \x6D \x78 \x3E \x1C \x2A \x76 \x6E \x5B \x00 \x40 \x63 ' )
15
15
16
16
class TM1637 (object ):
17
17
"""Library for quad 7-segment LED modules based on the TM1637 LED driver."""
@@ -106,30 +106,27 @@ def encode_string(self, string):
106
106
"""Convert an up to 4 character length string containing 0-9, a-f,
107
107
space, dash to an array of segments, matching the length of the
108
108
source string."""
109
- segments = bytearray (4 )
110
- for i in range (0 , min ( 4 , len (string ) )):
109
+ segments = bytearray (len ( string ) )
110
+ for i in range (len (string )):
111
111
segments [i ] = self .encode_char (string [i ])
112
112
return segments
113
113
114
114
def encode_char (self , char ):
115
- """Convert a character 0-9, a-f , space or dash to a segment."""
115
+ """Convert a character 0-9, a-z , space or dash to a segment."""
116
116
o = ord (char )
117
- # space
118
117
if o == 32 :
119
- return _SEGMENTS [16 ]
120
- # dash
118
+ return _SEGMENTS [36 ] # space
119
+ if o == 42 :
120
+ return _SEGMENTS [38 ] # star/degrees
121
121
if o == 45 :
122
- return _SEGMENTS [17 ]
123
- # uppercase A-F
124
- if o >= 65 and o <= 70 :
125
- return _SEGMENTS [o - 55 ]
126
- # lowercase a-f
127
- if o >= 97 and o <= 102 :
128
- return _SEGMENTS [o - 87 ]
129
- # 0-9
122
+ return _SEGMENTS [37 ] # dash
123
+ if o >= 65 and o <= 90 :
124
+ return _SEGMENTS [o - 55 ] # uppercase A-Z
125
+ if o >= 97 and o <= 122 :
126
+ return _SEGMENTS [o - 87 ] # lowercase a-z
130
127
if o >= 48 and o <= 57 :
131
- return _SEGMENTS [o - 48 ]
132
- raise ValueError ("Character out of range" )
128
+ return _SEGMENTS [o - 48 ] # 0-9
129
+ raise ValueError ("Character out of range: {:d} '{:s}'" . format ( o , chr ( o )) )
133
130
134
131
def hex (self , val ):
135
132
"""Display a hex value 0x0000 through 0xffff, right aligned."""
@@ -149,17 +146,30 @@ def numbers(self, num1, num2, colon=True):
149
146
num1 = max (- 9 , min (num1 , 99 ))
150
147
num2 = max (- 9 , min (num2 , 99 ))
151
148
segments = self .encode_string ('{0:0>2d}{1:0>2d}' .format (num1 , num2 ))
152
- # colon on
153
149
if colon :
154
- segments [1 ] |= 0x80
150
+ segments [1 ] |= 0x80 # colon on
155
151
self .write (segments )
156
152
157
153
def temperature (self , num ):
158
154
if num < - 9 :
159
- self .write ([ 0x38 , 0x3F ] ) # LO
155
+ self .show ( 'lo' ) # low
160
156
elif num > 99 :
161
- self .write ([ 0x76 , 0x06 ] ) # HI
157
+ self .show ( 'hi' ) # high
162
158
else :
163
159
string = '{0: >2d}' .format (num )
164
160
self .write (self .encode_string (string ))
165
- self .write ([0x63 , 0x39 ], 2 ) # degrees C
161
+ self .write ([_SEGMENTS [38 ], _SEGMENTS [12 ]], 2 ) # degrees C
162
+
163
+ def show (self , string , colon = False ):
164
+ segments = self .encode_string (string )
165
+ if len (segments ) > 1 and colon :
166
+ segments [1 ] |= 128
167
+ self .write (segments [:4 ])
168
+
169
+ def scroll (self , string , delay = 250 ):
170
+ segments = string if isinstance (string , list ) else self .encode_string (string )
171
+ data = [0 ] * 8
172
+ data [4 :0 ] = list (segments )
173
+ for i in range (len (segments ) + 5 ):
174
+ self .write (data [0 + i :4 + i ])
175
+ sleep_ms (delay )
0 commit comments