diff options
author | Erich Eckner <git@eckner.net> | 2018-10-26 10:42:43 +0200 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2018-10-26 10:42:43 +0200 |
commit | 3d7d5934ce19203b1b795cfea6a51599a20c9639 (patch) | |
tree | 0d41be92dbb1e866ef405a49676a59153d6fd2b8 | |
parent | cab5ead7656173c415c56587bae5cf14774ecee4 (diff) | |
download | anzeige-3d7d5934ce19203b1b795cfea6a51599a20c9639.tar.xz |
longer scroll-buffer; append all messages
-rw-r--r-- | anzeige.c | 29 | ||||
-rw-r--r-- | input_gadgets.c | 99 | ||||
-rw-r--r-- | input_gadgets.h | 6 | ||||
-rw-r--r-- | multiplexer.h | 2 |
4 files changed, 78 insertions, 58 deletions
@@ -20,8 +20,9 @@ int main(int argc, char **argv) pthread_t thread_id; t_display_data display_data; int ret_val; - #define TEXT_BUFFER_LENGTH 42 + #define TEXT_BUFFER_LENGTH 256 unsigned char *text_buffer; + unsigned char *text_buffer_end; t_scroll_buffer scroll_buffers[2]; // Set up gpi pointer for direct register access @@ -51,12 +52,28 @@ int main(int argc, char **argv) } memset(text_buffer, 0, TEXT_BUFFER_LENGTH); -/* ret_val = gadgets_retrieve_weather_warnings(text_buffer,TEXT_BUFFER_LENGTH); - if (ret_val) { + text_buffer_end = text_buffer; + + text_buffer_end = gadgets_retrieve_weather_warnings(text_buffer_end,text_buffer + TEXT_BUFFER_LENGTH - text_buffer_end); + if (text_buffer_end == NULL) { + fprintf(stderr, "gadgets_retrieve_weather_warnings failed\n"); free(text_buffer); - exit(ret_val); - }*/ - strcpy(text_buffer, "ABCDEFGHIJKLMNO"); + return EXIT_FAILURE; + } + + text_buffer_end = gadgets_retrieve_current_temperature(text_buffer_end,text_buffer + TEXT_BUFFER_LENGTH - text_buffer_end); + if (text_buffer_end == NULL) { + fprintf(stderr, "gadgets_retrieve_current_temperature failed\n"); + free(text_buffer); + return EXIT_FAILURE; + } + + text_buffer_end = gadgets_retrieve_weather_forecast(text_buffer_end,text_buffer + TEXT_BUFFER_LENGTH - text_buffer_end); + if (text_buffer_end == NULL) { + fprintf(stderr, "gadgets_retrieve_weather_forecast failed\n"); + free(text_buffer); + return EXIT_FAILURE; + } scroll_buffers[0].scroll_len = render(text_buffer, TEXT_BUFFER_LENGTH, scroll_buffers[0].buf, sizeof(scroll_buffers[0].buf), 1); diff --git a/input_gadgets.c b/input_gadgets.c index 43e38b0..24c5eaa 100644 --- a/input_gadgets.c +++ b/input_gadgets.c @@ -36,7 +36,7 @@ WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) return realsize; } -int gadgets_retrieve_current_temperature(unsigned char *output, int max_len) +unsigned char *gadgets_retrieve_current_temperature(unsigned char *output, int max_len) { CURL *curl_handle; CURLcode res; @@ -52,7 +52,7 @@ int gadgets_retrieve_current_temperature(unsigned char *output, int max_len) if (!curl_handle) { perror("Failed to init curl"); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } curl_easy_setopt(curl_handle, CURLOPT_URL, "https://wetter.mb.fh-jena.de/station/datenbank/php_giese/online.php"); curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L); @@ -64,7 +64,7 @@ int gadgets_retrieve_current_temperature(unsigned char *output, int max_len) if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } regex_t re; @@ -73,7 +73,7 @@ int gadgets_retrieve_current_temperature(unsigned char *output, int max_len) { fprintf(stderr, "Failed to compile regex '%s'\n", fh_temperature_regex); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } if (ret_val = regexec(&re, chunk.memory, 5, rm, 0)) { @@ -83,7 +83,7 @@ int gadgets_retrieve_current_temperature(unsigned char *output, int max_len) fprintf(stderr, "%d %s\n",ret_val,reg_err); regfree(&re); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } regfree(&re); int i = snprintf(output, max_len, "%.*s (%.*s %.*s)", @@ -91,6 +91,7 @@ int gadgets_retrieve_current_temperature(unsigned char *output, int max_len) (int)(rm[2].rm_eo - rm[2].rm_so), (char*)(chunk.memory + rm[2].rm_so), (int)(rm[1].rm_eo - rm[1].rm_so), (char*)(chunk.memory + rm[1].rm_so) ); + unsigned char* ende = output + i; for (; i>=0; i--) if (output[i] == '(') break; @@ -98,7 +99,7 @@ int gadgets_retrieve_current_temperature(unsigned char *output, int max_len) if (output[i] == '.') output[i] = ','; free(chunk.memory); - return 0; + return ende; } xmlNode *xml_find_node_by_name(xmlNode *cur_node, char *name) @@ -121,7 +122,7 @@ xmlChar *xml_extract_string(xmlDoc *doc, xmlNode *cur_node, char *name) return xmlNodeListGetString(doc, sub_node -> xmlChildrenNode, 0); } -int gadgets_retrieve_weather_forecast(unsigned char *output, int max_len) +unsigned char *gadgets_retrieve_weather_forecast(unsigned char *output, int max_len) { CURL *curl_handle; CURLcode res; @@ -137,7 +138,7 @@ int gadgets_retrieve_weather_forecast(unsigned char *output, int max_len) if (!curl_handle) { perror("Failed to init curl"); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } curl_easy_setopt(curl_handle, CURLOPT_URL, "https://api.met.no/weatherapi/locationforecast/1.9/?lat=50.8830&lon=11.6223&msl=170"); curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L); @@ -149,7 +150,7 @@ int gadgets_retrieve_weather_forecast(unsigned char *output, int max_len) if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } LIBXML_TEST_VERSION @@ -159,7 +160,7 @@ int gadgets_retrieve_weather_forecast(unsigned char *output, int max_len) free(chunk.memory); if (doc == NULL) { fprintf(stderr, "Failed to parse document\n"); - return EXIT_FAILURE; + return NULL; } xmlNode *cur_node = NULL; @@ -167,17 +168,17 @@ int gadgets_retrieve_weather_forecast(unsigned char *output, int max_len) cur_node = xmlDocGetRootElement(doc); if (!cur_node) { fprintf(stderr, "Failed to reach root node of xml\n"); - return EXIT_FAILURE; + return NULL; } cur_node = xml_find_node_by_name(cur_node, "weatherdata"); if (!cur_node) { fprintf(stderr, "Failed to enter <weatherdata>\n"); - return EXIT_FAILURE; + return NULL; } cur_node = xml_find_node_by_name(cur_node -> children, "product"); if (!cur_node) { fprintf(stderr, "Failed to enter <product>\n"); - return EXIT_FAILURE; + return NULL; } float rain_total = 0, temp_min = NAN, temp_max = NAN, wind_max = NAN, cur_val; @@ -222,16 +223,17 @@ int gadgets_retrieve_weather_forecast(unsigned char *output, int max_len) xmlCleanupParser(); int i = snprintf(output, max_len, "%0.1f ,, %0.1f °C; %0.1f mm; %0.1f m/s;", temp_min, temp_max, rain_total, wind_max); + unsigned char *ende = output + i; for (; i>=0; i--) if (output[i] == '.') output[i] = ','; else if (output[i] == ',') output[i] = '.'; - return EXIT_SUCCESS; + return ende; } -int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) +unsigned char *gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) { CURL *curl_handle; CURLcode res; @@ -247,7 +249,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) if (!curl_handle) { perror("Failed to init curl"); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } curl_easy_setopt(curl_handle, CURLOPT_URL, "https://opendata.dwd.de/weather/alerts/cap/COMMUNEUNION_DWD_STAT/Z_CAP_C_EDZW_LATEST_PVW_STATUS_PREMIUMDWD_COMMUNEUNION_DE.zip"); curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L); @@ -259,7 +261,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } zip_error_t error; @@ -273,14 +275,14 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) fprintf(stderr, "can't create zip source: %s\n", zip_error_strerror(&error)); zip_error_fini(&error); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } if ((za = zip_open_from_source(src, 0, &error)) == NULL) { fprintf(stderr, "can't open zip from source: %s\n", zip_error_strerror(&error)); zip_source_free(src); zip_error_fini(&error); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } zip_error_fini(&error); @@ -291,7 +293,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) if (warnings == NULL) { fprintf(stderr, "malloc failed allocating %d bytes\n", sizeof(warning_t)); free(chunk.memory); - return EXIT_FAILURE; + return NULL; } for (int i=0; i<zip_get_num_entries(za, 0); i++) { @@ -307,7 +309,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) free(warnings[i].event); } free(warnings); - return EXIT_FAILURE; + return NULL; } int read = zip_fread(zf, inflated, zs.size); zip_fclose(zf); @@ -320,7 +322,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) free(warnings[i].event); } free(warnings); - return EXIT_FAILURE; + return NULL; } xmlDocPtr doc; @@ -334,7 +336,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) free(warnings[i].event); } free(warnings); - return EXIT_FAILURE; + return NULL; } xmlNode *cur_node = NULL; @@ -352,7 +354,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } cur_node = xml_find_node_by_name(cur_node, "alert"); if (cur_node == NULL) { @@ -365,7 +367,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } cur_node = cur_node -> children; text = xml_extract_string(doc, cur_node, "status"); @@ -379,7 +381,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } if (strcmp(text, "Actual") != 0) { // Warning is not an Actual warning printf("no warning, but '%s'\n",text); @@ -400,7 +402,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } if (strcmp(text, "Update") == 0) warnings[warnings_len].is_update=1; @@ -423,7 +425,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } cur_node = xml_find_node_by_name(cur_node, "info"); if (cur_node == NULL) { @@ -436,7 +438,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } cur_node = cur_node -> children; text = xml_extract_string(doc, cur_node, "category"); @@ -450,7 +452,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } if (strcmp(text,"Met") == 0) warnings[warnings_len].category = 0; @@ -469,7 +471,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } text = xml_extract_string(doc, cur_node, "responseType"); if (!text) { @@ -482,7 +484,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } if ((strcmp(text,"Prepare") != 0) && (strcmp(text,"None")!=0)) { fprintf(stderr, "responseType is '%s', not Prepare nor None\n", text); @@ -501,7 +503,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } text = xml_extract_string(doc, cur_node, "severity"); if (!text) { @@ -514,7 +516,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } if (strcmp(text,"Minor") == 0) warnings[warnings_len].severity = 0; @@ -535,7 +537,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } xmlFree(text); struct tm time_struct; @@ -550,7 +552,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } if (strptime(text,"%FT%T+",&time_struct) == NULL) { fprintf(stderr, "Failed to parse time '%s'\n",text); @@ -563,7 +565,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } xmlFree(text); warnings[warnings_len].onset = mktime(&time_struct); @@ -580,7 +582,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } xmlFree(text); warnings[warnings_len].expires = mktime(&time_struct); @@ -602,7 +604,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } if (strcmp(text,"WARNCELLID")!=0) { xmlFree(text); @@ -620,7 +622,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } if (strcmp(text,"816053000")==0) // Stadt Jena is_relevant = 1; @@ -638,7 +640,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } /* if (point_is_inside_polygon(50.8830, 11.6223, text)) <<TODO>> is_relevant = 1; */ @@ -659,7 +661,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) } free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } warnings[warnings_len].headline = malloc(strlen(text)+1); if (warnings[warnings_len].headline == NULL) { @@ -672,7 +674,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) free(warnings); free(chunk.memory); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } strcpy(warnings[warnings_len].headline, text); xmlFree(text); @@ -689,7 +691,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) free(warnings[warnings_len].headline); free(warnings); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } warnings[warnings_len].event = malloc(strlen(text)+1); if (warnings[warnings_len].event == NULL) { @@ -703,7 +705,7 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) free(warnings); free(chunk.memory); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } strcpy(warnings[warnings_len].event, text); xmlFree(text); @@ -719,19 +721,20 @@ int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len) free(warnings); free(chunk.memory); xmlFreeDoc(doc); - return EXIT_FAILURE; + return NULL; } free(inflated); xmlFreeDoc(doc); } free(chunk.memory); + unsigned char *ende = output; for (int i=0; i<warnings_len; i++) { - snprintf(output, max_len, "%s: %s ", warnings[i].headline, warnings[i].event); + ende += snprintf(ende, output + max_len - ende, "%s: %s ", warnings[i].headline, warnings[i].event); free(warnings[i].headline); free(warnings[i].event); } - return EXIT_SUCCESS; + return ende; } diff --git a/input_gadgets.h b/input_gadgets.h index ec6aab3..37542f6 100644 --- a/input_gadgets.h +++ b/input_gadgets.h @@ -12,6 +12,6 @@ typedef struct { time_t onset, expires; } warning_t; -int gadgets_retrieve_current_temperature(unsigned char *output, int max_len); -int gadgets_retrieve_weather_forecast(unsigned char *output, int max_len); -int gadgets_retrieve_weather_warnings(unsigned char *output, int max_len); +unsigned char *gadgets_retrieve_current_temperature(unsigned char *output, int max_len); +unsigned char *gadgets_retrieve_weather_forecast(unsigned char *output, int max_len); +unsigned char *gadgets_retrieve_weather_warnings(unsigned char *output, int max_len); diff --git a/multiplexer.h b/multiplexer.h index 77d3232..f42fb8d 100644 --- a/multiplexer.h +++ b/multiplexer.h @@ -32,7 +32,7 @@ typedef struct { } t_display_data; typedef struct { - unsigned char buf[256]; + unsigned char buf[2048]; int scroll_len; } t_scroll_buffer; |