2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 ST7735::ST7735 (uint8_t csLCD, uint8_t cSD, uint8_t rs, uint8_t rst) : GFX (160, 128, m_map, font) {
48 maa_result_t error = MAA_SUCCESS;
49 error = maa_spi_stop(m_spi);
50 if (error != MAA_SUCCESS) {
51 maa_result_print(error);
53 error = maa_gpio_close (m_csLCDPinCtx);
54 if (error != MAA_SUCCESS) {
55 maa_result_print(error);
57 error = maa_gpio_close (m_cSDPinCtx);
58 if (error != MAA_SUCCESS) {
59 maa_result_print(error);
61 error = maa_gpio_close (m_rSTPinCtx);
62 if (error != MAA_SUCCESS) {
63 maa_result_print(error);
65 error = maa_gpio_close (m_rSPinCtx);
66 if (error != MAA_SUCCESS) {
67 maa_result_print(error);
72 ST7735::initModule () {
73 maa_result_t error = MAA_SUCCESS;
78 m_csLCDPinCtx = maa_gpio_init (m_csLCD);
79 if (m_csLCDPinCtx == NULL) {
80 fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", m_csLCD);
84 m_cSDPinCtx = maa_gpio_init (m_cSD);
85 if (m_cSDPinCtx == NULL) {
86 fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", m_cSD);
90 m_rSTPinCtx = maa_gpio_init (m_rST);
91 if (m_rSTPinCtx == NULL) {
92 fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", m_rST);
96 m_rSPinCtx = maa_gpio_init (m_rS);
97 if (m_rSPinCtx == NULL) {
98 fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", m_rS);
102 error = maa_gpio_dir (m_csLCDPinCtx, MAA_GPIO_OUT);
103 if (error != MAA_SUCCESS) {
104 maa_result_print (error);
107 error = maa_gpio_dir (m_cSDPinCtx, MAA_GPIO_OUT);
108 if (error != MAA_SUCCESS) {
109 maa_result_print (error);
112 error = maa_gpio_dir (m_rSTPinCtx, MAA_GPIO_OUT);
113 if (error != MAA_SUCCESS) {
114 maa_result_print (error);
117 error = maa_gpio_dir (m_rSPinCtx, MAA_GPIO_OUT);
118 if (error != MAA_SUCCESS) {
119 maa_result_print (error);
122 m_spi = maa_spi_init (0);
123 error = maa_spi_frequency(m_spi, 15 * 1000000);
124 if (error != MAA_SUCCESS) {
125 maa_result_print (error);
132 ST7735::write (uint8_t value) {
134 maa_spi_write (m_spi, value);
138 ST7735::data (uint8_t value) {
140 maa_spi_write (m_spi, value);
144 ST7735::executeCMDList(const uint8_t *addr) {
145 uint8_t numCommands, numArgs;
148 numCommands = *(addr++); // Number of commands to follow
149 while(numCommands--) { // For each command...
150 write (*(addr++)); // Read, issue command
151 numArgs = *(addr++); // Number of args to follow
152 ms = numArgs & DELAY; // If hibit set, delay follows args
153 numArgs &= ~DELAY; // Mask out delay bit
154 while(numArgs--) { // For each argument...
155 data (*(addr++)); // Read, issue argument
159 ms = *(addr++); // Read post-command delay time (ms)
161 ms = 500; // If 255, delay for 500 ms
169 ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
170 uint8_t colstart, rowstart;
171 colstart = rowstart = 0;
173 write (ST7735_CASET); // Column addr set
176 m_spiBuffer[0] = 0x00;
177 m_spiBuffer[1] = x0 + colstart; // XSTART
178 m_spiBuffer[2] = 0x00;
179 m_spiBuffer[3] = x1 + colstart; // XEND
180 maa_spi_write_buf(m_spi, m_spiBuffer, 4);
182 write (ST7735_RASET); // Row addr set
185 m_spiBuffer[0] = 0x00;
186 m_spiBuffer[1] = y0 + rowstart; // YSTART
187 m_spiBuffer[2] = 0x00;
188 m_spiBuffer[3] = y1 + rowstart; // YEND
189 maa_spi_write_buf(m_spi, m_spiBuffer, 4);
191 write (ST7735_RAMWR); // write to RAM
195 ST7735::drawPixel(int16_t x, int16_t y, uint16_t color) {
196 if (MAA_SUCCESS != setPixel (x, y, color)) {
207 int fragmentSize = m_height * m_width * 2 / 20;
208 for (int fragment = 0; fragment < 20; fragment++) {
209 maa_spi_write_buf(m_spi, &m_map[fragment * fragmentSize], fragmentSize);
214 ST7735::configModule() {
219 maa_gpio_write (m_rSTPinCtx, HIGH);
221 maa_gpio_write (m_rSTPinCtx, LOW);
223 maa_gpio_write (m_rSTPinCtx, HIGH);
226 executeCMDList (Rcmd1);
227 executeCMDList (Rcmd2red);
228 executeCMDList (Rcmd3);
230 write (ST7735_MADCTL);
233 setAddrWindow (0, 0, m_width - 1, m_height - 1);
235 fillScreen (ST7735_BLACK);
241 maa_result_t error = MAA_SUCCESS;
243 error = maa_gpio_write (m_csLCDPinCtx, LOW);
244 if (error != MAA_SUCCESS) {
245 maa_result_print (error);
248 error = maa_gpio_write (m_cSDPinCtx, HIGH);
249 if (error != MAA_SUCCESS) {
250 maa_result_print (error);
257 ST7735::lcdCSOff () {
258 maa_result_t error = MAA_SUCCESS;
260 error = maa_gpio_write (m_csLCDPinCtx, HIGH);
261 if (error != MAA_SUCCESS) {
262 maa_result_print (error);
270 maa_result_t error = MAA_SUCCESS;
272 error = maa_gpio_write (m_cSDPinCtx, LOW);
273 if (error != MAA_SUCCESS) {
274 maa_result_print (error);
277 error = maa_gpio_write (m_csLCDPinCtx, HIGH);
278 if (error != MAA_SUCCESS) {
279 maa_result_print (error);
287 maa_result_t error = MAA_SUCCESS;
289 error = maa_gpio_write (m_cSDPinCtx, HIGH);
290 if (error != MAA_SUCCESS) {
291 maa_result_print (error);
299 maa_result_t error = MAA_SUCCESS;
301 error = maa_gpio_write (m_rSPinCtx, HIGH);
302 if (error != MAA_SUCCESS) {
303 maa_result_print (error);
311 maa_result_t error = MAA_SUCCESS;
313 error = maa_gpio_write (m_rSPinCtx, LOW);
314 if (error != MAA_SUCCESS) {
315 maa_result_print (error);