diff --git a/fbdash.c b/fbdash.c index 84865d6..e88c260 100644 --- a/fbdash.c +++ b/fbdash.c @@ -34,9 +34,15 @@ #define WIDTH 240 #define HEIGHT 320 + char *fbp = 0; - +struct s_color { + unsigned char r; + unsigned char g; + unsigned char b; +}; +typedef struct s_color color; struct s_sourcedata { char* text; @@ -45,14 +51,24 @@ struct s_sourcedata { }; typedef struct s_sourcedata sourcedata; + void finish_with_error(MYSQL *con) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } +void updateSlot(char *fbp, sourcedata data, int x_offset, int y_offset, color c) { + render_string(fbp, ubuntu_mono_24, data.text, x_offset, y_offset, c.r, c.g, c.b); + + render_string(fbp, ubuntu_mono_48, data.temperature, x_offset + 115, y_offset - 9, c.r, c.g, c.b); + render_string(fbp, ubuntu_mono_48, "C", x_offset + 195, y_offset - 9, c.r, c.g, c.b); + + render_string(fbp, ubuntu_mono_48, data.humidity, x_offset + 115, y_offset + 26, c.r, c.g, c.b); + render_string(fbp, ubuntu_mono_48, "%", x_offset + 195, y_offset + 26, c.r, c.g, c.b); +} -void updateData() { +void updateData(char *fbp, int slot_height) { // query database for latest values printf("MySQL client version: %s\n", mysql_get_client_info()); MYSQL *con = mysql_init(NULL); @@ -77,11 +93,12 @@ INNER JOIN metrics AS m ON r.metric_id = m.id \ WHERE \ time > (NOW() - 60 * 15) AND \ metric IN ('Luftfeuchte','Temperatur') AND \ - source IN ('Esszimmer','Wohnzimmer') \ + source IN ('Esszimmer','Wohnzimmer', 'Schlafzimmer', 'Abstellkammer') \ ORDER BY time DESC \ LIMIT 100;"; -printf("%s\n",query); + //printf("%s\n",query); + printf("Fetching data ...\n"); if (mysql_query(con, query)) { finish_with_error(con); @@ -91,55 +108,51 @@ printf("%s\n",query); finish_with_error(con); } - sourcedata esszimmer = { "Esszimmer", NULL, NULL }; - sourcedata wohnzimmer = { "Wohnzimmer", NULL, NULL }; + // define what to display + sourcedata data[4] = { + { "Esszimmer", NULL, NULL }, + { "Wohnzimmer", NULL, NULL }, + { "Schlafzimmer", NULL, NULL }, + { "Abstellkammer", NULL, NULL } + }; int num_fields = mysql_num_fields(result); - MYSQL_ROW row; - while ((row = mysql_fetch_row(result))) { - row[3][5] = '\0'; // loose the third decimal ... + if (num_fields > 3) { + MYSQL_ROW row; + while ((row = mysql_fetch_row(result))) { + if (strlen(row[3]) > 4) row[3][4] = '\0'; // loose the secnod and third decimal ... - // data binding - if (strcmp(row[1], "Esszimmer") == 0) { - if (strcmp(row[2], "Temperatur") == 0 && esszimmer.temperature == NULL) esszimmer.temperature = row[3]; - else if (strcmp(row[2], "Luftfeuchte") == 0 && esszimmer.humidity == NULL) esszimmer.humidity = row[3]; - } - if (strcmp(row[1], "Wohnzimmer") == 0) { - if (strcmp(row[2], "Temperatur") == 0 && wohnzimmer.temperature == NULL) wohnzimmer.temperature = row[3]; - else if (strcmp(row[2], "Luftfeuchte") == 0 && wohnzimmer.humidity == NULL) wohnzimmer.humidity = row[3]; - } + // data binding + for (int i = 0; i < 4; i++) { + if (strcmp(row[1], data[i].text) == 0) { + if (strcmp(row[2], "Temperatur") == 0 && data[i].temperature == NULL) data[i].temperature = row[3]; + else if (strcmp(row[2], "Luftfeuchte") == 0 && data[i].humidity == NULL) data[i].humidity = row[3]; + } + } - // debugging - for(int i = 0; i < num_fields; i++) { - printf("%s ", row[i] ? row[i] : "NULL"); - } - printf("\n"); - } + // debugging + //for(int i = 0; i < num_fields; i++) printf("%s ", row[i] ? row[i] : "NULL"); + //printf("\n"); + } + } + + printf("Updating dashboard...\n"); + for (int i = 0; i < 4; i++) { + color c = {255,125 + 25*i,25 + 25*i}; + updateSlot(fbp, data[i], 15, 7 + slot_height * i, c); + } - render_string(fbp, ubuntu_mono_24, esszimmer.text, 15, 7, 255, 255, 255); - - render_string(fbp, ubuntu_mono_48, esszimmer.temperature, 115, -2, 255, 255, 255); - render_string(fbp, ubuntu_mono_48, "C", 200, -2, 255, 255, 255); - - render_string(fbp, ubuntu_mono_48, esszimmer.humidity, 115, 33, 255, 255, 255); - render_string(fbp, ubuntu_mono_48, "%", 200, 33, 255, 255, 255); - - - - render_string(fbp, ubuntu_mono_24, wohnzimmer.text, 15, 87, 255, 255, 255); - - render_string(fbp, ubuntu_mono_48, wohnzimmer.temperature, 115, 77, 255, 255, 255); - render_string(fbp, ubuntu_mono_48, "C", 200, 77, 255, 255, 255); - - render_string(fbp, ubuntu_mono_48, wohnzimmer.humidity, 115, 113, 255, 255, 255); - render_string(fbp, ubuntu_mono_48, "%", 200, 113, 255, 255, 255); + printf("Done!\n"); mysql_free_result(result); mysql_close(con); } + + + void draw() { clear_screen(fbp); @@ -159,14 +172,8 @@ void draw() { draw_line(fbp, x0, y+1, x1, y+1, r, g, b); } - - updateData(); -/* - char *text = "Hell O_o!"; - render_string(fbp, ubuntu_mono_48, text, 15, 0, 255, 255, 255); - render_string(fbp, ubuntu_mono_24, text, 15, 50, 255, 255, 255); - render_string(fbp, basic_8, text, 15, 100, 255, 255, 255); -*/ + // fetch data from database and update screen + updateData(fbp, y_step_width); } diff --git a/fblib.c b/fblib.c index a8c3bb2..f21cc99 100644 --- a/fblib.c +++ b/fblib.c @@ -169,10 +169,10 @@ void draw_line(char *fbp, int x0, int y0, int x1, int y1, int r, int g, int b) { void render_string(char *fbp, fbfont font, char *text, int x, int y, int r, int g, int b) { int text_length = strlen(text); - printf("text_length = %d\n", text_length); + //printf("text_length = %d\n", text_length); int bytes_per_line = ceil(font.width / 8.0); - printf("bytes_per_line = %d\n", bytes_per_line); + //printf("bytes_per_line = %d\n", bytes_per_line); int char_offset = bytes_per_line * font.height; @@ -199,7 +199,7 @@ void render_string(char *fbp, fbfont font, char *text, int x, int y, int r, int //printf("x"); int x_pos = x_offset + byte_num * 8 + bit_number; - put_pixel(fbp, x_pos, y_pos, 255, 255, 255); + put_pixel(fbp, x_pos, y_pos, r, g, b); } else { // leave empty (or maybe plot 'text backgr color')