Added line and rect drawing

This commit is contained in:
Sebastian 2016-12-05 23:18:40 +01:00
parent e073e6c750
commit d8180ba10a
2 changed files with 52 additions and 4 deletions

View File

@ -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();
}

View File

@ -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