From 92d0a71af71973ac3f80398ed4f7f61dd97733b3 Mon Sep 17 00:00:00 2001 From: Gautham Yerroju Date: Wed, 15 Jul 2020 22:48:04 -0700 Subject: [PATCH] OLED driver function to set pixels (#9713) * Add a function to set individual pixels * Add documentation for oled_write_pixel * use smaller data type for oled_write_pixel * Fix boundary check edge case * Update oled_write_pixel doc Co-authored-by: Ryan Co-authored-by: Ryan --- docs/feature_oled_driver.md | 4 ++++ drivers/oled/oled_driver.c | 13 +++++++++++++ drivers/oled/oled_driver.h | 4 ++++ 3 files changed, 21 insertions(+) diff --git a/docs/feature_oled_driver.md b/docs/feature_oled_driver.md index 772ce57bdd..5f3095198f 100644 --- a/docs/feature_oled_driver.md +++ b/docs/feature_oled_driver.md @@ -247,6 +247,10 @@ void oled_write_raw_byte(const char data, uint16_t index); // Writes a PROGMEM string to the buffer at current cursor position void oled_write_raw_P(const char *data, uint16_t size); +// Sets a specific pixel on or off +// Coordinates start at top-left and go right and down for positive x and y +void oled_write_pixel(uint8_t x, uint8_t y, bool on); + // Can be used to manually turn on the screen if it is off // Returns true if the screen was on or turns on bool oled_on(void); diff --git a/drivers/oled/oled_driver.c b/drivers/oled/oled_driver.c index eedaedcd36..977b701783 100644 --- a/drivers/oled/oled_driver.c +++ b/drivers/oled/oled_driver.c @@ -462,6 +462,19 @@ void oled_write_raw(const char *data, uint16_t size) { } } +void oled_write_pixel(uint8_t x, uint8_t y, bool on) { + if (x >= OLED_DISPLAY_WIDTH || y >= OLED_DISPLAY_HEIGHT) { + return; + } + uint16_t index = x + (y / 8) * OLED_DISPLAY_WIDTH; + if (on) { + oled_buffer[index] |= (1 << (y % 8)); + } else { + oled_buffer[index] &= ~(1 << (y % 8)); + } + oled_dirty |= (1 << (index / OLED_BLOCK_SIZE)); +} + #if defined(__AVR__) void oled_write_P(const char *data, bool invert) { uint8_t c = pgm_read_byte(data); diff --git a/drivers/oled/oled_driver.h b/drivers/oled/oled_driver.h index 3e5a5bcabe..af6e5a2b61 100644 --- a/drivers/oled/oled_driver.h +++ b/drivers/oled/oled_driver.h @@ -206,6 +206,10 @@ void oled_pan(bool left); void oled_write_raw(const char *data, uint16_t size); void oled_write_raw_byte(const char data, uint16_t index); +// Sets a specific pixel on or off +// Coordinates start at top-left and go right and down for positive x and y +void oled_write_pixel(uint8_t x, uint8_t y, bool on); + #if defined(__AVR__) // Writes a PROGMEM string to the buffer at current cursor position // Advances the cursor while writing, inverts the pixels if true