diff options
author | 2019-06-09 23:08:36 +0300 | |
---|---|---|
committer | 2019-06-09 23:08:36 +0300 | |
commit | 7c5042af3c606081d3f4b917ef667f6ed05780a8 (patch) | |
tree | d6f7adcaca6cb22ca44002b41dba43e069ffbebd /src/device | |
parent | 613622e8cb3c1d32c50e19d7f446d0c00c91250e (diff) | |
download | usurpation-7c5042af3c606081d3f4b917ef667f6ed05780a8.tar.gz usurpation-7c5042af3c606081d3f4b917ef667f6ed05780a8.tar.bz2 usurpation-7c5042af3c606081d3f4b917ef667f6ed05780a8.zip |
hook various previously unused modules together.
This now allows us to send messages to the device and such. Yay? Yay.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/device')
-rw-r--r-- | src/device/device_network.cpp | 21 | ||||
-rw-r--r-- | src/device/device_network.h | 3 | ||||
-rw-r--r-- | src/device/main.ino | 90 | ||||
-rw-r--r-- | src/device/screen.cpp | 121 | ||||
-rw-r--r-- | src/device/screen.h | 30 |
5 files changed, 147 insertions, 118 deletions
diff --git a/src/device/device_network.cpp b/src/device/device_network.cpp index e526519..bd8467b 100644 --- a/src/device/device_network.cpp +++ b/src/device/device_network.cpp @@ -63,13 +63,17 @@ int udp_flush(void) size_t udp_get_data(char *buf, size_t size) { size_t ret; + + state.udp.parsePacket(); + if ((ret = state.udp.available())) { state.udp.read(buf, size); } + return ret; } -void discover_client(const int port) +IPAddress *discover_client(const int port) { IPAddress bcastip(255, 255, 255, 255); char buffer[128] = {0}; @@ -92,20 +96,19 @@ void discover_client(const int port) delay(5); expected_s = sizeof(field.head) + sizeof(heartbeat_server); - while (state.udp.parsePacket()) { - if (state.udp.available() >= expected_s) { - state.udp.read(buffer, sizeof(buffer)); - tlv_get(buffer, &field, NULL); - if (field.head.type == HEARTBEAT && !(strcmp(heartbeat_server, field.data))) { - state.daemon_ip = state.udp.remoteIP(); - ++state.acquired; - } + if (udp_get_data(buffer, sizeof(buffer)) >= expected_s) { + tlv_get(buffer, &field, NULL); + if (field.head.type == HEARTBEAT && !(strcmp(heartbeat_server, field.data))) { + state.daemon_ip = state.udp.remoteIP(); + ++state.acquired; } } delay(95); } while (!state.acquired); tlv_packet_destroy(&pack); + + return &state.daemon_ip; } IPAddress *get_daemon_address(void) diff --git a/src/device/device_network.h b/src/device/device_network.h index ae719f0..dbe77c2 100644 --- a/src/device/device_network.h +++ b/src/device/device_network.h @@ -32,7 +32,6 @@ void udp_init_packet(const int port); void udp_push(const void * const data, const size_t size); int udp_flush(void); size_t udp_get_data(char *buf, size_t size); -void discover_client(const int port); -IPAddress *get_daemon_address(void); +IPAddress *discover_client(const int port); #endif /* DEVICE_UDP_H */ diff --git a/src/device/main.ino b/src/device/main.ino index fc307f9..49cb0e7 100644 --- a/src/device/main.ino +++ b/src/device/main.ino @@ -31,9 +31,16 @@ #include "utils.h" #include "tlv.h" +#define seconds(a) (((a) * 1000) / (period)) + static const unsigned int internal_led = 2; static unsigned int led_state = 0; -static char *fugg = "O Fugg, ids an errror :DDDDD"; +static char fugg[] = "error :3ccc"; +static const char devstr[] = "Device IP:"; +static const char daemonstr[] = "Daemon IP:"; +static char ipstr[] = " "; +static unsigned int period = 1000; + SSD1306Wire display(0x3c, 4, 5, GEOMETRY_128_32); static void init_OLED(void); @@ -47,9 +54,9 @@ static struct progstate_t { struct display_status ds; struct tlv field; struct tlv_packet heartbeat; - size_t bytes_read; char buf[MTU]; char hbcounter; + IPAddress *daemon_ip; } progstate; void setup(void) @@ -61,93 +68,84 @@ void setup(void) pinMode(internal_led, OUTPUT); toggle_led(internal_led); init_OLED(); + display.fillCircle(32, 16, 12); display.display(); + wifi_connect(ssid, password, 1, internal_led); udp_init(com_port); + display.fillCircle(64, 16, 12); display.display(); - discover_client(com_port); + + progstate.daemon_ip = discover_client(com_port); + display.fillCircle(92, 16, 12); display.display(); - progstate.ip_print_count = 5; + tlv_init(&field, HEARTBEAT); field.data = strdup(heartbeat_device); field.head.size = sizeof(heartbeat_device); + tlv_packet_init(&progstate.heartbeat); tlv_pack(&progstate.heartbeat, &field); tlv_packet_finalize(&progstate.heartbeat); tlv_destroy(&field); - display_status_init(&display, &progstate.ds, fugg); + + progstate.ip_print_count = 5; } void loop(void) { - static const String devstr = "Device IP:"; - static const String daemonstr = "Daemon IP:"; - static String prefix; - static IPAddress ip_to_print; - static IPAddress *daemon_ip = NULL; -#if 0 - char *saveptr; -#endif - - static unsigned int delta = 2000; /* sleep length to use (ms) */ - - delay(delta); + static int displayed = END_OF_MESSAGE; + String current_ip; + + delay(period); + if (!progstate.hbcounter) { udp_init_packet(com_port); udp_push(progstate.heartbeat.data, progstate.heartbeat.cursor + 1); udp_flush(); - progstate.hbcounter = 5; + progstate.hbcounter = seconds(30); } - if (progstate.ip_print_count) { - /* Initial display of ip's. */ - if (!daemon_ip) { - daemon_ip = get_daemon_address(); - } - - prefix = (progstate.ip_print_count % 2) ? devstr : daemonstr; - ip_to_print = (progstate.ip_print_count % 2) ? WiFi.localIP() : *daemon_ip; - display.clear(); - display.drawString(0, 0, prefix); - display.drawString(0, 16, ip_to_print.toString()); - display.display(); - progstate.ip_print_count--; - } else { - progstate.bytes_read = udp_get_data(progstate.buf, sizeof(progstate.buf)); - if (progstate.bytes_read > 0) { + /* Initial display of ips. */ + if (!progstate.ip_print_count) { + if (udp_get_data(progstate.buf, sizeof(progstate.buf))) { tlv_get(progstate.buf, &progstate.field, NULL); handle_tlv(&progstate.field); -#if 0 - /* Dealing with tlv's one at a time. */ - saveptr = progstate.buf; - while (!(tlv_get(saveptr, &progstate.field, &saveptr))) { - handle_tlv(&progstate.field); - } -#endif } - - display_update_scroll(&progstate.ds); + } else if (displayed == END_OF_MESSAGE) { + current_ip = (progstate.ip_print_count % 2) ? WiFi.localIP().toString() : progstate.daemon_ip->toString(); + memcpy(ipstr, (progstate.ip_print_count % 2) ? devstr : daemonstr, strlen(devstr)); + memcpy(ipstr + 16, current_ip.c_str(), strlen(current_ip.c_str())); + display_status_init(&display, &progstate.ds, ipstr, strlen(ipstr), seconds(1)); + progstate.ip_print_count--; } + displayed = display_update_scroll(&progstate.ds); --progstate.hbcounter; } void handle_tlv(const struct tlv *in) { - static char *fugg = "O Fugg, ids an errror :DDDDD"; /* Currently just dealing with text. * */ switch (in->head.type) { case TEXT: - display_status_init(&display, &progstate.ds, in->data); + if (in->head.size) { + display_status_init(&display, &progstate.ds, in->data, in->head.size, seconds(3)); + } + case INVALID: + case HEARTBEAT: break; default: - display_status_init(&display, &progstate.ds, fugg); + display_status_init(&display, &progstate.ds, fugg, sizeof(fugg), seconds(5)); break; } + + /* clear the tlv after handling it */ + progstate.field = tlv_none; } static void init_OLED(void) diff --git a/src/device/screen.cpp b/src/device/screen.cpp index cf1428a..bc25d65 100644 --- a/src/device/screen.cpp +++ b/src/device/screen.cpp @@ -1,83 +1,104 @@ -#include <time.h> #include <stdlib.h> #include <string.h> #include <OLEDDisplay.h> #include <Wire.h> #include "screen.h" -void draw_lines(OLEDDisplay *screen, struct display_status *status); -void update_lines(struct display_status *status); -void init_msg(char *msg, size_t size); +static void draw_lines(OLEDDisplay *screen, struct display_status *status); +static void update_lines(struct display_status *status); +static void sanitize_msg(char *msg); -static char empty_string[] = ""; - -void display_status_init(OLEDDisplay *screen, struct display_status *status, char *msg) +void display_status_init(OLEDDisplay *screen, struct display_status *status, char *msg, size_t size, unsigned long int period) { - status->delta = 2000; /* Currently default */ + status->period = period; status->screen = screen; - init_msg(msg, strlen(msg)); - status->message = msg; - status->line_cursor = 0; - status->last_scroll_time = time(NULL); + size = MESSAGE_MAX < size ? MESSAGE_MAX : size; + memset(status->message, 0, size + 1); + memcpy(status->message, msg, size); + status->msg_len = size; + sanitize_msg(status->message); + status->cursor = status->message; + status->remaining = status->msg_len; update_lines(status); + draw_lines(status->screen, status); + status->last_scroll_time = millis(); +} + +int display_update_scroll(struct display_status *status) +{ + unsigned long crr_time; + int ret = END_OF_MESSAGE; + + if (status->remaining) { + crr_time = millis(); + /* Only scroll lines once a period, because --- duh! */ + if ((crr_time - status->last_scroll_time) > status->period) { + status->last_scroll_time = crr_time; + update_lines(status); + draw_lines(status->screen, status); + } + ret = 0; + } + + return ret; } /** - * Turns all whitespace into literal spaces to save screen real-estate and - * possible misinterpretation. + * Turns all whitespace and special characters into literal spaces to save + * screen real-estate and possible misinterpretation. */ -void init_msg(char *msg, size_t size) +static void sanitize_msg(char *msg) { - size_t i; - - for (i = 0; i < size; i++) { - switch (msg[i]) { + do { + switch (*msg) { + case '\f': case '\n': - case '\t': case '\r': - msg[i] = ' '; + case '\t': + case '\v': + *msg = ' '; break; - case '\0': - goto end; default: + if (*msg < 0x20 || *msg > 0x7f) { + *msg = '?'; + } break; } - } + } while (*(++msg)); end: return; } -int display_update_scroll(struct display_status *status) +static void draw_lines(OLEDDisplay *screen, struct display_status *status) { - unsigned long crr_time = millis(); - /* 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->screen, status); - } - if (!(*status->first_line) && !(*(status->second_line))) { - return END_OF_MESSAGE; - } else { - return 0; + size_t i; + screen->clear(); + + for (i = 0; i < SCREEN_LINE_COUNT; ++i) { + screen->drawString(0, i * FONT_HEIGHT, status->lines[i]); } -} -void draw_lines(OLEDDisplay *screen, struct display_status *status) -{ - screen->clear(); - screen->drawString(0, 0, status->first_line); - screen->drawString(0, SCREEN_HEIGHT / 2, status->second_line); screen->display(); } -void update_lines(struct display_status *status) +static void update_lines(struct display_status *status) { - status->first_line = (status->line_cursor * SCREEN_MAX_CHARS < status->message_len) - ? status->message + status->line_cursor * SCREEN_MAX_CHARS - : empty_string; - status->second_line = (status->line_cursor * SCREEN_MAX_CHARS < status->message_len) - ? status->message + (status->line_cursor + 1) * SCREEN_MAX_CHARS - : empty_string; + size_t i; + + if (status->remaining) { + + memset(status->lines, 0, sizeof(status->lines)); + + for (i = 0; i < SCREEN_LINE_COUNT; ++i) { + if (status->remaining > SCREEN_LINE_CHARS) { + memcpy(status->lines[i], status->cursor, SCREEN_LINE_CHARS); + status->cursor += SCREEN_LINE_CHARS; + status->remaining -= SCREEN_LINE_CHARS; + } else if (status->remaining) { + memcpy(status->lines[i], status->cursor, status->remaining); + status->cursor += status->remaining; + status->remaining = 0; + } + } + } } diff --git a/src/device/screen.h b/src/device/screen.h index 5d0e3b3..3efff6d 100644 --- a/src/device/screen.h +++ b/src/device/screen.h @@ -13,20 +13,24 @@ #define SCREEN_WIDTH (128) #define SCREEN_HEIGHT (32) #define FONT_WIDTH (8) -#define SCREEN_MAX_CHARS (SCREEN_WIDTH / FONT_WIDTH) +#define FONT_HEIGHT (16) +#define SCREEN_LINE_COUNT (SCREEN_HEIGHT / FONT_HEIGHT) +#define SCREEN_LINE_CHARS (SCREEN_WIDTH / FONT_WIDTH) +#define SCREEN_TOTAL_CHARS (SCREEN_LINE_COUNT * SCREEN_LINE_CHARS) +#define MESSAGE_MAX (1023) /** * Struct that keeps track of the lines on the screen. */ struct display_status { - OLEDDisplay *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 */ - char *first_line; /* First line on display */ - char *second_line; /* Second line on display */ - size_t message_len; /* Length of the message */ - size_t line_cursor; /* Index of the first line being displayed. */ + OLEDDisplay *screen; + unsigned long period; /* milliseconds/Line */ + unsigned long last_scroll_time; + char message[MESSAGE_MAX + 1]; /* Entire message to be shown */ + char lines[SCREEN_LINE_COUNT][SCREEN_LINE_CHARS + 1]; + char *cursor; /* the begining of the *next* location in the message to be shown*/ + size_t msg_len; + size_t remaining; /* offset of the next part of text to be shown */ }; /** @@ -44,7 +48,11 @@ int display_update_scroll(struct display_status *status); * status - structure to Initialise * * msg - message to scroll on the screen + * + * size - length of the message + * + * period - time between [elder] scrolls */ -void display_status_init(OLEDDisplay *screen, struct display_status *status, char *msg); +void display_status_init(OLEDDisplay *screen, struct display_status *status, char *msg, size_t size, unsigned long int period); -#endif /* DEVICE_SCREEN_H */
\ No newline at end of file +#endif /* DEVICE_SCREEN_H */ |