Explorar el Código

Merge pull request #55 from jancoow/Twitch

Twitch & NTP fixes
Janco Kock hace 9 años
padre
commit
ed8e3424d0
Se han modificado 9 ficheros con 71 adiciones y 30 borrados
  1. 1 1
      alarm.h
  2. 14 7
      contentparser.c
  3. 23 5
      displayHandler.c
  4. 1 1
      jsmn.c
  5. 16 7
      main.c
  6. 7 1
      ntp.c
  7. 1 1
      ntp.h
  8. 5 4
      twitch.c
  9. 3 3
      twitch.h

+ 1 - 1
alarm.h

@@ -25,7 +25,7 @@ int compareTime(tm t1, tm t2);
 void setState(int idx);
 int getState(int idx);
 struct _alarm* getAlarm(int idx);
-char getRunningAlarmID();
+char getRunningAlarmID(void);
 int maxAlarms(void);
 int isLeapYear(int y);
 void setSnooze(int idx);

+ 14 - 7
contentparser.c

@@ -12,7 +12,7 @@
 #include "vs10xx.h"
 #include "twitch.h"
 #include "Twitter.h"
-int streamid;
+#include <string.h>
 
 void parseAlarmJson(char* content){
     int r;
@@ -189,7 +189,13 @@ void parseCommandQue(char* content){
 void parsetimezone(char* content)
 {
     int timezone = atoi(content); //parsing string to int (only works when everything is int)
-    setTimeZone(timezone);
+    if(strlen(content) == 0)
+    {
+        setTimeZone(50);
+    }
+    else {
+        setTimeZone(timezone);
+    }
 }
 
 void parseTwitch(char* content) {
@@ -214,10 +220,11 @@ void parseTwitch(char* content) {
     char name[20];
     char title[20];
     char game[20];
-    int date;
+    char date[15];
     memset(name, 0, 20);
     memset(title, 0, 20);
     memset(game, 0, 20);
+    memset(date, 0, 15);
 
     for (i = 1; i < r; i++) {
         if (jsoneq(content, &token[i], "Name") == 0) {
@@ -233,18 +240,18 @@ void parseTwitch(char* content) {
             i++;
         }
         else if (jsoneq(content, &token[i], "Date") == 0) {
-            date = getIntegerToken(content, &token[i + 1]);
+            getStringToken(content, &token[i + 1], date, 15);
             i++;
         }
     }
-    printf("%d", date);
-    if(streamid != date)
+    printf("%s", date);
+    if(strncmp(date, streamid, 15) != 0)
     {
         strcpy(data.title, title);
         strcpy(data.game, game);
         strcpy(data.name, name);
         printf("%s - %s - %s", name, title, game);
-        streamid = date;
+        strcpy(streamid, date);
         setCurrentDisplay(DISPLAY_Twitch, 100);
     }
 }

+ 23 - 5
displayHandler.c

@@ -165,11 +165,29 @@ void displayTwitter(char* text)
         NutDelay(500);
 }
 
-void displayTwitch(char name[], char title[], char game[])
-{
-    ClearLcd();
-    LcdArrayLineOne(name, strlen(name));
-    LcdArrayLineTwo("Streaming", 9);
+void displayTwitch(char name[], char title[], char game[]) {
+
+    if (timerStruct(lastDisplayTime) % 10 < 5) {
+        ClearLcd();
+        if(strlen(name) > 16) {
+            LcdArrayLineOne(name, strlen(name));
+        }else {
+            LcdArrayLineOne(name, 16);
+        }
+        LcdArrayLineTwo("is streaming", 12);
+    }
+    else {
+        ClearLcd();
+        if(strlen(title) > 16) {
+            LcdArrayLineOne(title, 16);
+        }else {
+            LcdArrayLineOne(title, strlen(title));
+        }if(strlen(game) > 16) {
+            LcdArrayLineTwo(game, 16);
+        }else{
+            LcdArrayLineTwo(game, strlen(game));
+        }
+    }
     LcdBackLight(LCD_BACKLIGHT_ON);
 }
 

+ 1 - 1
jsmn.c

@@ -339,7 +339,7 @@ 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);
+		printf("ERROR: String too 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';
 	};
 

+ 16 - 7
main.c

@@ -218,10 +218,14 @@ THREAD(AlarmSync, arg)
     while(initialized == false){
         NutSleep(1000);
     }
-
-    NtpSync();
-    int dayCounter;
-    dayCounter = 0;
+    int dayCounter = 0;
+    int counter = 0;
+    while(!NtpSync() && counter < 10)
+    {
+        NutSleep(1000);
+        counter++;
+    }
+    counter = 0;
     for(;;)
     {
 
@@ -245,10 +249,15 @@ THREAD(AlarmSync, arg)
             sprintf(url, "%s%s", "/getCommands.php?radiomac=", getMacAdress());
             httpGet(url, parseCommandQue);
         }
-        if(dayCounter > 28800 && (hasNetworkConnection() == true))
+        while(dayCounter > 28800 && (hasNetworkConnection() == true))
         {
-            NtpSync();
-            dayCounter = 0;
+            while(!NtpSync() && counter < 10)
+            {
+                NutSleep(1000);
+                counter++;
+            }
+            dayCounter = 28800;
+            counter = 0;
         }
         dayCounter++;
         NutSleep(3000);

+ 7 - 1
ntp.c

@@ -82,12 +82,17 @@ bool NtpTimeIsValid(void){
     return validTime;
 }
 
-void NtpSync(void){
+int NtpSync(void){
     /* Ophalen van pool.ntp.org */
     isSyncing = true;
+    setTimeZone(50);
     httpGet("/gettimezone.php", parsetimezone);
     _daylight = 0;
     printf("Timezone is: %d", TIME_ZONE);
+    if(TIME_ZONE == 50)
+    {
+        return 0;
+    }
     NutDelay(100);
     //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
     timeserver = inet_addr("213.154.229.24");
@@ -112,6 +117,7 @@ void NtpSync(void){
 
     isSyncing = false;
     validTime = true;
+    return 1;
 }
 
 void NtpWriteTimeToEeprom(tm time_struct){

+ 1 - 1
ntp.h

@@ -10,7 +10,7 @@
 
 extern bool NtpIsSyncing(void);
 extern void NtpInit(void);
-extern void NtpSync(void);
+extern int NtpSync(void);
 extern void setTimeZone(int timezone);
 extern bool NtpTimeIsValid(void);
 

+ 5 - 4
twitch.c

@@ -3,16 +3,17 @@
 //
 
 #include "twitch.h"
+#include <string.h>
 
-int streamid = 0;
+char streamid[15];
 struct streamdata data;
 
-void setID(int id)
+void setID(char* id)
 {
-    streamid = id;
+    strcpy(streamid, id);
 }
 
-int getID()
+char* getID()
 {
     return streamid;
 }

+ 3 - 3
twitch.h

@@ -7,9 +7,9 @@
 
 #endif //INTERNETRADIO_TWITCH_H
 
-void setID(int id);
-int getID(void);
-int streamid;
+void setID(char* id);
+char* getID(void);
+char streamid[15];
 struct streamdata data;
 
 struct streamdata