From d8180ba10a1c4c16d86c0235ed60ef618a1b715a Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Mon, 5 Dec 2016 23:18:40 +0100 Subject: [PATCH] Added line and rect drawing --- st7735.c | 40 ++++++++++++++++++++++++++++++++++++---- st7735.h | 16 ++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/st7735.c b/st7735.c index 7537e8e..59fb635 100644 --- a/st7735.c +++ b/st7735.c @@ -44,6 +44,12 @@ static inline void st7735_write_data(uint8_t data) { spi_set_cs(); } +static inline void st7735_write_color(uint16_t color) { + spi_write(color >> 8); + spi_write(color); +} + + static inline void st7735_reset() { spi_unset_cs(); st7735_set_rst(); @@ -228,17 +234,43 @@ void st7735_set_addr_win(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) { } void st7735_draw_pixel(int16_t x, int16_t y, uint16_t color) { - if((x < 0) || (x >= st7735_width) || (y < 0) || (y >= st7735_height)){ + 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); + st7735_write_color(color); spi_set_cs(); } + + +void st7735_fill_rect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) { + if(x >= st7735_width || y >= st7735_height) { + return; + } + + if((x + w - 1) >= st7735_width) { + w = st7735_width - x; + } + if((y + h - 1) >= st7735_height) { + h = st7735_height - y; + } + + st7735_set_addr_win(x, y, x + w - 1, y + h - 1); + + st7735_set_rs(); + spi_unset_cs(); + + for(uint8_t i = 0; i < h; i++) { + for(uint8_t j = 0; j < w; j++) { + st7735_write_color(color); + } + } + + spi_set_cs(); +} diff --git a/st7735.h b/st7735.h index c9ec1b5..36f09aa 100644 --- a/st7735.h +++ b/st7735.h @@ -88,6 +88,22 @@ enum ST7735_ORIENTATION { }; void st7735_init(void); + void st7735_set_orientation(enum ST7735_ORIENTATION orientation); +void st7735_draw_pixel(int16_t x, int16_t y, uint16_t color); +void st7735_fill_rect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); + +static inline void st7735_draw_fast_vline(int16_t x, int16_t y, int16_t h, uint16_t color) { + st7735_fill_rect(x, y, 1, h, color); +} + +static inline void st7735_draw_fast_hline(int16_t x, int16_t y, int16_t w, uint16_t color) { + st7735_fill_rect(x, y, w, 1, color); +} + +static inline uint16_t st7735_color(uint8_t r, uint8_t g, uint8_t b) { + return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3); +} + #endif