From 02dca8c008c1aa755c5272e379fdfac5b0566a7f Mon Sep 17 00:00:00 2001 From: bartool Date: Sun, 30 May 2021 12:21:27 +0200 Subject: [PATCH] nowa wersja write_to_buffer --- Core/Inc/SSD1306_oled.h | 2 + Core/Src/SSD1306_oled.c | 115 +++++++++++++++++++++++++++------------ test/test_SSD1306_oled.c | 22 ++++---- 3 files changed, 93 insertions(+), 46 deletions(-) diff --git a/Core/Inc/SSD1306_oled.h b/Core/Inc/SSD1306_oled.h index d3d9444..07a9a06 100644 --- a/Core/Inc/SSD1306_oled.h +++ b/Core/Inc/SSD1306_oled.h @@ -85,6 +85,8 @@ 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); void ssd1306_clear_buffer (uint8_t width, uint8_t height, uint8_t pos_x, uint8_t pos_y); +void next(const uint8_t* bitmap, uint8_t bitmap_width, uint8_t bitmap_height, int8_t pos_x, int8_t pos_y); + #ifdef TEST extern uint8_t buffer_oled[SSD1306_BUF_SIZE]; #endif diff --git a/Core/Src/SSD1306_oled.c b/Core/Src/SSD1306_oled.c index 4c53eca..997bc00 100644 --- a/Core/Src/SSD1306_oled.c +++ b/Core/Src/SSD1306_oled.c @@ -236,38 +236,83 @@ void ssd1306_clear_buffer (uint8_t width, uint8_t height, uint8_t pos_x, uint8_t } -// -//void next(void) -//{ -// uint16_t tmp; -// for(uint8_t col = col_start; col < col_end; col++, bitmap_col++) -// { -// tmp = buffer_oled[page_start * SSD1306_LCDWIDTH + col] & (0xFF >> (8 - pos_y%8)); -// -// for( uint8_t page = page_start; page < page_end; page++, bitmap_row++ ) -// { -// bitmap_idx = bitmap_width * bitmap_row + bitmap_col; -// -// if (bitmap_idx < bitmap_max_idx) -// { -//// mask_buf = 0xFF; -// -// pixels_left = bitmap_height - bitmap_row * 8; //poprawic -// if (pixels_left < 8) -// { -// mask_buf = 0xFF >> pixels_left; -// } -// -// tmp &= ~(mask_buf); // mask_buf zeruje tylko to co zostanie nadpisane -// tmp |= bitmap[bitmap_idx] << pos_y%8; // mask_data maskuje tylko starsza czesc bajtu -// } -// -// buffer_oled[page * SSD1306_LCDWIDTH + col] = (uint8_t) tmp; -// tmp = tmp >> 8; -// } -// } -// -// -//} -// -// + +void next(const uint8_t* bitmap, uint8_t bitmap_width, uint8_t bitmap_height, int8_t pos_x, int8_t pos_y) +{ + if (bitmap_width + pos_x < 0 || bitmap_height + pos_y < 0) return; + + uint16_t tmp, bitmap_idx, bitmap_max_idx; + uint8_t buf_row_first, buf_row_last, buf_col_first, buf_col_last, bitmap_col, bitmap_row, bitmap_row_first; + uint8_t mask_buf; + + if (pos_x < 0) + { + bitmap_col = pos_x * -1; + buf_col_first = 0; + } + else + { + bitmap_col = 0; + buf_col_first = pos_x; + } + + if (pos_y < 0) + { + bitmap_row_first = (pos_y * -1) / 8; + buf_row_first = 0; + } + else + { + bitmap_row_first = 0; + buf_row_first = pos_y / 8; + } + + bitmap_max_idx = bitmap_width * ((bitmap_height + 7) / 8); + buf_col_last = (bitmap_width + pos_x) > SSD1306_LCDWIDTH ? SSD1306_LCDWIDTH : (bitmap_width + pos_x); + // buf_row_last = (bitmap_height + pos_y) > SSD1306_LCDHEIGHT ? SSD1306_LCDHEIGHT / 8: (bitmap_height + pos_y + 7) / 8; + if (bitmap_height + pos_y > SSD1306_LCDHEIGHT) + { + buf_row_last = SSD1306_LCDHEIGHT / 8; + } + else + { + buf_row_last = (bitmap_height + pos_y + 7) / 8; + } + + for(uint8_t col = buf_col_first; col < buf_col_last; col++, bitmap_col++) + { + tmp = 0; + bitmap_row = bitmap_row_first; + for( uint8_t buf_row = buf_row_first; buf_row < buf_row_last; buf_row++, bitmap_row++ ) + { + bitmap_idx = bitmap_width * bitmap_row + bitmap_col; + + mask_buf = 0; + + if (bitmap_row == 0) + { + mask_buf |= 0xFF >> (8 - pos_y%8); + } + + uint8_t shifted_pixels_left = (pos_y + bitmap_height) - bitmap_row * 8; + if (shifted_pixels_left < 8) + { + mask_buf |= (0xFF << shifted_pixels_left); + } + + tmp |= buffer_oled[buf_row * SSD1306_LCDWIDTH + col] & mask_buf; + + if (bitmap_idx < bitmap_max_idx) + { + tmp |= bitmap[bitmap_idx] << pos_y%8; + } + + buffer_oled[buf_row * SSD1306_LCDWIDTH + col] = (uint8_t) tmp; + tmp = tmp >> 8; + } + } + + +} + + diff --git a/test/test_SSD1306_oled.c b/test/test_SSD1306_oled.c index 722a21a..2f2ad6a 100644 --- a/test/test_SSD1306_oled.c +++ b/test/test_SSD1306_oled.c @@ -20,7 +20,7 @@ void tearDown(void) void test_wrtie_to_buffer_at_pos_0_0_height_8pt(void) { // uint8_t data[8] = {}; - ssd1306_write_to_buffer(picture_8pt, SSD1306_LCDWIDTH, 8, 0, 0); + next(picture_8pt, SSD1306_LCDWIDTH, 8, 0, 0); TEST_ASSERT_EQUAL_UINT8_ARRAY(posX_0_posY_0_8pt, buffer_oled, SSD1306_BUF_SIZE); // TEST_ASSERT_EQUAL_UINT8(0xFF, buffer_oled[8]); @@ -29,14 +29,14 @@ void test_wrtie_to_buffer_at_pos_0_0_height_8pt(void) void test_wrtie_to_buffer_at_pos_1_0_height_8pt(void) { // uint8_t data[8] = {}; - ssd1306_write_to_buffer(picture_8pt, SSD1306_LCDWIDTH, 8, 0, 1); + next(picture_8pt, SSD1306_LCDWIDTH, 8, 0, 1); TEST_ASSERT_EQUAL_UINT8_ARRAY(posX_0_posY_1_8pt, buffer_oled, SSD1306_BUF_SIZE); } void test_wrtie_to_buffer_at_pos_7_0_height_8pt(void) { - ssd1306_write_to_buffer(picture_8pt, SSD1306_LCDWIDTH, 8, 0, 7); + next(picture_8pt, SSD1306_LCDWIDTH, 8, 0, 7); TEST_ASSERT_EQUAL_UINT8_ARRAY(posX_0_posY_7_8pt, buffer_oled, SSD1306_BUF_SIZE); } @@ -44,7 +44,7 @@ void test_wrtie_to_buffer_at_pos_7_0_height_8pt(void) void test_wrtie_to_buffer_at_pos_0_0_height_16pt(void) { // uint8_t data[8] = {}; - ssd1306_write_to_buffer(picture_16pt, SSD1306_LCDWIDTH, 16, 0, 0); + next(picture_16pt, SSD1306_LCDWIDTH, 16, 0, 0); TEST_ASSERT_EQUAL_UINT8_ARRAY(posX_0_posY_0_16pt, buffer_oled, SSD1306_BUF_SIZE); // TEST_ASSERT_EQUAL_UINT8(0xFF, buffer_oled[8]); @@ -53,14 +53,14 @@ void test_wrtie_to_buffer_at_pos_0_0_height_16pt(void) void test_wrtie_to_buffer_at_pos_1_0_height_16pt(void) { // uint8_t data[8] = {}; - ssd1306_write_to_buffer(picture_16pt, SSD1306_LCDWIDTH, 16, 0, 1); + next(picture_16pt, SSD1306_LCDWIDTH, 16, 0, 1); TEST_ASSERT_EQUAL_UINT8_ARRAY(posX_0_posY_1_16pt, buffer_oled, SSD1306_BUF_SIZE); } void test_wrtie_to_buffer_at_pos_7_0_height_16pt(void) { - ssd1306_write_to_buffer(picture_16pt, SSD1306_LCDWIDTH, 16, 0, 7); + next(picture_16pt, SSD1306_LCDWIDTH, 16, 0, 7); TEST_ASSERT_EQUAL_UINT8_ARRAY(posX_0_posY_7_16pt, buffer_oled, SSD1306_BUF_SIZE); } @@ -68,7 +68,7 @@ void test_wrtie_to_buffer_at_pos_7_0_height_16pt(void) void test_wrtie_to_buffer_at_pos_0_0_height_12pt(void) { // uint8_t data[8] = {}; - ssd1306_write_to_buffer(picture_12pt, SSD1306_LCDWIDTH, 12, 0, 0); + next(picture_12pt, SSD1306_LCDWIDTH, 12, 0, 0); TEST_ASSERT_EQUAL_UINT8_ARRAY(posX_0_posY_0_12pt, buffer_oled, SSD1306_BUF_SIZE); // TEST_ASSERT_EQUAL_UINT8(0xFF, buffer_oled[8]); @@ -77,7 +77,7 @@ void test_wrtie_to_buffer_at_pos_0_0_height_12pt(void) void test_wrtie_to_buffer_at_pos_1_0_height_12pt(void) { // uint8_t data[8] = {}; - ssd1306_write_to_buffer(picture_12pt, SSD1306_LCDWIDTH, 12, 0, 1); + next(picture_12pt, SSD1306_LCDWIDTH, 12, 0, 1); TEST_ASSERT_EQUAL_UINT8_ARRAY(posX_0_posY_1_12pt, buffer_oled, SSD1306_BUF_SIZE); } @@ -85,7 +85,7 @@ void test_wrtie_to_buffer_at_pos_1_0_height_12pt(void) void test_wrtie_to_buffer_at_pos_0_4_height_12pt(void) { // uint8_t data[8] = {}; - ssd1306_write_to_buffer(picture_12pt, SSD1306_LCDWIDTH, 12, 0, 4); + next(picture_12pt, SSD1306_LCDWIDTH, 12, 0, 4); TEST_ASSERT_EQUAL_UINT8_ARRAY(posX_0_posY_4_12pt, buffer_oled, SSD1306_BUF_SIZE); // TEST_ASSERT_EQUAL_UINT8(0xFF, buffer_oled[8]); @@ -93,7 +93,7 @@ void test_wrtie_to_buffer_at_pos_0_4_height_12pt(void) void test_wrtie_to_buffer_at_pos_7_0_height_12pt(void) { - ssd1306_write_to_buffer(picture_12pt, SSD1306_LCDWIDTH, 12, 0, 7); + next(picture_12pt, SSD1306_LCDWIDTH, 12, 0, 7); - TEST_ASSERT_EQUAL_UINT8_ARRAY(posX_0_posY_7_12pt, buffer_oled, SSD1306_BUF_SIZE); + TEST_ASSERT_EQUAL_HEX8_ARRAY(posX_0_posY_7_12pt, buffer_oled, SSD1306_BUF_SIZE); } \ No newline at end of file