working version of song control with initialization from sd card

Dependencies:   MFRC522 NRF2401P SDFileSystem SPI_TFT_ILI9341 TFT_fonts mbed

Fork of Song_Control by Malcolm McCulloch

Revision:
2:d1eae91343a9
Parent:
1:c2232b1eaf31
Child:
3:86773d65ed58
Child:
4:74afa8c5ffe9
--- a/hub.cpp	Sun Jan 24 16:15:53 2016 +0000
+++ b/hub.cpp	Thu Jan 28 14:38:45 2016 +0000
@@ -4,16 +4,116 @@
 */
 #include "mbed.h"
 #include "utils.h"
+#include "NRF2401P.h"
 #define debug 
 
 // Flags
+unsigned char flag1sH = 0;
+
 // Variables
+Ticker tick1sHub;
+float currentMaxH = 1.5; // Maximum current that each cube can consume.
+
 // tx nRF2401
-
+extern char txBuff[];
+extern NRF2401P nrf1;
+extern int channel;
 long long addrLcker=0xBBBBBBBBBB;
-extern int channel;
+// -----------------------------------------------------------------------------
+// Interupt routines
+// -----------------------------------------------------------------------------
+/**
+* Fast interrupt routine for every 1 second
+*/
+void int1sH()
+{
+    flag1sH=1;
+    // add only fast code.
+}
+/**
+* Sends a time stamp
+*/
+void txTimeH()
+{
+#ifdef debug
+    printf("Send time \n\r");
+#endif
+    time_t now= time(NULL);
+    sprintf(txBuff,"T %X",now);
+    nrf1.transmitData(txBuff,strlen(txBuff));
+#ifdef debug
+    printf("Tx %s [nrf:%s] \n\r", txBuff,nrf1.statusString());
+#endif  
+}
+
+/**
+* Sends max Current
+*/
+void txCurrentH()
+{
+#ifdef debug
+    printf("Send current \n\r");
+#endif
+    sprintf(txBuff,"I %04X", *((int *) &currentMaxH));
+    nrf1.transmitData(txBuff,strlen(txBuff));
+#ifdef debug
+    printf("Tx %s [nrf:%s] \n\r", txBuff,nrf1.statusString());
+#endif  
+}
 
+/**
+* Slow interrupt routine for every 1 second
+*/
+void do1sH()
+{
+#ifdef debug
+    printf("Hub 1s \n\r");
+#endif
+    
+    time_t now= time(NULL);
+    if ((now % 60)==0){
+    txTimeH();  
+    }
+    txCurrentH();
+#ifdef debug
+    printf("Tx %s [nrf:%s] \n\r", txBuff,nrf1.statusString());
+#endif  
+}
+// -----------------------------------------------------------------------------
 // Initializaton
+// -----------------------------------------------------------------------------
+/**
+* Sets up the interrupts for the hub
+*/
+void initInteruptsHub(){
+    tick1sHub.attach(&int1sH, 10.0);
+}
+/**
+* asks the user for the time
+*/
+
+void setRTCpc()
+{
+    // get the current time from the terminal
+    struct tm t;
+    printf("Enter current date :\n\r");
+    printf("YYYY MM DD [enter]\n\r");
+    scanf("%d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday);
+    printf("Enter current time:\n\r");
+    printf("HH MM SS [enter]\n\r");
+    scanf("%d %d %d", &t.tm_hour, &t.tm_min, &t.tm_sec);
+
+    // adjust for tm structure required values
+    t.tm_year = t.tm_year - 1900;
+    t.tm_mon = t.tm_mon - 1;
+
+    // set the time
+    set_time(mktime(&t));
+
+}
+
+
+
 /**
 * Initialise for a locker
 * fp is the config file if additonal information is needed.
@@ -30,14 +130,35 @@
 #ifdef debug
     printf("  Channel:%x, Hub Address %llx \n\r",channel, addrLcker);
 #endif
+    setRTCpc();
+
+    // Setup nrf
+
+#ifdef debug
+    printf("Steup doNrf \n\r");
+#endif
+    spiNrf();
+    nrf1.quickTxSetup(channel, addrLcker);
+#ifdef debug
+    nrf1.printDetails();
+    nrf1.checkStatus();
+    printf("Setup doNrf complete [nrf:%s]\n\r",nrf1.statusString());
+#endif
+
+    // Setup interupts
+    initInteruptsHub();
 
 
 }
+
 // Interupt routines
 
-
 // Loop through slow routines
 
 void loopHub(){
 
+    if (flag1sH){
+        do1sH();
+        flag1sH=0;
+    }
 }
\ No newline at end of file