-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMatrix.cpp
More file actions
1517 lines (1376 loc) · 79 KB
/
Copy pathMaxMatrix.cpp
File metadata and controls
1517 lines (1376 loc) · 79 KB
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
/******************************************************************************************************************************************************
* COPYRIGHT
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* \verbatim
* Copyright (c) Andreas Burnickl All rights reserved.
*
* \endverbatim
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* FILE DESCRIPTION
* -------------------------------------------------------------------------------------------------------------------------------------------------*/
/** \file MaxMatrix.c
* \brief Main file of MaxMatrix library
*
* \details Arduino library to drive 8x8 LED display Matrix modules with MAX7219
*
*
*****************************************************************************************************************************************************/
#define _MAXMATRIX_SOURCE_
/******************************************************************************************************************************************************
* INCLUDES
*****************************************************************************************************************************************************/
#include "MaxMatrix.h"
#if(MAXMATRIX_USE_SPI == STD_ON)
#include "SPI.h"
#endif
#if(MAXMATRIX_USE_DIGITAL_WRITE_FAST == STD_ON)
#include "digitalWriteFast.h"
#endif
/******************************************************************************************************************************************************
* P U B L I C F U N C T I O N S
*****************************************************************************************************************************************************/
/******************************************************************************************************************************************************
CONSTRUCTOR OF MaxMatrix
******************************************************************************************************************************************************/
/*! \brief MaxMatrix constructor
* \details Instantiation of the MaxMatrix library
*
* \param[in] sDataInPin number of the Data Input Pin
* \param[in] sChipSelectPin number of the Chip Select (CS) Pin
* \param[in] sClockPin number of the Clock Pin
* \return -
*****************************************************************************************************************************************************/
MaxMatrix::MaxMatrix(byte sDataInPin, byte sChipSelectPin, byte sClockPin)
{
DataInPin = sDataInPin;
ChipSelectPin = sChipSelectPin;
ClockPin = sClockPin;
String = NULL;
SpriteShiftCounter = MAXMATRIX_SPRITE_SHIFT_STATE_READY;
SpaceBetweenChars = MAXMATRIX_SPACE_BETWEEN_CHARS_INIT_VALUE;
Orientation = MAXMATRIX_MODULE_ORIENTATION_0;
State = MAXMATRIX_STATE_NONE;
/* initialize buffers */
for (int i = 0; i < MAXMATRIX_NUMBER_OF_COLUMNS; i++) MatrixBuffer[i] = 0;
for (int i = 0; i < MAXMATRIX_SPRITE_TABLE_NUMBER_OF_COLUMNS; i++) SpriteBuffer[i] = 0;
} /* MaxMatrix */
/******************************************************************************************************************************************************
DESTRUCTOR OF MaxMatrix
******************************************************************************************************************************************************/
MaxMatrix::~MaxMatrix()
{
} /* ~MaxMatrix */
/******************************************************************************************************************************************************
init()
******************************************************************************************************************************************************/
/*! \brief initialization of the Matrix hardware
* \details this function initializes the IO hardware and the MAX7219 controller
* all LEDs of the matrix will be cleared and intensity will be set to init value
* \return -
*****************************************************************************************************************************************************/
void MaxMatrix::init()
{
State = MAXMATRIX_STATE_INIT;
#if(MAXMATRIX_USE_SPI == STD_ON)
SPI.begin();
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
#else
pinMode(DataInPin, OUTPUT);
pinMode(ClockPin, OUTPUT);
pinMode(ChipSelectPin, OUTPUT);
# if(MAXMATRIX_USE_DIGITAL_WRITE_FAST == STD_ON)
digitalWriteFast(ClockPin, HIGH);
# else
digitalWrite(ClockPin, HIGH);
# endif
#endif
RegisterWrite(MAX7219_REG_SCAN_LIMIT_ADDRESS, MAX7219_REG_SCAN_LIMIT_DISPLAY_DIGIT_0_TO_7); // display all digits
RegisterWrite(MAX7219_REG_DECODE_MODE_ADDRESS, MAX7219_REG_DECODE_MODE_NO_DECODE); // using an led matrix (not digits)
RegisterWrite(MAX7219_REG_SHUTDOWN_ADDRESS, MAX7219_REG_SHUTDOWN_MODE_NORMAL_OPERATION); // normal operation mode
RegisterWrite(MAX7219_REG_DISPLAY_TEST_ADDRESS, MAX7219_REG_DISPLAY_TEST_NORMAL_OPERATION); // no display test
/* initialize registers, turn all LEDs off */
clear();
setIntensity(MAXMATRIX_INTENSITY_INIT_VALUE);
State = MAXMATRIX_STATE_READY;
} /* init */
/******************************************************************************************************************************************************
shiftTask()
******************************************************************************************************************************************************/
/*! \brief shifts char or string over the matrix
* \details this function has to be called periodically until state change from
* MAXMATRIX_STATE_CHAR_SHIFT or MAXMATRIX_STATE_STRING_SHIFT to MAXMATRIX_STATE_READY
* \return -
* \pre setCharWithShift or setTextWithShift must be called first
*****************************************************************************************************************************************************/
void MaxMatrix::shiftTask()
{
if(MAXMATRIX_STATE_STRING_SHIFT == State) stringShiftTask();
if(MAXMATRIX_STATE_CHAR_SHIFT == State) charShiftTask();
} /* shiftTask */
/******************************************************************************************************************************************************
setIntensity()
******************************************************************************************************************************************************/
/*! \brief set led intensity of matrix module
* \details
*
* \param[in] Intensity intensity value from 0 to 15
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setIntensity(byte Intensity)
{
if(Intensity >= MAX7219_REG_INTENSITY_MIN_VALUE && Intensity <= MAX7219_REG_INTENSITY_MAX_VALUE) {
RegisterWrite(MAX7219_REG_INTENSITY_ADDRESS, Intensity);
return E_OK;
} else {
return E_NOT_OK;
}
} /* setIntensity */
/******************************************************************************************************************************************************
clear()
******************************************************************************************************************************************************/
/*! \brief clears all LEDs of matrix module
* \details
*
* \return -
*****************************************************************************************************************************************************/
void MaxMatrix::clear()
{
/* clear all LEDs on Matrix */
for(int i = 0; i < MAXMATRIX_COLUMN_NUMBER_OF_MODULE; i++) { setColumnOnAllModulesLL(i, 0); }
/* clear Matrix buffer */
for(int i = 0; i < MAXMATRIX_NUMBER_OF_COLUMNS; i++) { MatrixBuffer[i] = 0; }
} /* clear */
/******************************************************************************************************************************************************
RegisterWrite()
******************************************************************************************************************************************************/
/*! \brief send SPI command to MAX7219
* \details
*
* \param[in] RegisterAddress Address of the MAX7219 Register
* \param[in] Value Register Value
* \return -
*****************************************************************************************************************************************************/
void MaxMatrix::RegisterWrite(byte RegisterAddress, byte RegisterValue)
{
#if(MAXMATRIX_USE_DIGITAL_WRITE_FAST == STD_ON)
digitalWriteFast(ChipSelectPin, LOW);
#else
digitalWrite(ChipSelectPin, LOW);
#endif
for(int Module = 0; Module < MAXMATRIX_NUMBER_OF_MODULES; Module++)
{
#if(MAXMATRIX_USE_SPI == STD_ON)
SPI.transfer(RegisterAddress);
SPI.transfer(RegisterValue);
#else
shiftOut(DataInPin, ClockPin, MSBFIRST, RegisterAddress);
shiftOut(DataInPin, ClockPin, MSBFIRST, RegisterValue);
#endif
}
#if(MAXMATRIX_USE_DIGITAL_WRITE_FAST == STD_ON)
digitalWriteFast(ChipSelectPin, LOW);
digitalWriteFast(ChipSelectPin, HIGH);
#else
digitalWrite(ChipSelectPin, LOW);
digitalWrite(ChipSelectPin, HIGH);
#endif
} /* RegisterWrite */
/******************************************************************************************************************************************************
getColumn()
******************************************************************************************************************************************************/
/*! \brief get a given column from Matrix buffer
* \details this function returns a column from the LED matrix
* column reaches from zero to number of modules multiplied by eight
*
* \param[in] Column column on matrix which contains eight LEDs
* \param[out] Value led value in column each bit means one led state
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::getColumn(byte Column, byte* Value)
{
int Module = Column / MAXMATRIX_COLUMN_NUMBER_OF_MODULE;
int ModuleColumn = Column % MAXMATRIX_COLUMN_NUMBER_OF_MODULE;
byte ValueReversed;
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_0) return getColumnLL(Column, Value);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_90) return getRowLL(Module, ModuleColumn, Value);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_180) {
if(E_OK == getColumnLL(MAXMATRIX_NUMBER_OF_COLUMNS - Column - 1, &ValueReversed)) {
*Value = reverseByte(ValueReversed);
return E_OK;
} else return E_NOT_OK;
}
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_270) {
if(E_OK == getRowLL(Module, MAXMATRIX_ROW_NUMBER_OF_MODULE - ModuleColumn - 1, &ValueReversed)) {
*Value = reverseByte(ValueReversed);
return E_OK;
} else return E_NOT_OK;
}
return E_NOT_OK;
} /* getColumn */
/******************************************************************************************************************************************************
getColumn()
******************************************************************************************************************************************************/
/*! \brief get a given column from Matrix buffer
* \details this function returns a column from the LED matrix
*
* \param[in] Module module where column will be returned
* \param[in] Column column on module which contains eight LEDs
* \param[out] Value led value in column each bit means one led
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::getColumn(byte Module, byte Column, byte* Value)
{
return getColumn((Module * MAXMATRIX_COLUMN_NUMBER_OF_MODULE) + Column, Value);
} /* setColumn */
/******************************************************************************************************************************************************
setColumn()
******************************************************************************************************************************************************/
/*! \brief set a given column on whole matrix
* \details this function sets a column on the LED matrix with the given values
* column reaches from zero to number of modules multiplied by eight
*
* \param[in] Column column on matrix which contains eight LEDs
* \param[in] Value led value in column each bit means one led state
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setColumn(byte Column, byte Value)
{
int Module = Column / MAXMATRIX_COLUMN_NUMBER_OF_MODULE;
int ModuleColumn = Column % MAXMATRIX_COLUMN_NUMBER_OF_MODULE;
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_0) return setColumnLL(Column, Value);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_90) return setRowLL(Module, ModuleColumn, Value);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_180) return setColumnLL(MAXMATRIX_NUMBER_OF_COLUMNS - Column - 1, reverseByte(Value));
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_270) return setRowLL(Module, MAXMATRIX_ROW_NUMBER_OF_MODULE - ModuleColumn - 1, reverseByte(Value));
return E_NOT_OK;
} /* setColumn */
/******************************************************************************************************************************************************
setColumn()
******************************************************************************************************************************************************/
/*! \brief set a given column on whole matrix
* \details this function sets a column on the LED matrix with the given values
*
* \param[in] Module module where column should be set
* \param[in] Column column on module which contains eight LEDs
* \param[in] Value led value in column each bit means one led
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setColumn(byte Module, byte Column, byte Value)
{
return setColumn((Module * MAXMATRIX_COLUMN_NUMBER_OF_MODULE) + Column, Value);
} /* setColumn */
/******************************************************************************************************************************************************
getRow()
******************************************************************************************************************************************************/
/*! \brief get a given row from Matrix buffer
* \details this function returns a row from the LED matrix
*
* \param[in] Row row on matrix which contains eight LEDs
* \param[out] Value led value in row each bit means one led state
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::getRow(byte Row, rowType Value)
{
rowType ValueReversed;
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_0) return getRowLL(Row, Value);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_90) {
for(int Module = 0; Module < MAXMATRIX_NUMBER_OF_MODULES; Module++) {
if(E_NOT_OK == getColumnLL(Module, MAXMATRIX_COLUMN_NUMBER_OF_MODULE - Row - 1, &Value[Module])) return E_NOT_OK;
}
return E_OK;
}
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_180) {
if(E_OK == getRowLL(MAXMATRIX_ROW_NUMBER_OF_MODULE - Row - 1, ValueReversed)) {
for(int Module = 0; Module < MAXMATRIX_NUMBER_OF_MODULES; Module++) {
Value[Module] = reverseByte(ValueReversed[Module]);
}
return E_OK;
} else return E_NOT_OK;
}
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_270) {
for(int Module = 0; Module < MAXMATRIX_NUMBER_OF_MODULES; Module++) {
if(E_NOT_OK == getColumnLL(Module, Row, &ValueReversed[Module])) return E_NOT_OK;
}
for(int Module = 0; Module < MAXMATRIX_NUMBER_OF_MODULES; Module++) {
Value[Module] = reverseByte(ValueReversed[Module]);
}
return E_OK;
}
return E_NOT_OK;
} /* getRow */
/******************************************************************************************************************************************************
getRow()
******************************************************************************************************************************************************/
/*! \brief get a given row from Matrix buffer
* \details this function returns a row from the LED matrix
*
* \param[in] Module module where Row should be get
* \param[in] Row row on matrix which contains eight LEDs
* \param[out] Value led value in row each bit means one led state
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::getRow(byte Module, byte Row, byte* Value)
{
byte ValueReversed;
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_0) return getRowLL(Module, Row, Value);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_90) return getColumnLL(Module, MAXMATRIX_COLUMN_NUMBER_OF_MODULE - Row - 1, Value);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_180) {
if(E_OK == getRowLL(Module, MAXMATRIX_ROW_NUMBER_OF_MODULE - Row - 1, &ValueReversed)) {
*Value = reverseByte(ValueReversed);
return E_OK;
} else return E_NOT_OK;
}
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_270) {
if(E_OK == getColumnLL(Module, Row, &ValueReversed)) {
*Value = reverseByte(ValueReversed);
return E_OK;
} else return E_NOT_OK;
}
return E_NOT_OK;
} /* getRow */
/******************************************************************************************************************************************************
setRow()
******************************************************************************************************************************************************/
/*! \brief set a given column on whole matrix
* \details this function sets a column on the LED matrix with the given values
* column reaches from zero to number of modules multiplied by eight
*
* \param[in] Row row on matrix which contains eight LEDs
* \param[in] Value led value in row each bit means one led
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setRow(byte Row, const rowType Value)
{
rowType ValueReversed;
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_0) return setRowLL(Row, Value);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_90) {
for(int Module = 0; Module < MAXMATRIX_NUMBER_OF_MODULES; Module++) {
if(E_NOT_OK == setColumnLL(Module, MAXMATRIX_COLUMN_NUMBER_OF_MODULE - Row - 1, Value[Module])) return E_NOT_OK;
}
return E_OK;
}
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_180) {
for(int Module = 0; Module < MAXMATRIX_NUMBER_OF_MODULES; Module++) {
ValueReversed[Module] = reverseByte(Value[Module]);
}
return setRowLL(MAXMATRIX_ROW_NUMBER_OF_MODULE - Row - 1, ValueReversed);
}
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_270) {
for(int Module = 0; Module < MAXMATRIX_NUMBER_OF_MODULES; Module++) {
ValueReversed[Module] = reverseByte(Value[Module]);
}
for(int Module = 0; Module < MAXMATRIX_NUMBER_OF_MODULES; Module++) {
if(E_NOT_OK == setColumnLL(Module, Row, ValueReversed[Module])) return E_NOT_OK;
}
return E_OK;
}
return E_NOT_OK;
} /* setRow */
/******************************************************************************************************************************************************
setRow()
******************************************************************************************************************************************************/
/*! \brief set a given column on whole matrix
* \details this function sets a column on the LED matrix with the given values
*
* \param[in] Module Module where row should be set
* \param[in] Column column on matrix which contains eight LEDs
* \param[in] Value led value in column each bit means one led
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setRow(byte Module, byte Row, byte Value)
{
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_0) return setRowLL(Module, Row, Value);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_90) return setColumnLL(Module, MAXMATRIX_COLUMN_NUMBER_OF_MODULE - Row - 1, Value);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_180) return setRowLL(Module, MAXMATRIX_ROW_NUMBER_OF_MODULE - Row - 1, reverseByte(Value));
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_270) return setColumnLL(Module, Row, reverseByte(Value));
return E_NOT_OK;
} /* setRow */
/******************************************************************************************************************************************************
getDot()
******************************************************************************************************************************************************/
/*! \brief get a Pixel (LED) on the matrix
* \details
*
* \param[in] Column column where to get the pixel
* \param[in] Row row where to get the pixel
* \param[out] Value value of the pixel
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::getDot(byte Column, byte Row, bool* Value)
{
byte ColumnValue;
if(Column >= 0 && Column < MAXMATRIX_NUMBER_OF_COLUMNS && Row >= 0 && Row < MAXMATRIX_ROW_NUMBER_OF_MODULE) {
/* Dot means Bit in a specific column */
getColumn(Column, &ColumnValue);
*Value = bitRead(ColumnValue, Row);
return E_OK;
} else {
return E_NOT_OK;
}
} /* getDot */
/******************************************************************************************************************************************************
setDot()
******************************************************************************************************************************************************/
/*! \brief set a Pixel (LED) on the matrix
* \details
*
* \param[in] Column column where to set the pixel
* \param[in] Row row where to set the pixel
* \param[in] Value value of the pixel
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setDot(byte Column, byte Row, bool Value)
{
byte ColumnValue;
if(Column >= 0 && Column < MAXMATRIX_NUMBER_OF_COLUMNS && Row >= 0 && Row < MAXMATRIX_ROW_NUMBER_OF_MODULE) {
/* Dot means Bit in a specific column */
getColumn(Column, &ColumnValue);
bitWrite(ColumnValue, Row, Value);
return setColumn(Column, ColumnValue);
} else {
return E_NOT_OK;
}
} /* setDot */
/******************************************************************************************************************************************************
setChar()
******************************************************************************************************************************************************/
/*! \brief prints a char on the matrix
* \details
*
* \param[in] X x coordinate
* \param[in] Y y coordinate
* \param[in] Char char to print on matrix
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setChar(int X, int Y, char Char)
{
spriteIndexType SpriteIndex;
spriteType Sprite;
if(E_OK == convertCharToSprite(Char, &SpriteIndex)) {
if(E_OK == getSprite(SpriteIndex, &Sprite)) {
setSprite(X, Y, Sprite);
return E_OK;
} else return E_NOT_OK;
} else return E_NOT_OK;
} /* setChar */
/******************************************************************************************************************************************************
setCharWithShift()
******************************************************************************************************************************************************/
/*! \brief shifts a char into the matrix and stops on the right side
* \details this function shifts a given char into the matrix and stops shifting on the right side.
* Every shift process will be triggered by calling the shiftMainFunction.
*
* \param[in] Char char to print on matrix
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setCharWithShift(char Char)
{
spriteIndexType SpriteIndex;
if(E_OK == convertCharToSprite(Char, &SpriteIndex)) {
if(E_OK == getSprite(SpriteIndex, &SpriteBuffer)) {
SpriteShiftCounter = MAXMATRIX_SPRITE_SHIFT_STATE_RUNNING;
State = MAXMATRIX_STATE_CHAR_SHIFT;
return E_OK;
} else return E_NOT_OK;
} else return E_NOT_OK;
} /* setCharWithShift */
/******************************************************************************************************************************************************
setText()
******************************************************************************************************************************************************/
/*! \brief prints a string on the matrix
* \details
*
* \param[in] String string to print on matrix
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setText(const char* String)
{
spriteIndexType SpriteIndex;
spriteType Sprite;
int CharColumn = 0;
/* check for valid string */
if(String != NULL)
{
while(*String != STD_NULL_CHARACTER)
{
if(E_NOT_OK == convertCharToSprite(*String, &SpriteIndex)) { return E_NOT_OK; }
else if(E_NOT_OK == getSprite(SpriteIndex, &Sprite)) { return E_NOT_OK; }
else {
setSprite(CharColumn, 0, Sprite);
CharColumn += Sprite[ASCII_TABLE_SPRITE_WIDTH] + SpaceBetweenChars;
if(CharColumn > MAXMATRIX_NUMBER_OF_COLUMNS) break;
}
String++;
}
return E_OK;
} else {
return E_NOT_OK;
}
} /* setText */
/******************************************************************************************************************************************************
setTextWithShift()
******************************************************************************************************************************************************/
/*! \brief shifts a string into the matrix and stops after the last char on the right side
* \details this function shifts a given string into the matrix and stops shifting on the right side,
* when the last char appears on the matrix.
*
* \param[in] sString string to print on matrix
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setTextWithShift(char* sString)
{
if(sString != NULL) {
SpriteShiftCounter = MAXMATRIX_SPRITE_SHIFT_STATE_READY;
String = sString;
State = MAXMATRIX_STATE_STRING_SHIFT;
return E_OK;
} else return E_NOT_OK;
} /* setTextWithShift */
/******************************************************************************************************************************************************
shiftLeft()
******************************************************************************************************************************************************/
/*! \brief shift the whole matrix led states one column to the left
* \details this functions shifts the states of all LEDs on the matrix one column to the left.
* By activating fill with zero, the column on the right side will be initialized with LED off.
* By activating rotation, the last column will be first.
*
* \param[in] Rotate activate rotation of the matrix LED states
* \param[in] FillWithZero initialize the column on the right side with zero.
* \return -
*****************************************************************************************************************************************************/
void MaxMatrix::shiftLeft(bool Rotate, bool FillWithZero)
{
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_0) shiftLeftLL(Rotate, FillWithZero, true);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_90) shiftUpLL(Rotate, true);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_180) shiftRightLL(Rotate, FillWithZero, true);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_270) shiftDownLL(Rotate, true);
} /* shiftLeft */
/******************************************************************************************************************************************************
shiftRight()
******************************************************************************************************************************************************/
/*! \brief shift the whole matrix led states one column to the right
* \details this functions shifts the states of all LEDs on the matrix one column to the right.
* By activating fill with zero, the column on the left side will be initialized with LED off.
* By activating rotation, the last column will be first.
*
* \param[in] Rotate activate rotation of the matrix LED states
* \param[in] FillWithZero initialize the column on the right side with zero.
* \return -
*****************************************************************************************************************************************************/
void MaxMatrix::shiftRight(bool Rotate, bool FillWithZero)
{
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_0) shiftRightLL(Rotate, FillWithZero, true);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_90) shiftDownLL(Rotate, true);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_180) shiftLeftLL(Rotate, FillWithZero, true);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_270) shiftUpLL(Rotate, true);
} /* shiftRight */
/******************************************************************************************************************************************************
shiftUp()
******************************************************************************************************************************************************/
/*! \brief shift the whole matrix led states one row up to the top
* \details this function shifts the states of all LEDS on the matrix one row up to the top.
* The row on the bottom will be initialized with LED off.
* By activating rotation, the last row will be first.
* \param[in] Rotate activate rotation of the matrix LED states
* \return -
*****************************************************************************************************************************************************/
void MaxMatrix::shiftUp(bool Rotate)
{
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_0) shiftUpLL(Rotate, false);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_90) shiftLeftLL(Rotate, true, false);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_180) shiftDownLL(Rotate, false);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_270) shiftRightLL(Rotate, true, false);
} /* shiftUp */
/******************************************************************************************************************************************************
shiftDown()
******************************************************************************************************************************************************/
/*! \brief shift the whole matrix led states one row down to the bottom
* \details this function shifts the states of all LEDS on the matrix one row down to the bottom.
* The row on the top will be initialized with LED off.
* By activating rotation, the last row will be first.
* \param[in] Rotate activate rotation of the matrix LED states
* \return -
*****************************************************************************************************************************************************/
void MaxMatrix::shiftDown(bool Rotate)
{
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_0) shiftDownLL(Rotate, false);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_90) shiftRightLL(Rotate, true, false);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_180) shiftUpLL(Rotate, false);
if(Orientation == MAXMATRIX_MODULE_ORIENTATION_270) shiftLeftLL(Rotate, true, false);
} /* shiftDown */
/******************************************************************************************************************************************************
getSprite()
******************************************************************************************************************************************************/
/*! \brief get an sprite from the sprite table
* \details this function returns an sprite by an given sprite index
*
* \param[in] SpriteIndex index of sprite from sprite table
* \param[out] Sprite sprite from sprite table (width, height, value column 1 ... 5)
* \return -
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::getSprite(spriteIndexType SpriteIndex, spriteType* Sprite)
{
if(SpriteIndex >= 0 && SpriteIndex < MAXMATRIX_SPRITE_TABLE_NUMBER_OF_ROWS) {
memcpy_P(*Sprite, SpriteTable + MAXMATRIX_SPRITE_TABLE_NUMBER_OF_COLUMNS * SpriteIndex, MAXMATRIX_SPRITE_TABLE_NUMBER_OF_COLUMNS);
return E_OK;
} else return E_NOT_OK;
} /* getSprite */
/******************************************************************************************************************************************************
setSprite()
******************************************************************************************************************************************************/
/*! \brief print sprite on matrix
* \details this function prints an sprite on the matrix located to the given coordinates
*
* \param[in] X x coordinate
* \param[in] Y y coordinate
* \param[in] Sprite sprite from sprite table (width, height, value column 1 ... 5)
* \return -
*****************************************************************************************************************************************************/
void MaxMatrix::setSprite(int X, int Y, const spriteType Sprite)
{
int SpriteWidth = Sprite[ASCII_TABLE_SPRITE_WIDTH];
int SpriteHeight = Sprite[ASCII_TABLE_SPRITE_HEIGHT];
/* if height of Sprite = height of Matrix set whole column */
if(SpriteHeight == MAXMATRIX_ROW_NUMBER_OF_MODULE && Y == 0) {
for(int SpriteColumn = 0; SpriteColumn < SpriteWidth; SpriteColumn++)
{
int Column = X + SpriteColumn;
if(Column >= 0 && Column < MAXMATRIX_NUMBER_OF_COLUMNS)
setColumn(Column, Sprite[SpriteColumn + ASCII_TABLE_SPRITE_COLUMN1]);
}
} else { /* otherwise we have to set every single dot */
for(int SpriteColumn = 0; SpriteColumn < SpriteWidth; SpriteColumn++)
{
for(int SpriteRow = 0; SpriteRow < SpriteHeight; SpriteRow++)
{
int Column = X + SpriteColumn;
int Row = Y + SpriteRow;
if(Column >= 0 && Column < MAXMATRIX_NUMBER_OF_COLUMNS && Row >= 0 && Row < MAXMATRIX_ROW_NUMBER_OF_MODULE)
setDot(Column, Row, bitRead(Sprite[SpriteColumn + ASCII_TABLE_SPRITE_COLUMN1], SpriteRow));
}
}
}
} /* setSprite */
/******************************************************************************************************************************************************
setSpaceBetweenChars()
******************************************************************************************************************************************************/
/*! \brief set space between chars
* \details this function sets the number of spaces between chars
*
* \param[in] sSpaceBetweenChars number of spaces
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::setSpaceBetweenChars(byte sSpaceBetweenChars)
{
if(sSpaceBetweenChars >= 0) {
SpaceBetweenChars = sSpaceBetweenChars;
return E_OK;
} else {
return E_NOT_OK;
}
} /* setSpaceBetweenChars */
/******************************************************************************************************************************************************
setOrientation()
******************************************************************************************************************************************************/
/*! \brief set Orientation of matrix
* \details this function sets the orientation of the matrix
* known values are 0, 90, 180, 270 degrees
*
* \param[in] sOrientation orientation in degree
* \return -
*****************************************************************************************************************************************************/
void MaxMatrix::setModuleOrientation(MaxMatrixModuleOrientationType sOrientation)
{
byte MatrixBufferRotated[MAXMATRIX_BUFFER_SIZE];
byte ColumnRotated = 0, RowRotated = 0;
if(sOrientation != Orientation)
{
// rotate 90 degree
if((Orientation == MAXMATRIX_MODULE_ORIENTATION_0 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_90) ||
(Orientation == MAXMATRIX_MODULE_ORIENTATION_90 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_180) ||
(Orientation == MAXMATRIX_MODULE_ORIENTATION_180 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_270) ||
(Orientation == MAXMATRIX_MODULE_ORIENTATION_270 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_0))
{ /* rotate every module */
for(byte Module = 1; Module <= MAXMATRIX_NUMBER_OF_MODULES; Module++) {
/* Bit in Column 7 and Row 7 will set to Column 0 and Row 7 ... */
for(int Row = MAXMATRIX_ROW_NUMBER_OF_MODULE - 1; Row >= 0; Row--) {
RowRotated = MAXMATRIX_ROW_NUMBER_OF_MODULE - 1;
for(int Column = MAXMATRIX_COLUMN_NUMBER_OF_MODULE*Module-1; Column >= MAXMATRIX_COLUMN_NUMBER_OF_MODULE*(Module-1); Column--) {
bitWrite(MatrixBufferRotated[ColumnRotated], RowRotated, bitRead(MatrixBuffer[Column], Row));
RowRotated--;
}
ColumnRotated++;
}
}
}
// rotate 180 degree
if((Orientation == MAXMATRIX_MODULE_ORIENTATION_0 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_180) ||
(Orientation == MAXMATRIX_MODULE_ORIENTATION_90 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_270) ||
(Orientation == MAXMATRIX_MODULE_ORIENTATION_180 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_0) ||
(Orientation == MAXMATRIX_MODULE_ORIENTATION_270 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_90))
{ /* rotate every module */
for(byte Module = 1; Module <= MAXMATRIX_NUMBER_OF_MODULES; Module++) {
/* Bit in Column 7 and Row 0 will set to Column 0 and Row 7 ... */
for(int Column = MAXMATRIX_COLUMN_NUMBER_OF_MODULE*Module-1; Column >= MAXMATRIX_COLUMN_NUMBER_OF_MODULE*(Module-1); Column--) {
RowRotated = MAXMATRIX_ROW_NUMBER_OF_MODULE - 1;
for(int Row = 0; Row <= MAXMATRIX_ROW_NUMBER_OF_MODULE - 1; Row++) {
bitWrite(MatrixBufferRotated[ColumnRotated], RowRotated, bitRead(MatrixBuffer[Column], Row));
RowRotated--;
}
ColumnRotated++;
}
}
}
// rotate 270 degree
if((Orientation == MAXMATRIX_MODULE_ORIENTATION_0 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_270) ||
(Orientation == MAXMATRIX_MODULE_ORIENTATION_90 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_0) ||
(Orientation == MAXMATRIX_MODULE_ORIENTATION_180 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_90) ||
(Orientation == MAXMATRIX_MODULE_ORIENTATION_270 && sOrientation == MAXMATRIX_MODULE_ORIENTATION_180))
{ /* rotate every module */
for(byte Module = 1; Module <= MAXMATRIX_NUMBER_OF_MODULES; Module++) {
/* Bit in Column 0 and Row 0 will set to Column 0 and Row 7 ... */
for(int Row = 0; Row <= MAXMATRIX_ROW_NUMBER_OF_MODULE - 1; Row++) {
RowRotated = MAXMATRIX_ROW_NUMBER_OF_MODULE - 1;
for(int Column = MAXMATRIX_COLUMN_NUMBER_OF_MODULE *(Module-1); Column <= MAXMATRIX_COLUMN_NUMBER_OF_MODULE*Module-1; Column++) {
bitWrite(MatrixBufferRotated[ColumnRotated], RowRotated, bitRead(MatrixBuffer[Column], Row));
RowRotated--;
}
ColumnRotated++;
}
}
}
Orientation = sOrientation;
for(int i = 0; i < MAXMATRIX_BUFFER_SIZE; i++) MatrixBuffer[i] = MatrixBufferRotated[i];
reload();
}
} /* setOrientation */
/******************************************************************************************************************************************************
* P R I V A T E F U N C T I O N S
*****************************************************************************************************************************************************/
/******************************************************************************************************************************************************
reverseByte()
******************************************************************************************************************************************************/
/*! \brief reverse a given byte
* \details this function returns a reversed byte from a given byte
*
* \param[in] Input byte which should be reversed
* \return reversed Byte
*****************************************************************************************************************************************************/
byte MaxMatrix::reverseByte(byte Input)
{
Input = (Input & 0xF0) >> 4 | (Input & 0x0F) << 4;
Input = (Input & 0xCC) >> 2 | (Input & 0x33) << 2;
Input = (Input & 0xAA) >> 1 | (Input & 0x55) << 1;
return Input;
} /* reverseByte */
/******************************************************************************************************************************************************
reload()
******************************************************************************************************************************************************/
/*! \brief reload whole matrix
* \details this function reloads the whole matrix with the matrix buffer values
*
* \return -
*****************************************************************************************************************************************************/
void MaxMatrix::reload()
{
for(int i = 0; i < MAXMATRIX_COLUMN_NUMBER_OF_MODULE; i++)
{
int Column = i;
#if(MAXMATRIX_USE_DIGITAL_WRITE_FAST == STD_ON)
digitalWriteFast(ChipSelectPin, LOW);
#else
digitalWrite(ChipSelectPin, LOW);
#endif
for(int j = 0; j < MAXMATRIX_NUMBER_OF_MODULES; j++)
{
#if(MAXMATRIX_USE_SPI == STD_ON)
SPI.transfer(i + 1);
SPI.transfer(MatrixBuffer[Column]);
#else
shiftOut(DataInPin, ClockPin, MSBFIRST, i + 1);
shiftOut(DataInPin, ClockPin, MSBFIRST, MatrixBuffer[Column]);
#endif
Column += MAXMATRIX_COLUMN_NUMBER_OF_MODULE;
}
#if(MAXMATRIX_USE_DIGITAL_WRITE_FAST == STD_ON)
digitalWriteFast(ChipSelectPin, LOW);
digitalWriteFast(ChipSelectPin, HIGH);
#else
digitalWrite(ChipSelectPin, LOW);
digitalWrite(ChipSelectPin, HIGH);
#endif
}
} /* reload */
/******************************************************************************************************************************************************
charShiftTask()
******************************************************************************************************************************************************/
/*! \brief shifts char one column to the left
* \details this function has to be called periodically until state change from
* MAXMATRIX_STATE_CHAR_SHIFT or MAXMATRIX_STATE_STRING_SHIFT to MAXMATRIX_STATE_READY
* \return -
* \pre has to be called from shiftTask()
*****************************************************************************************************************************************************/
void MaxMatrix::charShiftTask()
{
shiftLeft(false, true);
SpriteShiftCounter++;
/* if sprite is completed set only spaces */
if(SpriteShiftCounter <= SpriteBuffer[ASCII_TABLE_SPRITE_WIDTH]) {
setSprite(MAXMATRIX_NUMBER_OF_COLUMNS - SpriteShiftCounter, 0, SpriteBuffer);
}
/* if sprite and spaces are completed task has finished */
else if(SpriteShiftCounter == SpriteBuffer[ASCII_TABLE_SPRITE_WIDTH] + SpaceBetweenChars + 1) {
if(MAXMATRIX_STATE_CHAR_SHIFT == State) State = MAXMATRIX_STATE_READY;
SpriteShiftCounter = MAXMATRIX_SPRITE_SHIFT_STATE_READY;
}
} /* charShiftTask */
/******************************************************************************************************************************************************
stringShiftTask()
******************************************************************************************************************************************************/
/*! \brief shifts string one column to the left
* \details this function has to be called periodically until state change from
* MAXMATRIX_STATE_CHAR_SHIFT or MAXMATRIX_STATE_STRING_SHIFT to MAXMATRIX_STATE_READY
* \return -
* \pre has to be called from shiftTask()
*****************************************************************************************************************************************************/
void MaxMatrix::stringShiftTask()
{
spriteIndexType SpriteIndex;
/* has char shift task finished shifting sprite or char */
if(SpriteShiftCounter == MAXMATRIX_SPRITE_SHIFT_STATE_READY)
{
/* If end of string is not reached, load next char */
if(*String != STD_NULL_CHARACTER) {
convertCharToSprite(*String, &SpriteIndex);
getSprite(SpriteIndex, &SpriteBuffer);
SpriteShiftCounter = MAXMATRIX_SPRITE_SHIFT_STATE_RUNNING;
charShiftTask();
String++;
} else { /* otherwise task has finished */
State = MAXMATRIX_STATE_READY;
shiftLeft(false, true);
}
} else {
/* go on shifting Sprite */
charShiftTask();
}
} /* stringShiftTask */
/******************************************************************************************************************************************************
convertCharToSprite()
******************************************************************************************************************************************************/
/*! \brief converts char to sprite
* \details this function converts the ASCII char to sprite index
*
* \param[in] Char char to convert to sprite
* \param[out] SpriteIndex appropriate sprite
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
stdReturnType MaxMatrix::convertCharToSprite(char Char, spriteIndexType* SpriteIndex)
{
stdReturnType ReturnValue = E_NOT_OK;
/* for umlauts we need a special treatment */
if('Ä' == Char) { *SpriteIndex = 95; ReturnValue = E_OK; }
else if('Ö' == Char) { *SpriteIndex = 96; ReturnValue = E_OK; }
else if('Ü' == Char) { *SpriteIndex = 97; ReturnValue = E_OK; }
else if('ä' == Char) { *SpriteIndex = 98; ReturnValue = E_OK; }
else if('ö' == Char) { *SpriteIndex = 99; ReturnValue = E_OK; }
else if('ü' == Char) { *SpriteIndex = 100; ReturnValue = E_OK; }
/* for all others only add offset */
else if(Char >= MAXMATRIX_ASCII_CHAR_MIN && Char <= MAXMATRIX_ASCII_CHAR_MAX)
{ *SpriteIndex = Char + MAXMATRIX_ASCII_TABLE_OFFSET; ReturnValue = E_OK; }