write_to_buffer wszystkie testy pomyslne

This commit is contained in:
2021-05-30 13:43:25 +02:00
parent 02dca8c008
commit 8189b0b947
5 changed files with 415 additions and 95 deletions

View File

@@ -82,10 +82,9 @@ 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);
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];

View File

@@ -148,62 +148,62 @@ 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;
// 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;
// // 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;
}
// // 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);
// // 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;
}
// // 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;
// 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];
// 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 (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);
// if (shift != 0 && index >= width)
// temp = data[index - width] >> (8 - shift) | (temp & mask_msb);
buffer_oled[buf_idx] = temp;
}
row++;
}
}
// buffer_oled[buf_idx] = temp;
// }
// row++;
// }
// }
void ssd1306_clear_buffer (uint8_t width, uint8_t height, uint8_t pos_x, uint8_t pos_y)
{
@@ -236,83 +236,93 @@ void ssd1306_clear_buffer (uint8_t width, uint8_t height, uint8_t pos_x, uint8_t
}
void next(const uint8_t* bitmap, uint8_t bitmap_width, uint8_t bitmap_height, int8_t pos_x, int8_t pos_y)
/**
* @brief A function that writes a bitmap into the buffer at the given position.
* 0,0 -------->x
* |
* |
* \ /
* y
* @param bitmap A pointer to bitmap array.
* @param bitmap_width Bitmap witdh in pixels.
* @param bitmap_height Bitmap height in pixels.
* @param pos_x Position x in the display
* @param pos_y Position y in the display
*/
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)
{
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;
uint16_t tmp_buf16, bitmap_idx, bitmap_max_idx;
uint8_t buf_row_first, buf_row_last, buf_col_first, buf_col_last;
uint8_t bitmap_col, bitmap_row, bitmap_row_first;
uint8_t mask_buf, shift;
if (pos_x < 0)
{
if (pos_x < 0) {
bitmap_col = pos_x * -1;
buf_col_first = 0;
}
else
{
} else {
bitmap_col = 0;
buf_col_first = pos_x;
}
if (pos_y < 0)
{
bitmap_row_first = (pos_y * -1) / 8;
if (pos_y < 0) {
shift = (pos_y * -1);
bitmap_row_first = shift / 8;
buf_row_first = 0;
}
else
{
} else {
shift = pos_y;
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;
if ((bitmap_width + pos_x) > SSD1306_LCDWIDTH) {
buf_col_last = SSD1306_LCDWIDTH;
} else {
buf_col_last = bitmap_width + pos_x;
}
else
{
if (bitmap_height + pos_y > SSD1306_LCDHEIGHT) {
buf_row_last = SSD1306_LCDHEIGHT / 8;
} else {
buf_row_last = (bitmap_height + pos_y + 7) / 8;
}
bitmap_max_idx = bitmap_width * ((bitmap_height + 7) / 8);
for(uint8_t col = buf_col_first; col < buf_col_last; col++, bitmap_col++)
{
tmp = 0;
tmp_buf16 = 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);
if (bitmap_row == 0 && pos_y > 0) {
mask_buf |= 0xFF >> (8 - (shift % 8));
}
uint8_t shifted_pixels_left = (pos_y + bitmap_height) - bitmap_row * 8;
if (shifted_pixels_left < 8)
{
if (shifted_pixels_left < 8) {
mask_buf |= (0xFF << shifted_pixels_left);
}
tmp |= buffer_oled[buf_row * SSD1306_LCDWIDTH + col] & mask_buf;
tmp_buf16 |= buffer_oled[buf_row * SSD1306_LCDWIDTH + col] & mask_buf;
if (bitmap_idx < bitmap_max_idx)
{
tmp |= bitmap[bitmap_idx] << pos_y%8;
if (pos_y < 0) {
tmp_buf16 |= bitmap[bitmap_idx] >> (shift % 8);
} else {
tmp_buf16 |= bitmap[bitmap_idx] << (shift % 8);
}
}
buffer_oled[buf_row * SSD1306_LCDWIDTH + col] = (uint8_t) tmp;
tmp = tmp >> 8;
buffer_oled[buf_row * SSD1306_LCDWIDTH + col] = (uint8_t) tmp_buf16;
tmp_buf16 = tmp_buf16 >> 8;
}
}
}