瀏覽代碼

Multiple small fixes.

- Improved thread-creation
- Alarm-thread now works in two phases (Idle, Running)
- Alarm.h: Add initial commit
- DisplayHandler.c: Sorted includes on alphabetical order.
- DisplayHandler.c: Swapped the direct function for function ptr.
- Eeprom.c/h: Add eeprom-controller to the project.
- Httpstream.c/h: Add newline at EOF
- Httpstream.h: Included typedefs.h (for bool) and fixed prototypes
  (Add void for 0-parameters functions)
- Main.c: Fixed volume by adding a global variable. + Improved
  thread-creation + 2-phase alarm thread + remove test-code (debug)
- Network.c: Sorted includes + Remove unnecessary printf-statements +
  Improved the printf-statements that aren't removed.
- Network.h: Included typedefs.h and fixed prototypes.
- Ntp.c: Remove timezone-sync.
- Ntp.h: Remove typedefinition for bool (moved to typedefs.h)
- Typedefs.h: Move program-global typedefs to typedefs.h
Jordy Sipkema 9 年之前
父節點
當前提交
9df6f03a8d
共有 12 個文件被更改,包括 321 次插入128 次删除
  1. 17 3
      alarm.h
  2. 7 6
      displayHandler.c
  3. 84 0
      eeprom.c
  4. 67 0
      eeprom.h
  5. 1 1
      httpstream.c
  6. 5 3
      httpstream.h
  7. 35 51
      main.c
  8. 10 10
      network.c
  9. 5 3
      network.h
  10. 49 47
      ntp.c
  11. 13 4
      ntp.h
  12. 28 0
      typedefs.h

+ 17 - 3
alarm.h

@@ -1,3 +1,15 @@
+/*
+ *  Copyright 20152016-TI2.3A6, 2016.
+ *
+ *  Project             : 20152016-TI2.3a6-Internet Radio
+ *  Module              : Alarm
+ *  File name           : alarm.h
+ *  Revision            : 1.0
+ *  Creation Date       : 2016
+ *
+ *  Description         :
+ */
+
 /* Alarm get/set status values */
 #define ALARM_1 	5
 #define ALARM_2		6
@@ -16,8 +28,6 @@ struct _alarm
 	int id;
 	int state;
 };
-#define _ALARM_DEFINED
-#endif
 
 void handleAlarm(int idx);
 int checkAlarms(void);
@@ -27,4 +37,8 @@ void deleteAlarm(int idx);
 int compareTime(tm t1, tm t2);
 void setState(int idx);
 int getState(int idx);
-struct _alarm getAlarm(int idx);
+struct _alarm getAlarm(int idx);
+
+#define _ALARM_DEFINED
+#endif
+

+ 7 - 6
displayHandler.c

@@ -7,14 +7,14 @@
 #include <string.h>
 #include <time.h>
 
+#include "alarm.h"
 #include "display.h"
 #include "displayHandler.h"
-#include "ntp.h"
+#include "httpstream.h"
 #include "log.h"
-#include "rtc.h"
-#include "alarm.h"
 #include "network.h"
-#include "httpstream.h"
+#include "ntp.h"
+#include "rtc.h"
 
 #define MONTH_OFFSET 1
 #define YEAR_OFFSET 1900
@@ -85,7 +85,6 @@ void displayVolume(int pos)
 {
     ClearLcd();
     int i;
-    LcdArrayLineOne("     Volume     ", 16);
 
     char characters[17];
 
@@ -93,7 +92,9 @@ void displayVolume(int pos)
     {
         characters[i] = 0xFF;
     }
-        LcdArrayLineTwo(characters,pos);
+
+    (*write_display_ptr[0])("     Volume     ",  17);
+    (*write_display_ptr[1])(characters, pos);
 }
 
 

+ 84 - 0
eeprom.c

@@ -0,0 +1,84 @@
+/*
+ *  Copyright Jordy Sipkema, 2016.
+ *
+ *  Project             : 20152016-TI2.3a6-Internet Radio
+ *  Module              : Eeprom
+ *  File name           : Eeprom.h
+ *  Revision            : 2.0
+ *  Creation Date       : 2016/03/04
+ *
+ *  Description         : This module stores the non-volatile settings
+ *                        for the radio in the atmega128's internal eeprom
+ */
+
+#include "eeprom.h"
+
+
+
+void EepromRead(int addr, void* settings, size_t size){
+
+}
+
+void EepromWrite(int addr, void* settings, size_t size){
+
+}
+
+bool EepromGetAll(TSettings *settings){
+    NutNvMemLoad(EEPROM_BASE, settings, sizeof(*settings));
+
+
+    if (settings->Checksum != sizeof(*settings)){
+        // Size mismatch: There is no valid configuration present.
+        puts("EepromGetAll(): Size mismatch");
+        return false;
+    }
+
+    return true;
+}
+
+void EepromSetAll(TSettings *settings){
+    int success = NutNvMemSave(EEPROM_BASE, settings, sizeof(*settings));
+    if (success == 0){ puts("EepromSetAll: SettingsSetAll successful."); }
+    else { puts("EepromSetAll: SettingsSetAll successful."); }
+
+    NutDelay(100);
+}
+
+void EepromWriteDefaults(void){
+    puts("EepromWriteDefaults()");
+
+    // Declare TSettings:
+    TSettings settings;
+    settings.Checksum = sizeof(settings);
+
+    settings.Cache = (const struct TCache){ 0 };
+    settings.System = (const struct TSettingsSystem){ 0 };
+
+    EepromSetAll(&settings);
+}
+
+
+//bool EepromGetSystemSettings(TSettingsSystem *SystemSettings){
+//
+//}
+//
+
+bool EepromGetCache(TCache *cache){
+    TSettings *settings;
+    if (EepromGetAll(settings) == false){
+        return false;
+    }
+
+    *cache = settings->Cache;
+    return true;
+}
+
+void EepromSetCache(TCache *cache){
+    TSettings settings;
+    settings.Checksum = sizeof(settings);
+
+    settings.System = (const struct TSettingsSystem){ 0 };
+    settings.Cache = *cache;
+
+    EepromSetAll(&settings);
+}

+ 67 - 0
eeprom.h

@@ -0,0 +1,67 @@
+/*
+ *  Copyright Jordy Sipkema, 2016.
+ *
+ *  Project             : 20152016-TI2.3a6-Internet Radio
+ *  Module              : Eeprom
+ *  File name           : Eeprom.h
+ *  Revision            : 2.0
+ *  Creation Date       : 2016/03/04
+ *
+ *  Description         : This module stores the non-volatile settings
+ *                        for the radio in the atmega128's internal eeprom
+ */
+
+#ifndef MUTLI_OS_BUILD_EEPROM_H
+#define MUTLI_OS_BUILD_EEPROM_H
+
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+#include "typedefs.h"
+
+#define EEPROM_BASE            0x0100 //The address where we'll start writing values.
+
+#define SETTINGS_POINTER            ((TSettings *)EEPROM_USER_BASE)
+
+/*!\brief System settings */
+typedef struct _TSettingsSystem
+{
+    u_long  lSerialnumber;             // size = 4
+} TSettingsSystem;                     // totalsize = 4
+
+/*!\brief System Cache */
+typedef struct _TCache
+{
+    tm last_sync;                      // TODO: figure out the size
+} TCache;
+
+typedef struct _TAlarm // Max 5
+{
+    tm alarm_time;
+    char name[16];
+    char snooze_time;                 // size = ?, desc: snooze_time in minutes.
+} TAlarm;
+
+/*!\brief Settings to write on Eeprom */
+typedef struct _TSettings
+{
+    size_t              Checksum;      // Checksum for validation TSettings-struct
+    TSettingsSystem     System;        // System settings
+    TAlarm              Alarm_general; // Alarms settings
+    TCache              Cache;         // Cache
+} TSettings;
+
+
+extern void EepromRead(int, void*, size_t);
+extern void EepromWrite(int, void*, size_t);
+
+extern bool EepromGetAll(TSettings*);
+extern void EepromSetAll(TSettings *settings);
+
+extern bool EepromGetSystemSettings(TSettingsSystem*);
+
+extern bool EepromGetCache(TCache*);
+extern void EepromSetCache(TCache*);
+
+#endif //MUTLI_OS_BUILD_EEPROM_H

+ 1 - 1
httpstream.c

@@ -282,4 +282,4 @@ void PlayMp3Stream(FILE *stream, u_long metaint)
             break;
         }
     }
-}
+}

+ 5 - 3
httpstream.h

@@ -5,9 +5,11 @@
 #ifndef _Httpstream_H
 #define _Httpstream_H
 
-extern bool HttpIsStreaming();
+#include "typedefs.h"
+
+extern bool HttpIsStreaming(void);
 extern void playStream(char *ipaddr, u_short port, char *radiourl);
-extern void stopStream();
+extern void stopStream(void);
 
 
-#endif /* _Httpstream_H */
+#endif /* _Httpstream_H */

+ 35 - 51
main.c

@@ -183,6 +183,7 @@ static void SysControlMainBeat(u_char OnOff)
 /*-------------------------------------------------------------------------*/
 int isAlarmSyncing;
 int initialized;
+u_char VS_volume = 7; //[0-15];
 
 
 /*-------------------------------------------------------------------------*/
@@ -194,44 +195,42 @@ int initialized;
 /*-------------------------------------------------------------------------*/
 THREAD(StartupInit, arg)
 {
-    NetworkInit();
+    NutThreadSetPriority(5);
 
-    NtpSync();
+    puts("Thread: StartupInit start\n\n");
+    NetworkInit();
 
     initialized = 1;
+
+    puts("Thread: StartupInit stop\n\n");
     NutThreadExit();
 }
 
-THREAD(NTPSync, arg)
+THREAD(AlarmSync, arg)
 {
-    for(;;)
-    {
-        if(initialized && (hasNetworkConnection() == true))
-        {
-            while(isAlarmSyncing)
-            {
-                NutSleep(2000);
-            }
-            NtpSync();
-        }
-        NutSleep(86400000);
+    NutThreadSetPriority(200);
+
+    puts("Thread: AlarmSync > Ready and on hold.\n\n");
+    while(!(initialized && (hasNetworkConnection() == true))){
+        NutSleep(1000);
     }
-}
 
-THREAD(AlarmSync, arg)
-{
+    puts("Thread: AlarmSync > Starting normal operation\n\n");
+
+    NtpSync();
+
     for(;;)
     {
-        if(initialized && (hasNetworkConnection() == true))
-        {
-            isAlarmSyncing = 1;
-            char* content = httpGet("/getAlarmen.php?radioid=DE370");
-            parseAlarmJson(content);
-            free(content);
-            isAlarmSyncing = 0;
-        }
+        isAlarmSyncing = 1;
+        char* content = httpGet("/getAlarmen.php?radioid=DE370");
+        parseAlarmJson(content);
+        free(content);
+        isAlarmSyncing = 0;
+
         NutSleep(3000);
     }
+
+    // Unreachable code...
     NutThreadExit();
 }
 
@@ -289,11 +288,6 @@ int main(void)
     LcdBackLight(LCD_BACKLIGHT_ON);
     NtpInit();
 
-    NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
-    NutThreadCreate("BackgroundThread", AlarmSync, NULL, 2500);
-    NutThreadCreate("BackgroundThread", NTPSync, NULL, 700);
-    /** Quick fix for turning off the display after 10 seconds boot */
-
     RcInit();
 
 	KbInit();
@@ -308,21 +302,13 @@ int main(void)
 	/* Enable global interrupts */
 	sei();
 
-   /* struct _tm tm;
-	tm = GetRTCTime();
-	tm.tm_sec += 10;
-    setAlarm(tm,"    test1234      ", "0.0.0.0", 8001,1,0,0);
-	tm.tm_sec +=20;
-	setAlarm(tm,"    test5678      ", "0.0.0.0", 8001,1,0,1);*/
-
-/*    if(hasNetworkConnection() == true){
-        playStream("145.58.53.152", 80, "/3fm-bb-mp3");
-    }*/
     start = time(0) - 10;
-    unsigned char VOL = 64;
 
     running = 1;
 
+    NutThreadCreate("Startup", StartupInit, NULL, 1024);
+    NutThreadCreate("Alarm", AlarmSync, NULL, 2500);
+
     for (;;)
     {
 		//Check if a button is pressed
@@ -340,25 +326,24 @@ int main(void)
 			}
 		}
 
-        VOL = VOL2;
         if(KbGetKey() == KEY_DOWN)
         {
             NutSleep(150);
             start = time(0);
-            if(VOL > 8){
-                VOL -= 8;
-                VsSetVolume (128-VOL, 128-VOL);
-                displayVolume(VOL/8);
+            if (VS_volume >= 1){
+                VS_volume = (--VS_volume) % 17;
+                VsSetVolume (128-(VS_volume*8), 128-(VS_volume*8));
+                displayVolume(VS_volume);
             }
         }
         else if(KbGetKey() == KEY_UP)
         {
             NutSleep(150);
             start = time(0);
-            if(VOL < 128) {
-                VOL += 8;
-                VsSetVolume(128-VOL, 128-VOL);
-                displayVolume(VOL/8);
+            if (VS_volume <= 15){
+                VS_volume = (++VS_volume) % 17;
+                VsSetVolume (128-(VS_volume*8), 128-(VS_volume*8));
+                displayVolume(VS_volume);
             }
         }
         else if(timer(start) >= 5 && checkAlarms() == 1)
@@ -381,7 +366,6 @@ int main(void)
             displayDate(1);
 		}
 
-        VOL2 = VOL;
         WatchDogRestart();
     }
     return(0);

+ 10 - 10
network.c

@@ -21,14 +21,14 @@
 #include <pro/dhcp.h>
 #include <pro/sntp.h>
 
+#include "alarm.h"
+#include "jsmn.h"
 #include "ntp.h"
 #include "network.h"
-#include "jsmn.h"
 #include "rtc.h"
-#include "alarm.h"
 
-bool isReceiving;
-bool hasNetwork;
+bool isReceiving = false;
+bool hasNetwork = false;
 
 void NetworkInit() {
     hasNetwork = false;
@@ -41,14 +41,14 @@ void NetworkInit() {
     }else {
         printf("Ik heb een internet connectie. Ip is: %s \n\n", inet_ntoa(confnet.cdn_ip_addr));
     }
-    NutSleep(2000);
+    NutSleep(100);
     hasNetwork = true;
 }
 
 char* httpGet(char address[]){
     isReceiving = true;
     NutDelay(1000);
-    printf("\n\n #-- HTTP get -- #\n");
+    //printf("\n\n #-- HTTP get -- #\n");
 
     TCPSOCKET* sock = NutTcpCreateSocket();
 
@@ -68,12 +68,12 @@ char* httpGet(char address[]){
             printf("Writing headers failed.\n");
             NutDelay(1000);
         }else{
-            printf("Headers %s writed. Now reading.", http);
+            printf("Headers written: %sNow reading...\n", http);
             NutDelay(1000);
             NutTcpReceive(sock, buffer, sizeof(buffer));
             //fread(buffer, 1, sizeof(buffer), stream);
             NutDelay(1200);
-            printf(buffer);
+            //printf(buffer);
         };
         //fclose(stream);
     }
@@ -97,7 +97,7 @@ char* httpGet(char address[]){
         }
     }
     content[t] = '\0';
-    printf("\nContent size: %d, Content: %s \n", t, content);
+    //printf("\nContent size: %d, Content: %s \n", t, content);
     isReceiving = false;
     return content;
 }
@@ -122,7 +122,7 @@ void parseAlarmJson(char* content){
     if (r < 0) {
         printf("Failed to parse JSON: %d \n", r);
     }else{
-        printf("Aantal tokens found: %d \n", r);
+        printf("Aantal tokens found: %d \n\n", r);
     }
 
 

+ 5 - 3
network.h

@@ -5,11 +5,13 @@
 #ifndef _Network_H
 #define _Network_H
 
-//bool hasNetworkConnection(void);
-//bool NetworkIsReceiving(void);
+#include "typedefs.h"
+
+bool hasNetworkConnection(void);
+bool NetworkIsReceiving(void);
 extern void NetworkInit(void);
 char* httpGet(char address[]);
 void parseAlarmJson(char* content);
-int getTimeZone();
+int getTimeZone(void);
 
 #endif /* _Network_H */

+ 49 - 47
ntp.c

@@ -1,6 +1,16 @@
-//
-// Created by janco on 25-2-16.
-//
+/*
+ *  Copyright 20152016-TI2.3A6, 2016.
+ *
+ *  Project             : 20152016-TI2.3a6-Internet Radio
+ *  Module              : NTP
+ *  File name           : ntp.c
+ *  Revision            : 1.1
+ *  Creation Date       : 2016
+ *
+ *  Description         : This module syncs the time from a network time
+ *                        server using the NTP-protocol.
+ */
+
 #include <dev/board.h>
 #include <dev/debug.h>
 #include <dev/nvmem.h>
@@ -18,22 +28,18 @@
 #include <string.h>
 #include <time.h>
 
+#include "eeprom.h"
 #include "log.h"
 #include "ntp.h"
+#include "typedefs.h"
 
-int TIME_ZONE = 1;
 #define LOG_MODULE  LOG_NTP_MODULE
 
-typedef struct _Eeprom_tm {
-    size_t len;
-    tm tm_struct;
-} Eeprom_tm;
-
-bool isSyncing;
-bool validTime = false;
+extern bool isSyncing = false;
+extern bool validTime = false;
 time_t ntp_time = 0;
 tm *ntp_datetime;
-uint32_t timeserver = 0;
+int TIME_ZONE = 1;
 
 void NtpInit(void) {
     puts("Func: NtpInit(void)");
@@ -47,26 +53,24 @@ bool NtpIsSyncing(void){
 }
 
 void NtpCheckValidTime(void){
+    puts("Func: NtpCheckValidTime(void)");
+    TCache *cache;
 
-    Eeprom_tm eeprom_tm_struct;
-
-    NutNvMemLoad(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
-
-    if (eeprom_tm_struct.len != sizeof(eeprom_tm_struct)){
-        // Size mismatch: There is no valid configuration present.
-        puts("NtpCheckValidTime(): Size mismatch \n");
+    if (EepromGetCache(cache) == false){
+        puts("NtpCheckValidTime(): No cache available");
         validTime = false;
         return;
     }
 
-    // Valid configuration available.
-    puts("NtpCheckValidTime(): Valid config available \n");
-    tm stored_tm = eeprom_tm_struct.tm_struct;
+    // Cache is present
+    puts("NtpCheckValidTime(): Cache is available");
 
     // Check time is valid;
     tm current_tm;
     X12RtcGetClock(&current_tm);
 
+    tm stored_tm = cache->last_sync;
+
     validTime = NtpCompareTime(current_tm, stored_tm);
     if (validTime){
         puts("NtpCheckValidTime(): Time was valid \n");
@@ -95,20 +99,25 @@ bool NtpCompareTime(tm t1, tm t2){
     );
     puts(debug);
 
-    if (t1.tm_year > t2.tm_year)
+    if (t1.tm_year > t2.tm_year){
         return true;
-    if (t1.tm_mon > t2.tm_mon)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
         return true;
-    if (t1.tm_mday > t2.tm_mday)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
         return true;
-    if (t1.tm_hour > t2.tm_hour)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour > t2.tm_hour){
         return true;
-    if (t1.tm_min > t2.tm_min)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min > t2.tm_min){
         return true;
-    if (t1.tm_sec > t2.tm_sec)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min == t2.tm_min &&t1.tm_sec > t2.tm_sec){
         return true;
+    }
 
-    //else
     return false;
 }
 
@@ -119,21 +128,21 @@ bool NtpTimeIsValid(void){
 void NtpSync(void){
     /* Ophalen van pool.ntp.org */
     isSyncing = true;
-    _timezone = -getTimeZone() * 3600;
-    printf(TIME_ZONE);
+    //_timezone = -getTimeZone() * 3600;
+    puts("NtpSync(): Timezone fetched. ");
+
     NutDelay(100);
-    //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
-    timeserver = inet_addr("213.154.229.24");
+    puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
+    uint32_t timeserver = inet_addr("213.154.229.24");
 
     for (;;) {
         if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
             break;
         } else {
-            NutSleep(400);
             puts("Fout bij het ontvangen van de tijd");
+            NutSleep(1000);
         }
     }
-    //puts("Opgehaald.\n");
 
     ntp_datetime = localtime(&ntp_time);
 
@@ -145,20 +154,13 @@ void NtpSync(void){
 
     isSyncing = false;
     validTime = true;
+
+    NutSleep(100);
 }
 
 void NtpWriteTimeToEeprom(tm time_struct){
-    Eeprom_tm eeprom_tm_struct;
+    TCache cache;
 
-    eeprom_tm_struct.len = sizeof(eeprom_tm_struct);
-    eeprom_tm_struct.tm_struct = time_struct;
-
-    int success = NutNvMemSave(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
-    if (success == 0){ puts("NtpWriteTimeToEeprom: Time succesfully written to eeprom \n"); }
-
-    NutDelay(100);
+    cache.last_sync = time_struct;
+    EepromSetCache(&cache);
 }
-
-//unsigned long TmStructToEpoch(tm tm_struct){
-//
-//}

+ 13 - 4
ntp.h

@@ -1,11 +1,20 @@
-//
-// Created by janco on 25-2-16.
-//
+/*
+ *  Copyright 20152016-TI2.3A6, 2016.
+ *
+ *  Project             : 20152016-TI2.3a6-Internet Radio
+ *  Module              : NTP
+ *  File name           : ntp.h
+ *  Revision            : 1.1
+ *  Creation Date       : 2016
+ *
+ *  Description         : This module syncs the time from a network time
+ *                        server using the NTP-protocol.
+ */
 
 #ifndef _Ntp_H
 #define _Ntp_H
 
-typedef enum {false, true} bool;
+#include "typedefs.h"
 
 extern bool NtpIsSyncing(void);
 extern void NtpInit(void);

+ 28 - 0
typedefs.h

@@ -12,6 +12,22 @@
  * [PURPOSE]    global typedefs
  * ======================================================================== */
 
+/*
+ *  Copyright 20152016-TI2.3a6, 2016.
+ *
+ *  Project             : 20152016-TI2.3a6-Internet Radio
+ *  Module              : Type definitions
+ *  File name           : Typedefs.h
+ *  Revision            : 1.1
+ *  Creation Date       : 2016/03/04
+ *
+ *  Description         : Global type definitions for the SIR100/120
+ *                        firmware.
+ *
+ *  Changelog       1.1 : Add type definitions for TI2.3a6 software.
+ *                        - Add bool
+ */
+
 
 /*--------------------------------------------------------------------------*/
 /*  Include files                                                           */
@@ -25,6 +41,18 @@
 /*--------------------------------------------------------------------------*/
 /*  Type declarations                                                       */
 /*--------------------------------------------------------------------------*/
+
+// Additions based on version 1.1
+
+typedef enum {false, true} bool;
+
+
+
+
+
+
+// Original StreamIT v1.0 code
+
 /* RL: this 'table' has now a mirror table in 'Display.c' (LcdErrorStrings) */
 /*     Make sure that any modification made to this table are reflected by  */
 /*     the LcdErrorStirngs table! (an error is bad but showing the wrong    */