remove screen blinking in loop
[Arduino] / Hub08_LedMatrix / Hub08_LedMatrix.ino
1 /**
2  * LED Matrix library for http://www.seeedstudio.com/depot/ultrathin-16x32-red-led-matrix-panel-p-1582.html
3  * The LED Matrix panel has 32x16 pixels. Several panel can be combined together as a large screen.
4  *
5  * Coordinate & Connection (Arduino -> panel 0 -> panel 1 -> ...)
6  *   (0, 0)                                     (0, 0)
7  *     +--------+--------+--------+               +--------+--------+
8  *     |   5    |    4   |    3   |               |    1   |    0   |
9  *     |        |        |        |               |        |        |<----- Arduino
10  *     +--------+--------+--------+               +--------+--------+
11  *     |   2    |    1   |    0   |                              (64, 16)
12  *     |        |        |        |<----- Arduino
13  *     +--------+--------+--------+
14  *                             (96, 32)
15  *
16  */
17
18 #define FONT8x8 0
19
20 #include <avr/pgmspace.h>
21 #include "LEDMatrix.h"
22 #include "font.h"
23
24 #define WIDTH   64*1 // 2 panels * 64
25 #define HEIGHT  16
26
27 //     LEDMatrix(a, b, c, d, oe, r1, stb, clk);
28 LEDMatrix matrix(4, 5, 6, 7, 9,  11, 10,  13);
29
30 // Display Buffer 128 = 64 * 16 / 8
31 uint8_t displaybuf[WIDTH * HEIGHT / 8];
32
33 uint8_t displaybuf_w[((WIDTH/8)+1) * HEIGHT];
34
35
36 byte cell[16];
37
38 void MatrixWriteCharacter(int x,int y, char character)
39 {
40   //Serial.print(x);
41   //Serial.print("  ");
42   //Serial.println(character);
43 #if FONT8x8
44   for(int i=0; i<8; i++) {
45      uint8_t *pDst = displaybuf_w + (i+y) * ((WIDTH / 8) + 1) + x  ;
46      *pDst = (font_8x8[character - 0x20][i]);
47   }
48 #else
49   for(int i=0; i<16; i++) {
50      cell[i] = pgm_read_byte_near(&font_8x16[(character - 0x20)][i]);
51   }
52
53   //uint8_t *pDst = displaybuf_w + (y) * ((WIDTH / 8) + 1) + x  ;
54   uint8_t *pDst = displaybuf_w + x  ;
55
56   byte mask = 1;
57   for(int j=0; j<8; j++) {
58
59     byte out  = 0;
60     for(int i=0; i<8; i++) {
61         out <<= 1;
62         if ( cell[i] & mask ) {
63                 out |= 1;
64         }
65      }
66      *pDst = out;
67      pDst += (WIDTH/8)+1;
68
69      mask <<= 1;
70   }
71
72   mask = 1;
73   for(int j=0; j<8; j++) {
74     byte out  = 0;
75     for(int i=8; i<16; i++) {
76         out <<= 1;
77         if ( cell[i] & mask ) {
78                 out |= 1;
79         }
80      }
81      *pDst = out;
82      pDst += (WIDTH/8)+1;
83
84      mask <<= 1;
85   }
86 #endif
87 }
88
89
90
91 void matrixPrint(String c) {
92   //c="33";
93   //Serial.println(c);
94   //Serial.println(c.length());
95   for (int i=0 ; i<c.length() ; i++) {
96       MatrixWriteCharacter(i,3,c[i]);
97       matrix.scan();
98       //Serial.print(i);
99       //Serial.print(" -> ");
100       //Serial.println(c[i]);
101   }
102 }
103
104
105 void setup()
106 {
107     matrix.begin(displaybuf, WIDTH, HEIGHT);
108     Serial.begin(57600);
109     matrix.clear();
110     //matrixPrint("12345678");
111     // uint8_t *pDst = displaybuf + y * (WIDTH / 8) + x / 8;
112         memset(displaybuf_w, 0, sizeof(displaybuf_w));
113 }
114
115 void matrixDelay(int x) {
116   for (int y=0; y<x; y++) { matrix.scan(); }
117   
118 }
119
120 String poruka="X       XX      XOX     XOOX    XOoOX   XOooOX  XOoIoOX XOoiioOX        ";
121 //String poruka="        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras blandit libero id ex dapibus suscipit. Proin vitae cursus eros. Ut porttitor congue metus at viverra. In consectetur ex massa.";
122
123 void loop()
124 {
125     for (int p=0; p<poruka.length() ; p++) {
126           matrixPrint(poruka.substring(p,p+(WIDTH/8)+1));
127           for (int o=0; o<8; o++) {
128                 uint8_t *src  = displaybuf_w;
129                 uint8_t *dest = displaybuf;
130                 int i = 0;
131                 int j = 0;
132                 for (int y = 0; y < HEIGHT; y++ ) {
133                         for (int x = 0; x < (WIDTH/8); x++) {
134                                 *(dest + i) = ( *(src + j) << o ) | (( *(src + j + 1) & ( 0xff << 8 - o ) ) >> 8 - o );
135                                 i++;
136                                 j++;
137 #if USE_SPI
138                                 matrix.scan();
139 #else
140                                 if ( x == 0 ) matrix.scan();
141 #endif
142                         }
143                         j++; // skip off-screen character used for smooth scroll
144                 }
145           }
146     }
147
148 }