From 195e81a5af69fefc045c07816298e4ce82683318 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Wed, 15 May 2024 19:10:28 +0200 Subject: [PATCH] improvement I will get ice cream tomorrow --- .vscode/settings.json | 5 ++- include/constants.h | 7 ++++ include/st7789_lcd.h | 5 +++ src/main.c | 26 +++++++++++++ src/st7789_lcd.c | 85 +++++++++++++++++++++---------------------- 5 files changed, 83 insertions(+), 45 deletions(-) create mode 100644 include/constants.h create mode 100644 include/st7789_lcd.h create mode 100644 src/main.c diff --git a/.vscode/settings.json b/.vscode/settings.json index 7d8103a..e7fa0f9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,5 +35,8 @@ "raspberry-pi-pico.cmakeAutoConfigure": true, "raspberry-pi-pico.useCmakeTools": false, "raspberry-pi-pico.cmakePath": "${HOME}/.pico-sdk/cmake/v3.28.0-rc6/bin/cmake", - "raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.11.1/ninja" + "raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.11.1/ninja", + "files.associations": { + "gpio.h": "c" + } } diff --git a/include/constants.h b/include/constants.h new file mode 100644 index 0000000..825a657 --- /dev/null +++ b/include/constants.h @@ -0,0 +1,7 @@ +#ifndef CONSTANTSH +#define CONSTANTSH + +#define SCREEN_WIDTH 240 +#define SCREEN_HEIGHT 360 + +#endif \ No newline at end of file diff --git a/include/st7789_lcd.h b/include/st7789_lcd.h new file mode 100644 index 0000000..3282e67 --- /dev/null +++ b/include/st7789_lcd.h @@ -0,0 +1,5 @@ +void lcd_start_pixels(); +void lcd_end_pixels(); +void lcd_put_rgb(uint8_t r, uint8_t g, uint8_t b); + +void lcd_initialize(); \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..b6ed63f --- /dev/null +++ b/src/main.c @@ -0,0 +1,26 @@ +#include "hardware/gpio.h" + +#include "constants.h" +#include "st7789_lcd.h" + + + +int main() { + gpio_init(PICO_DEFAULT_LED_PIN); + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + + lcd_initialize(); + + lcd_start_pixels(); + for (int y = 0; y < SCREEN_HEIGHT; ++y) { + for (int x = 0; x < SCREEN_WIDTH; ++x) { + //uint16_t colour = 31; // red + //st7789_lcd_put(pio, sm, colour >> 8); + // st7789_lcd_put(pio, sm, colour & 0xff); + lcd_put_rgb(0, 0, 255); + } + } + lcd_end_pixels(); + + gpio_put(PICO_DEFAULT_LED_PIN, 1); +} \ No newline at end of file diff --git a/src/st7789_lcd.c b/src/st7789_lcd.c index c113dd7..b1fcefb 100644 --- a/src/st7789_lcd.c +++ b/src/st7789_lcd.c @@ -12,14 +12,9 @@ #include "hardware/gpio.h" #include "hardware/interp.h" +#include "constants.h" #include "st7789_lcd.pio.h" -// Tested with the parts that have the height of 240 and 320 -#define SCREEN_WIDTH 240 -#define SCREEN_HEIGHT 320 -#define IMAGE_SIZE 256 -#define LOG_IMAGE_SIZE 8 - #define PIN_DIN 0 #define PIN_CLK 1 #define PIN_CS 2 @@ -29,6 +24,9 @@ #define SERIAL_CLK_DIV 1.f +#define pio pio0 +#define sm 0 + // Format: cmd length (including cmd byte), post delay in units of 5 ms, then cmd payload // Note the delays have been shortened a little static const uint8_t st7789_init_seq[] = { @@ -38,7 +36,6 @@ static const uint8_t st7789_init_seq[] = { 2, 0, 0x36, 0x00, // Set MADCTL: row then column, refresh is bottom to top ???? 5, 0, 0x2a, 0x00, 0x00, SCREEN_WIDTH >> 8, SCREEN_WIDTH & 0xff, // CASET: column addresses 5, 0, 0x2b, 0x00, 0x00, SCREEN_HEIGHT >> 8, SCREEN_HEIGHT & 0xff, // RASET: row addresses - // 1, 2, 0x21, // Inversion on, then 10 ms delay (supposedly a hack?) 1, 2, 0x13, // Normal display on, then 10 ms delay 1, 2, 0x29, // Main screen turn on, then wait 500 ms 0 // Terminate list @@ -50,7 +47,7 @@ static inline void lcd_set_dc_cs(bool dc, bool cs) { sleep_us(1); } -static inline void lcd_write_cmd(PIO pio, uint sm, const uint8_t *cmd, size_t count) { +static inline void lcd_write_cmd(const uint8_t *cmd, size_t count) { st7789_lcd_wait_idle(pio, sm); lcd_set_dc_cs(0, 0); st7789_lcd_put(pio, sm, *cmd++); @@ -64,56 +61,56 @@ static inline void lcd_write_cmd(PIO pio, uint sm, const uint8_t *cmd, size_t co lcd_set_dc_cs(1, 1); } -static inline void lcd_init(PIO pio, uint sm, const uint8_t *init_seq) { - const uint8_t *cmd = init_seq; +static inline void lcd_init() { + const uint8_t *cmd = st7789_init_seq; while (*cmd) { - lcd_write_cmd(pio, sm, cmd + 2, *cmd); + lcd_write_cmd(cmd + 2, *cmd); sleep_ms(*(cmd + 1) * 5); cmd += *cmd + 2; } } -static inline void st7789_start_pixels(PIO pio, uint sm) { +static inline void initialize_pins() { + gpio_init(PIN_CS); + gpio_set_dir(PIN_CS, GPIO_OUT); + gpio_put(PIN_CS, 1); + + gpio_init(PIN_DC); + gpio_set_dir(PIN_DC, GPIO_OUT); + + gpio_init(PIN_RESET); + gpio_set_dir(PIN_RESET, GPIO_OUT); + gpio_put(PIN_RESET, 1); + + gpio_init(PIN_BL); + gpio_set_dir(PIN_BL, GPIO_OUT); + gpio_put(PIN_BL, 1); +} + +void lcd_start_pixels() { uint8_t cmd = 0x2c; // RAMWR - lcd_write_cmd(pio, sm, &cmd, 1); + lcd_write_cmd(&cmd, 1); lcd_set_dc_cs(1, 0); } -int main() { +void lcd_end_pixels() { + lcd_set_dc_cs(0, 1); +} + +void lcd_put_rgb(uint8_t r, uint8_t g, uint8_t b) { + uint16_t colour = ((b >> 3 & 0x1f) << 11) | ((g >> 2 & 0x3f) << 5) | (r >> 3 & 0x1f); + + st7789_lcd_put(pio, sm, colour >> 8); + st7789_lcd_put(pio, sm, colour & 0xff); +} + +void lcd_initialize() { stdio_init_all(); - gpio_init(PICO_DEFAULT_LED_PIN); - gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + initialize_pins(); - PIO pio = pio0; - uint sm = 0; // state machine uint offset = pio_add_program(pio, &st7789_lcd_program); st7789_lcd_program_init(pio, sm, offset, PIN_DIN, PIN_CLK, SERIAL_CLK_DIV); - gpio_init(PIN_CS); - gpio_init(PIN_DC); - gpio_init(PIN_RESET); - gpio_init(PIN_BL); - gpio_set_dir(PIN_CS, GPIO_OUT); - gpio_set_dir(PIN_DC, GPIO_OUT); - gpio_set_dir(PIN_RESET, GPIO_OUT); - gpio_set_dir(PIN_BL, GPIO_OUT); - - gpio_put(PIN_CS, 1); - gpio_put(PIN_RESET, 1); - lcd_init(pio, sm, st7789_init_seq); - gpio_put(PIN_BL, 1); - - - while (1) { - st7789_start_pixels(pio, sm); - for (int y = 0; y < SCREEN_HEIGHT; ++y) { - for (int x = 0; x < SCREEN_WIDTH; ++x) { - uint16_t colour = 31; // red - st7789_lcd_put(pio, sm, colour >> 8); - st7789_lcd_put(pio, sm, colour & 0xff); - } - } - gpio_put(PICO_DEFAULT_LED_PIN, 1); - } + lcd_init(); }