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/screen.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/device/screen.cpp (limited to 'src/device/screen.cpp') 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 -- cgit v1.2.3 From f267021479ab1f020c7956378d5bf23405ee3e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 28 May 2019 20:00:23 +0300 Subject: Screen: adjustments, bug fixes and abstraction. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Line drawing code no longer assumes a particular screen as long as it's interface includes OLEDDisplay. Signed-off-by: Ramūnas Mažeikis --- src/device/screen.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'src/device/screen.cpp') diff --git a/src/device/screen.cpp b/src/device/screen.cpp index cfabcc5..c923b99 100644 --- a/src/device/screen.cpp +++ b/src/device/screen.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include "screen.h" @@ -9,6 +9,9 @@ void draw_lines(struct display_status *status); void update_lines(struct display_status *status); void init_msg(char *msg, size_t size); +/* Effectively const. For type safety reasons. */ +static char NOTHING[] = {'\0'}; + void display_status_init(struct display_status *status, char *msg) { status->delta = 2; /* Currently default */ @@ -44,7 +47,7 @@ end: return; } -void display_update_scroll(struct display_status *status) +int display_update_scroll(struct display_status *status) { time_t crr_time = time(NULL); /* Only scroll lines once a delta, because --- duh! */ @@ -54,9 +57,14 @@ void display_update_scroll(struct display_status *status) update_lines(status); draw_lines(status); } + if (status->first_line == NOTHING && status->second_line == NOTHING) { + return END_OF_MESSAGE; + } else { + return 0; + } } -void draw_lines(SSD1306Brzo *screen, struct display_status *status) +void draw_lines(OLEDDisplay *screen, struct display_status *status) { screen->clear(); screen->drawString(0, 0, status->first_line); @@ -65,6 +73,10 @@ void draw_lines(SSD1306Brzo *screen, struct display_status *status) 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); + status->first_line = (status->line_cursor * SCREEN_MAX_CHARS < status->message_len) + ? status->message + status->line_cursor * SCREEN_MAX_CHARS + : NOTHING; + status->second_line = (status->line_cursor * SCREEN_MAX_CHARS < status->message_len) + ? status->message + (status->line_cursor + 1) * SCREEN_MAX_CHARS + : NOTHING; } \ No newline at end of file -- cgit v1.2.3 From 227a0e12ee262dbabdd8d988fec194273cf90029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Wed, 29 May 2019 14:33:16 +0300 Subject: Tweaked function declarations and build files to make it build. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ramūnas Mažeikis --- src/device/screen.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/device/screen.cpp') diff --git a/src/device/screen.cpp b/src/device/screen.cpp index c923b99..2857161 100644 --- a/src/device/screen.cpp +++ b/src/device/screen.cpp @@ -5,16 +5,17 @@ #include #include "screen.h" -void draw_lines(struct display_status *status); +void draw_lines(OLEDDisplay *screen, struct display_status *status); void update_lines(struct display_status *status); void init_msg(char *msg, size_t size); /* Effectively const. For type safety reasons. */ static char NOTHING[] = {'\0'}; -void display_status_init(struct display_status *status, char *msg) +void display_status_init(OLEDDisplay *screen, struct display_status *status, char *msg) { status->delta = 2; /* Currently default */ + status->screen = screen; init_msg(msg, strlen(msg)); status->message = msg; status->line_cursor = 0; @@ -55,7 +56,7 @@ int display_update_scroll(struct display_status *status) status->last_scroll_time += status->delta; status->line_cursor++; update_lines(status); - draw_lines(status); + draw_lines(status->screen, status); } if (status->first_line == NOTHING && status->second_line == NOTHING) { return END_OF_MESSAGE; @@ -79,4 +80,4 @@ void update_lines(struct display_status *status) status->second_line = (status->line_cursor * SCREEN_MAX_CHARS < status->message_len) ? status->message + (status->line_cursor + 1) * SCREEN_MAX_CHARS : NOTHING; -} \ No newline at end of file +} -- cgit v1.2.3