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
master
Sylaina 3 years ago
parent a8fb621eff
commit d684610c00

93
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<x2 ? 1 : -1;
int dy = -abs(y2-y1), sy = y1<y2 ? 1 : -1;
int err = dx+dy, e2; /* error value e_xy */
while(1){
lcd_drawPixel(x1, y1, color);
result = lcd_drawPixel(x1, y1, color);
if (x1==x2 && y1==y2) break;
e2 = 2*err;
if (e2 > 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<y) {
if (f >= 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)

16
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

@ -24,7 +24,7 @@ Memory:
</tr>
<tr>
<td>I2C-Core</td>
<td>234 Bytes</td>
<td>220 Bytes</td>
<td>0 Bytes</td>
</tr>
<tr>
@ -34,12 +34,12 @@ Memory:
</tr>
<tr>
<td>OLED (Text-Mode)</td>
<td>1449 Bytes</td>
<td>1395 Bytes</td>
<td>3 Bytes</td>
</tr>
<tr>
<td>OLED (Graphic-Mode)</td>
<td>2403 Bytes</td>
<td>2541 Bytes</td>
<td>1027 Bytes</td>
</tr>
</table>

Loading…
Cancel
Save