瀏覽代碼

Merge branch 'Twitch' into TwitterFeed

# Conflicts:
#	main.c
MalekSediqi 9 年之前
父節點
當前提交
0dec5d56ad
共有 8 個文件被更改,包括 103 次插入11 次删除
  1. 55 4
      contentparser.c
  2. 1 0
      contentparser.h
  3. 17 0
      displayHandler.c
  4. 4 0
      displayHandler.h
  5. 8 2
      jsmn.c
  6. 1 1
      jsmn.h
  7. 16 3
      main.c
  8. 1 1
      network.c

+ 55 - 4
contentparser.c

@@ -8,6 +8,9 @@
 #include "rtc.h"
 #include "alarm.h"
 #include "displayHandler.h"
+
+int streamid;
+
 void parseAlarmJson(char* content){
     int r;
     int i = 2;
@@ -59,11 +62,11 @@ void parseAlarmJson(char* content){
             }else if (jsoneq(content, &token[i], "port") == 0) {
                 port = getIntegerToken(content, &token[i + 1]);
             }else if (jsoneq(content, &token[i], "ip") == 0) {
-                getStringToken(content, &token[i + 1], ip);
+                getStringToken(content, &token[i + 1], ip, 24);
             }else if (jsoneq(content, &token[i], "url") == 0) {
-                getStringToken(content, &token[i + 1], url);
+                getStringToken(content, &token[i + 1], url, 24);
             }else if (jsoneq(content, &token[i], "name") == 0) {
-                getStringToken(content, &token[i + 1], name);
+                getStringToken(content, &token[i + 1], name, 16);
             }else if (jsoneq(content, &token[i], "st") == 0) {
                 st = getIntegerToken(content, &token[i + 1]);
                 i+=2;
@@ -99,10 +102,58 @@ void parseAlarmJson(char* content){
 
 void parsetimezone(char* content)
 {
-    int timezone = atoi(content);
+    int timezone = atoi(content); //parsing string to int (only works when everything is int)
     setTimeZone(timezone);
 }
 
+void parseTwitch(char* content) {
+    if (!strcmp("null", content)) {
+        printf("Nobody is streaming");
+        return;
+    }
+    int r;
+    int i;
+    jsmn_parser p;
+    jsmntok_t token[20]; /* We expect no more than 20 tokens */
+
+    jsmn_init(&p);
+    r = jsmn_parse(&p, content, strlen(content), token, sizeof(token) / sizeof(token[0]));
+    if (r <= 0) {
+        printf("Failed to parse JSON: %d \n", r);
+        return;
+    } else {
+        printf("Aantal tokens found: %d \n", r);
+    }
+
+    char name[20];
+    char title[30];
+    char game[20];
+    memset(name, 0, 20);
+    memset(title, 0, 30);
+    memset(game, 0, 20);
+
+    for (i = 1; i < r; i++) {
+        if (jsoneq(content, &token[i], "Name") == 0) {
+            getStringToken(content, &token[i + 1], name, 20);
+            i++;
+        }
+        else if (jsoneq(content, &token[i], "Title") == 0) {
+            getStringToken(content, &token[i + 1], title, 30);
+            i++;
+        }
+        else if (jsoneq(content, &token[i], "Game") == 0) {
+            getStringToken(content, &token[i + 1], game, 20);
+            i++;
+        }
+        else if (jsoneq(content, &token[i], "Date") == 0) {
+            //convert date to int
+        }
+    }
+
+    printf("%s - %s - %s", name, title, game);
+
+    displayTwitch(name, title, game);
+}
 void TwitterParser(char* content)
 {
     char* tweet = content;

+ 1 - 0
contentparser.h

@@ -6,6 +6,7 @@
 
 void parseAlarmJson(char* content);
 void parsetimezone(char* content);
+void parseTwitch(char* content);
 void TwitterParser(char* content);
 
 #endif //CONTENTPARSER_H

+ 17 - 0
displayHandler.c

@@ -19,6 +19,8 @@
 #define MONTH_OFFSET 1
 #define YEAR_OFFSET 1900
 
+bool displayingCustomMessage = false;
+
 void (*write_display_ptr[2])(char*, int) = {LcdArrayLineOne, LcdArrayLineTwo};
 
 void displayTime(int line_number){
@@ -130,4 +132,19 @@ void displayTwitter(int lineNumber,char text[])
 }
 
 
+void displayTwitch(char name[], char title[], char game[])
+{
+    displayingCustomMessage = true;
+    ClearLcd();
+    LcdArrayLineOne(name, strlen(name));
+    LcdArrayLineTwo("Streaming", 9);
+    LcdBackLight(LCD_BACKLIGHT_ON);
+}
 
+bool isDisplayingCustomMessage(){
+    return displayingCustomMessage;
+}
+
+void setDisplayingCustomMessage(bool value){
+    displayingCustomMessage = value;
+}

+ 4 - 0
displayHandler.h

@@ -4,10 +4,14 @@
 
 #ifndef MUTLI_OS_BUILD_DISPLAYHANDLER_H
 #define MUTLI_OS_BUILD_DISPLAYHANDLER_H
+#include "ntp.h"
 
 void displayTime(int);
 void displayDate(int);
 void displayAlarm(int line_number, int line_numberTwo, int idx);
 void displayVolume(int pos);
+void displayTwitch(char name[], char title[], char game[]);
+bool isDisplayingCustomMessage();
+void setDisplayingCustomMessage(bool value);
 void displayTwitter(int lineNumber,char text[]);
 #endif //MUTLI_OS_BUILD_DISPLAYHANDLER_H

+ 8 - 2
jsmn.c

@@ -1,4 +1,5 @@
 #include "jsmn.h"
+#include <assert.h>
 
 /**
  * Allocates a fresh unused token from the token pull.
@@ -332,6 +333,11 @@ int getIntegerToken(const char *json, jsmntok_t *tok){
 /**
  * Get the value of the token in a char[] format.
  */
-void getStringToken(const char *json, jsmntok_t *tok, char *res){
-	sprintf(res, "%.*s", tok->end - tok->start, json + tok->start);
+void getStringToken(const char *json, jsmntok_t *tok, char *res, char maxlength){
+	if((tok->end - tok->start) < maxlength - 1){
+		sprintf(res, "%.*s", tok->end - tok->start, json + tok->start);
+	}else{
+		printf("ERROR: String to large! output string length: %d - Input string length: %d - String: %.*s \n", maxlength - 1, (tok->end - tok->start), tok->end - tok->start, json + tok->start);
+		res[0] = '\0';
+	};
 }

+ 1 - 1
jsmn.h

@@ -75,7 +75,7 @@ int getIntegerToken(const char *json, jsmntok_t *tok);
 /**
  * Get the value of the token in a string format.
  */
-void getStringToken(const char *json, jsmntok_t *tok, char* res);
+void getStringToken(const char *json, jsmntok_t *tok, char* res, char maxlength);
 
 /**
  * Run JSON parser. It parses a JSON data string into and array of tokens, each describing

+ 16 - 3
main.c

@@ -226,6 +226,9 @@ THREAD(AlarmSync, arg)
             httpGet(url, parseAlarmJson);
             sprintf(url,"/getTwitter.php?radiomac=%s&tz=%d", getMacAdress());
             httpGet(url,TwitterParser);
+            char url2[43];
+            sprintf(url2, "/getTwitch.php?radiomac=%s", getMacAdress());
+            httpGet(url2, parseTwitch);
             isAlarmSyncing = false;
         }
         NutSleep(3000);
@@ -261,8 +264,6 @@ int checkOffPressed(){
     }
 }
 
-
-
 int main(void)
 {
 	initialized = 0;
@@ -341,6 +342,11 @@ int main(void)
             LcdBackLight(LCD_BACKLIGHT_ON);
 		}
 
+        //If escape is pressed, stop displaying custom message
+        if(KbGetKey() == KEY_ESC){
+            setDisplayingCustomMessage(false);
+        }
+
 		//Check if background LED is on, and compare to timer
 		if (running == true){
 			if (timerStruct(start) >= 10 || running > 1){
@@ -394,7 +400,14 @@ int main(void)
                     }
 				}
 			}
-		}
+		}else if(isDisplayingCustomMessage() == true){
+            if(timerStruct(timeCheck) >= 5)
+            {
+                setDisplayingCustomMessage(false);
+                LcdBackLight(LCD_BACKLIGHT_OFF);
+            }
+
+        }
 		else if (timerStruct(timeCheck) >= 5){
             displayTime(0);
             displayDate(1);

+ 1 - 1
network.c

@@ -51,7 +51,7 @@ char* getMacAdress(){
 }
 
 void httpGet(char address[], void (*parser)(char*)){
-    u_long rx_to = 3000;
+    u_long rx_to = 10000;
     isReceiving = true;
     printf("\n\n #-- HTTP get -- #\n");