1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Credits to Adafruit.
6  * Based on Adafruit ST7735 library, see original license in license.txt file.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 #pragma once
28 
29 #include <string>
30 #include <mraa/aio.hpp>
31 #include <mraa/common.hpp>
32 
33 #include <mraa/gpio.hpp>
34 
35 #include <mraa/spi.hpp>
36 #include <gfx.h>
37 
38 #define INITR_GREENTAB      0x0
39 #define INITR_REDTAB        0x1
40 #define INITR_BLACKTAB      0x2
41 
42 #define ST7735_TFTWIDTH     128
43 #define ST7735_TFTHEIGHT    160
44 
45 #define ST7735_NOP          0x00
46 #define ST7735_SWRESET      0x01
47 #define ST7735_RDDID        0x04
48 #define ST7735_RDDST        0x09
49 
50 #define ST7735_SLPIN        0x10
51 #define ST7735_SLPOUT       0x11
52 #define ST7735_PTLON        0x12
53 #define ST7735_NORON        0x13
54 
55 #define ST7735_INVOFF       0x20
56 #define ST7735_INVON        0x21
57 #define ST7735_DISPOFF      0x28
58 #define ST7735_DISPON       0x29
59 #define ST7735_CASET        0x2A
60 #define ST7735_RASET        0x2B
61 #define ST7735_RAMWR        0x2C
62 #define ST7735_RAMRD        0x2E
63 
64 #define ST7735_PTLAR        0x30
65 #define ST7735_COLMOD       0x3A
66 #define ST7735_MADCTL       0x36
67 
68 #define ST7735_FRMCTR1      0xB1
69 #define ST7735_FRMCTR2      0xB2
70 #define ST7735_FRMCTR3      0xB3
71 #define ST7735_INVCTR       0xB4
72 #define ST7735_DISSET5      0xB6
73 
74 #define ST7735_PWCTR1       0xC0
75 #define ST7735_PWCTR2       0xC1
76 #define ST7735_PWCTR3       0xC2
77 #define ST7735_PWCTR4       0xC3
78 #define ST7735_PWCTR5       0xC4
79 #define ST7735_VMCTR1       0xC5
80 
81 #define ST7735_RDID1        0xDA
82 #define ST7735_RDID2        0xDB
83 #define ST7735_RDID3        0xDC
84 #define ST7735_RDID4        0xDD
85 
86 #define ST7735_PWCTR6       0xFC
87 
88 #define ST7735_GMCTRP1      0xE0
89 #define ST7735_GMCTRN1      0xE1
90 
91 // Color definitions
92 #define ST7735_BLACK        0x0000
93 #define ST7735_BLUE         0x001F
94 #define ST7735_RED          0xF800
95 #define ST7735_GREEN        0x07E0
96 #define ST7735_CYAN         0x07FF
97 #define ST7735_MAGENTA      0xF81F
98 #define ST7735_YELLOW       0xFFE0
99 #define ST7735_WHITE        0xFFFF
100 
101 #define HIGH                1
102 #define LOW                 0
103 
104 #define DELAY               0x80
105 
106 namespace upm {
107 
108 static const uint8_t Bcmd[] = {
109     // Initialization commands for 7735B screens
110       18,                     // 18 commands in list:
111     ST7735_SWRESET,   DELAY,  //  1: Software reset, no args, w/delay
112       50,                     //     50 ms delay
113     ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, no args, w/delay
114       255,                    //     255 = 500 ms delay
115     ST7735_COLMOD , 1+DELAY,  //  3: Set color mode, 1 arg + delay:
116       0x05,                   //     16-bit color
117       10,                     //     10 ms delay
118     ST7735_FRMCTR1, 3+DELAY,  //  4: Frame rate control, 3 args + delay:
119       0x00,                   //     fastest refresh
120       0x06,                   //     6 lines front porch
121       0x03,                   //     3 lines back porch
122       10,                     //     10 ms delay
123     ST7735_MADCTL , 1      ,  //  5: Memory access ctrl (directions), 1 arg:
124       0x08,                   //     Row addr/col addr, bottom to top refresh
125     ST7735_DISSET5, 2      ,  //  6: Display settings #5, 2 args, no delay:
126       0x15,                   //     1 clk cycle nonoverlap, 2 cycle gate
127                               //     rise, 3 cycle osc equalize
128       0x02,                   //     Fix on VTL
129     ST7735_INVCTR , 1      ,  //  7: Display inversion control, 1 arg:
130       0x0,                    //     Line inversion
131     ST7735_PWCTR1 , 2+DELAY,  //  8: Power control, 2 args + delay:
132       0x02,                   //     GVDD = 4.7V
133       0x70,                   //     1.0uA
134       10,                     //     10 ms delay
135     ST7735_PWCTR2 , 1      ,  //  9: Power control, 1 arg, no delay:
136       0x05,                   //     VGH = 14.7V, VGL = -7.35V
137     ST7735_PWCTR3 , 2      ,  // 10: Power control, 2 args, no delay:
138       0x01,                   //     Opamp current small
139       0x02,                   //     Boost frequency
140     ST7735_VMCTR1 , 2+DELAY,  // 11: Power control, 2 args + delay:
141       0x3C,                   //     VCOMH = 4V
142       0x38,                   //     VCOML = -1.1V
143       10,                     //     10 ms delay
144     ST7735_PWCTR6 , 2      ,  // 12: Power control, 2 args, no delay:
145       0x11, 0x15,
146     ST7735_GMCTRP1,16      ,  // 13: Magical unicorn dust, 16 args, no delay:
147       0x09, 0x16, 0x09, 0x20, //     (seriously though, not sure what
148       0x21, 0x1B, 0x13, 0x19, //      these config values represent)
149       0x17, 0x15, 0x1E, 0x2B,
150       0x04, 0x05, 0x02, 0x0E,
151     ST7735_GMCTRN1,16+DELAY,  // 14: Sparkles and rainbows, 16 args + delay:
152       0x0B, 0x14, 0x08, 0x1E, //     (ditto)
153       0x22, 0x1D, 0x18, 0x1E,
154       0x1B, 0x1A, 0x24, 0x2B,
155       0x06, 0x06, 0x02, 0x0F,
156       10,                     //     10 ms delay
157     ST7735_CASET  , 4      ,  // 15: Column addr set, 4 args, no delay:
158       0x00, 0x02,             //     XSTART = 2
159       0x00, 0x81,             //     XEND = 129
160     ST7735_RASET  , 4      ,  // 16: Row addr set, 4 args, no delay:
161       0x00, 0x02,             //     XSTART = 1
162       0x00, 0x81,             //     XEND = 160
163     ST7735_NORON  ,   DELAY,  // 17: Normal display on, no args, w/delay
164       10,                     //     10 ms delay
165     ST7735_DISPON ,   DELAY,  // 18: Main screen turn on, no args, w/delay
166       255 },                  //     255 = 500 ms delay
167 
168   Rcmd1[] = {                 // Init for 7735R, part 1 (red or green tab)
169       15,                     // 15 commands in list:
170     ST7735_SWRESET,   DELAY,  //  1: Software reset, 0 args, w/delay
171       150,                    //     150 ms delay
172     ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, 0 args, w/delay
173       255,                    //     500 ms delay
174     ST7735_FRMCTR1, 3      ,  //  3: Frame rate ctrl - normal mode, 3 args:
175       0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
176     ST7735_FRMCTR2, 3      ,  //  4: Frame rate control - idle mode, 3 args:
177       0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
178     ST7735_FRMCTR3, 6      ,  //  5: Frame rate ctrl - partial mode, 6 args:
179       0x01, 0x2C, 0x2D,       //     Dot inversion mode
180       0x01, 0x2C, 0x2D,       //     Line inversion mode
181     ST7735_INVCTR , 1      ,  //  6: Display inversion ctrl, 1 arg, no delay:
182       0x07,                   //     No inversion
183     ST7735_PWCTR1 , 3      ,  //  7: Power control, 3 args, no delay:
184       0xA2,
185       0x02,                   //     -4.6V
186       0x84,                   //     AUTO mode
187     ST7735_PWCTR2 , 1      ,  //  8: Power control, 1 arg, no delay:
188       0xC5,                   //     VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
189     ST7735_PWCTR3 , 2      ,  //  9: Power control, 2 args, no delay:
190       0x0A,                   //     Opamp current small
191       0x00,                   //     Boost frequency
192     ST7735_PWCTR4 , 2      ,  // 10: Power control, 2 args, no delay:
193       0x8A,                   //     BCLK/2, Opamp current small & Medium low
194       0x2A,
195     ST7735_PWCTR5 , 2      ,  // 11: Power control, 2 args, no delay:
196       0x8A, 0xEE,
197     ST7735_VMCTR1 , 1      ,  // 12: Power control, 1 arg, no delay:
198       0x0E,
199     ST7735_INVOFF , 0      ,  // 13: Don't invert display, no args, no delay
200     ST7735_MADCTL , 1      ,  // 14: Memory access control (directions), 1 arg:
201       0xC8,                   //     row addr/col addr, bottom to top refresh
202     ST7735_COLMOD , 1      ,  // 15: set color mode, 1 arg, no delay:
203       0x05 },                 //     16-bit color
204 
205   Rcmd2green[] = {            // Init for 7735R, part 2 (green tab only)
206       2,                      //  2 commands in list:
207     ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
208       0x00, 0x02,             //     XSTART = 0
209       0x00, 0x7F+0x02,        //     XEND = 127
210     ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
211       0x00, 0x01,             //     XSTART = 0
212       0x00, 0x9F+0x01 },      //     XEND = 159
213   Rcmd2red[] = {              // Init for 7735R, part 2 (red tab only)
214       2,                      //  2 commands in list:
215     ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
216       0x00, 0x00,             //     XSTART = 0
217       0x00, 0x7F,             //     XEND = 127
218     ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
219       0x00, 0x00,             //     XSTART = 0
220       0x00, 0x9F },           //     XEND = 159
221 
222   Rcmd3[] = {                 // Init for 7735R, part 3 (red or green tab)
223       4,                      //  4 commands in list:
224     ST7735_GMCTRP1, 16      , //  1: Magical unicorn dust, 16 args, no delay:
225       0x02, 0x1c, 0x07, 0x12,
226       0x37, 0x32, 0x29, 0x2d,
227       0x29, 0x25, 0x2B, 0x39,
228       0x00, 0x01, 0x03, 0x10,
229     ST7735_GMCTRN1, 16      , //  2: Sparkles and rainbows, 16 args, no delay:
230       0x03, 0x1d, 0x07, 0x06,
231       0x2E, 0x2C, 0x29, 0x2D,
232       0x2E, 0x2E, 0x37, 0x3F,
233       0x00, 0x00, 0x02, 0x10,
234     ST7735_NORON  ,    DELAY, //  3: Normal display on, no args, w/delay
235       10,                     //     10 ms delay
236     ST7735_DISPON ,    DELAY, //  4: Main screen turn on, no args w/delay
237       100 };                  //     100 ms delay
238 
239 #define swap(a, b) { int16_t t = a; a = b; b = t; }
240 
241 const unsigned char font[] = {
242     0x00, 0x00, 0x00, 0x00, 0x00,
243     0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
244     0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
245     0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
246     0x18, 0x3C, 0x7E, 0x3C, 0x18,
247     0x1C, 0x57, 0x7D, 0x57, 0x1C,
248     0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
249     0x00, 0x18, 0x3C, 0x18, 0x00,
250     0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
251     0x00, 0x18, 0x24, 0x18, 0x00,
252     0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
253     0x30, 0x48, 0x3A, 0x06, 0x0E,
254     0x26, 0x29, 0x79, 0x29, 0x26,
255     0x40, 0x7F, 0x05, 0x05, 0x07,
256     0x40, 0x7F, 0x05, 0x25, 0x3F,
257     0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
258     0x7F, 0x3E, 0x1C, 0x1C, 0x08,
259     0x08, 0x1C, 0x1C, 0x3E, 0x7F,
260     0x14, 0x22, 0x7F, 0x22, 0x14,
261     0x5F, 0x5F, 0x00, 0x5F, 0x5F,
262     0x06, 0x09, 0x7F, 0x01, 0x7F,
263     0x00, 0x66, 0x89, 0x95, 0x6A,
264     0x60, 0x60, 0x60, 0x60, 0x60,
265     0x94, 0xA2, 0xFF, 0xA2, 0x94,
266     0x08, 0x04, 0x7E, 0x04, 0x08,
267     0x10, 0x20, 0x7E, 0x20, 0x10,
268     0x08, 0x08, 0x2A, 0x1C, 0x08,
269     0x08, 0x1C, 0x2A, 0x08, 0x08,
270     0x1E, 0x10, 0x10, 0x10, 0x10,
271     0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
272     0x30, 0x38, 0x3E, 0x38, 0x30,
273     0x06, 0x0E, 0x3E, 0x0E, 0x06,
274     0x00, 0x00, 0x00, 0x00, 0x00,
275     0x00, 0x00, 0x5F, 0x00, 0x00,
276     0x00, 0x07, 0x00, 0x07, 0x00,
277     0x14, 0x7F, 0x14, 0x7F, 0x14,
278     0x24, 0x2A, 0x7F, 0x2A, 0x12,
279     0x23, 0x13, 0x08, 0x64, 0x62,
280     0x36, 0x49, 0x56, 0x20, 0x50,
281     0x00, 0x08, 0x07, 0x03, 0x00,
282     0x00, 0x1C, 0x22, 0x41, 0x00,
283     0x00, 0x41, 0x22, 0x1C, 0x00,
284     0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
285     0x08, 0x08, 0x3E, 0x08, 0x08,
286     0x00, 0x80, 0x70, 0x30, 0x00,
287     0x08, 0x08, 0x08, 0x08, 0x08,
288     0x00, 0x00, 0x60, 0x60, 0x00,
289     0x20, 0x10, 0x08, 0x04, 0x02,
290     0x3E, 0x51, 0x49, 0x45, 0x3E,
291     0x00, 0x42, 0x7F, 0x40, 0x00,
292     0x72, 0x49, 0x49, 0x49, 0x46,
293     0x21, 0x41, 0x49, 0x4D, 0x33,
294     0x18, 0x14, 0x12, 0x7F, 0x10,
295     0x27, 0x45, 0x45, 0x45, 0x39,
296     0x3C, 0x4A, 0x49, 0x49, 0x31,
297     0x41, 0x21, 0x11, 0x09, 0x07,
298     0x36, 0x49, 0x49, 0x49, 0x36,
299     0x46, 0x49, 0x49, 0x29, 0x1E,
300     0x00, 0x00, 0x14, 0x00, 0x00,
301     0x00, 0x40, 0x34, 0x00, 0x00,
302     0x00, 0x08, 0x14, 0x22, 0x41,
303     0x14, 0x14, 0x14, 0x14, 0x14,
304     0x00, 0x41, 0x22, 0x14, 0x08,
305     0x02, 0x01, 0x59, 0x09, 0x06,
306     0x3E, 0x41, 0x5D, 0x59, 0x4E,
307     0x7C, 0x12, 0x11, 0x12, 0x7C,
308     0x7F, 0x49, 0x49, 0x49, 0x36,
309     0x3E, 0x41, 0x41, 0x41, 0x22,
310     0x7F, 0x41, 0x41, 0x41, 0x3E,
311     0x7F, 0x49, 0x49, 0x49, 0x41,
312     0x7F, 0x09, 0x09, 0x09, 0x01,
313     0x3E, 0x41, 0x41, 0x51, 0x73,
314     0x7F, 0x08, 0x08, 0x08, 0x7F,
315     0x00, 0x41, 0x7F, 0x41, 0x00,
316     0x20, 0x40, 0x41, 0x3F, 0x01,
317     0x7F, 0x08, 0x14, 0x22, 0x41,
318     0x7F, 0x40, 0x40, 0x40, 0x40,
319     0x7F, 0x02, 0x1C, 0x02, 0x7F,
320     0x7F, 0x04, 0x08, 0x10, 0x7F,
321     0x3E, 0x41, 0x41, 0x41, 0x3E,
322     0x7F, 0x09, 0x09, 0x09, 0x06,
323     0x3E, 0x41, 0x51, 0x21, 0x5E,
324     0x7F, 0x09, 0x19, 0x29, 0x46,
325     0x26, 0x49, 0x49, 0x49, 0x32,
326     0x03, 0x01, 0x7F, 0x01, 0x03,
327     0x3F, 0x40, 0x40, 0x40, 0x3F,
328     0x1F, 0x20, 0x40, 0x20, 0x1F,
329     0x3F, 0x40, 0x38, 0x40, 0x3F,
330     0x63, 0x14, 0x08, 0x14, 0x63,
331     0x03, 0x04, 0x78, 0x04, 0x03,
332     0x61, 0x59, 0x49, 0x4D, 0x43,
333     0x00, 0x7F, 0x41, 0x41, 0x41,
334     0x02, 0x04, 0x08, 0x10, 0x20,
335     0x00, 0x41, 0x41, 0x41, 0x7F,
336     0x04, 0x02, 0x01, 0x02, 0x04,
337     0x40, 0x40, 0x40, 0x40, 0x40,
338     0x00, 0x03, 0x07, 0x08, 0x00,
339     0x20, 0x54, 0x54, 0x78, 0x40,
340     0x7F, 0x28, 0x44, 0x44, 0x38,
341     0x38, 0x44, 0x44, 0x44, 0x28,
342     0x38, 0x44, 0x44, 0x28, 0x7F,
343     0x38, 0x54, 0x54, 0x54, 0x18,
344     0x00, 0x08, 0x7E, 0x09, 0x02,
345     0x18, 0xA4, 0xA4, 0x9C, 0x78,
346     0x7F, 0x08, 0x04, 0x04, 0x78,
347     0x00, 0x44, 0x7D, 0x40, 0x00,
348     0x20, 0x40, 0x40, 0x3D, 0x00,
349     0x7F, 0x10, 0x28, 0x44, 0x00,
350     0x00, 0x41, 0x7F, 0x40, 0x00,
351     0x7C, 0x04, 0x78, 0x04, 0x78,
352     0x7C, 0x08, 0x04, 0x04, 0x78,
353     0x38, 0x44, 0x44, 0x44, 0x38,
354     0xFC, 0x18, 0x24, 0x24, 0x18,
355     0x18, 0x24, 0x24, 0x18, 0xFC,
356     0x7C, 0x08, 0x04, 0x04, 0x08,
357     0x48, 0x54, 0x54, 0x54, 0x24,
358     0x04, 0x04, 0x3F, 0x44, 0x24,
359     0x3C, 0x40, 0x40, 0x20, 0x7C,
360     0x1C, 0x20, 0x40, 0x20, 0x1C,
361     0x3C, 0x40, 0x30, 0x40, 0x3C,
362     0x44, 0x28, 0x10, 0x28, 0x44,
363     0x4C, 0x90, 0x90, 0x90, 0x7C,
364     0x44, 0x64, 0x54, 0x4C, 0x44,
365     0x00, 0x08, 0x36, 0x41, 0x00,
366     0x00, 0x00, 0x77, 0x00, 0x00,
367     0x00, 0x41, 0x36, 0x08, 0x00,
368     0x02, 0x01, 0x02, 0x04, 0x02,
369     0x3C, 0x26, 0x23, 0x26, 0x3C,
370     0x1E, 0xA1, 0xA1, 0x61, 0x12,
371     0x3A, 0x40, 0x40, 0x20, 0x7A,
372     0x38, 0x54, 0x54, 0x55, 0x59,
373     0x21, 0x55, 0x55, 0x79, 0x41,
374     0x21, 0x54, 0x54, 0x78, 0x41,
375     0x21, 0x55, 0x54, 0x78, 0x40,
376     0x20, 0x54, 0x55, 0x79, 0x40,
377     0x0C, 0x1E, 0x52, 0x72, 0x12,
378     0x39, 0x55, 0x55, 0x55, 0x59,
379     0x39, 0x54, 0x54, 0x54, 0x59,
380     0x39, 0x55, 0x54, 0x54, 0x58,
381     0x00, 0x00, 0x45, 0x7C, 0x41,
382     0x00, 0x02, 0x45, 0x7D, 0x42,
383     0x00, 0x01, 0x45, 0x7C, 0x40,
384     0xF0, 0x29, 0x24, 0x29, 0xF0,
385     0xF0, 0x28, 0x25, 0x28, 0xF0,
386     0x7C, 0x54, 0x55, 0x45, 0x00,
387     0x20, 0x54, 0x54, 0x7C, 0x54,
388     0x7C, 0x0A, 0x09, 0x7F, 0x49,
389     0x32, 0x49, 0x49, 0x49, 0x32,
390     0x32, 0x48, 0x48, 0x48, 0x32,
391     0x32, 0x4A, 0x48, 0x48, 0x30,
392     0x3A, 0x41, 0x41, 0x21, 0x7A,
393     0x3A, 0x42, 0x40, 0x20, 0x78,
394     0x00, 0x9D, 0xA0, 0xA0, 0x7D,
395     0x39, 0x44, 0x44, 0x44, 0x39,
396     0x3D, 0x40, 0x40, 0x40, 0x3D,
397     0x3C, 0x24, 0xFF, 0x24, 0x24,
398     0x48, 0x7E, 0x49, 0x43, 0x66,
399     0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
400     0xFF, 0x09, 0x29, 0xF6, 0x20,
401     0xC0, 0x88, 0x7E, 0x09, 0x03,
402     0x20, 0x54, 0x54, 0x79, 0x41,
403     0x00, 0x00, 0x44, 0x7D, 0x41,
404     0x30, 0x48, 0x48, 0x4A, 0x32,
405     0x38, 0x40, 0x40, 0x22, 0x7A,
406     0x00, 0x7A, 0x0A, 0x0A, 0x72,
407     0x7D, 0x0D, 0x19, 0x31, 0x7D,
408     0x26, 0x29, 0x29, 0x2F, 0x28,
409     0x26, 0x29, 0x29, 0x29, 0x26,
410     0x30, 0x48, 0x4D, 0x40, 0x20,
411     0x38, 0x08, 0x08, 0x08, 0x08,
412     0x08, 0x08, 0x08, 0x08, 0x38,
413     0x2F, 0x10, 0xC8, 0xAC, 0xBA,
414     0x2F, 0x10, 0x28, 0x34, 0xFA,
415     0x00, 0x00, 0x7B, 0x00, 0x00,
416     0x08, 0x14, 0x2A, 0x14, 0x22,
417     0x22, 0x14, 0x2A, 0x14, 0x08,
418     0xAA, 0x00, 0x55, 0x00, 0xAA,
419     0xAA, 0x55, 0xAA, 0x55, 0xAA,
420     0x00, 0x00, 0x00, 0xFF, 0x00,
421     0x10, 0x10, 0x10, 0xFF, 0x00,
422     0x14, 0x14, 0x14, 0xFF, 0x00,
423     0x10, 0x10, 0xFF, 0x00, 0xFF,
424     0x10, 0x10, 0xF0, 0x10, 0xF0,
425     0x14, 0x14, 0x14, 0xFC, 0x00,
426     0x14, 0x14, 0xF7, 0x00, 0xFF,
427     0x00, 0x00, 0xFF, 0x00, 0xFF,
428     0x14, 0x14, 0xF4, 0x04, 0xFC,
429     0x14, 0x14, 0x17, 0x10, 0x1F,
430     0x10, 0x10, 0x1F, 0x10, 0x1F,
431     0x14, 0x14, 0x14, 0x1F, 0x00,
432     0x10, 0x10, 0x10, 0xF0, 0x00,
433     0x00, 0x00, 0x00, 0x1F, 0x10,
434     0x10, 0x10, 0x10, 0x1F, 0x10,
435     0x10, 0x10, 0x10, 0xF0, 0x10,
436     0x00, 0x00, 0x00, 0xFF, 0x10,
437     0x10, 0x10, 0x10, 0x10, 0x10,
438     0x10, 0x10, 0x10, 0xFF, 0x10,
439     0x00, 0x00, 0x00, 0xFF, 0x14,
440     0x00, 0x00, 0xFF, 0x00, 0xFF,
441     0x00, 0x00, 0x1F, 0x10, 0x17,
442     0x00, 0x00, 0xFC, 0x04, 0xF4,
443     0x14, 0x14, 0x17, 0x10, 0x17,
444     0x14, 0x14, 0xF4, 0x04, 0xF4,
445     0x00, 0x00, 0xFF, 0x00, 0xF7,
446     0x14, 0x14, 0x14, 0x14, 0x14,
447     0x14, 0x14, 0xF7, 0x00, 0xF7,
448     0x14, 0x14, 0x14, 0x17, 0x14,
449     0x10, 0x10, 0x1F, 0x10, 0x1F,
450     0x14, 0x14, 0x14, 0xF4, 0x14,
451     0x10, 0x10, 0xF0, 0x10, 0xF0,
452     0x00, 0x00, 0x1F, 0x10, 0x1F,
453     0x00, 0x00, 0x00, 0x1F, 0x14,
454     0x00, 0x00, 0x00, 0xFC, 0x14,
455     0x00, 0x00, 0xF0, 0x10, 0xF0,
456     0x10, 0x10, 0xFF, 0x10, 0xFF,
457     0x14, 0x14, 0x14, 0xFF, 0x14,
458     0x10, 0x10, 0x10, 0x1F, 0x00,
459     0x00, 0x00, 0x00, 0xF0, 0x10,
460     0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
461     0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
462     0xFF, 0xFF, 0xFF, 0x00, 0x00,
463     0x00, 0x00, 0x00, 0xFF, 0xFF,
464     0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
465     0x38, 0x44, 0x44, 0x38, 0x44,
466     0x7C, 0x2A, 0x2A, 0x3E, 0x14,
467     0x7E, 0x02, 0x02, 0x06, 0x06,
468     0x02, 0x7E, 0x02, 0x7E, 0x02,
469     0x63, 0x55, 0x49, 0x41, 0x63,
470     0x38, 0x44, 0x44, 0x3C, 0x04,
471     0x40, 0x7E, 0x20, 0x1E, 0x20,
472     0x06, 0x02, 0x7E, 0x02, 0x02,
473     0x99, 0xA5, 0xE7, 0xA5, 0x99,
474     0x1C, 0x2A, 0x49, 0x2A, 0x1C,
475     0x4C, 0x72, 0x01, 0x72, 0x4C,
476     0x30, 0x4A, 0x4D, 0x4D, 0x30,
477     0x30, 0x48, 0x78, 0x48, 0x30,
478     0xBC, 0x62, 0x5A, 0x46, 0x3D,
479     0x3E, 0x49, 0x49, 0x49, 0x00,
480     0x7E, 0x01, 0x01, 0x01, 0x7E,
481     0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
482     0x44, 0x44, 0x5F, 0x44, 0x44,
483     0x40, 0x51, 0x4A, 0x44, 0x40,
484     0x40, 0x44, 0x4A, 0x51, 0x40,
485     0x00, 0x00, 0xFF, 0x01, 0x03,
486     0xE0, 0x80, 0xFF, 0x00, 0x00,
487     0x08, 0x08, 0x6B, 0x6B, 0x08,
488     0x36, 0x12, 0x36, 0x24, 0x36,
489     0x06, 0x0F, 0x09, 0x0F, 0x06,
490     0x00, 0x00, 0x18, 0x18, 0x00,
491     0x00, 0x00, 0x10, 0x10, 0x00,
492     0x30, 0x40, 0xFF, 0x01, 0x01,
493     0x00, 0x1F, 0x01, 0x01, 0x1E,
494     0x00, 0x19, 0x1D, 0x17, 0x12,
495     0x00, 0x3C, 0x3C, 0x3C, 0x3C,
496     0x00, 0x00, 0x00, 0x00, 0x00
497 };
498 
499 /**
500  * @brief ST7735 LCD library
501  * @defgroup st7735 libupm-st7735
502  * @ingroup adafruit spi display
503  */
504 /**
505  * @library st7735
506  * @sensor st7735
507  * @comname ST7735 LCD
508  * @type display
509  * @man adafruit
510  * @web http://www.adafruit.com/product/358
511  * @con spi
512  *
513  * @brief API for the ST7735 LCD
514  *
515  * This module defines the interface for the ST7735 display library
516  *
517  * @image html st7735.jpg
518  * @snippet st7735.cxx Interesting
519  */
520 class ST7735 : public GFX {
521     public:
522         /**
523          * Instantiates an ST7735 object
524          *
525          * @param csLCD LCD chip select pin
526          * @param cSD SD card chip select pin
527          * @param rs Data/command pin
528          * @param rst Reset pin
529          */
530         ST7735 (uint8_t csLCD, uint8_t cSD, uint8_t rs, uint8_t rst);
531 
532         /**
533          * Returns the name of the component
534          */
name()535         std::string name()
536         {
537             return m_name;
538         }
539 
540         /**
541          * Initializes the module GPIOs
542          */
543         void initModule ();
544 
545         /**
546          * Configures the chip via the SPI
547          */
548         void configModule ();
549 
550         /**
551          * Sends a command to an SPI bus (rs must be LOW)
552          *
553          * @param value Command number
554          */
555         void write (uint8_t value);
556 
557         /**
558          * Sends data to an SPI bus (rs must be HIGH)
559          *
560          * @param value Command number
561          */
562         void data (uint8_t value);
563 
564         /**
565          * Executes a set of commands and data
566          *
567          * @param addr Pointer to the start of the commands/data section
568          */
569         void executeCMDList (const uint8_t *addr);
570 
571         /**
572          * Sets the window size inside the screen where pixels data
573          * is written.
574          *
575          * @param x0 First coordinate
576          * @param y0 First coordinate
577          * @param x1 Second coordinate
578          * @param y1 Second coordinate
579          */
580         void setAddrWindow (uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1);
581 
582         /**
583          * Sends a pixel color (RGB) to the chip.
584          *
585          * @param x Axis on the horizontal scale
586          * @param y Axis on the vertical scale
587          * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15])
588          */
589         void drawPixel (int16_t x, int16_t y, uint16_t color);
590 
591         /**
592          * Copies the buffer to the chip via the SPI.
593          */
594         void refresh ();
595 
596         /**
597          * LCD chip select is LOW
598          */
599         mraa::Result lcdCSOn ();
600 
601         /**
602          * LCD chip select is HIGH
603          */
604         mraa::Result lcdCSOff ();
605 
606         /**
607          * CD card chip select is LOW
608          */
609         mraa::Result sdCSOn ();
610 
611         /**
612          * CD card select is HIGH
613          */
614         mraa::Result sdCSOff ();
615 
616         /**
617          * Data select is HIGH
618          */
619         mraa::Result rsHIGH ();
620 
621         /**
622          * Data select is LOW
623          */
624         mraa::Result rsLOW ();
625 
626         uint8_t m_map[160 * 128 * 2]; /**< Screens buffer */
627     private:
628         mraa::Spi      m_spi;
629         uint8_t        m_spiBuffer[32];
630 
631         mraa::Gpio     m_csLCDPinCtx;
632         mraa::Gpio     m_cSDPinCtx;
633         mraa::Gpio     m_rSTPinCtx;
634         mraa::Gpio     m_rSPinCtx;
635 
636         std::string    m_name;
637 };
638 
639 }
640