From d684610c00aafaa27f39a2c5e766cda52f715499 Mon Sep 17 00:00:00 2001 From: Sylaina Date: Thu, 8 Apr 2021 20:47:51 +0200 Subject: [PATCH] Some improvements Add return-values for some graphic function for error handling. Improved some graphic functions to reduce needed flash. Update readme for flash size values --- lcd.c | 93 ++++++++++++++++++++++++++++++------------------------- lcd.h | 16 +++++----- readme.md | 6 ++-- 3 files changed, 62 insertions(+), 53 deletions(-) diff --git a/lcd.c b/lcd.c index 330d3c6..e255310 100644 --- a/lcd.c +++ b/lcd.c @@ -384,42 +384,47 @@ void lcd_puts_p(const char* progmem_s){ #ifdef GRAPHICMODE #pragma mark - #pragma mark GRAPHIC FUNCTIONS -void lcd_drawPixel(uint8_t x, uint8_t y, uint8_t color){ - if( x > DISPLAY_WIDTH-1 || y > (DISPLAY_HEIGHT-1)) return; // out of Display +uint8_t lcd_drawPixel(uint8_t x, uint8_t y, uint8_t color){ + if( x > DISPLAY_WIDTH-1 || y > (DISPLAY_HEIGHT-1)) return 1; // out of Display + if( color == WHITE){ displayBuffer[(y / 8)][x] |= (1 << (y % 8)); } else { displayBuffer[(y / 8)][x] &= ~(1 << (y % 8)); } + + return 0; } -void lcd_drawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t color){ - if( x1 > DISPLAY_WIDTH-1 || - x2 > DISPLAY_WIDTH-1 || - y1 > DISPLAY_HEIGHT-1 || - y2 > DISPLAY_HEIGHT-1) return; +uint8_t lcd_drawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t color){ + uint8_t result; + int dx = abs(x2-x1), sx = x1 dy) { err += dy; x1 += sx; } /* e_xy+e_x > 0 */ if (e2 < dx) { err += dx; y1 += sy; } /* e_xy+e_y < 0 */ } + + return result; } -void lcd_drawRect(uint8_t px1, uint8_t py1, uint8_t px2, uint8_t py2, uint8_t color){ - if( px1 > DISPLAY_WIDTH-1 || - px2 > DISPLAY_WIDTH-1 || - py1 > DISPLAY_HEIGHT-1 || - py2 > DISPLAY_HEIGHT-1) return; - lcd_drawLine(px1, py1, px2, py1, color); - lcd_drawLine(px2, py1, px2, py2, color); - lcd_drawLine(px2, py2, px1, py2, color); - lcd_drawLine(px1, py2, px1, py1, color); +uint8_t lcd_drawRect(uint8_t px1, uint8_t py1, uint8_t px2, uint8_t py2, uint8_t color){ + uint8_t result; + + result = lcd_drawLine(px1, py1, px2, py1, color); + result = lcd_drawLine(px2, py1, px2, py2, color); + result = lcd_drawLine(px2, py2, px1, py2, color); + result = lcd_drawLine(px1, py2, px1, py1, color); + + return result; } -void lcd_fillRect(uint8_t px1, uint8_t py1, uint8_t px2, uint8_t py2, uint8_t color){ +uint8_t lcd_fillRect(uint8_t px1, uint8_t py1, uint8_t px2, uint8_t py2, uint8_t color){ + uint8_t result; + if( px1 > px2){ uint8_t temp = px1; px1 = px2; @@ -429,24 +434,24 @@ void lcd_fillRect(uint8_t px1, uint8_t py1, uint8_t px2, uint8_t py2, uint8_t co py2 = temp; } for (uint8_t i=0; i<=(py2-py1); i++){ - lcd_drawLine(px1, py1+i, px2, py1+i, color); + result = lcd_drawLine(px1, py1+i, px2, py1+i, color); } + + return result; } -void lcd_drawCircle(uint8_t center_x, uint8_t center_y, uint8_t radius, uint8_t color){ - if( ((center_x + radius) > DISPLAY_WIDTH-1) || - ((center_y + radius) > DISPLAY_HEIGHT-1) || - center_x < radius || - center_y < radius) return; +uint8_t lcd_drawCircle(uint8_t center_x, uint8_t center_y, uint8_t radius, uint8_t color){ + uint8_t result; + int16_t f = 1 - radius; int16_t ddF_x = 1; int16_t ddF_y = -2 * radius; int16_t x = 0; int16_t y = radius; - lcd_drawPixel(center_x , center_y+radius, color); - lcd_drawPixel(center_x , center_y-radius, color); - lcd_drawPixel(center_x+radius, center_y , color); - lcd_drawPixel(center_x-radius, center_y , color); + result = lcd_drawPixel(center_x , center_y+radius, color); + result = lcd_drawPixel(center_x , center_y-radius, color); + result = lcd_drawPixel(center_x+radius, center_y , color); + result = lcd_drawPixel(center_x-radius, center_y , color); while (x= 0) { @@ -458,32 +463,36 @@ void lcd_drawCircle(uint8_t center_x, uint8_t center_y, uint8_t radius, uint8_t ddF_x += 2; f += ddF_x; - lcd_drawPixel(center_x + x, center_y + y, color); - lcd_drawPixel(center_x - x, center_y + y, color); - lcd_drawPixel(center_x + x, center_y - y, color); - lcd_drawPixel(center_x - x, center_y - y, color); - lcd_drawPixel(center_x + y, center_y + x, color); - lcd_drawPixel(center_x - y, center_y + x, color); - lcd_drawPixel(center_x + y, center_y - x, color); - lcd_drawPixel(center_x - y, center_y - x, color); + result = lcd_drawPixel(center_x + x, center_y + y, color); + result = lcd_drawPixel(center_x - x, center_y + y, color); + result = lcd_drawPixel(center_x + x, center_y - y, color); + result = lcd_drawPixel(center_x - x, center_y - y, color); + result = lcd_drawPixel(center_x + y, center_y + x, color); + result = lcd_drawPixel(center_x - y, center_y + x, color); + result = lcd_drawPixel(center_x + y, center_y - x, color); + result = lcd_drawPixel(center_x - y, center_y - x, color); } + return result; } -void lcd_fillCircle(uint8_t center_x, uint8_t center_y, uint8_t radius, uint8_t color) { +uint8_t lcd_fillCircle(uint8_t center_x, uint8_t center_y, uint8_t radius, uint8_t color) { + uint8_t result; for(uint8_t i=0; i<= radius;i++){ - lcd_drawCircle(center_x, center_y, i, color); + result = lcd_drawCircle(center_x, center_y, i, color); } + return result; } -void lcd_drawBitmap(uint8_t x, uint8_t y, const uint8_t *picture, uint8_t width, uint8_t height, uint8_t color){ - uint8_t i,j, byteWidth = (width+7)/8; +uint8_t lcd_drawBitmap(uint8_t x, uint8_t y, const uint8_t *picture, uint8_t width, uint8_t height, uint8_t color){ + uint8_t result,i,j, byteWidth = (width+7)/8; for (j = 0; j < height; j++) { for(i=0; i < width;i++){ if(pgm_read_byte(picture + j * byteWidth + i / 8) & (128 >> (i & 7))){ - lcd_drawPixel(x+i, y+j, color); + result = lcd_drawPixel(x+i, y+j, color); } else { - lcd_drawPixel(x+i, y+j, !color); + result = lcd_drawPixel(x+i, y+j, !color); } } } + return result; } void lcd_display() { #if defined (SSD1306) || defined (SSD1309) diff --git a/lcd.h b/lcd.h index 3fa6313..c5b4193 100644 --- a/lcd.h +++ b/lcd.h @@ -69,7 +69,7 @@ extern "C" { // using 7-bit-adress for lcd-library // if you use your own library for twi check I2C-adress-handle -#define LCD_I2C_ADR (0x7a >> 1) // 7 bit slave-adress without r/w-bit +#define LCD_I2C_ADR (0x78 >> 1) // 7 bit slave-adress without r/w-bit // r/w-bit are set/unset by library // e.g. 8 bit slave-adress: // 0x78 = adress 0x3C with cleared r/w-bit (write-mode) @@ -130,13 +130,13 @@ extern "C" { // at GRAPHICMODE print character to buffer void lcd_charMode(uint8_t mode); // set size of chars #if defined GRAPHICMODE - void lcd_drawPixel(uint8_t x, uint8_t y, uint8_t color); - void lcd_drawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t color); - void lcd_drawRect(uint8_t px1, uint8_t py1, uint8_t px2, uint8_t py2, uint8_t color); - void lcd_fillRect(uint8_t px1, uint8_t py1, uint8_t px2, uint8_t py2, uint8_t color); - void lcd_drawCircle(uint8_t center_x, uint8_t center_y, uint8_t radius, uint8_t color); - void lcd_fillCircle(uint8_t center_x, uint8_t center_y, uint8_t radius, uint8_t color); - void lcd_drawBitmap(uint8_t x, uint8_t y, const uint8_t picture[], uint8_t width, uint8_t height, uint8_t color); + uint8_t lcd_drawPixel(uint8_t x, uint8_t y, uint8_t color); + uint8_t lcd_drawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t color); + uint8_t lcd_drawRect(uint8_t px1, uint8_t py1, uint8_t px2, uint8_t py2, uint8_t color); + uint8_t lcd_fillRect(uint8_t px1, uint8_t py1, uint8_t px2, uint8_t py2, uint8_t color); + uint8_t lcd_drawCircle(uint8_t center_x, uint8_t center_y, uint8_t radius, uint8_t color); + uint8_t lcd_fillCircle(uint8_t center_x, uint8_t center_y, uint8_t radius, uint8_t color); + uint8_t lcd_drawBitmap(uint8_t x, uint8_t y, const uint8_t picture[], uint8_t width, uint8_t height, uint8_t color); void lcd_display(void); // copy buffer to display RAM void lcd_clear_buffer(void); // clear display buffer uint8_t lcd_check_buffer(uint8_t x, uint8_t y); // read a pixel value from the display buffer diff --git a/readme.md b/readme.md index 7f1f04b..2d445ce 100644 --- a/readme.md +++ b/readme.md @@ -24,7 +24,7 @@ Memory: I2C-Core - 234 Bytes + 220 Bytes 0 Bytes @@ -34,12 +34,12 @@ Memory: OLED (Text-Mode) - 1449 Bytes + 1395 Bytes 3 Bytes OLED (Graphic-Mode) - 2403 Bytes + 2541 Bytes 1027 Bytes