From 9def1258a0a33360780f4f6cec16aadec960ebb7 Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Fri, 12 May 2023 07:46:27 +0900 Subject: [PATCH] fb: cfb: Add cfb_draw_point() API Add cfb_draw_point() API for rendering dot. Signed-off-by: TOKITA Hiroshi --- include/zephyr/display/cfb.h | 15 +++++++++++++++ subsys/fb/cfb.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/zephyr/display/cfb.h b/include/zephyr/display/cfb.h index 0d0da059d25..e7dc53b3f9c 100644 --- a/include/zephyr/display/cfb.h +++ b/include/zephyr/display/cfb.h @@ -57,6 +57,11 @@ struct cfb_font { uint8_t last_char; }; +struct cfb_position { + uint16_t x; + uint16_t y; +}; + /** * @brief Macro for creating a font entry. * @@ -104,6 +109,16 @@ int cfb_print(const struct device *dev, const char *const str, uint16_t x, uint1 */ int cfb_draw_text(const struct device *dev, const char *const str, int16_t x, int16_t y); +/** + * @brief Draw a point. + * + * @param dev Pointer to device structure for driver instance + * @param pos position of the point + * + * @return 0 on success, negative value otherwise + */ +int cfb_draw_point(const struct device *dev, const struct cfb_position *pos); + /** * @brief Clear framebuffer. * diff --git a/subsys/fb/cfb.c b/subsys/fb/cfb.c index f26fc45fd9e..010151ab8d7 100644 --- a/subsys/fb/cfb.c +++ b/subsys/fb/cfb.c @@ -183,6 +183,27 @@ static uint8_t draw_char_vtmono(const struct char_framebuffer *fb, return fptr->width; } +static inline void draw_point(struct char_framebuffer *fb, int16_t x, int16_t y) +{ + const bool need_reverse = ((fb->screen_info & SCREEN_INFO_MONO_MSB_FIRST) != 0); + const size_t index = ((y / 8) * fb->x_res); + uint8_t m = BIT(y % 8); + + if (x < 0 || x >= fb->x_res) { + return; + } + + if (y < 0 || y >= fb->y_res) { + return; + } + + if (need_reverse) { + m = byte_reverse(m); + } + + fb->buf[index + x] |= m; +} + static int draw_text(const struct device *dev, const char *const str, int16_t x, int16_t y, bool wrap) { @@ -215,6 +236,15 @@ static int draw_text(const struct device *dev, const char *const str, int16_t x, return -EINVAL; } +int cfb_draw_point(const struct device *dev, const struct cfb_position *pos) +{ + struct char_framebuffer *fb = &char_fb; + + draw_point(fb, pos->x, pos->y); + + return 0; +} + int cfb_draw_text(const struct device *dev, const char *const str, int16_t x, int16_t y) { return draw_text(dev, str, x, y, false);