diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a4832603..98d3be6c 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -14,6 +14,7 @@ add_executable (oled-1308 oled-1308.cxx) add_executable (oled-1327 oled-1327.cxx) add_executable (proximity max44000.cxx) add_executable (accelerometer mma7455.cxx) +add_executable (lcd st7735.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -26,6 +27,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/servo) include_directories (${PROJECT_SOURCE_DIR}/src/hcsr04) include_directories (${PROJECT_SOURCE_DIR}/src/max44000) include_directories (${PROJECT_SOURCE_DIR}/src/mma7455) +include_directories (${PROJECT_SOURCE_DIR}/src/st7735) target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT}) @@ -43,3 +45,4 @@ target_link_libraries (oled-1308 i2clcd ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (oled-1327 i2clcd ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (proximity max44000 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (accelerometer mma7455 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (lcd st7735 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/st7735.cxx b/examples/st7735.cxx new file mode 100644 index 00000000..13d262ee --- /dev/null +++ b/examples/st7735.cxx @@ -0,0 +1,64 @@ +/* + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include "st7735.h" +#include + +int +main(int argc, char **argv) +{ +//! [Interesting] + upm::ST7735 * lcd = new upm::ST7735(7, 4, 9, 8); + lcd->fillScreen (ST7735_RED); + lcd->refresh (); + + lcd->fillScreen (ST7735_CYAN); + lcd->refresh (); + + lcd->fillScreen (ST7735_BLACK); + lcd->refresh (); + + lcd->drawLine(10, 10, 10, 100, ST7735_MAGENTA); + lcd->drawLine(20, 20, 10, 100, ST7735_YELLOW); + lcd->drawLine(30, 30, 50, 100, ST7735_WHITE); + lcd->refresh (); + + lcd->drawPixel (20, 20, ST7735_GREEN); + lcd->refresh (); + + lcd->drawTriangle (50, 50, 80, 80, 60, 90, ST7735_GREEN); + lcd->refresh (); + + lcd->drawCircle (100, 110, 10, ST7735_BLUE); + lcd->refresh (); + + std::cout << "exiting application" << std::endl; + + delete lcd; +//! [Interesting] + return 0; +} diff --git a/src/st7735/CMakeLists.txt b/src/st7735/CMakeLists.txt new file mode 100644 index 00000000..f3b3a373 --- /dev/null +++ b/src/st7735/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "st7735") +set (libdescription "libupm SPI LCD") +set (module_src gfx.cxx st7735.cxx) +set (module_h gfx.h st7735.h) +upm_module_init() diff --git a/src/st7735/gfx.cxx b/src/st7735/gfx.cxx new file mode 100644 index 00000000..1a17bef9 --- /dev/null +++ b/src/st7735/gfx.cxx @@ -0,0 +1,152 @@ +/* + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include + +#include "gfx.h" + +using namespace upm; + +GFX::GFX (int width, int height) { + m_height = height; + m_width = width; +} + +GFX::~GFX () { +} + +maa_result_t +GFX::setPixel (int x, int y, uint16_t color) { + if((x < 0) ||(x >= m_width) || (y < 0) || (y >= m_height)) { + return MAA_ERROR_UNSPECIFIED; + } + + int index = ((y * m_width) + x) * sizeof(uint16_t); + m_map[index] = (uint8_t) (color >> 8); + m_map[++index] = (uint8_t)(color); + + return MAA_SUCCESS; +} + +void +GFX::fillScreen (uint16_t color) { + fillRect(0, 0, m_width, m_height, color); +} + +void +GFX::fillRect (int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) { + for (int16_t i=x; i abs(x1 - x0); + + if (steep) { + swap(x0, y0); + swap(x1, y1); + } + + if (x0 > x1) { + swap(x0, x1); + swap(y0, y1); + } + + int16_t dx, dy; + dx = x1 - x0; + dy = abs (y1 - y0); + + int16_t err = dx / 2; + int16_t ystep; + + if (y0 < y1) { + ystep = 1; + } else { + ystep = -1; + } + + for (; x0 <= x1; x0++) { + if (steep) { + setPixel(y0, x0, color); + } else { + setPixel(x0, y0, color); + } + err -= dy; + if (err < 0) { + y0 += ystep; + err += dx; + } + } +} + +void +GFX::drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) { + drawLine(x0, y0, x1, y1, color); + drawLine(x1, y1, x2, y2, color); + drawLine(x2, y2, x0, y0, color); +} + +void +GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) { + int16_t f = 1 - r; + int16_t ddF_x = 1; + int16_t ddF_y = -2 * r; + int16_t x = 0; + int16_t y = r; + + setPixel(x0 , y0+r, color); + setPixel(x0 , y0-r, color); + setPixel(x0+r, y0 , color); + setPixel(x0-r, y0 , color); + + while (x= 0) { + y--; + ddF_y += 2; + f += ddF_y; + } + x++; + + ddF_x += 2; + f += ddF_x; + + setPixel(x0 + x, y0 + y, color); + setPixel(x0 - x, y0 + y, color); + setPixel(x0 + x, y0 - y, color); + setPixel(x0 - x, y0 - y, color); + setPixel(x0 + y, y0 + x, color); + setPixel(x0 - y, y0 + x, color); + setPixel(x0 + y, y0 - x, color); + setPixel(x0 - y, y0 - x, color); + } +} diff --git a/src/st7735/gfx.h b/src/st7735/gfx.h new file mode 100644 index 00000000..8e6bc9f1 --- /dev/null +++ b/src/st7735/gfx.h @@ -0,0 +1,116 @@ +/* + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#pragma once + +#include +#include +#include +#include +#include + +#define swap(a, b) { int16_t t = a; a = b; b = t; } + +namespace upm { + +/** + * @brief GFX helper class + * + * This file used by the screen. + */ +class GFX { + public: + /** + * Instanciates a GFX object + * + * @param width + * @param height + */ + GFX (int width, int height); + + /** + * GFX object destructor + */ + ~GFX (); + + /** + * Set the window address + * + * @param x0 first coordinate + * @param y0 first coordinate + * @param x1 second coordinate + * @param y1 second coordinate + */ + virtual void setAddrWindow (uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) = 0; + + /** + * + */ + virtual void drawPixel (int16_t x, int16_t y, uint16_t color) = 0; + + /** + * + */ + virtual void refresh () = 0; + + /** + * + */ + maa_result_t setPixel (int x, int y, uint16_t color); + + /** + * + */ + void fillScreen (uint16_t color); + + /** + * + */ + void fillRect (int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); + + /** + * + */ + void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); + + /** + * + */ + void drawLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color); + + /** + * + */ + void drawTriangle (int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); + + /** + * + */ + void drawCircle (int16_t x0, int16_t y0, int16_t r, uint16_t color); + + int m_height; + int m_width; + + uint8_t m_map[160 * 128 * 2]; + }; +} diff --git a/src/st7735/jsupm_st7735.i b/src/st7735/jsupm_st7735.i new file mode 100644 index 00000000..047d9d44 --- /dev/null +++ b/src/st7735/jsupm_st7735.i @@ -0,0 +1,7 @@ +%module jsupm_st7735 + +%{ + #include "st7735.h" +%} + +%include "st7735.h" diff --git a/src/st7735/pyupm_st7735.i b/src/st7735/pyupm_st7735.i new file mode 100644 index 00000000..6190b7b2 --- /dev/null +++ b/src/st7735/pyupm_st7735.i @@ -0,0 +1,8 @@ +%module pyupm_st7735 + +%feature("autodoc", "3"); + +%include "st7735.h" +%{ + #include "st7735.h" +%} diff --git a/src/st7735/st7735.cxx b/src/st7735/st7735.cxx new file mode 100644 index 00000000..4b67d0c7 --- /dev/null +++ b/src/st7735/st7735.cxx @@ -0,0 +1,355 @@ +/* + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "st7735.h" + +using namespace upm; + +ST7735::ST7735 (uint8_t csLCD, uint8_t cSD, uint8_t rs, uint8_t rst) : GFX (160, 128) { + maa_init(); + + m_csLCD = csLCD; + m_cSD = cSD; + m_rST = rst; + m_rS = rs; + + initModule (); + configModule (); +} + +ST7735::~ST7735 () { + maa_result_t error = MAA_SUCCESS; + error = maa_spi_stop(m_spi); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } + error = maa_gpio_close (m_csLCDPinCtx); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } + error = maa_gpio_close (m_cSDPinCtx); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } + error = maa_gpio_close (m_rSTPinCtx); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } + error = maa_gpio_close (m_rSPinCtx); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } +} + +void +ST7735::initModule () { + maa_result_t error = MAA_SUCCESS; + + m_height = 160; + m_width = 128; + + m_csLCDPinCtx = maa_gpio_init (m_csLCD); + if (m_csLCDPinCtx == NULL) { + fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", m_csLCD); + exit (1); + } + + m_cSDPinCtx = maa_gpio_init (m_cSD); + if (m_cSDPinCtx == NULL) { + fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", m_cSD); + exit (1); + } + + m_rSTPinCtx = maa_gpio_init (m_rST); + if (m_rSTPinCtx == NULL) { + fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", m_rST); + exit (1); + } + + m_rSPinCtx = maa_gpio_init (m_rS); + if (m_rSPinCtx == NULL) { + fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", m_rS); + exit (1); + } + + error = maa_gpio_dir (m_csLCDPinCtx, MAA_GPIO_OUT); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + error = maa_gpio_dir (m_cSDPinCtx, MAA_GPIO_OUT); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + error = maa_gpio_dir (m_rSTPinCtx, MAA_GPIO_OUT); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + error = maa_gpio_dir (m_rSPinCtx, MAA_GPIO_OUT); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + m_spi = maa_spi_init (0); + error = maa_spi_frequency(m_spi, 15 * 1000000); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + lcdCSOn (); +} + +void +ST7735::write (uint8_t value) { + rsLOW (); + maa_spi_write (m_spi, value); +} + +void +ST7735::data (uint8_t value) { + rsHIGH (); + maa_spi_write (m_spi, value); +} + +void +ST7735::executeCMDList(const uint8_t *addr) { + uint8_t numCommands, numArgs; + uint16_t ms; + + numCommands = *(addr++); // Number of commands to follow + while(numCommands--) { // For each command... + write (*(addr++)); // Read, issue command + numArgs = *(addr++); // Number of args to follow + ms = numArgs & DELAY; // If hibit set, delay follows args + numArgs &= ~DELAY; // Mask out delay bit + while(numArgs--) { // For each argument... + data (*(addr++)); // Read, issue argument + } + + if(ms) { + ms = *(addr++); // Read post-command delay time (ms) + if (ms == 255) { + ms = 500; // If 255, delay for 500 ms + } + usleep (ms * 1000); + } + } +} + +void +ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) { + uint8_t colstart, rowstart; + colstart = rowstart = 0; + + write (ST7735_CASET); // Column addr set + + rsHIGH (); + m_spiBuffer[0] = 0x00; + m_spiBuffer[1] = x0 + colstart; // XSTART + m_spiBuffer[2] = 0x00; + m_spiBuffer[3] = x1 + colstart; // XEND + maa_spi_write_buf(m_spi, m_spiBuffer, 4); + + write (ST7735_RASET); // Row addr set + + rsHIGH (); + m_spiBuffer[0] = 0x00; + m_spiBuffer[1] = y0 + rowstart; // YSTART + m_spiBuffer[2] = 0x00; + m_spiBuffer[3] = y1 + rowstart; // YEND + maa_spi_write_buf(m_spi, m_spiBuffer, 4); + + write (ST7735_RAMWR); // write to RAM +} + +void +ST7735::drawPixel(int16_t x, int16_t y, uint16_t color) { + if (MAA_SUCCESS != setPixel (x, y, color)) { + return; + } + + refresh (); +} + +void +ST7735::refresh () { + rsHIGH (); + + int fragmentSize = m_height * m_width * 2 / 20; + for (int fragment = 0; fragment < 20; fragment++) { + maa_spi_write_buf(m_spi, &m_map[fragment * fragmentSize], fragmentSize); + } +} + +void +ST7735::configModule() { + rsHIGH (); + lcdCSOff (); + lcdCSOn (); + + maa_gpio_write (m_rSTPinCtx, HIGH); + usleep (500000); + maa_gpio_write (m_rSTPinCtx, LOW); + usleep (500000); + maa_gpio_write (m_rSTPinCtx, HIGH); + usleep (500000); + + executeCMDList (Rcmd1); + executeCMDList (Rcmd2red); + executeCMDList (Rcmd3); + + write (ST7735_MADCTL); + data (0xC0); + + setAddrWindow (0, 0, m_width - 1, m_height - 1); + + fillScreen (ST7735_BLACK); + refresh (); +} + +void +ST7735::fill (int col, uint16_t* buff, int len) { + uint8_t colstart, rowstart; + uint8_t buf[len * 2]; + + colstart = 0; + + write (ST7735_CASET); // Column addr set + + rsHIGH (); + m_spiBuffer[0] = 0x00; + m_spiBuffer[1] = col + 1; // XSTART + m_spiBuffer[2] = 0x00; + m_spiBuffer[3] = col + 1; // XEND + maa_spi_write_buf(m_spi, m_spiBuffer, 4); + + write (ST7735_RASET); // Row addr set + + rsHIGH (); + m_spiBuffer[0] = 0x00; + m_spiBuffer[1] = 0x01; // YSTART + m_spiBuffer[2] = 0x00; + m_spiBuffer[3] = 0x9F; // YEND + maa_spi_write_buf(m_spi, m_spiBuffer, 4); + + write (ST7735_RAMWR); // write to RAM + + /*for (int idx = 0; idx < len; idx++) { + buf[idx*2] = buff[idx] >> 8; + buf[(idx*2)+1] = buff[idx]; + }*/ + + rsHIGH (); + maa_spi_write_buf(m_spi, (uint8_t *)buff, len * 2); +} + +maa_result_t +ST7735::lcdCSOn () { + maa_result_t error = MAA_SUCCESS; + + error = maa_gpio_write (m_csLCDPinCtx, LOW); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + error = maa_gpio_write (m_cSDPinCtx, HIGH); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + return error; +} + +maa_result_t +ST7735::lcdCSOff () { + maa_result_t error = MAA_SUCCESS; + + error = maa_gpio_write (m_csLCDPinCtx, HIGH); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + return error; +} + +maa_result_t +ST7735::sdCSOn () { + maa_result_t error = MAA_SUCCESS; + + error = maa_gpio_write (m_cSDPinCtx, LOW); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + error = maa_gpio_write (m_csLCDPinCtx, HIGH); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + return error; +} + +maa_result_t +ST7735::sdCSOff () { + maa_result_t error = MAA_SUCCESS; + + error = maa_gpio_write (m_cSDPinCtx, HIGH); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + return error; +} + +maa_result_t +ST7735::rsHIGH () { + maa_result_t error = MAA_SUCCESS; + + error = maa_gpio_write (m_rSPinCtx, HIGH); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + return error; +} + +maa_result_t +ST7735::rsLOW () { + maa_result_t error = MAA_SUCCESS; + + error = maa_gpio_write (m_rSPinCtx, LOW); + if (error != MAA_SUCCESS) { + maa_result_print (error); + } + + return error; +} diff --git a/src/st7735/st7735.h b/src/st7735/st7735.h new file mode 100644 index 00000000..947258fd --- /dev/null +++ b/src/st7735/st7735.h @@ -0,0 +1,359 @@ +/* + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. + * + * Credits to Adafrut.com + * {Based on,Inspired from} original from . See original license in . + + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#pragma once + +#include +#include +#include +#include +#include + +#define INITR_GREENTAB 0x0 +#define INITR_REDTAB 0x1 +#define INITR_BLACKTAB 0x2 + +#define ST7735_TFTWIDTH 128 +#define ST7735_TFTHEIGHT 160 + +#define ST7735_NOP 0x00 +#define ST7735_SWRESET 0x01 +#define ST7735_RDDID 0x04 +#define ST7735_RDDST 0x09 + +#define ST7735_SLPIN 0x10 +#define ST7735_SLPOUT 0x11 +#define ST7735_PTLON 0x12 +#define ST7735_NORON 0x13 + +#define ST7735_INVOFF 0x20 +#define ST7735_INVON 0x21 +#define ST7735_DISPOFF 0x28 +#define ST7735_DISPON 0x29 +#define ST7735_CASET 0x2A +#define ST7735_RASET 0x2B +#define ST7735_RAMWR 0x2C +#define ST7735_RAMRD 0x2E + +#define ST7735_PTLAR 0x30 +#define ST7735_COLMOD 0x3A +#define ST7735_MADCTL 0x36 + +#define ST7735_FRMCTR1 0xB1 +#define ST7735_FRMCTR2 0xB2 +#define ST7735_FRMCTR3 0xB3 +#define ST7735_INVCTR 0xB4 +#define ST7735_DISSET5 0xB6 + +#define ST7735_PWCTR1 0xC0 +#define ST7735_PWCTR2 0xC1 +#define ST7735_PWCTR3 0xC2 +#define ST7735_PWCTR4 0xC3 +#define ST7735_PWCTR5 0xC4 +#define ST7735_VMCTR1 0xC5 + +#define ST7735_RDID1 0xDA +#define ST7735_RDID2 0xDB +#define ST7735_RDID3 0xDC +#define ST7735_RDID4 0xDD + +#define ST7735_PWCTR6 0xFC + +#define ST7735_GMCTRP1 0xE0 +#define ST7735_GMCTRN1 0xE1 + +// Color definitions +#define ST7735_BLACK 0x0000 +#define ST7735_BLUE 0x001F +#define ST7735_RED 0xF800 +#define ST7735_GREEN 0x07E0 +#define ST7735_CYAN 0x07FF +#define ST7735_MAGENTA 0xF81F +#define ST7735_YELLOW 0xFFE0 +#define ST7735_WHITE 0xFFFF + +#define HIGH 1 +#define LOW 0 + +#define DELAY 0x80 + +namespace upm { + +static const uint8_t Bcmd[] = { + // Initialization commands for 7735B screens + 18, // 18 commands in list: + ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay + 50, // 50 ms delay + ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay + 255, // 255 = 500 ms delay + ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay: + 0x05, // 16-bit color + 10, // 10 ms delay + ST7735_FRMCTR1, 3+DELAY, // 4: Frame rate control, 3 args + delay: + 0x00, // fastest refresh + 0x06, // 6 lines front porch + 0x03, // 3 lines back porch + 10, // 10 ms delay + ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg: + 0x08, // Row addr/col addr, bottom to top refresh + ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay: + 0x15, // 1 clk cycle nonoverlap, 2 cycle gate + // rise, 3 cycle osc equalize + 0x02, // Fix on VTL + ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg: + 0x0, // Line inversion + ST7735_PWCTR1 , 2+DELAY, // 8: Power control, 2 args + delay: + 0x02, // GVDD = 4.7V + 0x70, // 1.0uA + 10, // 10 ms delay + ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay: + 0x05, // VGH = 14.7V, VGL = -7.35V + ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay: + 0x01, // Opamp current small + 0x02, // Boost frequency + ST7735_VMCTR1 , 2+DELAY, // 11: Power control, 2 args + delay: + 0x3C, // VCOMH = 4V + 0x38, // VCOML = -1.1V + 10, // 10 ms delay + ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay: + 0x11, 0x15, + ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay: + 0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what + 0x21, 0x1B, 0x13, 0x19, // these config values represent) + 0x17, 0x15, 0x1E, 0x2B, + 0x04, 0x05, 0x02, 0x0E, + ST7735_GMCTRN1,16+DELAY, // 14: Sparkles and rainbows, 16 args + delay: + 0x0B, 0x14, 0x08, 0x1E, // (ditto) + 0x22, 0x1D, 0x18, 0x1E, + 0x1B, 0x1A, 0x24, 0x2B, + 0x06, 0x06, 0x02, 0x0F, + 10, // 10 ms delay + ST7735_CASET , 4 , // 15: Column addr set, 4 args, no delay: + 0x00, 0x02, // XSTART = 2 + 0x00, 0x81, // XEND = 129 + ST7735_RASET , 4 , // 16: Row addr set, 4 args, no delay: + 0x00, 0x02, // XSTART = 1 + 0x00, 0x81, // XEND = 160 + ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay + 10, // 10 ms delay + ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay + 255 }, // 255 = 500 ms delay + + Rcmd1[] = { // Init for 7735R, part 1 (red or green tab) + 15, // 15 commands in list: + ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay + 150, // 150 ms delay + ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay + 255, // 500 ms delay + ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args: + 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) + ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args: + 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) + ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args: + 0x01, 0x2C, 0x2D, // Dot inversion mode + 0x01, 0x2C, 0x2D, // Line inversion mode + ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay: + 0x07, // No inversion + ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay: + 0xA2, + 0x02, // -4.6V + 0x84, // AUTO mode + ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay: + 0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD + ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay: + 0x0A, // Opamp current small + 0x00, // Boost frequency + ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay: + 0x8A, // BCLK/2, Opamp current small & Medium low + 0x2A, + ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay: + 0x8A, 0xEE, + ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay: + 0x0E, + ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay + ST7735_MADCTL , 1 , // 14: Memory access control (directions), 1 arg: + 0xC8, // row addr/col addr, bottom to top refresh + ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay: + 0x05 }, // 16-bit color + + Rcmd2green[] = { // Init for 7735R, part 2 (green tab only) + 2, // 2 commands in list: + ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay: + 0x00, 0x02, // XSTART = 0 + 0x00, 0x7F+0x02, // XEND = 127 + ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay: + 0x00, 0x01, // XSTART = 0 + 0x00, 0x9F+0x01 }, // XEND = 159 + Rcmd2red[] = { // Init for 7735R, part 2 (red tab only) + 2, // 2 commands in list: + ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay: + 0x00, 0x00, // XSTART = 0 + 0x00, 0x7F, // XEND = 127 + ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay: + 0x00, 0x00, // XSTART = 0 + 0x00, 0x9F }, // XEND = 159 + + Rcmd3[] = { // Init for 7735R, part 3 (red or green tab) + 4, // 4 commands in list: + ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay: + 0x02, 0x1c, 0x07, 0x12, + 0x37, 0x32, 0x29, 0x2d, + 0x29, 0x25, 0x2B, 0x39, + 0x00, 0x01, 0x03, 0x10, + ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay: + 0x03, 0x1d, 0x07, 0x06, + 0x2E, 0x2C, 0x29, 0x2D, + 0x2E, 0x2E, 0x37, 0x3F, + 0x00, 0x00, 0x02, 0x10, + ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay + 10, // 10 ms delay + ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay + 100 }; // 100 ms delay + +#define swap(a, b) { int16_t t = a; a = b; b = t; } + + + +/** + * @brief C++ API for NRF24l01 transceiver module + * + * This file defines the NRF24l01 C++ interface for libnrf24l01 + * + * @snippet nrf_receiver.cxx Interesting + * @snippet nrf_transmitter.cxx Interesting + */ +class ST7735 : public GFX { + public: + /** + * Instanciates a NRF24l01 object + * + * @param cs chip select pin + */ + ST7735 (uint8_t csLCD, uint8_t cSD, uint8_t rs, uint8_t rst); + + /** + * NRF24l01 object destructor + */ + ~ST7735 (); + + /** + * Return name of the component + */ + std::string name() + { + return m_name; + } + + /** + * + */ + void initModule (); + + /** + * + */ + void configModule (); + + /** + * + */ + void write (uint8_t value); + + /** + * + */ + void data (uint8_t value); + + /** + * + */ + void executeCMDList (const uint8_t *addr); + + /** + * + */ + void setAddrWindow (uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1); + + /** + * + */ + void drawPixel (int16_t x, int16_t y, uint16_t color); + + /** + * + */ + void refresh (); + + /** + * + */ + maa_result_t lcdCSOn (); + + /** + * + */ + maa_result_t lcdCSOff (); + + /** + * + */ + maa_result_t sdCSOn (); + + /** + * + */ + maa_result_t sdCSOff (); + + /** + * + */ + maa_result_t rsHIGH (); + + /** + * + */ + maa_result_t rsLOW (); + + void fill (int col, uint16_t* buff, int len); + private: + maa_spi_context m_spi; + uint8_t m_csLCD; + uint8_t m_cSD; + uint8_t m_rST; + uint8_t m_rS; + + maa_gpio_context m_csLCDPinCtx; + maa_gpio_context m_cSDPinCtx; + maa_gpio_context m_rSTPinCtx; + maa_gpio_context m_rSPinCtx; + + uint8_t m_spiBuffer[128]; + + std::string m_name; +}; + +}