Close

Step 3: Firmware

A project log for 1.28" Screen With ADC

I want to make a thermometer and a weight scale using some sensors and ESP32 board.

makerfabsMakerfabs 04/09/2024 at 01:170 Comments

Define GC9A01_DRIVER, and define the pixel width and height in portrait orientation for GC9A01

#define GC9A01_DRIVER
#define TFT_WIDTH  240
#define TFT_HEIGHT 240 // GC9A01 240 x 240

Then set the drive pin according to the schematic diagram 

#define TFT_MOSI 13 // In some display driver board, it might be written as "SDA" and so on.
#define TFT_SCLK 14
#define TFT_CS   15  // Chip select control pin
#define TFT_DC   21  // Data Command control pin
#define TFT_RST  11  // Reset pin (could connect to Arduino RESET pin)
#define TFT_BL   45  // LED back-light

In Section 3. Define the fonts that are to be used here. We chose a variety of fonts to prevent them from being used without initializing them 

#define LOAD_GLCD   // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
#define LOAD_FONT2  // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
#define LOAD_FONT4  // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
#define LOAD_FONT6  // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm
#define LOAD_FONT7  // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:-.
#define LOAD_FONT8  // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.
//#define LOAD_FONT8N // Font 8. Alternative to Font 8 above, slightly narrower, so 3 digits fit a 160 pixel TFT
#define LOAD_GFXFF  // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts


// Comment out the #define below to stop the SPIFFS filing system and smooth font code being loaded
// this will save ~20kbytes of FLASH
#define SMOOTH_FONT

The touch panel driver is CST816S, so it need to call the touch function of CST816S. 

void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
{
    uint16_t touchX = 0, touchY = 0;


    bool touched = false; // tft.getTouch( &touchX, &touchY, 600 );


    if (!touch.available())
    {
        data->state = LV_INDEV_STATE_REL;
    }
    else
    {
        data->state = LV_INDEV_STATE_PR;


        /*Set the coordinates*/
        touchX = touch.data.x;
        touchY = touch.data.y;


        data->point.x = touchX;
        data->point.y = touchY;


        USBSerial.print("Data x ");
        USBSerial.println(touchX);


        USBSerial.print("Data y ");
        USBSerial.println(touchY);
    }
}

initial the TFT_BLK, touch and ADC pins 

pinMode(TFT_BLK, OUTPUT);
   digitalWrite(TFT_BLK, 1);
   pinMode(ADC_INPUT_1, INPUT);

   touch.begin();

 Modify the landscape orientation. The default value is 3. You need to change it to 0

tft.begin();        /* TFT init */
tft.setRotation(0); /* Landscape orientation, flipped */

Get the value of ADC, let the value of ui-Arc1/2/3 be changed with the output voltage of Mabee_Slide Potentiometer

int adc = analogRead(ADC_INPUT_1);
   long int adc_percent = adc * 100 / 4096;
   lv_arc_set_value(ui_Arc1, (int)adc_percent);
   lv_arc_set_value(ui_Arc2, (int)adc_percent);
   lv_arc_set_value(ui_Arc3, (int)adc_percent);

The raw code.

Discussions