From 1fc62b17a60b495765673e4f3e718f5a45b80d05 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Wed, 24 Mar 2021 15:29:48 +0000 Subject: [PATCH] Add support for 320x240 display --- demo/base.c | 4 +-- spilcd.h | 8 ++++-- st7735.c | 82 ++++++++++++++++++++++++++++++----------------------- st7735.h | 13 +++++++++ 4 files changed, 67 insertions(+), 40 deletions(-) diff --git a/demo/base.c b/demo/base.c index e94e97a..8a3a1c8 100644 --- a/demo/base.c +++ b/demo/base.c @@ -20,9 +20,9 @@ lcd_t* demo_init() wiringPiSetup(); - lcd_t* lcd = lcd_init(40000000, 1, 15, 7, 8); + lcd_t* lcd = lcd_init(30000000, 1, 15, 7, 8); - lcd_setOrientation(lcd, 2); + lcd_setOrientation(lcd, 4); return lcd; } diff --git a/spilcd.h b/spilcd.h index 3f15509..918312e 100644 --- a/spilcd.h +++ b/spilcd.h @@ -2,13 +2,17 @@ #define uint8 unsigned char #endif +#ifndef uint16 +#define uint16 unsigned short +#endif + #ifndef __SPILCD_H__ #define __SPILCD_H__ typedef struct { int channel; int cs, a0, rs; - uint8 width, height; + uint16 width, height; } lcd_t; /* @@ -49,7 +53,7 @@ void lcd_deinit(lcd_t *lcd); * Return: 0 for success, 1 for error. * */ -uint8 lcd_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2); +uint16 lcd_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2); /* * Push a pixel into the previously set drawing area. diff --git a/st7735.c b/st7735.c index b26f7db..1de1978 100644 --- a/st7735.c +++ b/st7735.c @@ -28,13 +28,13 @@ struct }; /****************************** END EASY PORT END *****************************/ -static uint8 screen_buffer[128 * 160 * 3]; // TODO: make dynamic -static uint8 screen_window_x1; -static uint8 screen_window_x2; -static uint8 screen_window_y1; -static uint8 screen_window_y2; -static uint8 screen_cursor_x; -static uint8 screen_cursor_y; +static uint8 screen_buffer[SCREEN_HEIGHT * SCREEN_WIDTH * 3]; // TODO: make dynamic +static uint16 screen_window_x1; +static uint16 screen_window_x2; +static uint16 screen_window_y1; +static uint16 screen_window_y2; +static uint16 screen_cursor_x; +static uint16 screen_cursor_y; void advance_screen_cursor() { @@ -74,12 +74,12 @@ static inline void *safeMalloc(size_t size) } /* safeMalloc */ -uint8 lcdhw_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2); +uint16 lcdhw_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2); void lcdhw_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b); void lcdhw_pushPixels(lcd_t* lcd, uint8* pixels, size_t count); -void lcd_setOrientation(lcd_t* lcd, uint8 orientation); -void lcd_setGamma(lcd_t* lcd, uint8 state); +void lcd_setOrientation(lcd_t* lcd, uint16 orientation); +void lcd_setGamma(lcd_t* lcd, uint16 state); void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b); void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count); @@ -174,43 +174,53 @@ void lcd_deinit(lcd_t *display) free(display); } /* lcdst_uninit */ -void lcd_setOrientation(lcd_t* lcd, uint8 orientation) +void lcd_setOrientation(lcd_t* lcd, uint16 orientation) { writeCommand(0x36); /* Memory Data Access Control */ + int sw = SCREEN_WIDTH; + int sh = SCREEN_HEIGHT; + + uint8 my = 1 << 7; // row address order bit + uint8 mx = 1 << 6; // column address order bit + uint8 mv = 1 << 5; // row/column exchange bit switch(orientation) { case 1: - writeData(0x60); /* MX + MV */ - activeDisplay->width = 160; - activeDisplay->height = 128; - lcdhw_setWindow(lcd, 0, 0, 159, 127); + writeData(mx & mv); + activeDisplay->width = sw; + activeDisplay->height = sh; break; case 2: - writeData(0xC0); /* MY + MX */ - activeDisplay->width = 128; - activeDisplay->height = 160; - lcdhw_setWindow(lcd, 0, 0, 127, 159); + writeData(my & mx); + activeDisplay->width = sh; + activeDisplay->height = sw; break; case 3: - writeData(0xA0); /* MY + MV */ - activeDisplay->width = 160; - activeDisplay->height = 128; - lcdhw_setWindow(lcd, 0, 0, 159, 127); + writeData(my & mv); + activeDisplay->width = sw; + activeDisplay->height = sh; break; + case 4: + writeData(mx); + activeDisplay->width = sh; + activeDisplay->height = sw; + break; + + default: - writeData(0x00); /* None */ - activeDisplay->width = 128; - activeDisplay->height = 160; - lcdhw_setWindow(lcd, 0, 0, 127, 159); + writeData(0); /* None */ + activeDisplay->width = sh; + activeDisplay->height = sw; break; } + lcdhw_setWindow(lcd, 0, 0, activeDisplay->width - 1, activeDisplay->height - 1); } /* lcdst_setOrientation */ -void lcd_setGamma(lcd_t* lcd, uint8 state) +void lcd_setGamma(lcd_t* lcd, uint16 state) { /* The status (0 or 1) of the GS pin can only be empirically tested */ switch(state) @@ -226,13 +236,13 @@ void lcd_setGamma(lcd_t* lcd, uint8 state) writeData(state); } /* lcdst_setGamma */ -void lcd_setInversion(lcd_t* lcd, uint8 state) +void lcd_setInversion(lcd_t* lcd, uint16 state) { /* Display inversion ON/OFF */ writeCommand(state ? 0x21 : 0x20); } /* lcdst_setInversion */ -uint8 lcdhw_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2) +uint16 lcdhw_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2) { /* Accept: 0 <= x1 <= x2 < activeDisplay->width */ if(x2 < x1) return 1; @@ -244,13 +254,13 @@ uint8 lcdhw_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2) /* Set column address */ writeCommand(0x2A); - writeData(0); writeData(x1); - writeData(0); writeData(x2); + writeData(x1 >> 8); writeData(x1 & 0xFF); + writeData(x2 >> 8); writeData(x2 & 0XFF); /* Set row address */ writeCommand(0x2B); - writeData(0); writeData(y1); - writeData(0); writeData(y2); + writeData(y1 >> 8); writeData(y1 & 0xFF); + writeData(y2 >> 8); writeData(y2 & 0xFF); /* Activate RAW write */ writeCommand(0x2C); @@ -282,7 +292,7 @@ void lcdhw_pushPixels(lcd_t* lcd, uint8* pixels, size_t count) gpio.spiDataRW(activeDisplay->channel, pixels, count * 3); } -uint8 line_buffer[160*3]; // lcd->width or lcd->height +uint8 line_buffer[SCREEN_WIDTH*3]; // lcd->width or lcd->height void lcd_redrawBuffer(lcd_t* lcd) { @@ -293,7 +303,7 @@ void lcd_redrawBuffer(lcd_t* lcd) } } -uint8 lcd_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2) +uint16 lcd_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2) { screen_window_x1 = x1; screen_window_x2 = x2; diff --git a/st7735.h b/st7735.h index cd83b45..7c99a3f 100644 --- a/st7735.h +++ b/st7735.h @@ -1,4 +1,17 @@ #include "spilcd.h" +#define SCREEN_320x240 +//#define SCREEN_160x128 + +#ifdef SCREEN_320x240 +#define SCREEN_WIDTH 320 +#define SCREEN_HEIGHT 240 +#endif + +#ifdef SCREEN_160x128 +#define SCREEN_WIDTH 160 +#define SCREEN_HEIGHT 128 +#endif + // TODO: optional HW-specific functions