Adsense HTML/JavaScript

Wednesday, February 16, 2022

ESP32-C3/arduino-esp32 + 0.96" 80x160 IPS, create custom class extends Adafruit ST7735 library.

 This exercise run on ESP32-C3-DevKitM-1 in arduino-esp32 2.0.2, display on unknown brand 0.96" 80x160 IPS. 

Library used:
- Adafruit ST7735 and ST7789 Library
- Adafruit GFX Library


Connect:

	TFT_ST7735 ESP32-C3
	-------------------
	VCC        3V3
	GND        GND
	CS         10
	RESET      9
	A0(DC)     8
	SDA        6
	SCK        4
	LED        3V3
	
In my test, if Adafruit_ST7735 is used directly (as in another exercise "ESP32-C3/arduino-esp32 to display on ST7735 and ST7789 SPI LCDs") with option INITR_MINI160x80, the drawing area is shifted and REG and BLUE is swapped.


In this exercise, I create a custom class (MyST7735) extend Adafruit_ST7735:
- call setColRowStart(26, 1) in init() to correct the shifting.
- override setRotation() function, to use ST7735_MADCTL_BGR instead of ST77xx_MADCTL_RGB.

Exercise code:

ESP32C3_ST7735_MINI160x80.ino
/*
 * arduino-esp32 exercise run on ESP32-C3-DevKitM-1,
 * display on 0.96" 80x160 TFT with SPI ST7735,
 * using 'Adafruit ST7735 and ST7789 Library'.
 * 
 * Base on Adafruit ST7735 and ST7789 Library 1.9.1
 * 
 * Fix offset and color with
 * custom class (MyST7735) extending Adafruit_ST7735,
 * to call protected function setColRowStart(),
 * and override setRotation().
 * 
 * ref:
 * Adafruit-ST7735-Library:
 * https://github.com/adafruit/Adafruit-ST7735-Library
 * Adafruit-GFX-Library:
 * https://github.com/adafruit/Adafruit-GFX-Library
 *
 */

#include <Adafruit_GFX.h>    // Core graphics library

//#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include "MyST7735.h"

#define TFT_CS_ST7735   10
#define TFT_RST_ST7735  9 // Or set to -1 and connect to Arduino RESET pin
#define TFT_CS_ST7789   2
#define TFT_RST_ST7789  3   
#define TFT_DC          8

/*
 * Erik:
 * on ESP32-C3-DevKitM-1
 * SS:   7  - not used
 * MOSI: 6
 * MISO: 5  - not used
 * SCK:  4
 * ===================
 * Conection:
 * 
 * TFT_ST7735 ESP32-C3
 * -------------------
 * VCC        3V3
 * GND        GND
 * CS         10
 * RESET      9
 * A0(DC)     8
 * SDA        6
 * SCK        4
 * LED        3V3
 *
 */

//Adafruit_ST7735 tft_ST7735 = Adafruit_ST7735(TFT_CS_ST7735, TFT_DC, TFT_RST_ST7735);
MyST7735 tft_ST7735 = MyST7735(TFT_CS_ST7735, TFT_DC, TFT_RST_ST7735);

void setup(void) {
  delay(500);
  Serial.begin(115200);
  delay(500);
  Serial.print(F("Hello!\n"));
  Serial.print(F("0.96 80X160 (RGB) IPS Test\n\n"));

  //tft_ST7735.initR(INITR_MINI160x80); // Init ST7735S mini display
  tft_ST7735.init();
  
  tft_ST7735.invertDisplay(true);
  tft_ST7735.setRotation(3);
  
  // SPI speed defaults to SPI_DEFAULT_FREQ defined in the library, you can override it here
  // Note that speed allowable depends on chip and quality of wiring, if you go too fast,
  // you may end up with a black screen some times, or all the time.
  //tft.setSPISpeed(40000000);

  Serial.println(F("Initialized"));


  // large block of text
  tft_ST7735.fillScreen(ST77XX_BLACK);
  tft_ST7735.setTextWrap(true);
  tft_ST7735.setTextColor(ST77XX_WHITE);

  drawBorderline();

  delay(1000);
  
  tft_ST7735.setCursor(0, 0);
  tft_ST7735.print("Hello ESP32C3");

  tft_ST7735.setCursor(0, 20);
  tft_ST7735.print("Chip Model: " + String(ESP.getChipModel()));
  tft_ST7735.setCursor(0, 30);
  tft_ST7735.print("rotation: " + String(tft_ST7735.getRotation()));
  tft_ST7735.setCursor(0, 40);
  tft_ST7735.print(String(tft_ST7735.width()) + " x " + String(tft_ST7735.height()));

  delay(1000);
  colorTest();

  delay(1000);
  for(int offset=0; offset<tft_ST7735.height()/2-10; offset++){
    int col;
    if(offset%4 == 0)
      col = ST77XX_WHITE;
    else
      col = ST77XX_BLACK;
      
    tft_ST7735.drawRect(offset, offset, 
                 tft_ST7735.width()-1-2*offset, tft_ST7735.height()-1-2*offset,
                 col);
    delay(100);
  }

  delay(2000);

  tft_ST7735.setRotation(0);

  tft_ST7735.fillScreen(ST77XX_BLACK);
  tft_ST7735.setTextWrap(true);
  tft_ST7735.setTextColor(ST77XX_WHITE);

  tft_ST7735.drawRect(0, 0, 
                 tft_ST7735.width()-1, tft_ST7735.height()-1,
                 ST77XX_WHITE);

  delay(1000);
  colorTest();

  drawBorderline();

  Serial.println("\n\n- setup() end -\n");
  delay(1000);
}

void drawBorderline(){
  tft_ST7735.fillScreen(ST77XX_BLACK);
  tft_ST7735.drawRect(0, 0, 
                 tft_ST7735.width()-1, tft_ST7735.height()-1,
                 ST77XX_WHITE);
  Serial.println();
  Serial.printf("\nrotation %d", tft_ST7735.getRotation());
  Serial.printf("\nwidth %d", tft_ST7735.width());
  Serial.printf("\nheight %d", tft_ST7735.height());
}

void colorTest(){
  tft_ST7735.fillScreen(ST77XX_RED);
  tft_ST7735.setCursor(50, 50);
  tft_ST7735.print("RED");
  delay(1000);
  tft_ST7735.fillScreen(ST77XX_GREEN);
  tft_ST7735.setCursor(50, 50);
  tft_ST7735.print("GREEN");
  delay(1000);
  tft_ST7735.fillScreen(ST77XX_BLUE);
  tft_ST7735.setCursor(50, 50);
  tft_ST7735.print("BLUE");
  delay(1000);
}

/*
 * Cnvert R, G, B (in uint8_t)
 * to color (in uint16_t) in 565 format for Adafruit GFX library
 * rrrrrggg gggbbbbb
 */
uint16_t convertRGBtoColor(uint8_t r, uint8_t g, uint8_t b){
  return (((r & 0xF8) << 8) |
          ((g & 0xFC) << 3) |
          (b >> 3));
}

void loop() {
  
  tft_ST7735.setRotation(1);  
  for (int x = 0; x < tft_ST7735.width(); x++){
    int c =255 *  x/tft_ST7735.width();
    uint16_t color = convertRGBtoColor(c, c, c);
    tft_ST7735.drawLine(x, 0, x, tft_ST7735.height()-1, color);
  }
  delay(1000);
  
  tft_ST7735.setRotation(2);
  for (int x = 0; x < tft_ST7735.width(); x++){
    int r =255 *  x/tft_ST7735.width();
    uint16_t color = convertRGBtoColor(r, 0, 0);
    tft_ST7735.drawLine(x, 0, x, tft_ST7735.height()-1, color);
  }
  delay(1000);
  
  tft_ST7735.setRotation(3);
  for (int x = 0; x < tft_ST7735.width(); x++){
    int g =255 *  x/tft_ST7735.width();
    uint16_t color = convertRGBtoColor(0, g, 0);
    tft_ST7735.drawLine(x, 0, x, tft_ST7735.height()-1, color);
  }
  delay(1000);
  
  tft_ST7735.setRotation(4);
  for (int x = 0; x < tft_ST7735.width(); x++){
    int b =255 *  x/tft_ST7735.width();
    uint16_t color = convertRGBtoColor(0, 0, b);
    tft_ST7735.drawLine(x, 0, x, tft_ST7735.height()-1, color);
  }
  delay(1000);
}


MyST7735.cpp
#include "MyST7735.h"
#include "Adafruit_ST7735.h"
#include "Adafruit_ST77xx.h"

MyST7735::MyST7735(int8_t cs, int8_t dc, int8_t rst)
    : Adafruit_ST7735(cs, dc, rst) {}

/*************************************************************************
    init() to fix offset
    by calling protected function setColRowStart().
*************************************************************************/

void MyST7735::init(void) {
  initR(INITR_MINI160x80);
  setColRowStart(26, 1);
}

/*************************************************************************
    override setRotation() to fix color order using ST7735_MADCTL_BGR.
    To make it simple, option INITR_MINI160x80 is assumed and handled only.
*************************************************************************/
void MyST7735::setRotation(uint8_t m) {
  uint8_t madctl = 0;

  rotation = m & 3; // can't be higher than 3

  switch (rotation) {
  case 0:

    madctl = ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST7735_MADCTL_BGR;
    _height = ST7735_TFTHEIGHT_160;
    _width = ST7735_TFTWIDTH_80;
    _xstart = _colstart;
    _ystart = _rowstart;
    break;
  case 1:

    madctl = ST77XX_MADCTL_MY | ST77XX_MADCTL_MV | ST7735_MADCTL_BGR;
    _width = ST7735_TFTHEIGHT_160;
    _height = ST7735_TFTWIDTH_80;
    _ystart = _colstart;
    _xstart = _rowstart;
    break;
  case 2:
    madctl = ST7735_MADCTL_BGR;
    _height = ST7735_TFTHEIGHT_160;
    _width = ST7735_TFTWIDTH_80;
    _xstart = _colstart;
    _ystart = _rowstart;
    break;
  case 3:

    madctl = ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST7735_MADCTL_BGR;
    _width = ST7735_TFTHEIGHT_160;
    _height = ST7735_TFTWIDTH_80;
    _ystart = _colstart;
    _xstart = _rowstart;
    break;
  }

  sendCommand(ST77XX_MADCTL, &madctl, 1);
}


MyST7735.h
#ifndef _MyST7735_
#define _MyST7735_

#include "Adafruit_ST77xx.h"
#include "Adafruit_ST7735.h"

#define ST7735_MADCTL_BGR 0x08
#define ST77XX_MADCTL_RGB 0x00

class MyST7735 : public Adafruit_ST7735 {

  public:
  MyST7735(int8_t cs, int8_t dc, int8_t rst);
  void init(void);
  void setRotation(uint8_t m);
  
};

#endif // _MyST7735_


No comments:

Post a Comment