From 765f57b4b3518e51681b26ce74e6c2876e239dd1 Mon Sep 17 00:00:00 2001 From: bartool Date: Mon, 31 May 2021 21:23:25 +0200 Subject: [PATCH] sprawdzone na sprzecie. wszystko ok --- Core/Inc/SSD1306_oled.h | 2 +- Core/Inc/fonts.h | 2 +- Core/Src/SSD1306_oled.c | 197 +++------------------------------------- Core/Src/fonts.c | 13 ++- Core/Src/main.c | 4 +- 5 files changed, 28 insertions(+), 190 deletions(-) diff --git a/Core/Inc/SSD1306_oled.h b/Core/Inc/SSD1306_oled.h index b8ac233..a662273 100644 --- a/Core/Inc/SSD1306_oled.h +++ b/Core/Inc/SSD1306_oled.h @@ -84,7 +84,7 @@ uint8_t SSD1306_display_page(void); void SSD1306_clear(uint8_t color); void SSD1306_set_pixel(uint8_t x, uint8_t y, uint8_t bw); -void SSD1306_write_to_buffer(const uint8_t* data, uint8_t width, uint8_t height, int8_t pos_x, int8_t pos_y, uint8_t color); +void SSD1306_write_to_buffer(const uint8_t* data, uint8_t width, uint8_t height, int8_t pos_x, int8_t pos_y, uint8_t mode); void SSD1306_clear_buffer(uint8_t width, uint8_t height, int8_t pos_x, int8_t pos_y, uint8_t color); #ifdef TEST diff --git a/Core/Inc/fonts.h b/Core/Inc/fonts.h index fd57ecf..fe1d1a9 100644 --- a/Core/Inc/fonts.h +++ b/Core/Inc/fonts.h @@ -41,6 +41,6 @@ extern const FONT_INFO arial_8ptFontInfo; -uint8_t font_print_str(const FONT_INFO * font, uint8_t * text, uint8_t pos_x, uint8_t pos_y); +uint8_t font_print_str(const FONT_INFO * font, uint8_t * text, uint8_t pos_x, uint8_t pos_y, uint8_t mode); uint8_t font_string_lenght_px(const FONT_INFO * font, uint8_t * text); #endif /* MK_FONTX_H_ */ diff --git a/Core/Src/SSD1306_oled.c b/Core/Src/SSD1306_oled.c index 01dd8b7..b59cca6 100644 --- a/Core/Src/SSD1306_oled.c +++ b/Core/Src/SSD1306_oled.c @@ -64,7 +64,7 @@ uint8_t SSD1306_display_page(void) SSD1306_SendCommand(SSD1306_LCDWIDTH - 1); SSD1306_SendData(buffer_oled + (page * SSD1306_LCDWIDTH), SSD1306_LCDWIDTH); -// page++; + if (++page > 7) { page = 0; @@ -148,94 +148,6 @@ void SSD1306_set_pixel(uint8_t x, uint8_t y, uint8_t bw) } } -// void ssd1306_write_to_buffer(const uint8_t* data, uint8_t width, uint8_t height, int8_t pos_x, int8_t pos_y) -// { -// int16_t max_x, max_y; -// uint8_t shift_x = 0, temp, row = 0; -// uint16_t buf_idx = 0, index = 0; - - -// // right boundry -// if (width + pos_x > SSD1306_LCDWIDTH) max_x = SSD1306_LCDWIDTH; -// else max_x = width + pos_x; - -// // left boundry -// if (pos_x < 0) -// { -// shift_x = pos_x * -1; -// pos_x = 0; -// } - -// // bottom boundry -// if (height + pos_y > SSD1306_LCDHEIGHT) max_y = SSD1306_LCDHEIGHT; -// else max_y = height + pos_y; -// max_y = max_y / 8 + (max_y % 8 == 0 ? 0 : 1); - -// // top boundry -// if (pos_y < 0) -// { -// uint8_t abs_pos_y = pos_y * -1; -// row = abs_pos_y/8 + (abs_pos_y % 8 == 0 ? 0 : 1); -// pos_y = (8 - abs_pos_y%8)%8; -// } - -// uint8_t shift = pos_y % 8; -// uint8_t mask_lsb = 0xFF >> (8 - shift); -// uint8_t mask_msb = 0xFF << shift; - -// for (uint8_t y = pos_y / 8; y < max_y; y++) -// { -// index = width * row + shift_x; -// for (uint8_t x = pos_x; x < max_x; x++, index++) -// { -// buf_idx = y * SSD1306_LCDWIDTH + x; -// temp = buffer_oled[buf_idx]; - -// if (index < width * ((height + 7) / 8)) -// temp = data[index] << shift | (temp & mask_lsb); -// // else if (height%8 != 0) -// // mask_msb = 0xFF << ((pos_y + height)%8); - -// if (shift != 0 && index >= width) -// temp = data[index - width] >> (8 - shift) | (temp & mask_msb); - -// buffer_oled[buf_idx] = temp; -// } -// row++; -// } -// } - -// void ssd1306_clear_buffer (uint8_t width, uint8_t height, uint8_t pos_x, uint8_t pos_y) -// { -// uint16_t max_x, max_y, index = 0, temp, row = 0; -// if (width + pos_x > SSD1306_LCDWIDTH) max_x = SSD1306_LCDWIDTH; -// else max_x = width + pos_x; - -// if (height + pos_y > SSD1306_LCDHEIGHT) max_y = SSD1306_LCDHEIGHT; -// else max_y = height + pos_y; -// max_y = max_y / 8 + (max_y % 8 == 0 ? 0 : 1); - -// uint8_t shift = pos_y % 8; -// uint8_t mask_right = 0xFF >> shift; -// uint8_t mask_left = 0xFF << (8 - shift); -// for (uint8_t y = pos_y / 8; y < max_y; y++, index = width * row) -// { -// for (uint8_t x = pos_x; x < max_x; x++, index++) -// { -// temp = buffer_oled[y * SSD1306_LCDWIDTH + x]; -// if (index < width * height/8) -// temp &= mask_left; - -// if (index >= width) -// temp &= mask_right; - -// buffer_oled[y * SSD1306_LCDWIDTH + x] = temp; -// } -// row++; -// } - -// } - typedef struct { uint16_t bitmap_max_idx; @@ -248,7 +160,7 @@ typedef struct uint8_t bitmap_col; uint8_t bitmap_row_first; uint8_t bitmap_row_last; - uint8_t shift; + uint8_t bitmap_shift; }buf_bitmap_boundry_t; static void get_boundry (buf_bitmap_boundry_t* boundry, uint8_t bitmap_width, uint8_t bitmap_height, int8_t pos_x, int8_t pos_y) @@ -262,16 +174,15 @@ static void get_boundry (buf_bitmap_boundry_t* boundry, uint8_t bitmap_width, ui } if (pos_y < 0) { - boundry->shift = 8 + (pos_y % 8) ; + boundry->bitmap_shift = 8 + (pos_y % 8) ; boundry->bitmap_row_first = (pos_y / 8) * (-1) + 1; boundry->buf_row_first = 0; boundry->buf_mask_top = 0; - // boundry->buf_mask_bottom = 0xFF << ((pos_y + bitmap_height) % 8); } else { - boundry->shift = pos_y % 8; + boundry->bitmap_shift = pos_y % 8; boundry->bitmap_row_first = 0; boundry->buf_row_first = pos_y / 8; - boundry->buf_mask_top = 0xFF >> (8 - boundry->shift); + boundry->buf_mask_top = 0xFF >> (8 - boundry->bitmap_shift); } boundry->buf_mask_bottom = 0xFF << ((pos_y + bitmap_height) % 8); if (boundry->buf_mask_bottom == 0xFF) { @@ -290,11 +201,11 @@ static void get_boundry (buf_bitmap_boundry_t* boundry, uint8_t bitmap_width, ui boundry->buf_row_last = (bitmap_height + pos_y + 7) / 8; } - boundry->bitmap_row_last = (bitmap_height + 7) / 8; + boundry->bitmap_row_last = (pos_y + bitmap_height) / 8; boundry->bitmap_max_idx = bitmap_width * ((bitmap_height + 7) / 8); } -uint8_t get_bitmap_byte (const uint8_t* bitmap, uint16_t index, uint8_t color) +static inline uint8_t get_bitmap_byte (const uint8_t* bitmap, uint16_t index, uint8_t color) { switch (color) { @@ -323,12 +234,12 @@ uint8_t get_bitmap_byte (const uint8_t* bitmap, uint16_t index, uint8_t color) * @param color NORMAL (2) normal mode or INVERSE mode for bitmap * WHITE (0) or black (1) for fill screen */ -void SSD1306_write_to_buffer(const uint8_t* bitmap, uint8_t bitmap_width, uint8_t bitmap_height, int8_t pos_x, int8_t pos_y, uint8_t color) +void SSD1306_write_to_buffer(const uint8_t* bitmap, uint8_t bitmap_width, uint8_t bitmap_height, int8_t pos_x, int8_t pos_y, uint8_t mode) { if (bitmap_width + pos_x < 0 || bitmap_height + pos_y < 0) return; uint16_t tmp_buf16, bitmap_idx, buf_idx; - uint8_t mask_buf, tmp_bitmap, bitmap_row; + uint8_t tmp_bitmap, bitmap_row; buf_bitmap_boundry_t b; get_boundry(&b, bitmap_width, bitmap_height, pos_x, pos_y); @@ -339,7 +250,7 @@ void SSD1306_write_to_buffer(const uint8_t* bitmap, uint8_t bitmap_width, uint8_ bitmap_row = b.bitmap_row_first; if (b.bitmap_row_first > 0) { - tmp_buf16 = get_bitmap_byte(bitmap, bitmap_width * (b.bitmap_row_first - 1) + b.bitmap_col, color) >> (8 - b.shift); + tmp_buf16 = get_bitmap_byte(bitmap, bitmap_width * (b.bitmap_row_first - 1) + b.bitmap_col, mode) >> (8 - b.bitmap_shift); } else { tmp_buf16 = buffer_oled[b.buf_row_first * SSD1306_LCDWIDTH + col] & b.buf_mask_top; } @@ -348,44 +259,17 @@ void SSD1306_write_to_buffer(const uint8_t* bitmap, uint8_t bitmap_width, uint8_ { bitmap_idx = bitmap_width * bitmap_row + b.bitmap_col; buf_idx = buf_row * SSD1306_LCDWIDTH + col; - // mask_buf = 0; - // if (bitmap_row == b.bitmap_row_first) { - // mask_buf |= b.buf_mask_bottom; - // } - - // uint8_t shifted_pixels_left = (pos_y%8 + bitmap_height) - bitmap_row * 8; - // if (shifted_pixels_left < 8) { - // mask_buf |= (0xFF << shifted_pixels_left); - // } - - // if (bitmap_height < (bitmap_row +1 ) * 8) { - // mask_buf |= b.buf_mask_top; - // } - - - - - if (bitmap_idx < b.bitmap_max_idx) - { - - - // if (bitmap_row == b.bitmap_row_first && pos_y < 0) { - // tmp_buf16 |= (tmp_bitmap >> (8 - b.shift));// & ~(mask_buf); - // bitmap_row++; - // } - tmp_bitmap = get_bitmap_byte(bitmap, bitmap_idx, color); - tmp_buf16 |= tmp_bitmap << b.shift; + if (bitmap_idx < b.bitmap_max_idx) { + tmp_bitmap = get_bitmap_byte(bitmap, bitmap_idx, mode); + tmp_buf16 |= tmp_bitmap << b.bitmap_shift; } - // if (buf_row + 1 == b.buf_row_last && bitmap_row + 1 >= b.bitmap_row_last) { - if ( (pos_y + bitmap_height) / 8 == buf_row) { + if ( b.bitmap_row_last == buf_row) { buffer_oled[buf_idx] = (buffer_oled[buf_idx] & b.buf_mask_bottom) | (tmp_buf16 & ~(b.buf_mask_bottom)); } else { buffer_oled[buf_idx] = (uint8_t) tmp_buf16; } - // tmp_buf16 = (buffer_oled[buf_idx] & mask_buf) | (tmp_buf16 & ~(mask_buf)); - // buffer_oled[buf_idx] = (uint8_t) tmp_buf16; tmp_buf16 = tmp_buf16 >> 8; } } @@ -395,57 +279,4 @@ void SSD1306_clear_buffer(uint8_t width, uint8_t height, int8_t pos_x, int8_t po { SSD1306_write_to_buffer(NULL, width, height, pos_x, pos_y, color); } -// { -// if (width + pos_x < 0 || height + pos_y < 0) return; -// uint16_t tmp_buf16, bitmap_idx; -// uint16_t mask_buf; -// uint8_t bitmap_row; - -// switch(color) -// { -// case WHITE: -// color = 0xFF; -// break; -// case BLACK: -// color = 0x00; -// break; -// } - -// buf_bitmap_boundry_t b; -// get_boundry(&b, width, height, pos_x, pos_y); - -// for(uint8_t col = b.buf_col_first; col < b.buf_col_last; col++, b.bitmap_col++) -// { -// tmp_buf16 = 0; -// bitmap_row = b.bitmap_row_first; -// for( uint8_t buf_row = b.buf_row_first; buf_row < b.buf_row_last; buf_row++, bitmap_row++ ) -// { -// bitmap_idx = width * bitmap_row + b.bitmap_col; -// mask_buf = 0; - -// if (bitmap_row == 0 && pos_y > 0) { -// mask_buf |= 0xFF >> (8 - (b.shift % 8)); -// } - -// uint8_t shifted_pixels_left = (pos_y%8 + height) - bitmap_row * 8; -// if (shifted_pixels_left < 8) { -// mask_buf |= (0xFF << shifted_pixels_left); -// } - -// tmp_buf16 |= buffer_oled[buf_row * SSD1306_LCDWIDTH + col] & mask_buf; - -// if (bitmap_idx < b.bitmap_max_idx) -// { -// if (pos_y < 0) { -// tmp_buf16 |= (color >> (b.shift % 8)) & ~(mask_buf); -// } else { -// tmp_buf16 |= (color << (b.shift % 8)) & ~(mask_buf); -// } -// } - -// buffer_oled[buf_row * SSD1306_LCDWIDTH + col] = (uint8_t) tmp_buf16; -// tmp_buf16 = tmp_buf16 >> 8; -// } -// } -// } diff --git a/Core/Src/fonts.c b/Core/Src/fonts.c index f5a8d99..fe1d1d3 100644 --- a/Core/Src/fonts.c +++ b/Core/Src/fonts.c @@ -9,7 +9,7 @@ #include "fonts.h" #include "SSD1306_oled.h" -uint8_t font_print_str(const FONT_INFO * font, uint8_t * text, uint8_t pos_x, uint8_t pos_y) +uint8_t font_print_str(const FONT_INFO * font, uint8_t * text, uint8_t pos_x, uint8_t pos_y, uint8_t mode) { uint8_t height = font->heightPixels; @@ -17,7 +17,14 @@ uint8_t font_print_str(const FONT_INFO * font, uint8_t * text, uint8_t pos_x, ui uint8_t width = font_string_lenght_px(font, text); - SSD1306_clear_buffer(width+2, height+2, pos_x-1, pos_y-1, BLACK); + switch (mode) { + case INVERSE: + SSD1306_clear_buffer(width+2, height+2, pos_x-1, pos_y-1, WHITE); + break; + default: + SSD1306_clear_buffer(width+2, height+2, pos_x-1, pos_y-1, BLACK); + break; + } while(*text) { @@ -30,7 +37,7 @@ uint8_t font_print_str(const FONT_INFO * font, uint8_t * text, uint8_t pos_x, ui uint8_t char_nr = actual_char - font->startChar; const FONT_CHAR_INFO *charinfo = &font->charInfo[char_nr]; - SSD1306_write_to_buffer(font->data + charinfo->offset, charinfo->widthBits, height, x, pos_y, WHITE); + SSD1306_write_to_buffer(font->data + charinfo->offset, charinfo->widthBits, height, x, pos_y, mode); x += charinfo->widthBits + font->interspacePixels; } diff --git a/Core/Src/main.c b/Core/Src/main.c index 0126037..0111fb0 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -111,13 +111,13 @@ int main(void) sprintf((char*)tekst, "%3d", counter); // font_print_str(&arial_12ptFontInfo, tekst, 5, 16); SSD1306_clear(BLACK); - font_print_str(&arial_8ptFontInfo, tekst, 40, 0); + font_print_str(&arial_8ptFontInfo, tekst, 40, 8, INVERSE); SSD1306_clear_buffer(24, 12, 0, counter++, WHITE); if (counter > 64+12){ counter = -16; } SSD1306_display_all(); - HAL_Delay(100); +// HAL_Delay(100); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */