-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathikosi_1.ino
2761 lines (2370 loc) · 75.2 KB
/
ikosi_1.ino
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ikosi_1.ino
requires valid "key_to_LED_map" and "gel" arrays generated by ikosi_train.ino
Includes:
- WS2812 (Data output at GPIO PA7)
- Sensor key read
- Sensor key mapping and learn mode
Linux:
stty -F /dev/ttyUSB0 sane 115200 && cat /dev/ttyUSB0
or stty -F /dev/ttyUSB0 sane 115200 igncr && cat /dev/ttyUSB0
screen /dev/ttyUSB0 115200 (terminate with "C-a k" or "C-a \")
minicom -D /dev/ttyUSB0 -b 115200 (terminate with "C-a x", change CR mode: "C-a u", disable HW control flow!)
ToDo:
Implement a eoChangeColor function
inital color is 0,0,0
fade to new color 0,0,0 with tick cnt
B12, D10
*/
#include <stdarg.h>
/*================================================*/
/* configuration */
/* As soon as the user select a key, then the corresponding edge will become this color */
/* if all rgb values are 0, then there will be no automatic key highlightning */
uint8_t sensor_key_select_color_r = 0;
uint8_t sensor_key_select_color_g = 0;
uint8_t sensor_key_select_color_b = 0;
/* if a edge is black, then it is replaced with this color */
uint8_t black_replacement_r = 20;
uint8_t black_replacement_g = 30;
uint8_t black_replacement_b = 20;
/* player 1 color */
uint8_t p1_r = 0;
uint8_t p1_g = 0;
uint8_t p1_b = 250;
/* player 1 correct edge indication color */
uint8_t p1v_r = 0;
uint8_t p1v_g = 150;
uint8_t p1v_b = 250;
/* illegal edge for player 1 */
uint8_t p1i_r = 0;
uint8_t p1i_g = 0;
uint8_t p1i_b = 250;
/* player 2 color */
uint8_t p2_r = 250;
uint8_t p2_g = 0;
uint8_t p2_b = 0;
/* player 2 correct edge indication color */
uint8_t p2v_r = 250;
uint8_t p2v_g = 150;
uint8_t p2v_b = 0;
/* illegal edge for player 2 */
uint8_t p2i_r = 250;
uint8_t p2i_g = 0;
uint8_t p2i_b = 0;
/*================================================*/
/* GEL Array: Graph definitions */
/* graph element struct (actually it is the edge of the ikosidodecaeder */
struct ge_struct
{
int16_t led; // led number, LED can be derived via key_to_LED_map[key]
int16_t key; // index into touch_status_list[]
int16_t pentagon; // pentagon number, starts with 0
int16_t triangle; // triangle number, starts with 0
int16_t next[6]; // each ikosidodecaeder has six neighbour edges
/*
if edge is assumed to be part of a pentagon then
next[0] is previous clock wise edge of the pentagon
next[5] is next clock wise edge of the pentagon
if edge is assumed to be part of a triangle then
next[3] is previous clock wise edge of the triangle
next[2] is next clock wise edge of the triangle
next[1] and next[4] describe the 10-edge ring of the ikosidodecaedron
Looping over that ring is a little bit more complicated, because the next
position is devinde alternating between next[1] and next[4]
pos = gel[pos].next[4];
for( i = 1; i < 10; i++ )
{
if ( (i & 1) != 0 )
pos = gel[pos].next[1];
else
pos = gel[pos].next[4];
}
*/
};
/*
low cap warning for key 14 (B9), cap=9, sensor might be disconnected
low cap warning for key 29 (C8), cap=9, sensor might be disconnected
low cap warning for key 36 (D1), cap=11, sensor might be disconnected
low cap warning for key 40 (D5), cap=11, sensor might be disconnected
low cap warning for key 12 (B7), cap=14, sensor might be disconnected
low cap warning for key 36 (D1), cap=6, sensor might be disconnected
low cap warning for key 38 (D3), cap=14, sensor might be disconnected
low cap warning for key 39 (D4), cap=13, sensor might be disconnected
low cap warning for key 40 (D5), cap=6, sensor might be disconnected
low cap warning for key 46 (D11), cap=5, sensor might be disconnected
low cap warning for key 47 (D12), cap=14, sensor might be disconnected
low cap warning for key 56 (E5), cap=13, sensor might be disconnected
low cap warning for key 62 (E11), cap=13, sensor might be disconnected
*/
/* gel is learned by ikosi_learn.ino, result has to be inserted here */
struct ge_struct gel[60] = {
{36,0 /* A2 */,0,0,{-1,-1,53,-1,-1,27}}
,{51,1 /* A3 */,0,1,{-1,-1,7,-1,-1,0}}
,{21,2 /* A4 */,1,2,{-1,-1,18,-1,-1,26}}
,{25,3 /* A5 */,1,3,{-1,-1,34,-1,-1,2}}
,{20,5 /* A8 */,2,4,{-1,-1,26,-1,-1,44}}
,{17,6 /* A15 */,3,5,{-1,-1,15,-1,-1,28}}
,{44,7 /* B0 */,0,6,{-1,-1,41,-1,-1,51}}
,{59,8 /* B1 */,4,1,{-1,-1,46,-1,-1,59}}
,{8,9 /* B3 */,5,7,{-1,-1,9,-1,-1,54}}
,{56,10 /* B5 */,6,7,{-1,-1,50,-1,-1,13}}
,{58,11 /* B6 */,6,8,{-1,-1,14,-1,-1,12}}
,{4,12 /* B7 */,6,9,{-1,-1,37,-1,-1,9}}
,{48,13 /* B8 */,6,10,{-1,-1,49,-1,-1,11}}
,{7,14 /* B9 */,6,11,{-1,-1,24,-1,-1,10}}
,{49,15 /* B10 */,4,8,{-1,-1,32,-1,-1,17}}
,{19,16 /* B11 */,7,5,{-1,-1,43,-1,-1,31}}
,{63,18 /* B13 */,8,12,{-1,-1,59,-1,-1,22}}
,{55,19 /* B14 */,4,11,{-1,-1,13,-1,-1,45}}
,{22,21 /* C0 */,9,2,{-1,-1,23,-1,-1,29}}
,{41,22 /* C1 */,10,13,{-1,-1,45,-1,-1,36}}
,{28,23 /* C2 */,2,14,{-1,-1,27,-1,-1,4}}
,{5,24 /* C3 */,3,15,{-1,-1,48,-1,-1,5}}
,{46,25 /* C4 */,8,6,{-1,-1,6,-1,-1,57}}
,{11,26 /* C5 */,3,2,{-1,-1,2,-1,-1,21}}
,{57,27 /* C6 */,5,11,{-1,-1,17,-1,-1,8}}
,{31,28 /* C7 */,2,16,{-1,-1,55,-1,-1,41}}
,{12,29 /* C8 */,1,4,{-1,-1,56,-1,-1,38}}
,{27,30 /* C9 */,0,14,{-1,-1,38,-1,-1,6}}
,{14,31 /* C10 */,3,17,{-1,-1,40,-1,-1,56}}
,{24,32 /* C11 */,9,3,{-1,-1,3,-1,-1,47}}
,{38,33 /* C12 */,1,0,{-1,-1,0,-1,-1,3}}
,{2,34 /* C13 */,7,15,{-1,-1,21,-1,-1,42}}
,{62,35 /* D0 */,8,8,{-1,-1,10,-1,-1,16}}
,{32,36 /* D1 */,11,10,{-1,-1,12,-1,-1,55}}
,{37,37 /* D2 */,10,3,{-1,-1,29,-1,-1,53}}
,{40,38 /* D3 */,5,18,{-1,-1,47,-1,-1,58}}
,{42,39 /* D4 */,10,18,{-1,-1,35,-1,-1,34}}
,{15,40 /* D5 */,11,9,{-1,-1,52,-1,-1,33}}
,{26,41 /* D6 */,1,14,{-1,-1,20,-1,-1,30}}
,{9,42 /* D7 */,9,19,{-1,-1,54,-1,-1,48}}
,{16,44 /* D9 */,11,17,{-1,-1,44,-1,-1,43}}
,{45,46 /* D11 */,2,6,{-1,-1,22,-1,-1,20}}
,{3,47 /* D12 */,7,19,{-1,-1,39,-1,-1,50}}
,{30,48 /* D13 */,11,5,{-1,-1,5,-1,-1,37}}
,{0,49 /* D14 */,2,17,{-1,-1,28,-1,-1,25}}
,{53,50 /* D15 */,4,13,{-1,-1,58,-1,-1,7}}
,{52,53 /* E2 */,10,1,{-1,-1,1,-1,-1,19}}
,{39,54 /* E3 */,9,18,{-1,-1,36,-1,-1,39}}
,{10,55 /* E4 */,9,15,{-1,-1,31,-1,-1,18}}
,{47,56 /* E5 */,8,10,{-1,-1,33,-1,-1,32}}
,{6,57 /* E6 */,7,7,{-1,-1,8,-1,-1,52}}
,{50,58 /* E7 */,0,12,{-1,-1,16,-1,-1,1}}
,{1,59 /* E8 */,7,9,{-1,-1,11,-1,-1,15}}
,{43,60 /* E9 */,10,0,{-1,-1,30,-1,-1,46}}
,{23,61 /* E10 */,5,19,{-1,-1,42,-1,-1,35}}
,{18,62 /* E11 */,11,16,{-1,-1,57,-1,-1,40}}
,{13,63 /* E12 */,3,4,{-1,-1,4,-1,-1,23}}
,{29,64 /* E13 */,8,16,{-1,-1,25,-1,-1,49}}
,{54,65 /* E14 */,5,13,{-1,-1,19,-1,-1,24}}
,{60,66 /* E15 */,4,12,{-1,-1,51,-1,-1,14}}
};
/*================================================*/
typedef int (*eo_cb)(struct _eo_struct *eo, unsigned msg, unsigned arg);
/*================================================*/
/* Wrapper for Arduino Serial.print to have something like printf() for debugging */
void p(const char *fmt, ...)
{
static char s[1024];
va_list va;
va_start(va, fmt);
vsnprintf(s, 1024, fmt, va);
va_end(va);
Serial.print(s);
}
/* same as p() but adds a line feed at the end */
void pn(const char *fmt, ...)
{
static char s[1024];
va_list va;
va_start(va, fmt);
vsnprintf(s, 1024, fmt, va);
va_end(va);
Serial.print(s);
Serial.print("\n");
}
/*================================================*/
/*
Interface to the 16x16 LED Matrix (64 LEDs, 4 unused)
Each LED includes a WS2812 controller (WS2812 Interface)
The WS2812 receives input via SPI bus.
The SPI bis is "misused" to generate the required wave form for the WS2812
4 Bit in the SPI Bus are used to sent one bit of information to the 64x WS2812
controller.
Assumptions:
SystemCoreClock: 168 MHz
APB2: 84Mhz
SPI Clock BR = 100 --> Clock Divide by 32 --> 84 / 32 = 2.625 MHz --> 380ns
per bit:
0: 1000 --> 380ns pulse for the WS2812 (logical 0 bit)
1: 1100 --> 760ns pulse for the WS2812 (logical 1 bit)
Each SPI word is 16 bit in size: We can transfer 4 bits per SPI word.
*/
struct _ws2812_spi
{
volatile uint8_t *spi_data;
volatile uint32_t byte_cnt; // remaining bytes
volatile uint32_t b; // the current byte
volatile uint32_t bit_cnt;
volatile uint32_t post_data_wait;
volatile uint32_t isr_cnt;
volatile uint32_t spi_status_register;
volatile uint32_t isr_ticks;
volatile uint32_t max_isr_ticks;
volatile int in_progress;
};
typedef struct _ws2812_spi ws2812_spi_t;
ws2812_spi_t ws2812_spi;
void ws2812_spi_init(void)
{
ws2812_spi.isr_cnt = 0;
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
delay(1);
SPI1->CR1 = 0; /* disable SPI */
SPI1->I2SCFGR = 0; /* no I2S */
SPI1->CR2 = 0; /* disable interrupts */
/* PA7, AF5 --> SPI1 MOSI */
GPIOA->MODER &= ~GPIO_MODER_MODER7;
GPIOA->MODER |= GPIO_MODER_MODER7_1; /* alternate function mode */
GPIOA->OTYPER &= ~GPIO_OTYPER_OT7; /* push pull */
GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD7; /* no pullup/pulldown */
GPIOA->AFR[0] &= ~GPIO_AFRL_AFSEL7;
GPIOA->AFR[0] |= GPIO_AFRL_AFSEL7_2 | GPIO_AFRL_AFSEL7_0; /* configure AF5, bitmask 0101 */
NVIC_SetPriority(SPI1_IRQn, 2); /* 0: highes priority, 3: lowest priority */
NVIC_EnableIRQ(SPI1_IRQn);
}
/*
This SPI handler will do an online conversion of the bit values to the WS2812B format
4 bits of data are converted into two bytes (16 bit) of data
This procedure is time critical: 64 Sys-Clock-Cycle per SPI bit 64*16 = 1024 Clock Cycles
==> Upper Limit for this procedure are 1024 clock cycles
*/
extern "C" void __attribute__ ((interrupt)) SPI1_IRQHandler(void)
{
ws2812_spi.spi_status_register = SPI1->SR;
ws2812_spi.isr_cnt++;
if ( (ws2812_spi.spi_status_register & SPI_SR_TXE) == 0 )
return;
if ( ws2812_spi.byte_cnt > 0 || ws2812_spi.bit_cnt > 0 )
{
uint16_t d = 0x4444;
uint16_t b;
if ( ws2812_spi.bit_cnt == 0 )
{
ws2812_spi.b = *ws2812_spi.spi_data;
ws2812_spi.spi_data++;
ws2812_spi.byte_cnt--;
ws2812_spi.bit_cnt = 2;
}
b = ws2812_spi.b;
if ( b & 128 )
d |= 0x2000;
if ( b & 64 )
d |= 0x0200;
if ( b & 32 )
d |= 0x0020;
if ( b & 16 )
d |= 0x0002;
b <<= 4;
ws2812_spi.b = b;
SPI1->DR = d;
ws2812_spi.bit_cnt--;
}
else
{
if ( ws2812_spi.post_data_wait > 0 )
{
/* wait for 50us, this are 125 SPI clocks (each is 0.4us) --> send 128 bits, 8x 16 Bit words */
SPI1->DR = 0;
ws2812_spi.post_data_wait--;
}
else
{
/* ensure, that the SCK goes to low after the byte transfer... */
/* disable interrupt, needs to be re-enabled whenever new data should be transmitted */
SPI1->CR2 = 0;
ws2812_spi.in_progress = 0;
}
}
}
void ws2812_spi_out(uint8_t *data, int cnt)
{
/* wait until data is transmitted */
if ( ws2812_spi.in_progress != 0 )
{
while( ws2812_spi.in_progress != 0 )
;
delay(1);
}
SPI1->CR1 = 0; /* disable SPI1 */
SPI1->CR1 = 0
| SPI_CR1_DFF /* select 16 bit data format */
| SPI_CR1_BR_2 /* 100: divide by 32 */
| SPI_CR1_MSTR /* master transmit */
| SPI_CR1_SSM /* SW Slave Management */
| SPI_CR1_SSI /* Internal Slave Select */
| SPI_CR1_BIDIMODE /* select single line (Master: MOSI pin) bidirectional mode */
| SPI_CR1_BIDIOE /* select transmit mode*/
;
SPI1->CR2 = 0
| SPI_CR2_TXEIE /* buffer empty interrupt */
;
ws2812_spi.spi_data = data;
ws2812_spi.byte_cnt = cnt;
ws2812_spi.bit_cnt = 0;
ws2812_spi.post_data_wait = 9; /* 8 would be sufficient, use 9 to be on the safe side */
ws2812_spi.in_progress = 1;
SPI1->CR1 |= SPI_CR1_SPE; /* enable SPI */
/* load first byte, so that the TXRDY interrupt will be generated */
/* this is just a zero byte and is ignored by the WS2812B */
SPI1->DR = 0;
}
/*================================================*/
/*
RGB Matrix Procedure
"Matrix" is misleading: Although the hardware is a 16x16 matrix, the
LEDs of that matrix are accessed by there sequence number (0...63)
The RGB Matrix is build on top of the WS2812 procedures.
One RGB Matrix "Plane": Contains the RGB information for 64 LEDs.
There are several special planes:
LEDMatrixData: This is the plane, which is set to the 16x16 LED Matrix by
the WS2812 procedures
LEDPlaneMatrixData: This is an array with multiple planes (LED_PLANE_CNT)
The idea is to add the RGB values of all the planes in "LEDPlaneMatrixData"
and store the resulting RGB value in LEDMatrixData (sendAllPlanes()).
LEDPlaneMatrixData[0] is an exception: none-zero values
are copied to LEDMatrixData without adding more data.
LEDPlaneMatrixData[0] will stay zero except fo the element which has
been touched by the user. So LEDPlaneMatrixData[0] will give
immediate feedback to the user, which element has been pressed.
Current plane usage:
LEDPlaneMatrixData[0] Cursor, not used any more
LEDPlaneMatrixData[1] Edge Objects (eol)
LEDPlaneMatrixData[2] Edge Objects (eol)
LEDPlaneMatrixData[4] Fixed Colors
*/
/* number of LEDs per plane */
#define LED_CNT 64
/* number of planes. RGB value of each plane are added and sent to the target */
#define LED_PLANE_CNT 4
uint8_t LEDMatrixData[LED_CNT*3];
uint8_t LEDPlaneMatrixData[LED_PLANE_CNT][LED_CNT*3];
void initLEDMatrix(void)
{
ws2812_spi_init();
}
void sendLEDMatrix(void)
{
ws2812_spi_out(LEDMatrixData, LED_CNT*3);
}
/*
Sum up planes 1 to LED_PLANE_CNT-1.
Plane 0 will overwrite the result and replace the result if the RGB value in
plane 0 is not black (plane 0 overrides all other planes).
The result is sent to the RGB 16x16 matrix
*/
void sendAllPlanes(void)
{
int i, plane;
unsigned v;
for( i = 0; i < 64*3; i+=3 )
{
LEDMatrixData[i] = 0;
LEDMatrixData[i+1] = 0;
LEDMatrixData[i+2] = 0;
/*
Sum up all RGB channels from all planes.
Summed up RGB values are limited to white
Start with the highest plane.
The lowest plane 0 may discard all calculations are overwrite
the color if the RGB color is not black in plane 0.
*/
for( plane = LED_PLANE_CNT-1; plane >= 0; plane-- )
{
if ( plane == 0 )
{
if ( LEDPlaneMatrixData[plane][i] == 0
&& LEDPlaneMatrixData[plane][i+1] == 0
&& LEDPlaneMatrixData[plane][i+2] == 0 )
{
/* do nothing */
}
else
{
/* replace color */
LEDMatrixData[i] = LEDPlaneMatrixData[plane][i];
LEDMatrixData[i+1] = LEDPlaneMatrixData[plane][i+1];
LEDMatrixData[i+2] = LEDPlaneMatrixData[plane][i+2];
}
}
else
{
/* green */
v = LEDMatrixData[i] + LEDPlaneMatrixData[plane][i];
if ( v >= 255 )
v = 255;
LEDMatrixData[i] = v;
/* red */
v = LEDMatrixData[i+1] + LEDPlaneMatrixData[plane][i+1];
if ( v >= 255 )
v = 255;
LEDMatrixData[i+1] = v;
/* blue */
v = LEDMatrixData[i+2] + LEDPlaneMatrixData[plane][i+2];
if ( v >= 255 )
v = 255;
LEDMatrixData[i+2] = v;
}
}
}
/* consider only LEDs which are in use */
for( int j = 0; j < 60; j++ )
{
i = gel[j].led*3;
/* replace full black by the replacement color */
if ( LEDMatrixData[i] == 0 && LEDMatrixData[i+1] == 0 && LEDMatrixData[i+2] == 0 )
{
LEDMatrixData[i+0] = black_replacement_g;
LEDMatrixData[i+1] = black_replacement_r;
LEDMatrixData[i+2] = black_replacement_b;
}
}
sendLEDMatrix();
}
void clearPlane(unsigned plane)
{
int i;
if ( plane < LED_PLANE_CNT )
{
for( i = 0; i < 64*3; i++ )
{
LEDPlaneMatrixData[plane][i] = 0;
}
}
}
/*
void clearAllPlanes(void)
Description:
Clear all planes with black color.
*/
void clearAllPlanes(void)
{
int plane;
for( plane = 0; plane < LED_PLANE_CNT; plane++ )
clearPlane(plane);
}
void clearAllPlanesExceptZero(void)
{
int plane;
for( plane = 1; plane < LED_PLANE_CNT; plane++ )
clearPlane(plane);
}
/*
void setPlaneRGB(unsigned plane, uint8_t pos, uint8_t r, uint8_t g, uint8_t b)
Description:
Set a single color in a plane.
Plane 0 is special: If plane 0 color is not black, than this color will
overwrite all other colors.
Colors in all other planes above 0 (1..LED_PLANE_CNT-1) are summed up
Args:
plane: 0..LED_PLANE_CNT-1
pos: LED number, either from the "key_to_LED_map[]" array or the "gel.led" member.
r Red channel of the LED color (0..255)
g Green channel of the LED color (0..255)
b Blue channel of the LED color (0..255)
*/
void setPlaneRGB(unsigned plane, uint8_t pos, uint8_t r, uint8_t g, uint8_t b)
{
if ( plane < LED_PLANE_CNT )
{
if ( pos < 64 )
{
LEDPlaneMatrixData[plane][pos*3] = g;
LEDPlaneMatrixData[plane][pos*3+1] = r;
LEDPlaneMatrixData[plane][pos*3+2] = b;
}
}
}
/* https://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to-rgb-in-range-0-255-for-both */
void hsv_to_rgb(uint8_t h, uint8_t s, uint8_t v, uint8_t *r, uint8_t *g, uint8_t *b)
{
uint8_t region, remainder, p, q, t;
if (s == 0)
{
*r = v;
*g = v;
*b = v;
return ;
}
region = h / 43;
remainder = (h - (region * 43)) * 6;
p = (v * (uint16_t)(255 - s)) >> 8;
q = (v * (uint16_t)(255 - ((s * remainder) >> 8))) >> 8;
t = (v * (uint16_t)(255 - ((s * (uint16_t)(255 - remainder)) >> 8))) >> 8;
switch (region)
{
case 0:
*r = v; *g = t; *b = p;
break;
case 1:
*r = q; *g = v; *b = p;
break;
case 2:
*r = p; *g = v; *b = t;
break;
case 3:
*r = p; *g = q; *b = v;
break;
case 4:
*r = t; *g = p; *b = v;
break;
default:
*r = v; *g = p; *b = q;
break;
}
}
/*
void setPlaneHSV(unsigned plane, uint8_t pos, uint8_t h, uint8_t s, uint8_t v)
Description:
Set a single color in a plane.
Plane 0 is special: If plane 0 color is not black, than this color will
overwrite all other colors.
Colors in all other planes above 0 (1..LED_PLANE_CNT-1) are summed up
Args:
plane: 0..LED_PLANE_CNT-1
pos: LED number, either from the "key_to_LED_map[]" array or the "gel.led" member.
h Hue of the LED color (0..255)
s Saturation of the LED color (0..255)
v Value (brightness) of the LED color (0..255)
*/
void setPlaneHSV(unsigned plane, uint8_t pos, uint8_t h, uint8_t s, uint8_t v)
{
hsv_to_rgb(h, s, v,
&(LEDPlaneMatrixData[plane][pos*3+1]),
&(LEDPlaneMatrixData[plane][pos*3]),
&(LEDPlaneMatrixData[plane][pos*3+2]));
}
/*================================================*/
/*
Low level touch sensor procedures
- There are 60 touch sensors (one for each edge of the ikosidodecaedron)
- Each of the 60 touch sensors is mapped to a LED (key_to_LED_map[])
- Each sensor in connected to one GPIO
- Possible GPIOs are listed in "touch_status_list"
- The size of "touch_status_list" is TOUCH_KEY_CNT
- Not all entries in "touch_status_list" are used.
- The key pressed/release value is an index int touch_status_list.
- The key value is a number between 0 and TOUCH_KEY_CNT-1.
- The key value might be larger than 59 because there are some unused GPIOs in "touch_status_list"
The sensor algorithm is based on the discharge time of a GPIO:
1. Step: Charge the GPIO with some current
2. Step: Disconnnect charge and measure discharge time
3. Step: If discarge time is lower then a given threshold, then it is assumed that the sensor is pressed
The measure algorithm combines GPIOs from several GPIO Ports (contains 16 GPIO pins)
to a single measure run. As a consequence, the measure has to happen only
once for all GPIOs of a single GPIO Port.
- Each GPIO Port contains 16 GPIO pins
- This controller contains 5 GPIO Ports (up to 5x16 = 80 GPIOs)
- The measure algorithm will use the touch_measure_list
*/
#define TOUCH_KEY_STATUS_RELEASED 0
#define TOUCH_KEY_STATUS_RP_DEBOUNCE1 1
#define TOUCH_KEY_STATUS_PRESSED 10
#define TOUCH_KEY_STATUS_PR_DEBOUNCE1 11
/*
the following value defines the sensitivity
higher values: less sensitive
lower values: more sensitive, but risk of faulty detects
*/
#define TOUCH_KEY_MIN_TH_DELTA_CAP 12
/* list of all GPIO lines, used as a sensor key */
struct touch_status_struct {
GPIO_TypeDef *gpio; /* e.g. GPIOB */
uint16_t pin; /* pin number within that GPIO block (0..15) */
uint16_t arduino_pin; /* arduino pin number */
uint16_t min_cap; /* automatically calculated, typical values seem to be 18..19 */
uint16_t threshold_cap; /* if the cap value is below this value, then an untouched touch pad is assumed, use 0 for default */
uint16_t status;
uint32_t time;
};
/* helper struct, used to measure up to 16 GPIO lines. This struct will refer to "touch_status_struct" */
struct touch_measure_struct {
GPIO_TypeDef *gpio; /* e.g. GPIOB */
uint16_t mask; /* each set pin means, that this GPIO should be considered */
int16_t touch_status_index[16]; /* index into the touch status list, negative value means, that the pin is not used (0 in the mask) */
};
/* Touch Sensor */
/*
the touch status list contains a list of all possible sensor inputs
some of the ports can be commented if they are used for otherwise
The index into touch_status_list is refered as "key" in this software.
*/
struct touch_status_struct touch_status_list[] = {
//{ GPIOA, 0, PA0, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
//{ GPIOA, 1, PA1, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOA, 2, PA2, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOA, 3, PA3, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOA, 4, PA4, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOA, 5, PA5, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOA, 6, PA6, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
//{ GPIOA, 7, PA7, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0}, // MOSI for LED matrix
{ GPIOA, 8, PA8, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
//{ GPIOA, 9, PA9, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0}, // for USART, not on header
//{ GPIOA, 10, PA10, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0}, // for USART, not on header
//{ GPIOA, 11, PA11, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0}, // for USART
//{ GPIOA, 12, PA12, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0}, // for USART
//{ GPIOA, 13, PA13, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0}, // SWDIO
//{ GPIOA, 14, PA14, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0}, // SWDCLK
{ GPIOA, 15, PA15, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 0, PB0, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 1, PB1, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
//{ GPIOB, 2, PB2, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 3, PB3, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
//{ GPIOB, 4, PB4, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 5, PB5, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 6, PB6, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 7, PB7, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 8, PB8, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 9, PB9, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 10, PB10, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 11, PB11, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 12, PB12, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 13, PB13, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 14, PB14, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOB, 15, PB15, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 0, PC0, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 1, PC1, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 2, PC2, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 3, PC3, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 4, PC4, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 5, PC5, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 6, PC6, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 7, PC7, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 8, PC8, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 9, PC9, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 10, PC10, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 11, PC11, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 12, PC12, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOC, 13, PC13, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
//{ GPIOC, 14, PC14, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0}, // not available on Header
//{ GPIOC, 15, PC15, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0}, // not available on Header
{ GPIOD, 0, PD0 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 1, PD1 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 2, PD2 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 3, PD3 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 4, PD4 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 5, PD5 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 6, PD6 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 7, PD7 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 8, PD8 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 9, PD9 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 10, PD10 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 11, PD11 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 12, PD12 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 13, PD13 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 14, PD14 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOD, 15, PD15 , 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 0, PE0, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 1, PE1, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 2, PE2, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 3, PE3, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 4, PE4, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 5, PE5, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 6, PE6, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 7, PE7, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 8, PE8, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 9, PE9, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 10, PE10, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 11, PE11, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 12, PE12, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 13, PE13, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 14, PE14, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
{ GPIOE, 15, PE15, 0, 0, TOUCH_KEY_STATUS_RELEASED, 0},
};
#define TOUCH_KEY_CNT (sizeof(touch_status_list)/sizeof(struct touch_status_struct))
//#define TOUCH_KEY_CNT 5
/* maps a key (index into touch_status_list) to a RGB LED number, -1 means, that this key is not used */
/* the key_to_LED_map can be trained/learns with ikosi_learn.ino */
int16_t key_to_LED_map[TOUCH_KEY_CNT]={
/*0:*/36,51,21,25,-1,20,17,44,59,8,
/*10:*/56,58,4,48,7,49,19,-1,63,55,
/*20:*/-1,22,41,28,5,46,11,57,31,12,
/*30:*/27,14,24,38,2,62,32,37,40,42,
/*40:*/15,26,9,-1,16,-1,45,3,30,0,
/*50:*/53,-1,-1,52,39,10,47,6,50,1,
/*60:*/43,23,18,13,29,54,60};
#define TOUCH_MEASURE_CNT 5 /* GPIOA .. GPIOE */
struct touch_measure_struct touch_measure_list[TOUCH_MEASURE_CNT];
#define TOUCH_IO_SAMPLE_COUNT 128 /* do not change: this must match the generated sample statements */
uint16_t touch_io_sample_array[TOUCH_IO_SAMPLE_COUNT];
/*================================================*/
/*
the following two procedures will establish the links between "touch_measure_list" and "touch_status_list"
called by buildTouchMeasureList()
*/
void fillTouchMeasure(struct touch_measure_struct *m, GPIO_TypeDef *gpio)
{
int i;
m->gpio = gpio;
m->mask = 0;
for( i = 0; i < 16; i++ )
m->touch_status_index[i] = -1;
for( i = 0; i < TOUCH_KEY_CNT; i++ )
{
if ( touch_status_list[i].gpio == gpio )
{
m->mask |= 1 << touch_status_list[i].pin;
m->touch_status_index[touch_status_list[i].pin] = i;
}
}
}
/*
Prepare the touch_measure_list[] by filling in values from touch_status_list[].
Called once during startup of the controller.
*/
void buildTouchMeasureList(void)
{
fillTouchMeasure(touch_measure_list+0, GPIOA);
fillTouchMeasure(touch_measure_list+1, GPIOB);
fillTouchMeasure(touch_measure_list+2, GPIOC);
fillTouchMeasure(touch_measure_list+3, GPIOD);
fillTouchMeasure(touch_measure_list+4, GPIOE);
}
/*
const char *getGPIONameByKey(int key)
Description:
Return the GPIO number of the uC port to which the key (sensor) is connected.
This should look like "B14" or similar.
"key" is an index into "touch_status_list"
This is used for debugging and called by getKeyInfoString()
Args:
key: Index into touch_status_list[] (the pressed/released sensor key)
Returns:
Statiic string with the GPIO Name to which the given sensor key is connected
*/
const char *getGPIONameByKey(int key)
{
static char name[16];
strcpy(name, "?");
if ( touch_status_list[key].gpio == GPIOA ) strcpy(name, "A");
if ( touch_status_list[key].gpio == GPIOB ) strcpy(name, "B");
if ( touch_status_list[key].gpio == GPIOC ) strcpy(name, "C");
if ( touch_status_list[key].gpio == GPIOD ) strcpy(name, "D");
if ( touch_status_list[key].gpio == GPIOE ) strcpy(name, "E");
sprintf(name+1, "%d", touch_status_list[key].pin);
return name;
}
/*
const char *getKeyInfoString(int key)
Description:
Return an string which describes the key enclosed in [ ].
It contains the key number, the GPIO port of the key and the mapped LED number.
"key" is an index into "touch_status_list"
Args:
key: Index into touch_status_list[] (the pressed/released sensor key)
Returns:
Statiic string with technical description for the given key
*/
const char *getKeyInfoString(int key)
{
static char s[32]; /* max 23 */
strcpy(s, "[");
strcat(s, "key=");
sprintf(s+strlen(s), "%d", key);
strcat(s, " io=");
strcat(s, getGPIONameByKey(key));
strcat(s, " led=");
sprintf(s+strlen(s), "%d]", key_to_LED_map[key]);
return s;
}
/*
get the key (index into "touch_status_list") for a given led
return -1 if the LED can't be controlled
Inverse operation of key_to_LED_map[]
OBSOLETE?
*/
/*
int getKeyByLED(int led)
{
int i;
for( i = 0; i < TOUCH_KEY_CNT; i++ )
if ( key_to_LED_map[i] == led )
return i;
return -1;
}
*/
/*
returns the number of sensor keys assigned, should be 60
If this is not 60, then the software has to be stopped.
*/
int16_t getAssignedKeyCount(void)
{
int16_t i;
int16_t cnt = 0;
for( i = 0; i < TOUCH_KEY_CNT; i++ )
if ( key_to_LED_map[i] >= 0 )
cnt++;
return cnt;
}
/*================================================*/
/* hardware self test for the sensor keys */
int executeKeySelfTest(void)
{
int i, j;
int is_error = 0;
char keyname[16];
for( i = 0; i < TOUCH_KEY_CNT; i++ )
{
strcpy(keyname, getGPIONameByKey(i));
pinMode(touch_status_list[i].arduino_pin, OUTPUT);
digitalWrite(touch_status_list[i].arduino_pin, 0);
for( j = 0; j < TOUCH_KEY_CNT; j++ )
{
if ( j != i ) pinMode(touch_status_list[i].arduino_pin, INPUT_PULLUP);
}
for( j = 0; j < TOUCH_KEY_CNT; j++ )
{
if ( j != i )
if ( digitalRead(touch_status_list[i].arduino_pin) == 0 )
pn("short circuit between key %d (%s) and %d (%s) found", i, keyname, j, getGPIONameByKey(j)), is_error=1;
}
digitalWrite(touch_status_list[i].arduino_pin, 1);
for( j = 0; j < TOUCH_KEY_CNT; j++ )
{
if ( j != i ) pinMode(touch_status_list[i].arduino_pin, INPUT_PULLDOWN);
}
for( j = 0; j < TOUCH_KEY_CNT; j++ )
{
if ( j != i )
if ( digitalRead(touch_status_list[i].arduino_pin) != 0 )
pn("short circuit between key %d (%s) and %d (%s) found", i, keyname, j, getGPIONameByKey(j)), is_error=1;
}
}
return is_error;
}
int checkCapValues(void)
{
int i;
for( i = 0; i < TOUCH_KEY_CNT; i++ )
{
//if ( touch_status_list[i].min_cap < 15 )
if ( key_to_LED_map[i] >= 0 )
{
if ( touch_status_list[i].min_cap < 15 )
{
pn("low cap warning for key %d (%s), cap=%d, sensor might be disconnected",
i, getGPIONameByKey(i), touch_status_list[i].min_cap );
}
}