Created
November 14, 2022 13:11
-
-
Save doofy-dev/afb2f19dd7cf2fb032e0bcf69ec2e5f5 to your computer and use it in GitHub Desktop.
Read and write pixels for Flipper zero
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef enum { | |
Black, | |
White, | |
Inverse | |
} DrawMode; | |
unsigned flipBit(uint8_t x, uint8_t bit) { | |
return x ^ (1 << bit); | |
} | |
unsigned setBit(uint8_t x, uint8_t bit) { | |
return x | (1 << bit); | |
} | |
unsigned unsetBit(uint8_t x, uint8_t bit) { | |
return x & ~(1 << bit); | |
} | |
bool in_screen(int16_t x, int16_t y) { | |
return x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT; | |
} | |
bool test_pixel(uint8_t *data, uint8_t x, uint8_t y, uint8_t width) { | |
uint8_t current_bit = (y % 8); | |
uint8_t current_row = ((y - current_bit) / 8); | |
uint8_t current_value = data[current_row * width + x]; | |
return current_value & (1 << current_bit); | |
} | |
bool read_pixel(Canvas *const canvas, int16_t x, int16_t y) { | |
if (in_screen(x, y)) { | |
return test_pixel(canvas->fb.tile_buf_ptr, x, y, SCREEN_WIDTH); | |
} | |
return false; | |
} | |
void set_pixel(Canvas *const canvas, int16_t x, int16_t y, DrawMode draw_mode) { | |
if (in_screen(x, y)) { | |
uint8_t current_bit = (y % 8); | |
uint8_t current_row = ((y - current_bit) / 8); | |
uint32_t i = current_row * SCREEN_WIDTH + x; | |
uint8_t current_value = canvas->fb.tile_buf_ptr[i]; | |
if (draw_mode == Inverse) { | |
canvas->fb.tile_buf_ptr[i] = flipBit(current_value, current_bit); | |
} else { | |
if (draw_mode == White) { | |
canvas->fb.tile_buf_ptr[i] = unsetBit(current_value, current_bit); | |
} else { | |
canvas->fb.tile_buf_ptr[i] = setBit(current_value, current_bit); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment