add hardware SPI support
[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
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     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33     0x01, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x01, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60,
34     0x01, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0xE0, 0x07, 0x8F, 0xC7, 0xC7, 0xC7, 0xE0,
35     0x00, 0x40, 0x0C, 0xCE, 0x6C, 0x6C, 0x6C, 0xE0, 0x00, 0xE0, 0x0C, 0x0C, 0x6C, 0x6C, 0x6C, 0x60,
36     0x01, 0xF0, 0x07, 0x8C, 0x6F, 0xEF, 0xEC, 0x60, 0x23, 0xF8, 0x00, 0xCC, 0x6C, 0x0C, 0x0C, 0x60,
37     0x33, 0xF8, 0x0C, 0xCE, 0x6C, 0x6C, 0x6C, 0xE0, 0x3B, 0xF8, 0x07, 0x8F, 0xC7, 0xC7, 0xC7, 0xE0,
38     0x3B, 0xF8, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x1B, 0xF8, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00,
39     0x0B, 0xF8, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 };
41
42 uint8_t displaybuf_w[((WIDTH/8)+1) * HEIGHT];
43
44
45 byte cell[16];
46
47 void MatrixWriteCharacter(int x,int y, char character)
48 {
49   //Serial.print(x);
50   //Serial.print("  ");
51   //Serial.println(character);
52 #if FONT8x8
53   for(int i=0; i<8; i++) {
54      uint8_t *pDst = displaybuf_w + (i+y) * ((WIDTH / 8) + 1) + x  ;
55      *pDst = (font_8x8[character - 0x20][i]);
56   }
57 #else
58   for(int i=0; i<16; i++) {
59      cell[i] = pgm_read_byte_near(&font_8x16[(character - 0x20)][i]);
60   }
61
62   //uint8_t *pDst = displaybuf_w + (y) * ((WIDTH / 8) + 1) + x  ;
63   uint8_t *pDst = displaybuf_w + x  ;
64
65   byte mask = 1;
66   for(int j=0; j<8; j++) {
67
68     byte out  = 0;
69     for(int i=0; i<8; i++) {
70         out <<= 1;
71         if ( cell[i] & mask ) {
72                 out |= 1;
73         }
74      }
75      *pDst = out;
76      pDst += (WIDTH/8)+1;
77
78      mask <<= 1;
79   }
80
81   mask = 1;
82   for(int j=0; j<8; j++) {
83     byte out  = 0;
84     for(int i=8; i<16; i++) {
85         out <<= 1;
86         if ( cell[i] & mask ) {
87                 out |= 1;
88         }
89      }
90      *pDst = out;
91      pDst += (WIDTH/8)+1;
92
93      mask <<= 1;
94   }
95 #endif
96 }
97
98
99
100 void matrixPrint(String c) {
101   //c="33";
102   //Serial.println(c);
103   //Serial.println(c.length());
104   for (int i=0 ; i<c.length() ; i++) {
105       MatrixWriteCharacter(i,3,c[i]);
106 //      matrix.scan();
107       //Serial.print(i);
108       //Serial.print(" -> ");
109       //Serial.println(c[i]);
110   }
111 }
112
113
114 void setup()
115 {
116     matrix.begin(displaybuf, WIDTH, HEIGHT);
117     Serial.begin(57600);
118     matrix.clear();
119     //matrixPrint("12345678");
120     // uint8_t *pDst = displaybuf + y * (WIDTH / 8) + x / 8;
121         memset(displaybuf_w, 0, sizeof(displaybuf_w));
122 }
123
124 void matrixDelay(int x) {
125   for (int y=0; y<x; y++) { matrix.scan(); }
126   
127 }
128
129 void loop()
130 {
131     matrix.clear();
132     int p=0;
133 //    String poruka="X       XX      XOX     XOOX    XOoOX   XOooOX  XOoIoOX XOoiioOX        ";
134     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.";
135
136     for (int p=0; p<poruka.length() ; p++) {
137           matrix.clear();
138           matrixPrint(poruka.substring(p,p+9));
139           for (int o=0; o<8; o++) {
140                 uint8_t *src  = displaybuf_w;
141                 uint8_t *dest = displaybuf;
142                 int i = 0;
143                 int j = 0;
144                 for (int y = 0; y < HEIGHT; y++ ) {
145                         for (int x = 0; x < (WIDTH/8); x++) {
146                                 *(dest + i) = ( *(src + j) << o ) | (( *(src + j + 1) & ( 0xff << 8 - o ) ) >> 8 - o );
147                                 i++;
148                                 j++;
149                         }
150                         j++;
151                         matrix.scan();
152                 }
153           }
154     }
155
156 }