-
Notifications
You must be signed in to change notification settings - Fork 0
/
shift_out.cpp
543 lines (448 loc) · 11.8 KB
/
shift_out.cpp
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include <Arduino.h>
#define EXPOSED_DIGITS
#undef EXPOSED_DIGITS
#define ULTRA_SLOW_ENABLED
#undef ULTRA_SLOW_ENABLED
const int latchPin = 2; /* STCP wokwi */
const int clockPin = 3; /* SHCP wokwi */
const int dataPin = 4; /* DS wokwi */
byte leds = 0;
byte uleds = 0;
byte pos = 15;
// this hardware uses pos values of
// zero 15 one 22 two 27 three 29 03 Jan 2022
// due to wiring and only 2 shift registers (16 bits)
byte bank = 0;
byte slew = 5;
uint8_t ledval = 0;
#define BLANKING 870
#define TICKED 22000
void timing_slowed(void) {
for (volatile unsigned long t_slo = TICKED; t_slo > 0; t_slo--) { }
}
void _bankSelect(void) {
if (bank == 1) {
uleds = 255;
shiftOut(dataPin, clockPin, MSBFIRST, uleds);
shiftOut(dataPin, clockPin, MSBFIRST, uleds);
return;
}
if (bank == 0) {
uleds = 0;
shiftOut(dataPin, clockPin, MSBFIRST, uleds);
shiftOut(dataPin, clockPin, MSBFIRST, uleds);
}
}
void _digitSelect(void) {
uleds = pos;
shiftOut(dataPin, clockPin, MSBFIRST, uleds);
}
#ifdef ULTRA_SLOW_ENABLED
#define ULTRA_SLOW -1
#endif
#ifndef ULTRA_SLOW_ENABLED
#define ULTRA_SLOW 0
#endif
void updateShiftRegister(void) {
digitalWrite(latchPin, LOW);
/*
if (bank == 0) {
uleds = 0;
shiftOut(dataPin, clockPin, MSBFIRST, uleds);
}
*/
// (near?) LAST artificial slowing 14:54z 03 Jan 2022: // timing_slowed();
_digitSelect(); // digit 0 1 2 3 4 5 6 or 7 using 'pos' as the index
/*
if (bank == 1) {
uleds = 0;
shiftOut(dataPin, clockPin, MSBFIRST, uleds);
}
*/
uleds = leds; // A-F 0-9 and a few other glyphs
shiftOut(dataPin, clockPin, MSBFIRST, uleds); // paint the character's glyph!
digitalWrite(latchPin, HIGH);
}
#ifdef EXPOSED_DIGITS
#define SLOW_POV_DEMO -1
#define DURATION 2 // was '2'
#define REPETITIONS 2 // was '1'
#define EXPOSE_DIGIT_PAINTING -1
#endif
#ifndef EXPOSED_DIGITS
#define SLOW_POV_DEMO 0
#define DURATION 480 // was '2' for exposed mode
#define REPETITIONS 2 // was '1'
#define EXPOSE_DIGIT_PAINTING 0
#endif
void blankleds(void) {
// leds = 255;
leds = 0;
updateShiftRegister();
}
#define MIN_BLANKING 1700
#define MIN_SHOWING 444
void setleds(void) {
leds = ledval;
updateShiftRegister();
if (!EXPOSE_DIGIT_PAINTING) {
for (volatile unsigned long t_min = MIN_BLANKING; t_min > 0; t_min--) { }
// delay(1); // CRITICAL - must be a finite, non-zero delay here
}
else {
// timing_slowed();
for (volatile unsigned long t_set = MIN_SHOWING; t_set > 0; t_set--) { }
// delay(1); // bright duration wokwi 14 dec
}
}
// #define MIN_ON_TIME 17000000 // 17 million great for slow demo
// #define MIN_ON_TIME 170000
#define MIN_ON_TIME 1700
void flash_digit(void) { // paint a single digit brightly, then immediately blank all LEDs
if (EXPOSE_DIGIT_PAINTING) {
if (SLOW_POV_DEMO) {
}
}
setleds();
if (EXPOSE_DIGIT_PAINTING) {
// timing_slowed();
}
// targetting
for (volatile unsigned long t_min_on = MIN_ON_TIME; t_min_on > 0; t_min_on--) { }
blankleds(); // waste no time in doing so!
// timing_slowed();
// for (volatile unsigned long bl = BLANKING; bl > 0; bl--) { }
}
void proc_encoding(void) {
uint8_t ledcpy;
// ledcpy = ledval ^ 0xff; // this seems very good
ledcpy = ledval;
ledval = ledcpy;
}
void in_column_zero(void) {
proc_encoding();
for (int i = REPETITIONS; i > 0; i--) {
pos = 15;
flash_digit();
}
}
void in_column_one(void) { // DIGIT 2
proc_encoding();
for (int i = REPETITIONS; i > 0; i--) {
pos = 22;
flash_digit();
}
}
void in_column_two(void) {
proc_encoding();
for (int i = REPETITIONS; i > 0; i--) {
pos = 27;
flash_digit();
}
}
void in_column_three(void) {
proc_encoding();
for (int i = REPETITIONS; i > 0; i--) {
pos = 29;
flash_digit();
}
}
void in_column_four(void) {
proc_encoding();
for (int i = REPETITIONS; i > 0; i--) {
pos = 1; // neg 226 pos 29
flash_digit();
}
}
void in_column_five(void) {
proc_encoding();
for (int i = REPETITIONS; i > 0; i--) {
pos = 2; // neg 226 pos 29
flash_digit();
}
}
void in_column_six(void) {
proc_encoding();
for (int i = REPETITIONS; i > 0; i--) {
pos = 4; // neg 226 pos 29
flash_digit();
}
}
void in_column_seven(void) {
proc_encoding();
for (int i = REPETITIONS; i > 0; i--) {
pos = 8; // neg 226 pos 29
flash_digit();
}
}
void encode_hw_testing(void) { // 3
ledval = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128;
}
void encode_bitpattern_aa(void) { // low nybble
ledval = 1 + 2 + 4 + 8;
}
void encode_bitpattern_bb(void) { // high nybble
ledval = 16 + 32 + 64 + 128;
}
void msg_tttt(void) { // message: '3223'
for (int j = 2; j > 0; j--) {
for (int k = DURATION; k > 0; k--) {
encode_hw_testing();
in_column_zero();
encode_hw_testing();
in_column_two();
encode_bitpattern_aa();
in_column_zero();
encode_bitpattern_bb();
in_column_zero();
}
}
}
int count = -1;
char buffer[8];
int line_reset;
void encode_one(void) { // 1
ledval = 0 + 2 + 4 + 0 + 0 + 0 + 0 + 0;
}
void encode_seven(void) { // 7
ledval = 1 + 2 + 4 + 0 + 0 + 0 + 0 + 0;
}
void encode_ltr_l(void) { // L
ledval = 0 + 0 + 0 + 8 + 16 + 32 + 0 + 0;
}
void encode_zero(void) { // 0
ledval = 1 + 2 + 4 + 8 + 16 + 32 + 0 + 0;
}
void encode_three(void) { // 3
ledval = 1 + 2 + 4 + 8 + 0 + 0 + 64 + 0;
}
void encode_four(void) { // 4
ledval = 0 + 2 + 4 + 0 + 0 + 32 + 64 + 0;
}
void encode_five(void) { // 5
ledval = 1 + 0 + 4 + 8 + 0 + 32 + 64 + 0;
}
void encode_six(void) { // 6
ledval = 1 + 0 + 4 + 8 + 16 + 32 + 64 + 0;
}
void encode_two(void) { // 2
ledval = 1 + 2 + 0 + 8 + 16 + 0 + 64 + 0;
}
void encode_eight(void) { // 8
ledval = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 0;
}
void encode_nine(void) { // 9
ledval = 1 + 2 + 4 + 8 + 0 + 32 + 64 + 0;
}
void encode_ltr_a(void) { // A -- the letter, A
ledval = 1 + 2 + 4 + 16 + 32 + 64;
}
void encode_ltr_b(void) { // b
ledval = 4 + 8 + 16 + 32 + 64;
}
void encode_ltr_c(void) { // C
ledval = 1 + 8 + 16 + 32;
}
void encode_ltr_d(void) { // d
ledval = 2 + 4 + 8 + 16 + 64;
}
void encode_ltr_e(void) { // E
ledval = 1 + 8 + 16 + 32 + 64;
}
void encode_ltr_f(void) { // F
ledval = 1 + 0 + 16 + 32 + 64;
}
void encode_ltr_blank(void) { // blank
ledval = 0;
}
void msg_le(void) { // message: 'LE '
for (int j = 2; j > 0; j--) {
for (int k = DURATION; k > 0; k--) {
encode_ltr_blank();
in_column_zero();
encode_ltr_blank();
in_column_one();
encode_ltr_e();
in_column_two();
encode_ltr_l();
in_column_three();
}
}
}
void msg_bef0(void) { // message: 'bEF0'
for (int j = 2; j > 0; j--) {
for (int k = DURATION; k > 0; k--) {
encode_zero();
in_column_zero();
encode_ltr_f();
in_column_one();
encode_ltr_e();
in_column_two();
encode_ltr_b();
in_column_three();
}
}
}
void msg_foca(void) { // message: 'F0CA'
for (int j = 2; j > 0; j--) {
for (int k = DURATION; k > 0; k--) {
encode_ltr_a();
in_column_zero();
encode_ltr_c();
in_column_one();
encode_zero();
in_column_two();
encode_ltr_f();
in_column_three();
}
}
}
void msg_cafe(void) { // message: 'CAFE'
for (int j = 2; j > 0; j--) {
for (int k = DURATION; k > 0; k--) {
encode_ltr_e();
in_column_zero();
encode_ltr_f();
in_column_one();
encode_ltr_a();
in_column_two();
encode_ltr_c();
in_column_three();
}
}
}
void letter_test(void) {
for (int j = 2; j > 0; j--) {
for (int k = DURATION; k > 0; k--) {
encode_ltr_d();
in_column_four();
blankleds();
encode_ltr_c();
in_column_five();
blankleds();
encode_ltr_b();
in_column_six();
blankleds();
encode_ltr_a();
in_column_seven();
}
}
}
void msg_full_house(void) { // message: '01234567'
for (int j = 2; j > 0; j--) {
for (int k = DURATION; k > 0; k--) {
encode_seven();
in_column_zero();
encode_six();
in_column_one();
encode_five();
in_column_two();
encode_four();
in_column_three();
encode_three();
in_column_four();
encode_two();
in_column_five();
encode_one();
in_column_six();
encode_zero();
in_column_seven();
}
}
}
void setup_sr(void) {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
#ifdef EXPOSED_DIGITS
#define REPS 2
#endif
#ifndef EXPOSED_DIGITS
// #define REPS 123
// #define REPS 128
// #define REPS 32
#define REPS 960
#endif
void t_btwn_msgs(void) {
// 22000 x 256 = 5632000
// for (volatile int idx = 256; idx > 0; idx --) {
// for (volatile int idx = (256 * 256) ; idx > 0; idx --) {
// ##bookmark
for (volatile unsigned long idx = (32 * 32 * 2) ; idx > 0; idx --) {
timing_slowed(); /* timing_slowed(); timing_slowed(); timing_slowed();
timing_slowed(); timing_slowed(); timing_slowed(); timing_slowed();
timing_slowed(); timing_slowed(); timing_slowed(); timing_slowed();
timing_slowed(); timing_slowed(); timing_slowed(); timing_slowed();
timing_slowed(); timing_slowed(); timing_slowed(); timing_slowed();
timing_slowed(); timing_slowed(); timing_slowed(); timing_slowed();
timing_slowed(); timing_slowed(); timing_slowed(); timing_slowed();
timing_slowed(); timing_slowed(); timing_slowed(); timing_slowed(); */
}
}
extern uint8_t FL; // =0; // flags
void lfc_test(void) {
blankleds();
if (FL != 179) { // stop blinking
msg_le(); t_btwn_msgs();
}
if (FL != 179) {
msg_foca(); t_btwn_msgs();
}
if (FL != 179) {
msg_cafe(); t_btwn_msgs();
}
}
void loop_sr(void) {
lfc_test();
/*
blankleds();
for (unsigned long count = REPS; count > 0; count--) {
bank = 0;
encode_seven();
in_column_zero();
encode_six();
in_column_one();
encode_five();
in_column_two();
encode_four();
in_column_three();
}
t_btwn_msgs(); blankleds();
for (unsigned long count = REPS; count > 0; count--) {
encode_three();
in_column_zero();
encode_two();
in_column_one();
encode_one();
in_column_two();
encode_zero();
in_column_three();
}
t_btwn_msgs(); blankleds();
for (int count = REPS; count > 0; count--) {
// bank = 1;
encode_ltr_blank();
in_column_zero();
encode_ltr_e();
in_column_one();
encode_ltr_e();
in_column_two();
encode_ltr_f();
in_column_three();
}
t_btwn_msgs(); blankleds();
for (unsigned long count = REPS; count > 0; count--) {
encode_ltr_f();
in_column_zero();
encode_zero();
in_column_one();
encode_ltr_c();
in_column_two();
encode_ltr_blank();
in_column_three();
}
t_btwn_msgs(); // mating blankleds() begins loop_sr()
*/
}
// END.