Преглед изворни кода

Merge pull request #28 from jancoow/revert-27-eeprom-controller

Revert "Eeprom controller"
Janco Kock пре 9 година
родитељ
комит
5e98f3674f
12 измењених фајлова са 153 додато и 245 уклоњено
  1. 28 8
      alarm.c
  2. 20 16
      alarm.h
  3. 2 3
      displayHandler.c
  4. 0 75
      eeprom.c
  5. 0 67
      eeprom.h
  6. 3 3
      httpstream.c
  7. 1 1
      httpstream.h
  8. 60 40
      main.c
  9. 1 1
      network.c
  10. 37 28
      ntp.c
  11. 1 1
      ntp.h
  12. 0 2
      typedefs.h

+ 28 - 8
alarm.c

@@ -5,11 +5,9 @@
 #include <time.h>
 #include <assert.h>
 
-#include "alarm.h"
 #include "log.h"
-#include "ntp.h"
-#include "typedefs.h"
 #include "rtc.h"
+#include "alarm.h"
 
 #define n 2
 
@@ -66,18 +64,18 @@ void setState(int idx){
 	struct _tm ct;
 	X12RtcGetClock(&ct);
 	
-	if (NtpCompareTime(ct, alarm[idx].time) == 1 && alarm[idx].time.tm_year != false){
+	if (compareTime(ct, alarm[idx].time) == 1 && alarm[idx].time.tm_year != 0){
 		alarm[idx].state = 1;
 	} else {
 		alarm[idx].state = 0;
 	}
 	
-	/*if (NtpCompareTime(alarm[idx].time,snooze[idx].snoozeStart)){
+	/*if (compareTime(alarm[idx].time,snooze[idx].snoozeStart)){
 		alarm[idx].state = 2;
 	}
 	
 	if (alarm[idx].state == 2){
-		if (NtpCompareTime(alarm[idx].time, snooze[idx].snoozeEnd)){
+		if (compareTime(alarm[idx].time, snooze[idx].snoozeEnd)){
 			snooze[idx].snoozeStart.tm_min += alarm[idx].snooze + 1;
 			snooze[idx].snoozeEnd.tm_min += alarm[idx].snooze + 1;
 		}
@@ -91,11 +89,11 @@ void setState(int idx){
 	}
 }*/
 
-void setAlarm(struct _tm time, char* name, u_long ip, u_short port, char* url, char snooze, int id, int idx){
+void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx){
 	alarm[idx].time = time;
 	
 	strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
-	alarm[idx].ip = ip;
+	strncpy(alarm[idx].ip, ip, sizeof(alarm[idx].ip));
 	alarm[idx].port = port;
 	strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
 
@@ -125,4 +123,26 @@ void handleAlarm(int idx){
 	alarm[idx].state = 0;
 }
 
+int compareTime(tm t1,tm t2){
+    if (t1.tm_year > t2.tm_year){
+        return 1;
+	}
+    if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
+        return 1;
+	}
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
+        return 1;
+	}
+    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 1;
+	}
+    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 1;
+	}
+    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 1;
+	}
+
+    return 0;
+}
 

+ 20 - 16
alarm.h

@@ -1,26 +1,30 @@
+/* Alarm get/set status values */
+#define ALARM_1 	5
+#define ALARM_2		6
+
+#define AFLGS		0b11111111
 
 #ifndef _ALARM_DEFINED
-typedef struct _alarm
+struct _alarm
 {
-    struct _tm time;   // Alarm time                          ?
-    u_long ip;         // IP of the stream to be played       4
-	u_short port;      // Port of the stream to be played     2
-	char url[24];      // Get url                            24
-	char name[16];     // Name of the alarm                  16
-	char snooze;       // Snooze time in minutes              1
-	int id;            // Database identifier                 2
-	signed char state; // Enum: [-1 ... 2]                    1
-} AAlarm;              // Total size:                        50 + ?
-
+	struct _tm time;
+	char ip[24];
+	u_short port;
+	char url[24];
+	char name[16];
+	int snooze;
+	int id;
+	int state;
+};
+#define _ALARM_DEFINED
+#endif
 
 void handleAlarm(int idx);
 int checkAlarms(void);
-void setAlarm(struct _tm time, char* name, u_long ip, u_short port, char* url, char snooze, int id, int idx); // TODO: Linked list?
+void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx);
 int alarmExist(int id);
 void deleteAlarm(int idx);
+int compareTime(tm t1, tm t2);
 void setState(int idx);
 int getState(int idx);
-struct _alarm getAlarm(int idx);
-
-#define _ALARM_DEFINED
-#endif
+struct _alarm getAlarm(int idx);

+ 2 - 3
displayHandler.c

@@ -85,7 +85,7 @@ void displayVolume(int pos)
 {
     ClearLcd();
     int i;
-    (*write_display_ptr[0])("     Volume     ", 16);
+    LcdArrayLineOne("     Volume     ", 16);
 
     char characters[17];
 
@@ -93,8 +93,7 @@ void displayVolume(int pos)
     {
         characters[i] = 0xFF;
     }
-
-    (*write_display_ptr[1])(characters, pos);
+        LcdArrayLineTwo(characters,pos);
 }
 
 

+ 0 - 75
eeprom.c

@@ -1,75 +0,0 @@
-//
-// Created by Jordy Sipkema on 04/03/16.
-//
-
-#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);
-}

+ 0 - 67
eeprom.h

@@ -1,67 +0,0 @@
-/*
- *  Copyright Jordy Sipkema, 2016.
- *
- *  Project             : 20152016-TI2.3a6-Internet Radio
- *  Module              : Eeprom
- *  File name           : Eeprom.h
- *  Revision            : 0.1
- *  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

+ 3 - 3
httpstream.c

@@ -48,10 +48,10 @@ THREAD(Stream, args)
     NutThreadExit();
 }
 
-void playStream(u_long ipaddr, u_short port, char *radiourl){
+void playStream(char *ipaddr, u_short port, char *radiourl){
     if(isStreaming != true){
         isStreaming = true;
-        ConnectStation(sock, ipaddr, port, radiourl, &metaint);
+        ConnectStation(sock, inet_addr(ipaddr), port, radiourl, &metaint);
         NutThreadCreate("Stream", Stream, NULL, 1024);
     }
 }
@@ -282,4 +282,4 @@ void PlayMp3Stream(FILE *stream, u_long metaint)
             break;
         }
     }
-}
+}

+ 1 - 1
httpstream.h

@@ -6,7 +6,7 @@
 #define _Httpstream_H
 
 extern bool HttpIsStreaming();
-extern void playStream(u_long ipaddr, u_short port, char *radiourl);
+extern void playStream(char *ipaddr, u_short port, char *radiourl);
 extern void stopStream();
 
 

+ 60 - 40
main.c

@@ -20,35 +20,35 @@
 /*--------------------------------------------------------------------------*/
 #include <stdio.h>
 #include <string.h>
-#include <time.h>
 
 #include <sys/thread.h>
 #include <sys/timer.h>
 #include <sys/version.h>
 #include <dev/irqreg.h>
 
-// Jordy: Please keep this in alphabetical order!
-#include "alarm.h"
-#include "display.h"
 #include "displayHandler.h"
-#include "eeprom.h"
-#include "flash.h"
-#include "httpstream.h"
+#include "system.h"
+#include "portio.h"
+#include "display.h"
+#include "remcon.h"
 #include "keyboard.h"
 #include "led.h"
 #include "log.h"
-#include "mmc.h"
-#include "network.h"
-#include "ntp.h"
-#include "portio.h"
-#include "remcon.h"
-#include "rtc.h"
-#include "spidrv.h"
-#include "system.h"
 #include "uart0driver.h"
+#include "mmc.h"
 #include "watchdog.h"
+#include "flash.h"
+#include "spidrv.h"
+#include "network.h"
+#include "typedefs.h"
 
 
+#include <time.h>
+#include "rtc.h"
+#include "alarm.h"
+#include "ntp.h"
+#include "httpstream.h"
+
 /*-------------------------------------------------------------------------*/
 /* local routines (prototyping)                                            */
 /*-------------------------------------------------------------------------*/
@@ -181,9 +181,8 @@ static void SysControlMainBeat(u_char OnOff)
 /*-------------------------------------------------------------------------*/
 /* global variable definitions                                             */
 /*-------------------------------------------------------------------------*/
-bool isAlarmSyncing;
-bool initialized = false;
-unsigned char VS_volume = 7; //[0-15];
+int isAlarmSyncing;
+int initialized;
 
 
 /*-------------------------------------------------------------------------*/
@@ -199,27 +198,37 @@ THREAD(StartupInit, arg)
 
     NtpSync();
 
-    initialized = true;
+    initialized = 1;
     NutThreadExit();
 }
 
-THREAD(Sync, arg)
+THREAD(NTPSync, arg)
 {
     for(;;)
     {
-        if((initialized == true) && (hasNetworkConnection() == true))
+        if(initialized && (hasNetworkConnection() == true))
         {
-            // NTP
-            if (0){ // TODO: Do a normal check
-                NtpSync();
+            while(isAlarmSyncing)
+            {
+                NutSleep(2000);
             }
+            NtpSync();
+        }
+        NutSleep(86400000);
+    }
+}
 
-            // Regular alarms
-            isAlarmSyncing = true;
+THREAD(AlarmSync, arg)
+{
+    for(;;)
+    {
+        if(initialized && (hasNetworkConnection() == true))
+        {
+            isAlarmSyncing = 1;
             char* content = httpGet("/getAlarmen.php?radioid=DE370");
             parseAlarmJson(content);
             free(content);
-            isAlarmSyncing = false;
+            isAlarmSyncing = 0;
         }
         NutSleep(3000);
     }
@@ -248,6 +257,7 @@ int checkOffPressed(){
 
 int main(void)
 {
+	initialized = 0;
     int VOL2;
     time_t start;
 	int idx = 0;
@@ -280,7 +290,8 @@ int main(void)
     NtpInit();
 
     NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
-    NutThreadCreate("BackgroundThread", Sync, NULL, 2500);
+    NutThreadCreate("BackgroundThread", AlarmSync, NULL, 2500);
+    NutThreadCreate("BackgroundThread", NTPSync, NULL, 700);
     /** Quick fix for turning off the display after 10 seconds boot */
 
     RcInit();
@@ -297,7 +308,18 @@ 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;
 
@@ -318,28 +340,25 @@ int main(void)
 			}
 		}
 
-        if (KbGetKey() == KEY_01){
-            playStream("145.58.53.152", 80, "/3fm-bb-mp3");
-        }
-
+        VOL = VOL2;
         if(KbGetKey() == KEY_DOWN)
         {
             NutSleep(150);
             start = time(0);
-            if (VS_volume >= 1){
-                VS_volume = (--VS_volume) % 17;
-                VsSetVolume (128-(VS_volume*8), 128-(VS_volume*8));
-                displayVolume(VS_volume);
+            if(VOL > 8){
+                VOL -= 8;
+                VsSetVolume (128-VOL, 128-VOL);
+                displayVolume(VOL/8);
             }
         }
         else if(KbGetKey() == KEY_UP)
         {
             NutSleep(150);
             start = time(0);
-            if (VS_volume <= 15){
-                VS_volume = (++VS_volume) % 17;
-                VsSetVolume (128-(VS_volume*8), 128-(VS_volume*8));
-                displayVolume(VS_volume);
+            if(VOL < 128) {
+                VOL += 8;
+                VsSetVolume(128-VOL, 128-VOL);
+                displayVolume(VOL/8);
             }
         }
         else if(timer(start) >= 5 && checkAlarms() == 1)
@@ -362,6 +381,7 @@ int main(void)
             displayDate(1);
 		}
 
+        VOL2 = VOL;
         WatchDogRestart();
     }
     return(0);

+ 1 - 1
network.c

@@ -190,7 +190,7 @@ void parseAlarmJson(char* content){
             //zoek naar een vrije plaats in de alarm array
             for(j = 0; j < maxAlarms(); j++){
                 if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
-                    setAlarm(time, name, inet_addr(ip), port, url, 5, id, j);
+                    setAlarm(time, name, ip, port, url, 5, id, j);
                     usedAlarms[j] = 1;
                     j = 10;
                 }

+ 37 - 28
ntp.c

@@ -20,16 +20,20 @@
 
 #include "log.h"
 #include "ntp.h"
-#include "eeprom.h"
-#include "typedefs.h"
 
+int TIME_ZONE = 1;
 #define LOG_MODULE  LOG_NTP_MODULE
 
-bool isSyncing = false;
+typedef struct _Eeprom_tm {
+    size_t len;
+    tm tm_struct;
+} Eeprom_tm;
+
+bool isSyncing;
 bool validTime = false;
 time_t ntp_time = 0;
 tm *ntp_datetime;
-int TIME_ZONE = 1;
+uint32_t timeserver = 0;
 
 void NtpInit(void) {
     puts("Func: NtpInit(void)");
@@ -43,23 +47,26 @@ bool NtpIsSyncing(void){
 }
 
 void NtpCheckValidTime(void){
-    TCache *cache;
 
-    if (EepromGetCache(cache) == false){
-        puts("NtpCheckValidTime(): No cache available");
+    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");
         validTime = false;
         return;
     }
 
-    // Cache is present
-    puts("NtpCheckValidTime(): Cache is available");
+    // Valid configuration available.
+    puts("NtpCheckValidTime(): Valid config available \n");
+    tm stored_tm = eeprom_tm_struct.tm_struct;
 
     // 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");
@@ -88,25 +95,20 @@ 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_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
+    if (t1.tm_mon > t2.tm_mon)
         return true;
-    }
-    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
+    if (t1.tm_mday > t2.tm_mday)
         return true;
-    }
-    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){
+    if (t1.tm_hour > t2.tm_hour)
         return true;
-    }
-    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){
+    if (t1.tm_min > t2.tm_min)
         return true;
-    }
-    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){
+    if (t1.tm_sec > t2.tm_sec)
         return true;
-    }
 
+    //else
     return false;
 }
 
@@ -118,11 +120,10 @@ void NtpSync(void){
     /* Ophalen van pool.ntp.org */
     isSyncing = true;
     _timezone = -getTimeZone() * 3600;
-    puts("NtpSync(): Timezone fetched. ");
     printf(TIME_ZONE);
     NutDelay(100);
     //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
-    uint32_t timeserver = inet_addr("213.154.229.24");
+    timeserver = inet_addr("213.154.229.24");
 
     for (;;) {
         if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
@@ -147,9 +148,17 @@ void NtpSync(void){
 }
 
 void NtpWriteTimeToEeprom(tm time_struct){
-    TCache cache;
+    Eeprom_tm eeprom_tm_struct;
 
-    cache.last_sync = time_struct;
-    EepromSetCache(&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);
 }
 
+//unsigned long TmStructToEpoch(tm tm_struct){
+//
+//}

+ 1 - 1
ntp.h

@@ -5,7 +5,7 @@
 #ifndef _Ntp_H
 #define _Ntp_H
 
-#include "typedefs.h"
+typedef enum {false, true} bool;
 
 extern bool NtpIsSyncing(void);
 extern void NtpInit(void);

+ 0 - 2
typedefs.h

@@ -25,8 +25,6 @@
 /*--------------------------------------------------------------------------*/
 /*  Type declarations                                                       */
 /*--------------------------------------------------------------------------*/
-typedef enum {false, true} bool;
-
 /* 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    */