From bf8b16d036e60eec56a42cbfcf012390c143e699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Mon, 27 May 2019 23:12:42 +0300 Subject: Screen: implementation of drawing and trivial docs. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ramūnas Mažeikis --- src/device/device_network.h | 0 src/device/screen.c | 64 ----------------------------------------- src/device/screen.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++ src/device/screen.h | 23 +++++++++++++-- 4 files changed, 91 insertions(+), 66 deletions(-) mode change 100755 => 100644 src/device/device_network.h delete mode 100755 src/device/screen.c create mode 100644 src/device/screen.cpp mode change 100755 => 100644 src/device/screen.h diff --git a/src/device/device_network.h b/src/device/device_network.h old mode 100755 new mode 100644 diff --git a/src/device/screen.c b/src/device/screen.c deleted file mode 100755 index ac42076..0000000 --- a/src/device/screen.c +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#include -#include "screen.h" - -void draw_lines(struct display_status *status); -void update_lines(struct display_status *status); -void init_msg(char *msg, size_t size); - -void display_status_init(struct display_status *status, char *msg) -{ - status->delta = 2; /* Currently default */ - init_msg(msg, strlen(msg)); - status->message = msg; - status->line_cursor = 0; - status->last_scroll_time = time(NULL); - update_lines(status); -} - -/** - * Turns all whitespace into literal spaces to save screen real-estate. - */ -void init_msg(char *msg, size_t size) -{ - size_t i; - - for (i = 0; i < size; i++) { - switch (msg[i]) { - case '\n': - case '\t': - case '\r': - msg[i] = ' '; - break; - case '\0': - goto end; - default: - break; - } - } -end: - return; -} - -void display_update_scroll(struct display_status *status) -{ - time_t crr_time = time(NULL); - if (status->last_scroll_time - crr_time > status->delta) { - status->last_scroll_time += status->delta; - status->line_cursor++; - update_lines(status); - } - draw_lines(status); -} - -void draw_lines(struct display_status *status) -{ - /* TODO */ -} - -void update_lines(struct display_status *status) -{ - status->first_line = get_line(status, status->line_cursor); - status->second_line = get_line(status, status->line_cursor + 1); -} \ No newline at end of file diff --git a/src/device/screen.cpp b/src/device/screen.cpp new file mode 100644 index 0000000..cfabcc5 --- /dev/null +++ b/src/device/screen.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include "screen.h" + +void draw_lines(struct display_status *status); +void update_lines(struct display_status *status); +void init_msg(char *msg, size_t size); + +void display_status_init(struct display_status *status, char *msg) +{ + status->delta = 2; /* Currently default */ + init_msg(msg, strlen(msg)); + status->message = msg; + status->line_cursor = 0; + status->last_scroll_time = time(NULL); + update_lines(status); +} + +/** + * Turns all whitespace into literal spaces to save screen real-estate and + * possible misinterpretation. + */ +void init_msg(char *msg, size_t size) +{ + size_t i; + + for (i = 0; i < size; i++) { + switch (msg[i]) { + case '\n': + case '\t': + case '\r': + msg[i] = ' '; + break; + case '\0': + goto end; + default: + break; + } + } +end: + return; +} + +void display_update_scroll(struct display_status *status) +{ + time_t crr_time = time(NULL); + /* Only scroll lines once a delta, because --- duh! */ + if (status->last_scroll_time - crr_time > status->delta) { + status->last_scroll_time += status->delta; + status->line_cursor++; + update_lines(status); + draw_lines(status); + } +} + +void draw_lines(SSD1306Brzo *screen, struct display_status *status) +{ + screen->clear(); + screen->drawString(0, 0, status->first_line); + screen->drawString(0, SCREEN_HEIGHT / 2, status->second_line); +} + +void update_lines(struct display_status *status) +{ + status->first_line = get_line(status, status->line_cursor); + status->second_line = get_line(status, status->line_cursor + 1); +} \ No newline at end of file diff --git a/src/device/screen.h b/src/device/screen.h old mode 100755 new mode 100644 index 7763bd2..54734e5 --- a/src/device/screen.h +++ b/src/device/screen.h @@ -1,9 +1,17 @@ +/** + * Simple API for scrolling lines. Keeps things simple by not even assuming + * which screen is being drawn on. + */ + #ifndef DEVICE_SCREEN_H #define DEVICE_SCREEN_H #include +#include +#include -#define SCREEN_WIDTH (64) +#define SCREEN_WIDTH (128) +#define SCREEN_HEIGHT (32) #define FONT_WIDTH (8) #define SCREEN_MAX_CHARS (SCREEN_WIDTH / FONT_WIDTH) @@ -11,6 +19,7 @@ * Struct that keeps track of the lines on the screen. */ struct display_status { + SSD1306Brzo *screen; /* Screen to draw on. */ time_t delta; /* Seconds/Line */ time_t last_scroll_time; /* Last second the line was scrolled */ char *message; /* Entire message to be shown */ @@ -25,6 +34,16 @@ struct display_status { */ void display_update_scroll(struct display_status *status); -void display_status_init(struct display_status *status, char *msg); +/** + * Initialises display_status structure so it can be used for + * display_update_scroll. + * + * screen - screen to draw on + * + * status - structure to Initialise + * + * msg - message to scroll on the screen + */ +void display_status_init(SSD1306Brzo *screen, struct display_status *status, char *msg); #endif /* DEVICE_SCREEN_H */ \ No newline at end of file -- cgit v1.2.3