|
| 1 | +""" |
| 2 | + Test script for display animations on an HT16K33 with alphanumeric display |
| 3 | +
|
| 4 | + The display must be initialized with auto_write=False. |
| 5 | +""" |
| 6 | + |
| 7 | +from time import sleep |
| 8 | +import board |
| 9 | +import busio |
| 10 | +from adafruit_ht16k33.segments import Seg14x4 |
| 11 | + |
| 12 | +# |
| 13 | +# Segment bits on the HT16K33 with alphanumeric display. |
| 14 | +# |
| 15 | +# Add the values of the segments you need to create a bitmask |
| 16 | +# |
| 17 | + |
| 18 | +N = 16384 |
| 19 | +M = 8192 |
| 20 | +L = 4096 |
| 21 | +K = 2048 |
| 22 | +J = 1024 |
| 23 | +I = 512 |
| 24 | +H = 256 |
| 25 | +G2= 128 |
| 26 | +G1= 64 |
| 27 | +F = 32 |
| 28 | +E = 16 |
| 29 | +D = 8 |
| 30 | +C = 4 |
| 31 | +B = 2 |
| 32 | +A = 1 |
| 33 | + |
| 34 | +# The number of seconds to delay between writing segments |
| 35 | +DEFAULT_CHAR_DELAY_SEC = 0.2 |
| 36 | + |
| 37 | +# The number of cycles to go for each animation |
| 38 | +DEFAULT_CYCLES = 5 |
| 39 | + |
| 40 | +# Brightness of the display (0 to 15) |
| 41 | +DEFAULT_DISPLAY_BRIGHTNESS = 2 |
| 42 | + |
| 43 | +# Initialize the I2C bus |
| 44 | +i2c = busio.I2C(board.SCL, board.SDA) |
| 45 | + |
| 46 | +# Initialize the HT16K33 with alphanumeric display featherwing. |
| 47 | +# |
| 48 | +# You MUST set auto_write=False |
| 49 | +display = Seg14x4(i2c, auto_write=False) |
| 50 | +display.brightness = DEFAULT_DISPLAY_BRIGHTNESS |
| 51 | + |
| 52 | +def animate(digits, bitmasks, delay=DEFAULT_CHAR_DELAY_SEC, auto_write=True): |
| 53 | + ''' |
| 54 | + Main driver for all alphanumeric display animations (WIP!!!) |
| 55 | + Param: digits - a list of the digits to write to, in order, like [0, 1, 3]. The digits are |
| 56 | + 0 to 3 starting at the left most digit. |
| 57 | + Param: bitmasks - a list of the bitmasks to write, in sequence, to the specified digits. |
| 58 | + Param: delay - The delay, in seconds (or fractions of), between writing bitmasks to a digit. |
| 59 | + Param: auto_write - Whether to actually write to the display immediately or not. |
| 60 | +
|
| 61 | + Returns: Nothing |
| 62 | + ''' |
| 63 | + if not isinstance(digits, list): |
| 64 | + raise ValueError("The first parameter MUST be a list!") |
| 65 | + elif not isinstance(bitmasks, list): |
| 66 | + raise ValueError("The second parameter MUST be a list!") |
| 67 | + elif delay < 0: |
| 68 | + raise ValueError("The delay between frames must be positive!") |
| 69 | + else: |
| 70 | + for dig in digits: |
| 71 | + if not 0 <= dig <= 3: |
| 72 | + raise ValueError('Digit value must be \ |
| 73 | + an integer in the range: 0-3') |
| 74 | + |
| 75 | + for bits in bitmasks: |
| 76 | + if not 0 <= bits <= 0xFFFF: |
| 77 | + raise ValueError('Bitmask value must be an \ |
| 78 | + integer in the range: 0-65535') |
| 79 | + |
| 80 | + display.set_digit_raw(dig, bits) |
| 81 | + |
| 82 | + if auto_write: |
| 83 | + display.show() |
| 84 | + sleep(delay) |
| 85 | + |
| 86 | +def chase_forward_and_reverse(delay=DEFAULT_CHAR_DELAY_SEC, cycles=DEFAULT_CYCLES): |
| 87 | + cy = 0 |
| 88 | + |
| 89 | + while cy < cycles: |
| 90 | + animate([0, 1, 2, 3], [A, 0], delay) |
| 91 | + animate([3], [B, C, D, 0], delay) |
| 92 | + animate([2, 1, 0], [D, 0], delay) |
| 93 | + animate([0], [E, F, H, G2, 0], delay) |
| 94 | + animate([1, 2], [G1, G2, 0], delay) |
| 95 | + animate([3], [G1, J, A, 0], delay) |
| 96 | + animate([2, 1], [A, 0], delay) |
| 97 | + animate([0], [A, F, E, D, 0], delay) |
| 98 | + animate([1, 2], [D, 0], delay) |
| 99 | + animate([3], [D, C, B, J, G1, 0], delay) |
| 100 | + animate([2, 1], [G2, G1, 0], delay) |
| 101 | + animate([0], [H, 0], delay) |
| 102 | + |
| 103 | + cy += 1 |
| 104 | + |
| 105 | +def prelude_to_spinners(delay=DEFAULT_CHAR_DELAY_SEC, cycles=DEFAULT_CYCLES): |
| 106 | + cy = 0 |
| 107 | + auto_write = False |
| 108 | + |
| 109 | + while cy < cycles: |
| 110 | + animate([1, 2], [A], 0, auto_write) |
| 111 | + display.show() |
| 112 | + sleep(delay) |
| 113 | + |
| 114 | + animate([0, 3], [A], 0, auto_write) |
| 115 | + display.show() |
| 116 | + sleep(delay) |
| 117 | + |
| 118 | + animate([0], [A+F], 0, auto_write) |
| 119 | + animate([3], [A+B], 0, auto_write) |
| 120 | + display.show() |
| 121 | + sleep(delay) |
| 122 | + |
| 123 | + animate([0], [A+E+F], 0, auto_write) |
| 124 | + animate([3], [A+B+C], 0, auto_write) |
| 125 | + display.show() |
| 126 | + sleep(delay) |
| 127 | + |
| 128 | + animate([0], [A+D+E+F], 0, auto_write) |
| 129 | + animate([3], [A+B+C+D], 0, auto_write) |
| 130 | + display.show() |
| 131 | + sleep(delay) |
| 132 | + |
| 133 | + animate([1], [A+D], 0, auto_write) |
| 134 | + animate([2], [A+D], 0, auto_write) |
| 135 | + display.show() |
| 136 | + sleep(delay) |
| 137 | + |
| 138 | + animate([1], [A+D+M], 0, auto_write) |
| 139 | + animate([2], [A+D+K], 0, auto_write) |
| 140 | + display.show() |
| 141 | + sleep(delay) |
| 142 | + |
| 143 | + animate([1], [A+D+M+H], 0, auto_write) |
| 144 | + animate([2], [A+D+K+J], 0, auto_write) |
| 145 | + display.show() |
| 146 | + sleep(delay) |
| 147 | + |
| 148 | + animate([0], [A+E+F+J+D], 0, auto_write) |
| 149 | + animate([3], [A+B+C+H+D], 0, auto_write) |
| 150 | + display.show() |
| 151 | + sleep(delay) |
| 152 | + |
| 153 | + animate([0], [A+E+F+J+K+D], 0, auto_write) |
| 154 | + animate([3], [A+B+C+H+M+D], 0, auto_write) |
| 155 | + display.show() |
| 156 | + sleep(delay) |
| 157 | + |
| 158 | + display.fill(0) |
| 159 | + display.show() |
| 160 | + sleep(delay) |
| 161 | + |
| 162 | + cy += 1 |
| 163 | + |
| 164 | +def spinners(delay=DEFAULT_CHAR_DELAY_SEC, cycles=DEFAULT_CYCLES): |
| 165 | + cy = 0 |
| 166 | + auto_write = False |
| 167 | + |
| 168 | + while cy < cycles: |
| 169 | + animate([0], [H+M], 0, auto_write) |
| 170 | + animate([1], [J+K], 0, auto_write) |
| 171 | + animate([2], [H+M], 0, auto_write) |
| 172 | + animate([3], [J+K], 0, auto_write) |
| 173 | + display.show() |
| 174 | + sleep(delay) |
| 175 | + |
| 176 | + animate([0], [G1+G2], 0, auto_write) |
| 177 | + animate([1], [G1+G2], 0, auto_write) |
| 178 | + animate([2], [G1+G2], 0, auto_write) |
| 179 | + animate([3], [G1+G2], 0, auto_write) |
| 180 | + display.show() |
| 181 | + sleep(delay) |
| 182 | + |
| 183 | + animate([0], [J+K], 0, auto_write) |
| 184 | + animate([1], [H+M], 0, auto_write) |
| 185 | + animate([2], [J+K], 0, auto_write) |
| 186 | + animate([3], [H+M], 0, auto_write) |
| 187 | + display.show() |
| 188 | + sleep(delay) |
| 189 | + |
| 190 | + cy += 1 |
| 191 | + |
| 192 | + display.fill(0) |
| 193 | + |
| 194 | +def enclosed_spinners(delay=DEFAULT_CHAR_DELAY_SEC, cycles=DEFAULT_CYCLES): |
| 195 | + cy = 0 |
| 196 | + auto_write = False |
| 197 | + |
| 198 | + while cy < cycles: |
| 199 | + animate([0], [A+D+E+F+H+M], 0, auto_write) |
| 200 | + animate([1], [A+D+J+K], 0, auto_write) |
| 201 | + animate([2], [A+D+H+M], 0, auto_write) |
| 202 | + animate([3], [A+B+C+D+J+K], 0, auto_write) |
| 203 | + display.show() |
| 204 | + sleep(delay) |
| 205 | + |
| 206 | + animate([0], [A+D+E+F+G1+G2], 0, auto_write) |
| 207 | + animate([1], [A+D+G1+G2], 0, auto_write) |
| 208 | + animate([2], [A+D+G1+G2], 0, auto_write) |
| 209 | + animate([3], [A+B+C+D+G1+G2], 0, auto_write) |
| 210 | + display.show() |
| 211 | + sleep(delay) |
| 212 | + |
| 213 | + animate([0], [A+D+E+F+J+K], 0, auto_write) |
| 214 | + animate([1], [A+D+H+M], 0, auto_write) |
| 215 | + animate([2], [A+D+J+K], 0, auto_write) |
| 216 | + animate([3], [A+B+C+D+H+M], 0, auto_write) |
| 217 | + display.show() |
| 218 | + sleep(delay) |
| 219 | + |
| 220 | + cy += 1 |
| 221 | + |
| 222 | + display.fill(0) |
| 223 | + |
| 224 | +def count_down(): |
| 225 | + auto_write = False |
| 226 | + numbers = [ [A+B+C+D+G1+G2+N], [A+B+D+E+G1+G2+N], [B+C+N] ] |
| 227 | + index = 0 |
| 228 | + |
| 229 | + display.fill(0) |
| 230 | + |
| 231 | + while index < len(numbers): |
| 232 | + animate([index], numbers[index], 0, auto_write) |
| 233 | + display.show() |
| 234 | + sleep(1) |
| 235 | + display.fill(0) |
| 236 | + sleep(0.5) |
| 237 | + |
| 238 | + index += 1 |
| 239 | + |
| 240 | + sleep(1) |
| 241 | + display.fill(0) |
| 242 | + |
| 243 | +try: |
| 244 | + text = "Init" |
| 245 | + |
| 246 | + display.fill(1) |
| 247 | + display.show() |
| 248 | + sleep(1) |
| 249 | + display.fill(0) |
| 250 | + display.show() |
| 251 | + |
| 252 | + display.print(text) |
| 253 | + display.show() |
| 254 | + sleep(2) |
| 255 | + display.fill(0) |
| 256 | + display.show() |
| 257 | + sleep(1) |
| 258 | + |
| 259 | + count_down() |
| 260 | + sleep(0.2) |
| 261 | + |
| 262 | + text = "Go!!" |
| 263 | + |
| 264 | + display.print(text) |
| 265 | + display.show() |
| 266 | + sleep(1.5) |
| 267 | + display.fill(0) |
| 268 | + display.show() |
| 269 | + sleep(0.5) |
| 270 | + print() |
| 271 | + |
| 272 | + while True: |
| 273 | + # Arrow |
| 274 | + print("Arrow") |
| 275 | + animate([0, 1, 2], [G1+G2], 0.1) |
| 276 | + animate([3], [G1+H+K], 0.1) |
| 277 | + sleep(1.0) |
| 278 | + display.fill(0) |
| 279 | + sleep(1.0) |
| 280 | + |
| 281 | + # Flying |
| 282 | + print("Flying") |
| 283 | + cyc = 0 |
| 284 | + |
| 285 | + while cyc < DEFAULT_CYCLES: |
| 286 | + animate([0], [H+J, G1+G2, K+M, G1+G2], DEFAULT_CHAR_DELAY_SEC) |
| 287 | + |
| 288 | + cyc += 1 |
| 289 | + |
| 290 | + animate([0], [0]) |
| 291 | + sleep(1.0) |
| 292 | + display.fill(0) |
| 293 | + sleep(1.0) |
| 294 | + |
| 295 | + # Chase forward and reverse. |
| 296 | + print("Chase forward and reverse") |
| 297 | + chase_forward_and_reverse(0.01, 5) |
| 298 | + sleep(1.0) |
| 299 | + display.fill(0) |
| 300 | + sleep(1.0) |
| 301 | + |
| 302 | + # Testing writing to more than one segment simultaneously |
| 303 | + print("Prelude to Spinners") |
| 304 | + prelude_to_spinners(0.1, 5) |
| 305 | + sleep(1.0) |
| 306 | + display.fill(0) |
| 307 | + display.show() |
| 308 | + sleep(1.0) |
| 309 | + |
| 310 | + print("Spinners") |
| 311 | + spinners(0.1, 20) |
| 312 | + sleep(1.0) |
| 313 | + display.fill(0) |
| 314 | + display.show() |
| 315 | + sleep(1.0) |
| 316 | + |
| 317 | + |
| 318 | + print("Enclosed Spinners") |
| 319 | + enclosed_spinners(0.1, 20) |
| 320 | + sleep(1.0) |
| 321 | + display.fill(0) |
| 322 | + display.show() |
| 323 | + sleep(1.0) |
| 324 | + |
| 325 | + print() |
| 326 | +except KeyboardInterrupt: |
| 327 | + display.fill(0) |
| 328 | + display.show() |
0 commit comments