-
Notifications
You must be signed in to change notification settings - Fork 1
/
life.py
471 lines (441 loc) · 12.7 KB
/
life.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# MicroPython python3
# vim: set sw=2 ai expandtab
#
# Released under the Apache 2.0 license.
# http://www.apache.org/licenses/
import os
import sys
import time
import apa102
from apa102 import DISC_RINGS, NUM_DISC_LEDS, NUM_RINGS, DISC_RING_OFFSETS
orig = [apa102.cyan, apa102.blue, apa102.indigo, apa102.violet,
apa102.orange, apa102.red, apa102.white]
xmas = [b'\xff\x04 \x00', b'\xff\x04\x00 ', b'\xff\x00\x80\x00',
b'\xff\x00\xc0\x00', b'\xff\x00\x00\x80', b'\xff\x00\x00\xc0',
apa102.white]
newyears = [b'\xff\x20\x00\x04', b'\xff\x04\x20\x20', b'\xff\x80\x00\x00',
b'\xff\xc0\x00\x00', b'\xff\x00\x80\x80', b'\xff\x00\xc0\xc0',
apa102.white]
class Life(object):
def __init__(self,
brightness=0x04,
bus_len=NUM_DISC_LEDS,
bus_offset=0,
stats_display=None):
"""Create a LIFE simulation mapped to an Adafruit circle of LED.
Args:
brightness: 1-31 value, beware of blindingly bright LEDs.
bus_len: The total number of LEDs on the SPI bus.
offset: The bus offset of the start of the LED disc.
stats_display: An optional instance of a class that will receive
information as our simulation runs.
"""
self.brightness = brightness
self.stats_display = stats_display
self.bus_len = bus_len
self.bus_offset = bus_offset
if apa102.spi:
self.spi = apa102.spi
else:
import machine
self.spi = machine.SPI(0)
self.spi.init(machine.SPI.MASTER, baudrate=8000000, bits=8,
pins=apa102.SPI_PINS)
self._static_set_neighbors() # sets self._neighbors
self.shape = 'disc'
num_finish_bytes = apa102.num_finish_bytes(self.bus_len)
self._spi_data = bytearray(apa102.START_FRAME
+ apa102.led_off*self.bus_len
+ apa102.FINISH_BYTE*num_finish_bytes)
# I randomly chose these, this particular start sequence does end
# up living as it quickly results in a circle of life at the center
# with plenty of exterior activity resulting in new births for a
# nice animation.
self._default_start_state = (1, 5, 9, 10, 11, 12, 13, 14, 15, 58, 59, 60, 61, 62, 63, 64, 200, 201, 202, 203, 209, 240, 241, 242, 243, 245, 254, 253, 252, 251)
def make_torus(self):
"""Turns the structure from a flat disc into a torus wrapping to the middle LED."""
if self.shape != 'disc':
raise RuntimeError('can only make a torus out of a disc, not a '+self.shape)
self.shape = 'torus'
self._neighbors = list(self._neighbors)
for outer_led in range(DISC_RINGS[0]):
self._neighbors[outer_led] += b'\xfe' # Inner dot is a neighbor.
# Entire outer ring is a neighbor of the inner dot.
self._neighbors[0xfe] += bytes(range(DISC_RINGS[0]))
def run_classic(self, *args, **kwargs):
return self.run(*args, stay_alive=(2,3), new_born=(3,), **kwargs)
def run(self, initial_state=(), *, alive=orig,
sleep_ms=50, iterations=-1, stay_alive=(2,3), new_born=(2,5)):
"""Classic life tunable using stay_alive and newborn sets.
Args:
initial_state: is a sequence of the [0,254] LEDs alive at the start.
sleep_ms: The number of milliseconds to display each frame.
alive: A tuple of colors a pixel will go through as it gets older.
iterations: if > 0, the number of iterations to go through.
stay_alive: LIFE - Number of neighbors required for a pixel to live.
new_born: LIFE - Number of neighbors for new life on a dead pixel.
Returns:
The final state after running through all iterations.
"""
assert len(alive)
if not initial_state:
initial_state = self._default_start_state
current_state = bytearray(NUM_DISC_LEDS) # wasteful
for led in initial_state:
current_state[led] = 1
start_state = current_state
colormap = {0: apa102._brightness(apa102.led_off, self.brightness)}
for idx, color in enumerate(alive, 1):
assert len(color) == 4
colormap[idx] = apa102._brightness(color, self.brightness)
max_alive = len(alive)
count_dieoffs = 0
count_iters = 0
ticks_ms_refresh = 0
if self.stats_display:
self.stats_display.clear()
self.stats_display.set_cursor(0,0)
self.stats_display.write(' Rounds alive: 0\n')
self.stats_display.write('Cyclic states: 0\n') # Unimplemented.
self.stats_display.write('Total dieoffs: 0')
while iterations != 0:
# Display the current state.
start_ms = time.ticks_ms()
self._display_state(current_state, colormap)
if self.stats_display and time.ticks_ms() - ticks_ms_refresh > 1000:
ticks_ms_refresh = time.ticks_ms()
self.stats_display.set_text_cursor(15,0)
self.stats_display.write(str(count_iters))
try:
self.stats_display.display()
except Exception:
# Error updating, nothing we can do about it.
self.stats_display = None
count_iters += 1
wait_until = start_ms + sleep_ms
now = time.ticks_ms()
while now < wait_until:
if wait_until - now > 10:
time.sleep_ms(10)
else:
time.sleep_ms(1)
now = time.ticks_ms()
# all dead, restart.
if (max(current_state) == 0):
count_dieoffs += 1
if self.stats_display:
self.stats_display.set_text_cursor(15,0)
self.stats_display.write(str(count_iters))
self.stats_display.set_text_cursor(15,2)
self.stats_display.write(str(count_dieoffs))
self.stats_display.display()
time.sleep_ms(1000+sleep_ms*3) # pause
# Randomly seed new life.
for led in os.urandom(23):
if led < len(current_state):
current_state[led] = not current_state[led]
# Compute the next iteration.
next_state = bytearray(current_state) # copy
for led, alive in enumerate(current_state):
live_neighbors = 0
for neighbor in self._neighbors[led]:
if current_state[neighbor]:
live_neighbors += 1
live_neighbors %= 7 # HACK, for torus to be meaningful.
if alive:
if live_neighbors in stay_alive:
next_state[led] = min(next_state[led]+1, max_alive)
else:
next_state[led] = 0 # death
else: # dead
if live_neighbors in new_born:
next_state[led] = 1 # birth
current_state = next_state
if iterations > 0:
iterations -= 1
return current_state
def _display_state(self, state, colormap):
disc_byte_ofs = (self.bus_offset+1)*4
for led, value in enumerate(state):
led_ofs = disc_byte_ofs + led*4
self._spi_data[led_ofs:led_ofs+4] = colormap[value]
self.spi.write(self._spi_data)
def demo_neighbors(self, color=apa102.cyan, neighbor_color=apa102.amber, sleep_ms=123):
"""Animate the LEDs highlighting which ones are considered neighbors."""
num_finish_bytes = apa102.num_finish_bytes(self.bus_len)
spi_data = bytearray(apa102.START_FRAME + apa102.led_off*self.bus_len + apa102.FINISH_BYTE*num_finish_bytes)
disc_byte_ofs = (self.bus_offset+1)*4
for led, neighbors in enumerate(self._neighbors):
for neighbor_led in neighbors:
led_ofs = disc_byte_ofs + neighbor_led*4
spi_data[led_ofs:led_ofs+4] = apa102._brightness(neighbor_color, self.brightness)
led_ofs = disc_byte_ofs + led*4
spi_data[led_ofs:led_ofs+4] = apa102._brightness(color, self.brightness)
print("writing", len(spi_data), "bytes:", self.spi.write(spi_data), "written")
print(spi_data[0:4], "...", spi_data[-15:])
time.sleep_ms(sleep_ms)
# reset before the next iteration.
for neighbor_led in neighbors:
led_ofs = disc_byte_ofs + neighbor_led*4
spi_data[led_ofs:led_ofs+4] = apa102.led_off
led_ofs = disc_byte_ofs + led*4
spi_data[led_ofs:led_ofs+4] = apa102.led_off
def _static_set_neighbors(self):
# This looks gross but is intended to be an low memory
# consumption data structure for LED neighbor lookups.
self._neighbors = \
(b'\x01/0',
b'\x00\x021',
b'\x01\x032',
b'\x02\x043',
b'\x03\x054',
b'\x04\x0645',
b'\x05\x0756',
b'\x06\x0867',
b'\x07\t78',
b'\x08\n8',
b'\t\x0b9',
b'\n\x0c:',
b'\x0b\r;',
b'\x0c\x0e<',
b'\r\x0f=',
b'\x0e\x10>',
b'\x0f\x11?',
b'\x10\x12?@',
b'\x11\x13@A',
b'\x12\x14AB',
b'\x13\x15BC',
b'\x14\x16C',
b'\x15\x17D',
b'\x16\x18E',
b'\x17\x19F',
b'\x18\x1aG',
b'\x19\x1bH',
b'\x1a\x1cI',
b'\x1b\x1dJ',
b'\x1c\x1eJK',
b'\x1d\x1fKL',
b'\x1e LM',
b'\x1f!MN',
b' "N',
b'!#O',
b'"$P',
b'#%Q',
b'$&R',
b"%'S",
b'&(T',
b"')U",
b'(*UV',
b')+VW',
b'*,WX',
b'+-XY',
b',.Y',
b'-/Z',
b'\x00.[',
b'\x001[\\',
b'\x0102]',
b'\x0213^',
b'\x0324_',
b'\x04\x0535_`',
b'\x05\x0646`a',
b'\x06\x0757ab',
b'\x07\x0868bc',
b'\t79c',
b'\n8:d',
b'\x0b9;e',
b'\x0c:<f',
b'\r;=g',
b'\x0e<>h',
b'\x0f=?i',
b'\x10\x11>@ij',
b'\x11\x12?Ajk',
b'\x12\x13@Bkl',
b'\x13\x14AClm',
b'\x15BDm',
b'\x16CEn',
b'\x17DFo',
b'\x18EGp',
b'\x19FHq',
b'\x1aGIr',
b'\x1bHJs',
b'\x1c\x1dIKst',
b'\x1d\x1eJLtu',
b'\x1e\x1fKMuv',
b'\x1f LNvw',
b'!MOw',
b'"NPx',
b'#OQy',
b'$PRz',
b'%QS{',
b'&RT|',
b"'SU}",
b'()TV}~',
b')*UW~\x7f',
b'*+VX\x7f\x80',
b'+,WY\x80\x81',
b'-XZ\x81',
b'.Y[\x82',
b'/0Z\x83',
b'0]\x83\x84',
b'1\\^\x85',
b'2]_\x85\x86',
b'3^`\x86\x87',
b'45_a\x87',
b'56`b\x88',
b'67ac\x89',
b'8bd\x89\x8a',
b'9ce\x8a\x8b',
b':df\x8b',
b';eg\x8c',
b'<fh\x8d',
b'=gi\x8d\x8e',
b'>hj\x8e\x8f',
b'?@ik\x8f',
b'@Ajl\x90',
b'ABkm\x91',
b'Cln\x91\x92',
b'Dmo\x92\x93',
b'Enp\x93',
b'Foq\x94',
b'Gpr\x95',
b'Hqs\x95\x96',
b'Irt\x96\x97',
b'JKsu\x97',
b'KLtv\x98',
b'LMuw\x99',
b'Nvx\x99\x9a',
b'Owy\x9a\x9b',
b'Pxz\x9b',
b'Qy{\x9c',
b'Rz|\x9d',
b'S{}\x9d\x9e',
b'T|~\x9e\x9f',
b'UV}\x7f\x9f',
b'VW~\x80\xa0',
b'WX\x7f\x81\xa1',
b'Y\x80\x82\xa1\xa2',
b'Z\x81\x83\xa2\xa3',
b'[\\\x82\xa3',
b'\\\x85\xa3\xa4',
b']\x84\x86\xa5',
b'^_\x85\x87\xa6',
b'`\x86\x88\xa6\xa7',
b'a\x87\x89\xa7\xa8',
b'b\x88\x8a\xa8\xa9',
b'cd\x89\x8b\xa9',
b'e\x8a\x8c\xaa',
b'f\x8b\x8d\xab',
b'g\x8c\x8e\xac',
b'hi\x8d\x8f\xad',
b'j\x8e\x90\xad\xae',
b'k\x8f\x91\xae\xaf',
b'l\x90\x92\xaf\xb0',
b'mn\x91\x93\xb0',
b'o\x92\x94\xb1',
b'p\x93\x95\xb2',
b'q\x94\x96\xb3',
b'rs\x95\x97\xb4',
b't\x96\x98\xb4\xb5',
b'u\x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