Browse Source

get ntp time and adjust brightness accordingly

Thomas Buck 11 months ago
parent
commit
33ea9f0cb3
14 changed files with 467 additions and 72 deletions
  1. 255
    33
      camp_pico.py
  2. 2
    0
      copy.sh
  3. 3
    2
      image.py
  4. 6
    4
      life.py
  5. 3
    2
      manager.py
  6. 81
    0
      mapper.py
  7. 18
    4
      net.py
  8. 24
    4
      pico.py
  9. 6
    2
      qr.py
  10. 4
    5
      scroll.py
  11. 3
    2
      snake.py
  12. 5
    3
      solid.py
  13. 3
    2
      tetris.py
  14. 54
    9
      util.py

+ 255
- 33
camp_pico.py View File

@@ -10,8 +10,8 @@
10 10
 camp_pink = (251, 72, 196)
11 11
 camp_green = (63, 255, 33)
12 12
 
13
-# QR code image for "Hello World"
14
-# size:21x21
13
+# QR code image for "http://ubabot.frubar.net"
14
+# size:25x25
15 15
 qr_data = [
16 16
     [
17 17
         (255, 255, 255),
@@ -22,11 +22,15 @@ qr_data = [
22 22
         (255, 255, 255),
23 23
         (255, 255, 255),
24 24
         (0, 0, 0),
25
-        (0, 0, 0),
25
+        (255, 255, 255),
26 26
         (255, 255, 255),
27 27
         (0, 0, 0),
28
+        (255, 255, 255),
28 29
         (0, 0, 0),
29 30
         (255, 255, 255),
31
+        (255, 255, 255),
32
+        (255, 255, 255),
33
+        (255, 255, 255),
30 34
         (0, 0, 0),
31 35
         (255, 255, 255),
32 36
         (255, 255, 255),
@@ -45,11 +49,15 @@ qr_data = [
45 49
         (0, 0, 0),
46 50
         (255, 255, 255),
47 51
         (0, 0, 0),
48
-        (255, 255, 255),
49 52
         (0, 0, 0),
50
-        (255, 255, 255),
53
+        (0, 0, 0),
54
+        (0, 0, 0),
55
+        (0, 0, 0),
56
+        (0, 0, 0),
57
+        (0, 0, 0),
51 58
         (255, 255, 255),
52 59
         (0, 0, 0),
60
+        (255, 255, 255),
53 61
         (0, 0, 0),
54 62
         (255, 255, 255),
55 63
         (0, 0, 0),
@@ -70,10 +78,14 @@ qr_data = [
70 78
         (0, 0, 0),
71 79
         (0, 0, 0),
72 80
         (255, 255, 255),
81
+        (255, 255, 255),
82
+        (255, 255, 255),
83
+        (255, 255, 255),
73 84
         (0, 0, 0),
74 85
         (0, 0, 0),
75 86
         (255, 255, 255),
76 87
         (0, 0, 0),
88
+        (0, 0, 0),
77 89
         (255, 255, 255),
78 90
         (0, 0, 0),
79 91
         (255, 255, 255),
@@ -91,10 +103,14 @@ qr_data = [
91 103
         (0, 0, 0),
92 104
         (255, 255, 255),
93 105
         (0, 0, 0),
94
-        (0, 0, 0),
95 106
         (255, 255, 255),
96 107
         (0, 0, 0),
97
-        (0, 0, 0),
108
+        (255, 255, 255),
109
+        (255, 255, 255),
110
+        (255, 255, 255),
111
+        (255, 255, 255),
112
+        (255, 255, 255),
113
+        (255, 255, 255),
98 114
         (0, 0, 0),
99 115
         (0, 0, 0),
100 116
         (255, 255, 255),
@@ -115,9 +131,13 @@ qr_data = [
115 131
         (255, 255, 255),
116 132
         (0, 0, 0),
117 133
         (255, 255, 255),
118
-        (255, 255, 255),
134
+        (0, 0, 0),
119 135
         (255, 255, 255),
120 136
         (0, 0, 0),
137
+        (0, 0, 0),
138
+        (255, 255, 255),
139
+        (255, 255, 255),
140
+        (255, 255, 255),
121 141
         (255, 255, 255),
122 142
         (0, 0, 0),
123 143
         (255, 255, 255),
@@ -137,11 +157,15 @@ qr_data = [
137 157
         (0, 0, 0),
138 158
         (255, 255, 255),
139 159
         (0, 0, 0),
140
-        (0, 0, 0),
141
-        (0, 0, 0),
160
+        (255, 255, 255),
161
+        (255, 255, 255),
162
+        (255, 255, 255),
142 163
         (255, 255, 255),
143 164
         (0, 0, 0),
144 165
         (255, 255, 255),
166
+        (255, 255, 255),
167
+        (0, 0, 0),
168
+        (0, 0, 0),
145 169
         (0, 0, 0),
146 170
         (255, 255, 255),
147 171
         (0, 0, 0),
@@ -167,6 +191,10 @@ qr_data = [
167 191
         (255, 255, 255),
168 192
         (0, 0, 0),
169 193
         (255, 255, 255),
194
+        (0, 0, 0),
195
+        (255, 255, 255),
196
+        (0, 0, 0),
197
+        (255, 255, 255),
170 198
         (255, 255, 255),
171 199
         (255, 255, 255),
172 200
         (255, 255, 255),
@@ -183,10 +211,14 @@ qr_data = [
183 211
         (0, 0, 0),
184 212
         (0, 0, 0),
185 213
         (0, 0, 0),
214
+        (255, 255, 255),
215
+        (0, 0, 0),
216
+        (255, 255, 255),
186 217
         (0, 0, 0),
187 218
         (255, 255, 255),
188 219
         (0, 0, 0),
189 220
         (255, 255, 255),
221
+        (0, 0, 0),
190 222
         (255, 255, 255),
191 223
         (0, 0, 0),
192 224
         (0, 0, 0),
@@ -200,40 +232,72 @@ qr_data = [
200 232
     [
201 233
         (255, 255, 255),
202 234
         (0, 0, 0),
235
+        (0, 0, 0),
236
+        (0, 0, 0),
237
+        (255, 255, 255),
238
+        (0, 0, 0),
203 239
         (255, 255, 255),
240
+        (255, 255, 255),
241
+        (255, 255, 255),
242
+        (255, 255, 255),
243
+        (0, 0, 0),
204 244
         (0, 0, 0),
205 245
         (255, 255, 255),
206 246
         (0, 0, 0),
247
+        (0, 0, 0),
248
+        (0, 0, 0),
249
+        (255, 255, 255),
250
+        (255, 255, 255),
251
+        (255, 255, 255),
252
+        (255, 255, 255),
253
+        (255, 255, 255),
207 254
         (255, 255, 255),
208 255
         (0, 0, 0),
209 256
         (0, 0, 0),
210 257
         (255, 255, 255),
258
+    ],
259
+    [
211 260
         (255, 255, 255),
212 261
         (255, 255, 255),
213 262
         (0, 0, 0),
214 263
         (0, 0, 0),
215 264
         (0, 0, 0),
265
+        (255, 255, 255),
266
+        (0, 0, 0),
267
+        (255, 255, 255),
268
+        (255, 255, 255),
269
+        (255, 255, 255),
270
+        (0, 0, 0),
271
+        (255, 255, 255),
272
+        (255, 255, 255),
273
+        (255, 255, 255),
216 274
         (0, 0, 0),
217 275
         (255, 255, 255),
276
+        (255, 255, 255),
277
+        (0, 0, 0),
218 278
         (0, 0, 0),
219 279
         (0, 0, 0),
220 280
         (255, 255, 255),
281
+        (255, 255, 255),
282
+        (0, 0, 0),
283
+        (255, 255, 255),
221 284
         (0, 0, 0),
222 285
     ],
223 286
     [
224 287
         (0, 0, 0),
288
+        (0, 0, 0),
289
+        (0, 0, 0),
225 290
         (255, 255, 255),
226 291
         (0, 0, 0),
227 292
         (255, 255, 255),
228 293
         (255, 255, 255),
229 294
         (0, 0, 0),
295
+        (255, 255, 255),
230 296
         (0, 0, 0),
231 297
         (255, 255, 255),
232 298
         (255, 255, 255),
233 299
         (0, 0, 0),
234
-        (0, 0, 0),
235
-        (0, 0, 0),
236
-        (0, 0, 0),
300
+        (255, 255, 255),
237 301
         (0, 0, 0),
238 302
         (255, 255, 255),
239 303
         (255, 255, 255),
@@ -242,36 +306,77 @@ qr_data = [
242 306
         (0, 0, 0),
243 307
         (255, 255, 255),
244 308
         (255, 255, 255),
309
+        (255, 255, 255),
310
+        (0, 0, 0),
311
+        (0, 0, 0),
245 312
     ],
246 313
     [
314
+        (255, 255, 255),
315
+        (255, 255, 255),
316
+        (0, 0, 0),
317
+        (0, 0, 0),
318
+        (0, 0, 0),
319
+        (255, 255, 255),
320
+        (0, 0, 0),
321
+        (0, 0, 0),
322
+        (0, 0, 0),
247 323
         (0, 0, 0),
248 324
         (255, 255, 255),
325
+        (255, 255, 255),
326
+        (0, 0, 0),
327
+        (0, 0, 0),
328
+        (0, 0, 0),
329
+        (0, 0, 0),
330
+        (0, 0, 0),
249 331
         (0, 0, 0),
250 332
         (0, 0, 0),
251 333
         (0, 0, 0),
252 334
         (0, 0, 0),
335
+        (0, 0, 0),
336
+        (255, 255, 255),
253 337
         (255, 255, 255),
338
+        (0, 0, 0),
339
+    ],
340
+    [
254 341
         (255, 255, 255),
342
+        (0, 0, 0),
255 343
         (255, 255, 255),
256 344
         (0, 0, 0),
345
+        (0, 0, 0),
346
+        (0, 0, 0),
257 347
         (255, 255, 255),
258 348
         (0, 0, 0),
349
+        (0, 0, 0),
350
+        (0, 0, 0),
351
+        (0, 0, 0),
259 352
         (255, 255, 255),
353
+        (0, 0, 0),
354
+        (0, 0, 0),
260 355
         (255, 255, 255),
261 356
         (0, 0, 0),
357
+        (0, 0, 0),
262 358
         (255, 255, 255),
263 359
         (255, 255, 255),
264 360
         (255, 255, 255),
361
+        (0, 0, 0),
362
+        (255, 255, 255),
265 363
         (255, 255, 255),
266 364
         (255, 255, 255),
267 365
         (255, 255, 255),
268 366
     ],
269 367
     [
368
+        (255, 255, 255),
369
+        (255, 255, 255),
270 370
         (0, 0, 0),
371
+        (0, 0, 0),
372
+        (255, 255, 255),
271 373
         (255, 255, 255),
374
+        (0, 0, 0),
272 375
         (255, 255, 255),
273 376
         (0, 0, 0),
274 377
         (255, 255, 255),
378
+        (0, 0, 0),
379
+        (0, 0, 0),
275 380
         (255, 255, 255),
276 381
         (0, 0, 0),
277 382
         (255, 255, 255),
@@ -280,35 +385,90 @@ qr_data = [
280 385
         (0, 0, 0),
281 386
         (0, 0, 0),
282 387
         (0, 0, 0),
388
+        (255, 255, 255),
389
+        (0, 0, 0),
390
+        (0, 0, 0),
391
+        (255, 255, 255),
392
+        (0, 0, 0),
393
+    ],
394
+    [
395
+        (0, 0, 0),
396
+        (0, 0, 0),
397
+        (255, 255, 255),
398
+        (255, 255, 255),
399
+        (255, 255, 255),
400
+        (0, 0, 0),
401
+        (255, 255, 255),
402
+        (255, 255, 255),
283 403
         (0, 0, 0),
284 404
         (0, 0, 0),
285 405
         (0, 0, 0),
286 406
         (255, 255, 255),
407
+        (255, 255, 255),
408
+        (255, 255, 255),
409
+        (255, 255, 255),
410
+        (255, 255, 255),
411
+        (255, 255, 255),
412
+        (255, 255, 255),
287 413
         (0, 0, 0),
414
+        (255, 255, 255),
288 415
         (0, 0, 0),
289 416
         (255, 255, 255),
417
+        (255, 255, 255),
418
+        (0, 0, 0),
290 419
         (0, 0, 0),
291 420
     ],
292 421
     [
293 422
         (0, 0, 0),
423
+        (0, 0, 0),
424
+        (0, 0, 0),
425
+        (0, 0, 0),
294 426
         (255, 255, 255),
295 427
         (0, 0, 0),
428
+        (0, 0, 0),
429
+        (0, 0, 0),
296 430
         (255, 255, 255),
297 431
         (255, 255, 255),
298 432
         (0, 0, 0),
299 433
         (255, 255, 255),
300 434
         (0, 0, 0),
435
+        (0, 0, 0),
436
+        (0, 0, 0),
437
+        (0, 0, 0),
438
+        (0, 0, 0),
439
+        (255, 255, 255),
440
+        (255, 255, 255),
441
+        (255, 255, 255),
442
+        (0, 0, 0),
301 443
         (255, 255, 255),
302 444
         (255, 255, 255),
303 445
         (255, 255, 255),
304 446
         (0, 0, 0),
447
+    ],
448
+    [
305 449
         (255, 255, 255),
306 450
         (255, 255, 255),
307 451
         (0, 0, 0),
452
+        (0, 0, 0),
453
+        (0, 0, 0),
454
+        (0, 0, 0),
455
+        (255, 255, 255),
456
+        (0, 0, 0),
457
+        (0, 0, 0),
308 458
         (255, 255, 255),
309 459
         (255, 255, 255),
310 460
         (0, 0, 0),
461
+        (255, 255, 255),
462
+        (0, 0, 0),
311 463
         (0, 0, 0),
464
+        (255, 255, 255),
465
+        (255, 255, 255),
466
+        (255, 255, 255),
467
+        (255, 255, 255),
468
+        (255, 255, 255),
469
+        (255, 255, 255),
470
+        (255, 255, 255),
471
+        (255, 255, 255),
312 472
         (0, 0, 0),
313 473
         (0, 0, 0),
314 474
     ],
@@ -322,18 +482,22 @@ qr_data = [
322 482
         (0, 0, 0),
323 483
         (0, 0, 0),
324 484
         (255, 255, 255),
325
-        (0, 0, 0),
326 485
         (255, 255, 255),
327 486
         (255, 255, 255),
328 487
         (0, 0, 0),
488
+        (0, 0, 0),
489
+        (0, 0, 0),
329 490
         (255, 255, 255),
330 491
         (0, 0, 0),
492
+        (255, 255, 255),
331 493
         (0, 0, 0),
332 494
         (0, 0, 0),
333 495
         (0, 0, 0),
334 496
         (255, 255, 255),
497
+        (0, 0, 0),
335 498
         (255, 255, 255),
336 499
         (0, 0, 0),
500
+        (0, 0, 0),
337 501
     ],
338 502
     [
339 503
         (255, 255, 255),
@@ -344,19 +508,23 @@ qr_data = [
344 508
         (255, 255, 255),
345 509
         (255, 255, 255),
346 510
         (0, 0, 0),
347
-        (0, 0, 0),
348
-        (255, 255, 255),
349
-        (255, 255, 255),
350 511
         (255, 255, 255),
351 512
         (0, 0, 0),
513
+        (255, 255, 255),
352 514
         (0, 0, 0),
353 515
         (0, 0, 0),
354 516
         (255, 255, 255),
355 517
         (255, 255, 255),
356 518
         (0, 0, 0),
357 519
         (255, 255, 255),
520
+        (0, 0, 0),
521
+        (255, 255, 255),
522
+        (0, 0, 0),
358 523
         (255, 255, 255),
359 524
         (255, 255, 255),
525
+        (0, 0, 0),
526
+        (0, 0, 0),
527
+        (0, 0, 0),
360 528
     ],
361 529
     [
362 530
         (255, 255, 255),
@@ -369,17 +537,21 @@ qr_data = [
369 537
         (0, 0, 0),
370 538
         (0, 0, 0),
371 539
         (0, 0, 0),
372
-        (0, 0, 0),
373 540
         (255, 255, 255),
541
+        (0, 0, 0),
374 542
         (255, 255, 255),
375 543
         (0, 0, 0),
376 544
         (0, 0, 0),
377 545
         (255, 255, 255),
546
+        (255, 255, 255),
378 547
         (0, 0, 0),
379 548
         (0, 0, 0),
380 549
         (0, 0, 0),
381
-        (0, 0, 0),
382 550
         (255, 255, 255),
551
+        (255, 255, 255),
552
+        (255, 255, 255),
553
+        (255, 255, 255),
554
+        (0, 0, 0),
383 555
     ],
384 556
     [
385 557
         (255, 255, 255),
@@ -394,13 +566,17 @@ qr_data = [
394 566
         (255, 255, 255),
395 567
         (255, 255, 255),
396 568
         (255, 255, 255),
397
-        (0, 0, 0),
569
+        (255, 255, 255),
398 570
         (0, 0, 0),
399 571
         (0, 0, 0),
400 572
         (0, 0, 0),
401 573
         (255, 255, 255),
574
+        (255, 255, 255),
575
+        (255, 255, 255),
576
+        (255, 255, 255),
577
+        (255, 255, 255),
402 578
         (0, 0, 0),
403
-        (0, 0, 0),
579
+        (255, 255, 255),
404 580
         (0, 0, 0),
405 581
         (0, 0, 0),
406 582
     ],
@@ -416,16 +592,20 @@ qr_data = [
416 592
         (0, 0, 0),
417 593
         (255, 255, 255),
418 594
         (255, 255, 255),
419
-        (255, 255, 255),
420
-        (0, 0, 0),
421 595
         (0, 0, 0),
422 596
         (255, 255, 255),
423 597
         (255, 255, 255),
424 598
         (255, 255, 255),
425 599
         (0, 0, 0),
600
+        (0, 0, 0),
426 601
         (255, 255, 255),
427 602
         (255, 255, 255),
428 603
         (0, 0, 0),
604
+        (0, 0, 0),
605
+        (255, 255, 255),
606
+        (255, 255, 255),
607
+        (255, 255, 255),
608
+        (255, 255, 255),
429 609
     ],
430 610
     [
431 611
         (255, 255, 255),
@@ -436,19 +616,23 @@ qr_data = [
436 616
         (0, 0, 0),
437 617
         (255, 255, 255),
438 618
         (0, 0, 0),
439
-        (255, 255, 255),
440 619
         (0, 0, 0),
441 620
         (255, 255, 255),
442
-        (0, 0, 0),
443 621
         (255, 255, 255),
444
-        (0, 0, 0),
445 622
         (255, 255, 255),
446 623
         (0, 0, 0),
624
+        (0, 0, 0),
447 625
         (255, 255, 255),
448 626
         (0, 0, 0),
449 627
         (255, 255, 255),
628
+        (255, 255, 255),
629
+        (255, 255, 255),
630
+        (255, 255, 255),
631
+        (0, 0, 0),
632
+        (0, 0, 0),
450 633
         (0, 0, 0),
451 634
         (255, 255, 255),
635
+        (0, 0, 0),
452 636
     ],
453 637
     [
454 638
         (255, 255, 255),
@@ -461,15 +645,19 @@ qr_data = [
461 645
         (0, 0, 0),
462 646
         (0, 0, 0),
463 647
         (0, 0, 0),
648
+        (0, 0, 0),
464 649
         (255, 255, 255),
465 650
         (255, 255, 255),
466 651
         (0, 0, 0),
652
+        (255, 255, 255),
467 653
         (0, 0, 0),
468 654
         (0, 0, 0),
469 655
         (0, 0, 0),
470 656
         (255, 255, 255),
471
-        (0, 0, 0),
472
-        (0, 0, 0),
657
+        (255, 255, 255),
658
+        (255, 255, 255),
659
+        (255, 255, 255),
660
+        (255, 255, 255),
473 661
         (255, 255, 255),
474 662
         (0, 0, 0),
475 663
     ],
@@ -483,16 +671,20 @@ qr_data = [
483 671
         (255, 255, 255),
484 672
         (0, 0, 0),
485 673
         (255, 255, 255),
486
-        (255, 255, 255),
487 674
         (0, 0, 0),
488
-        (255, 255, 255),
489
-        (255, 255, 255),
490 675
         (0, 0, 0),
491 676
         (0, 0, 0),
677
+        (0, 0, 0),
678
+        (0, 0, 0),
679
+        (0, 0, 0),
680
+        (255, 255, 255),
681
+        (0, 0, 0),
492 682
         (255, 255, 255),
493 683
         (0, 0, 0),
494 684
         (0, 0, 0),
495 685
         (0, 0, 0),
686
+        (0, 0, 0),
687
+        (255, 255, 255),
496 688
         (255, 255, 255),
497 689
         (255, 255, 255),
498 690
     ],
@@ -504,17 +696,47 @@ if True:#__name__ == "__main__":
504 696
     from net import CheckHTTP
505 697
     from qr import QRScreen
506 698
     from scroll import ScrollText
699
+    from splash import SplashScreen
507 700
     from manager import Manager
508 701
 
702
+    #url = "http://ubabot.frubar.net"
703
+    url = "http://xythobuz.de"
704
+
509 705
     import util
510 706
     t = util.getTarget()
511 707
 
708
+    # Loading fonts and graphics takes a while.
709
+    # So show a splash screen while the user waits.
710
+    splash = SplashScreen(t)
711
+    t.loop_start()
712
+    splash.draw()
713
+    t.loop_end()
714
+
715
+    # UbaBot is online
716
+    success = Manager(t)
717
+    success.add(ScrollText(t, "Visit UbaBot Cocktail machine at FruBar village for drinks!", "bitmap8", 1, 10, camp_pink))
718
+    success.add(Solid(t, 1.0))
719
+    success.add(QRScreen(t, qr_data, 30.0, "Drinks", "bitmap6", (255, 255, 255), (0, 0, 0)))
720
+    success.add(Solid(t, 1.0))
721
+
722
+    # UbaBot is offline
723
+    fail = Manager(t)
724
+    fail.add(ScrollText(t, "The UbaBot Cocktail machine is closed. Please come back later!", "bitmap8", 1, 10, camp_pink))
725
+    fail.add(Solid(t, 1.0))
726
+    fail.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), None, 2.0))
727
+    fail.add(Solid(t, 1.0))
728
+
729
+    # UbaBot status checker
730
+    d = CheckHTTP(url)
731
+    d.success(success)
732
+    d.fail(fail)
733
+
512 734
     # Main "Menu"
513 735
     m = Manager(t)
514 736
     m.add(ScrollText(t, "#CCCAMP23", "bitmap8", 1, 10, camp_green))
515 737
     m.add(Solid(t, 1.0))
516
-    #m.add(QRScreen(t, qr_data, 30.0, "Drinks:", "bitmap8", (255, 255, 255), (0, 0, 0)))
517
-    #m.add(Solid(t, 1.0))
738
+    m.add(d) # HTTP Check, either "success" or "fail"
739
+    m.add(Solid(t, 1.0))
518 740
 
519 741
     m.restart()
520 742
     t.loop(m.draw)

+ 2
- 0
copy.sh View File

@@ -6,6 +6,7 @@ cp config.py /pyboard
6 6
 cp pico.py /pyboard
7 7
 cp util.py /pyboard
8 8
 cp manager.py /pyboard
9
+cp mapper.py /pyboard
9 10
 cp net.py /pyboard
10 11
 cp solid.py /pyboard
11 12
 cp splash.py /pyboard
@@ -22,6 +23,7 @@ cp config.py /pyboard
22 23
 cp pico.py /pyboard
23 24
 cp util.py /pyboard
24 25
 cp manager.py /pyboard
26
+cp mapper.py /pyboard
25 27
 cp net.py /pyboard
26 28
 cp solid.py /pyboard
27 29
 cp splash.py /pyboard

+ 3
- 2
image.py View File

@@ -68,8 +68,9 @@ class ImageScreen:
68 68
 
69 69
     def draw(self):
70 70
         if self.image.is_animated:
71
-            if (time.time() - self.frame) >= self.time:
72
-                self.frame = time.time()
71
+            now = time.time()
72
+            if (now - self.frame) >= self.time:
73
+                self.frame = now
73 74
                 self.count = (self.count + 1) % self.image.n_frames
74 75
                 if self.count == 0:
75 76
                     self.done += 1

+ 6
- 4
life.py View File

@@ -122,13 +122,15 @@ class GameOfLife:
122 122
         self.lastDiff = diff
123 123
 
124 124
     def draw(self):
125
-        if (time.time() - self.last) > self.interval:
126
-            self.last = time.time()
125
+        now = time.time()
126
+        if (now - self.last) > self.interval:
127
+            self.last = now
127 128
             self.step()
128 129
 
129 130
         if (self.randomizeColors != None) and (self.randomizeColors != True):
130
-            if (time.time() - self.lastColor) > self.randomizeColors:
131
-                self.lastColor = time.time()
131
+            now = time.time()
132
+            if (now - self.lastColor) > self.randomizeColors:
133
+                self.lastColor = now
132 134
                 self.randomize()
133 135
 
134 136
         for x in range(0, self.gui.width):

+ 3
- 2
manager.py View File

@@ -41,8 +41,9 @@ class Manager:
41 41
                 self.screens[self.index][0].restart()
42 42
         else:
43 43
             # use given timeout
44
-            if (time.time() - self.lastTime) > self.screens[self.index][1]:
45
-                self.lastTime = time.time()
44
+            now = time.time()
45
+            if ((now - self.lastTime) > self.screens[self.index][1]) or (now < self.lastTime):
46
+                self.lastTime = now
46 47
                 self.index = (self.index + 1) % len(self.screens)
47 48
                 self.done = (self.index == 0)
48 49
                 self.screens[self.index][0].restart()

+ 81
- 0
mapper.py View File

@@ -7,6 +7,17 @@
7 7
 # think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
8 8
 # ----------------------------------------------------------------------------
9 9
 
10
+import util
11
+import time
12
+import sys
13
+
14
+useNTP = False
15
+try:
16
+    import ntptime
17
+    useNTP = True
18
+except:
19
+    pass
20
+
10 21
 # Does nothing. Take this as parent for new mappers.
11 22
 class MapperNull:
12 23
     def __init__(self, g):
@@ -17,6 +28,9 @@ class MapperNull:
17 28
         self.panelW = self.gui.panelW
18 29
         self.panelH = self.gui.panelH
19 30
 
31
+        if hasattr(self.gui, "matrix"):
32
+            self.matrix = self.gui.matrix
33
+
20 34
     def loop_start(self):
21 35
         return self.gui.loop_start()
22 36
 
@@ -69,3 +83,70 @@ class MapperStripToRect(MapperNull):
69 83
             y -= self.panelH
70 84
 
71 85
         self.gui.set_pixel(x, y, color)
86
+
87
+# Fetches current time via NTP.
88
+# System time will be in UTC afterwards, not with local time-zone
89
+# offset like when using rshell (eg. when using with a Pico).
90
+#
91
+# Brightness of display will be adjusted according to current time.
92
+# To avoid being too bright at night.
93
+#
94
+# When used with the Interstate75 Pico implementation,
95
+# this needs to be the "first" element of the mapper chain.
96
+# Otherwise special handling for PicoText will not work.
97
+class MapperReduceBrightness(MapperNull):
98
+    def __init__(self, g):
99
+        super().__init__(g)
100
+        self.last = None
101
+        self.connected = False
102
+        self.refresh = 60 * 60 * 6
103
+        self.factor = 1.0
104
+
105
+    def fetch(self):
106
+        self.factor = 1.0
107
+
108
+        if not useNTP:
109
+            return
110
+
111
+        if not self.connected:
112
+            self.connected = util.connectToWiFi()
113
+
114
+        if self.connected:
115
+            now = time.time()
116
+            if (self.last == None) or ((now - self.last) >= self.refresh) or (now < self.last):
117
+                self.last = now
118
+
119
+                try:
120
+                    print("Before sync: " + str(time.localtime()))
121
+                    ntptime.settime()
122
+                    print("After sync: "  + str(time.localtime()))
123
+                except Exception as e:
124
+                    print()
125
+                    sys.print_exception(e)
126
+                    print()
127
+                    return
128
+
129
+        # (year, month, day, hour, minute, second, ?, ?)
130
+        now = time.localtime()
131
+
132
+        # 8pm utc == 22pm dst germany
133
+        night = (now[0], now[1], now[2], 20, 0, 0, 0, 0)
134
+
135
+        # 5am utc == 7am dst germany
136
+        morning = (now[0], now[1], now[2], 5, 0, 0, 0, 0)
137
+
138
+        if (now > morning) and (now < night):
139
+            self.factor = 1.0
140
+        else:
141
+            self.factor = 0.25
142
+
143
+    def adjust(self, color):
144
+        return (int(color[0] * self.factor), int(color[1] * self.factor), int(color[2] * self.factor))
145
+
146
+    def loop_end(self):
147
+        super().loop_end()
148
+        self.fetch()
149
+
150
+    def set_pixel(self, x, y, color):
151
+        color = self.adjust(color)
152
+        self.gui.set_pixel(x, y, color)

+ 18
- 4
net.py View File

@@ -9,6 +9,7 @@
9 9
 
10 10
 import time
11 11
 import util
12
+import sys
12 13
 
13 14
 class CheckHTTP:
14 15
     def __init__(self, u, r = 600.0):
@@ -16,6 +17,7 @@ class CheckHTTP:
16 17
         self.refresh = r
17 18
         self.successScreen = None
18 19
         self.failScreen = None
20
+        self.response = None
19 21
         self.get = util.getRequests()
20 22
 
21 23
         self.restart()
@@ -28,7 +30,12 @@ class CheckHTTP:
28 30
 
29 31
     def restart(self):
30 32
         self.start = time.time()
31
-        self.response = None
33
+
34
+        # when set to None here, manager will cause re-request every time
35
+        # we don't do it, caching the response for the full refresh time.
36
+        # this assumes the URL never changes, of course.
37
+        #self.response = None
38
+
32 39
         self.request()
33 40
 
34 41
         if self.successScreen != None:
@@ -40,12 +47,19 @@ class CheckHTTP:
40 47
         if self.get == None:
41 48
             return
42 49
 
43
-        if (self.response == None) or ((time.time() - self.start) >= self.refresh):
44
-            self.start = time.time()
50
+        now = time.time()
51
+        if (self.response == None) or ((now - self.start) >= self.refresh):
52
+            self.start = now
45 53
             try:
54
+                print("Refreshing " + self.url)
46 55
                 r = self.get(self.url)
56
+                r.close()
57
+                print("Response: " + str(r.status_code))
47 58
                 self.response = (r.status_code < 400)
48
-            except:
59
+            except Exception as e:
60
+                print()
61
+                sys.print_exception(e)
62
+                print()
49 63
                 self.response = False
50 64
 
51 65
     def finished(self):

+ 24
- 4
pico.py View File

@@ -11,6 +11,9 @@
11 11
 # ----------------------------------------------------------------------------
12 12
 
13 13
 import interstate75
14
+from mapper import MapperReduceBrightness
15
+import time
16
+from machine import Pin
14 17
 
15 18
 class PicoMatrix:
16 19
     def __init__(self, w = 32, h = 32):
@@ -26,11 +29,15 @@ class PicoMatrix:
26 29
         if (w != 32) or (h != 32):
27 30
             raise RuntimeError("TODO not yet supported")
28 31
 
29
-        self.matrix = interstate75.Interstate75(display = interstate75.DISPLAY_INTERSTATE75_32X32)
32
+        mode = interstate75.DISPLAY_INTERSTATE75_32X32
33
+        self.matrix = interstate75.Interstate75(display = mode)
30 34
 
31 35
         self.black = self.matrix.display.create_pen(0, 0, 0)
32 36
         self.white = self.matrix.display.create_pen(255, 255, 255)
33 37
 
38
+        self.ledTime = time.time()
39
+        self.led = Pin("LED", Pin.OUT)
40
+
34 41
         self.loop_start() # initialize with blank image for ScrollText constructor
35 42
 
36 43
     def loop_start(self):
@@ -47,9 +54,17 @@ class PicoMatrix:
47 54
         while True:
48 55
             if self.loop_start():
49 56
                 break
57
+
50 58
             if func != None:
51 59
                 func()
60
+
52 61
             self.loop_end()
62
+
63
+            now = time.time()
64
+            if ((now - self.ledTime) >= 0.5) or (now < self.ledTime):
65
+                self.ledTime = now
66
+                self.led.toggle()
67
+
53 68
         self.matrix.stop()
54 69
 
55 70
     def set_pixel(self, x, y, color):
@@ -72,7 +87,11 @@ class PicoText:
72 87
         if not earlyAbort:
73 88
             return self.gui.matrix.display.measure_text(s, scale=1)
74 89
 
75
-        pen = self.gui.matrix.display.create_pen(self.fg[0], self.fg[1], self.fg[2])
90
+        color = self.fg
91
+        if isinstance(self.gui, MapperReduceBrightness):
92
+            color = self.gui.adjust(color)
93
+
94
+        pen = self.gui.matrix.display.create_pen(color[0], color[1], color[2])
76 95
         self.gui.matrix.display.set_pen(pen)
77 96
 
78 97
         self.gui.matrix.display.set_font(f)
@@ -107,8 +126,9 @@ if __name__ == "__main__":
107 126
     def helper():
108 127
         global s, start, i
109 128
 
110
-        if (time.time() - start) > 2.0:
111
-            start = time.time()
129
+        now = time.time()
130
+        if ((now - start) > 2.0) or (now < start):
131
+            start = now
112 132
             i = (i + 1) % 2
113 133
 
114 134
         if i == 0:

+ 6
- 2
qr.py View File

@@ -91,7 +91,11 @@ class QRScreen:
91 91
                     self.gui.set_pixel(x + self.xOff + self.image.width, y + self.yOff, self.c2)
92 92
 
93 93
         if self.heading != None:
94
-            self.text.text(self.heading, self.font, 0, True, -10)
94
+            off = -10
95
+            if self.font == "bitmap6":
96
+                off -= 3
97
+
98
+            self.text.text(self.heading, self.font, 0, True, off)
95 99
 
96 100
         for x in range(0, self.image.width):
97 101
             for y in range(0, self.image.height):
@@ -106,7 +110,7 @@ if __name__ == "__main__":
106 110
 
107 111
     t = util.getTarget()
108 112
 
109
-    d = QRScreen(t, "Hello World", 10.0, "Drinks:", "tom-thumb", (255, 255, 255), (0, 0, 0))
113
+    d = QRScreen(t, "http://ubabot.frubar.net", 10.0, "Drinks:", "tom-thumb", (255, 255, 255), (0, 0, 0))
110 114
 
111 115
     # dump generated QR image to console, for embedding in Pico script
112 116
     print("Dumping QR image to qr_tmp.txt")

+ 4
- 5
scroll.py View File

@@ -33,7 +33,6 @@ class ScrollText:
33 33
         self.text = t
34 34
         self.font = f
35 35
         self.width = self.drawer.text(self.text, self.font, 0, False)
36
-        print(self.text, self.font, self.width)
37 36
 
38 37
     def restart(self):
39 38
         self.offset = -self.gui.width
@@ -44,11 +43,11 @@ class ScrollText:
44 43
         return (self.count >= self.iterations)
45 44
 
46 45
     def draw(self):
47
-        if (time.time() - self.last) > self.speed:
48
-            off = (time.time() - self.last) / self.speed
49
-            print(self.offset, "->", self.offset + int(off))
46
+        now = time.time()
47
+        if (now - self.last) > self.speed:
48
+            off = (now - self.last) / self.speed
50 49
             self.offset += int(off)
51
-            self.last = time.time()
50
+            self.last = now
52 51
             if self.offset >= self.width:
53 52
                 self.offset = -self.gui.width
54 53
                 self.count += 1

+ 3
- 2
snake.py View File

@@ -136,8 +136,9 @@ class Snake:
136 136
             # TODO "AI"
137 137
             pass
138 138
 
139
-        if (time.time() - self.last) >= self.timestep:
140
-            self.last = time.time()
139
+        now = time.time()
140
+        if (now - self.last) >= self.timestep:
141
+            self.last = now
141 142
 
142 143
             # only allow valid inputs
143 144
             tmp = self.direction + self.directionTmp

+ 5
- 3
solid.py View File

@@ -23,7 +23,8 @@ class Solid:
23 23
         self.start = time.time()
24 24
 
25 25
     def finished(self):
26
-        return (time.time() - self.start) >= self.time
26
+        now = time.time()
27
+        return ((now - self.start) >= self.time) or (now < self.start)
27 28
 
28 29
     def draw(self):
29 30
         for x in range(0, self.gui.width):
@@ -55,8 +56,9 @@ if __name__ == "__main__":
55 56
     def helper():
56 57
         global s, colors, ci, n, c
57 58
 
58
-        if (time.time() - s) >= 0.1:
59
-            s = time.time()
59
+        now = time.time()
60
+        if ((now - s) >= 0.1) or (now < s):
61
+            s = now
60 62
             n += 1
61 63
             if n >= 15:
62 64
                 ci = (ci + 1) % len(colors)

+ 3
- 2
tetris.py View File

@@ -243,8 +243,9 @@ class Tetris:
243 243
             # TODO "AI"
244 244
             self.button = None
245 245
 
246
-        if (self.button != None) or ((time.time() - self.last) >= self.timestep):
247
-            self.last = time.time()
246
+        now = time.time()
247
+        if (self.button != None) or ((now - self.last) >= self.timestep) or (now < self.last):
248
+            self.last = now
248 249
             cont = self.step()
249 250
             if cont == False:
250 251
                 self.done = True

+ 54
- 9
util.py View File

@@ -7,7 +7,10 @@
7 7
 # think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
8 8
 # ----------------------------------------------------------------------------
9 9
 
10
+import sys
11
+
10 12
 targetPlatform = None
13
+wifiConnected = False
11 14
 
12 15
 def isPi():
13 16
     global targetPlatform
@@ -43,17 +46,29 @@ def getTarget():
43 46
             # only print once
44 47
             print("Raspberry Pi Adafruit RGB LED Matrix detected")
45 48
         targetPlatform = "pi"
46
-    except:
49
+    except Exception as e:
50
+        print()
51
+        sys.print_exception(e)
52
+        print()
53
+
47 54
         try:
48 55
             # Next we try the Pico Interstate75 interface
49 56
             from pico import PicoMatrix
50
-            target = PicoMatrix()
57
+            pico = PicoMatrix()
58
+
59
+            # TODO hard-coded adjustments
60
+            from mapper import MapperReduceBrightness
61
+            target = MapperReduceBrightness(pico)
51 62
 
52 63
             if targetPlatform == None:
53 64
                 # only print once
54 65
                 print("Raspberry Pi Pico Interstate75 RGB LED Matrix detected")
55 66
             targetPlatform = "pico"
56
-        except:
67
+        except Exception as e:
68
+            print()
69
+            sys.print_exception(e)
70
+            print()
71
+
57 72
             # If this fails fall back to the SDL/pygame GUI
58 73
             from test import TestGUI
59 74
             target = TestGUI()
@@ -67,6 +82,21 @@ def getTarget():
67 82
 
68 83
 # https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/python_test_tcp/micropython_test_tcp_client.py
69 84
 def connectToWiFi():
85
+    global wifiConnected
86
+
87
+    if wifiConnected:
88
+        return True
89
+
90
+    # only use WiFi on Pico
91
+    try:
92
+        from pico import PicoMatrix
93
+    except Exception as e:
94
+        print()
95
+        sys.print_exception(e)
96
+        print()
97
+        wifiConnected = True
98
+        return True
99
+
70 100
     import network
71 101
     import time
72 102
     from config import Config
@@ -74,6 +104,7 @@ def connectToWiFi():
74 104
     # Check if wifi details have been set
75 105
     if len(Config.networks) == 0:
76 106
         print('Please set wifi ssid and password in config.py')
107
+        wifiConnected = False
77 108
         return False
78 109
 
79 110
     # Start WiFi hardware
@@ -92,14 +123,15 @@ def connectToWiFi():
92 123
                 break
93 124
     if (ssid == None) or (password == None):
94 125
         print("No known network found")
126
+        wifiConnected = False
95 127
         return False
96 128
 
97 129
     # Start connection
98 130
     wlan.connect(ssid, password)
99 131
 
100 132
     # Wait for connect success or failure
101
-    max_wait = 20
102
-    error_count = 20
133
+    max_wait = 40
134
+    error_count = max_wait
103 135
     while max_wait > 0:
104 136
         if wlan.status() >= 3:
105 137
             break
@@ -116,26 +148,35 @@ def connectToWiFi():
116 148
     # Handle connection error
117 149
     if wlan.status() != 3:
118 150
         print('wifi connection failed %d' % wlan.status())
151
+        wifiConnected = False
119 152
         return False
120 153
     else:
121 154
         print('connected')
122 155
         status = wlan.ifconfig()
123 156
         print('ip = ' + status[0])
124 157
 
158
+    wifiConnected = True
125 159
     return True
126 160
 
127 161
 def getRequests():
162
+    global wifiConnected
163
+
128 164
     try:
129 165
         # try to get normal python lib
130 166
         import requests
131 167
         return requests.get
132
-    except:
168
+    except Exception as e:
169
+        print()
170
+        sys.print_exception(e)
171
+        print()
172
+
133 173
         # if it fails try the Pi Pico MicroPython implementation
134 174
         import urequests as requests
135 175
 
136 176
         # in this case we also need to connect to WiFi first
137
-        if not connectToWiFi():
138
-            return None
177
+        if not wifiConnected:
178
+            if not connectToWiFi():
179
+                return None
139 180
 
140 181
         return requests.get
141 182
 
@@ -146,7 +187,11 @@ def getTextDrawer():
146 187
         # Try BDF parser library
147 188
         from bdf import DrawText
148 189
         return DrawText
149
-    except:
190
+    except Exception as e:
191
+        print()
192
+        sys.print_exception(e)
193
+        print()
194
+
150 195
         # fall back to the Pico Interstate75 implementation
151 196
         from pico import PicoText
152 197
         return PicoText

Loading…
Cancel
Save