From e073e6c7500040673de9d03ee3a96a4744df6681 Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Mon, 5 Dec 2016 16:33:58 +0100 Subject: [PATCH] Added orientation and set pixel function --- st7735.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++----- st7735.h | 14 ++++-- 2 files changed, 134 insertions(+), 13 deletions(-) diff --git a/st7735.c b/st7735.c index c9621f2..7537e8e 100644 --- a/st7735.c +++ b/st7735.c @@ -6,9 +6,11 @@ #include "spi.h" #include "st7735initcmds.h" -static unit8_t st7735_row_start = 0; -static unit8_t st7735_column_start = 0; -static uint8_t st7735_height = st7735_height_18; +static uint8_t st7735_row_start = 0; +static uint8_t st7735_column_start = 0; +static uint8_t st7735_width = st7735_default_width; +static uint8_t st7735_height = st7735_default_height_18; +static enum ST7735_ORIENTATION st7735_orientation = ST7735_LANDSCAPE; static inline void st7735_set_rs() { PORTB |= (1 << PB1); @@ -26,7 +28,6 @@ static inline void st7735_unset_rst() { PORTB &= ~(1 << PB0); } - static inline void st7735_write_cmd(enum ST7735_COMMANDS cmd) { st7735_unset_rs(); @@ -35,15 +36,14 @@ static inline void st7735_write_cmd(enum ST7735_COMMANDS cmd) { spi_set_cs(); } -static inline void st7735_write_data(enum ST7735_COMMANDS cmd) { +static inline void st7735_write_data(uint8_t data) { st7735_set_rs(); spi_unset_cs(); - spi_write(cmd); + spi_write(data); spi_set_cs(); } - static inline void st7735_reset() { spi_unset_cs(); st7735_set_rst(); @@ -121,11 +121,124 @@ void st7735_init() { st7735_run_command_list(st7735_red_init1); st7735_run_command_list(st7735_red_init_green1442); st7735_run_command_list(st7735_red_init3); - st7735_height = st7735_height_144; + st7735_height = st7735_default_height_144; st7735_column_start = 2; st7735_row_start = 3; break; } - - +} + + +enum ST7735_MADCTL_ARGS { + MADCTL_MY = 0x80, // Mirror Y + MADCTL_MX = 0x40, // Mirrror x + MADCTL_MV = 0x20, // Swap XY + MADCTL_ML = 0x10, // Scan address order + MADCTL_RGB = 0x00, + MADCTL_BGR = 0x08, + MADCTL_MH = 0x04 // Horizontal scan oder +}; + +void st7735_set_orientation(enum ST7735_ORIENTATION orientation) { + st7735_write_cmd(ST7735_MADCTL); + + switch (orientation) { + case ST7735_LANDSCAPE: + if(st7735_type == ST7735_RED_18_BLACKTAB) { + st7735_write_data(MADCTL_MX | MADCTL_MY | MADCTL_RGB); + } else { + st7735_write_data(MADCTL_MX | MADCTL_MY | MADCTL_BGR); + } + + st7735_width = st7735_default_width; + + if(st7735_type == ST7735_RED144_GREENTAB) { + st7735_height = st7735_default_height_144; + } else { + st7735_height = st7735_default_height_18; + } + break; + + + case ST7735_PORTRAIT: + if(st7735_type == ST7735_RED_18_BLACKTAB) { + st7735_write_data(MADCTL_MY | MADCTL_MV | MADCTL_RGB); + } else { + st7735_write_data(MADCTL_MY | MADCTL_MV | MADCTL_BGR); + } + + if(st7735_type == ST7735_RED144_GREENTAB) { + st7735_width = st7735_default_height_144; + } + else { + st7735_width = st7735_default_height_18; + } + + st7735_height = st7735_default_width; + break; + + case ST7735_LANDSCAPE_INV: + if (st7735_type == ST7735_RED_18_BLACKTAB) { + st7735_write_data(MADCTL_RGB); + } else { + st7735_write_data(MADCTL_BGR); + } + + st7735_width = st7735_default_width; + + if(st7735_type == ST7735_RED144_GREENTAB) { + st7735_height = st7735_default_height_144; + } else { + st7735_height = st7735_default_height_18; + } + break; + + case ST7735_PORTRAIT_INV: + if (st7735_type == ST7735_RED_18_BLACKTAB) { + st7735_write_data(MADCTL_MX | MADCTL_MV | MADCTL_RGB); + } else { + st7735_write_data(MADCTL_MX | MADCTL_MV | MADCTL_BGR); + } + + if (st7735_type == ST7735_RED144_GREENTAB) { + st7735_width = st7735_default_height_144; + } else { + st7735_width = st7735_default_height_18; + } + + st7735_height = st7735_default_width; + break; + } +} + +void st7735_set_addr_win(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) { + st7735_write_cmd(ST7735_CASET); // Column addr set + st7735_write_data(0x00); + st7735_write_cmd(x0 + st7735_column_start); // XSTART + st7735_write_data(0x00); + st7735_write_data(x1 + st7735_column_start); // XEND + + st7735_write_cmd(ST7735_RASET); // Row addr set + st7735_write_data(0x00); + st7735_write_data(y0 + st7735_row_start); // YSTART + st7735_write_data(0x00); + st7735_write_data(y1 + st7735_row_start); // YEND + + st7735_write_cmd(ST7735_RAMWR); // write to RAM +} + +void st7735_draw_pixel(int16_t x, int16_t y, uint16_t color) { + if((x < 0) || (x >= st7735_width) || (y < 0) || (y >= st7735_height)){ + return; + } + + st7735_set_addr_win(x, y, x+1, y+1); + + st7735_set_rs(); + spi_unset_cs(); + + spi_write(color >> 8); + spi_write(color); + + spi_set_cs(); } diff --git a/st7735.h b/st7735.h index fa170dc..c9ec1b5 100644 --- a/st7735.h +++ b/st7735.h @@ -3,11 +3,11 @@ #include -const uint8_t st7735_width = 128; +const uint8_t st7735_default_width = 128; // for 1.44" display -const uint8_t st7735_height_144 = 128; +const uint8_t st7735_default_height_144 = 128; // for 1.8" display -const uint8_t st7735_height_18 = 160; +const uint8_t st7735_default_height_18 = 160; enum ST7735_DISPLAY_TYPE { ST7735_BLUE, @@ -80,6 +80,14 @@ enum ST7735_COLORS { ST7735_COLOR_WHITE = 0xFFFF }; +enum ST7735_ORIENTATION { + ST7735_LANDSCAPE, + ST7735_PORTRAIT, + ST7735_LANDSCAPE_INV, + ST7735_PORTRAIT_INV +}; + void st7735_init(void); +void st7735_set_orientation(enum ST7735_ORIENTATION orientation); #endif