diff --git a/st7735.c b/st7735.c index e864e97..9b51bbd 100644 --- a/st7735.c +++ b/st7735.c @@ -308,29 +308,36 @@ void st7735_fill_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color void st7735_draw_bitmap(uint8_t x, uint8_t y, PGM_P bitmap) { - if(x >= st7735_width || y >= st7735_height) { - return; - } - uint8_t w = pgm_read_word(bitmap); bitmap += 2; uint8_t h = pgm_read_word(bitmap); bitmap += 2; + uint8_t max_x = x + w - 1; + uint8_t max_y = y + h - 1; - if((x + w - 1) >= st7735_width) { - return; - } - if((y + h - 1) >= st7735_height) { + if(x >= st7735_width || y >= st7735_height) { return; } - st7735_set_addr_win(x, y, x + w - 1, y + h - 1); + if(max_x >= st7735_width) { + max_x = st7735_width - 1; + } + + if(max_y >= st7735_height) { + max_y = st7735_height - 1; + } + + st7735_set_addr_win(x, y, max_x, max_y); st7735_set_rs(); spi_unset_cs(); for(uint8_t i = 0; i < h; i++) { for(uint8_t j = 0; j < w; j++) { + if((x + j) >= st7735_width || (y + i) >= st7735_height) { + bitmap += 2; + continue; + } uint16_t color = pgm_read_word(bitmap); st7735_write_color(color); bitmap += 2; @@ -343,19 +350,22 @@ void st7735_draw_bitmap(uint8_t x, uint8_t y, PGM_P bitmap) { void st7735_draw_mono_bitmap(uint8_t x, uint8_t y, PGM_P bitmap, uint16_t color_set, uint16_t color_unset) { uint8_t w = pgm_read_byte(bitmap++); uint8_t h = pgm_read_byte(bitmap++); + uint8_t max_x = x + w - 1; + uint8_t max_y = y + h - 1; if(x >= st7735_width || y >= st7735_height) { return; } - if((x + w - 1) >= st7735_width) { - return; - } - if((y + h - 1) >= st7735_height) { - return; + if(max_x >= st7735_width) { + max_x = st7735_width - 1; } - st7735_set_addr_win(x, y, x + w - 1, y + h - 1); + if(max_y >= st7735_height) { + max_y = st7735_height - 1; + } + + st7735_set_addr_win(x, y, max_x, max_y); st7735_set_rs(); spi_unset_cs(); @@ -365,9 +375,13 @@ void st7735_draw_mono_bitmap(uint8_t x, uint8_t y, PGM_P bitmap, uint16_t color_ for(uint8_t i = 0; i < h; i++) { for(uint8_t j = 0; j < w; j++) { if(bit_pos % 8 == 0) { - byte = pgm_read_byte(bitmap++); + byte = pgm_read_byte(bitmap++); } + if((x + j) >= st7735_width || (y + i) >= st7735_height) { + bit_pos++; + continue; + } if(byte & (1 << (bit_pos % 8))) { st7735_write_color(color_set); } @@ -379,5 +393,4 @@ void st7735_draw_mono_bitmap(uint8_t x, uint8_t y, PGM_P bitmap, uint16_t color_ } spi_set_cs(); - }