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

Files at this revision

API Documentation at this revision

Comitter:
epgmdm
Date:
Thu Jan 28 14:38:45 2016 +0000
Parent:
1:c2232b1eaf31
Child:
3:86773d65ed58
Child:
4:74afa8c5ffe9
Commit message:
Got roles sorted

Changed in this revision

NRF2401P.lib Show annotated file Show diff for this revision Revisions of this file
hub.cpp Show annotated file Show diff for this revision Revisions of this file
locker.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NRF2401P.lib	Thu Jan 28 14:38:45 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/epgmdm/code/NRF2401P/#a9b83d2b6915
--- 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
--- a/locker.cpp	Sun Jan 24 16:15:53 2016 +0000
+++ b/locker.cpp	Thu Jan 28 14:38:45 2016 +0000
@@ -5,16 +5,85 @@
 */
 #include "mbed.h"
 #include "utils.h"
+#include "NRF2401P.h"
 #define debug 
 
 // Flags
+unsigned char flagNrfReceive = 0;
 // Variables
+char nrfStateL;
+float currentMaxL = 1.5; // Maximum current that each cube can consume.
+
 // tx nRF2401
 
 long long addrHub=0xBBBBBBBBBB;
+extern NRF2401P nrf1;
 extern int channel;
+InterruptIn nrf1IntL(PTC18); // IRQ of nRF24
+
+// -----------------------------------------------------------------------------
+// Interupt routines
+// -----------------------------------------------------------------------------
+/**
+*  Interrupt from nrf
+*
+*   RX_DR   Data Ready RX FIFO interrupt.
+*   TX_DS   Data Sent TX FIFO interrupt. Asserted when packet transmitted on TX. 
+*    -      - If AUTO_ACK is activated, this bit is set high only when ACK is received.
+*   MAX_RT  Maximum number of TX retransmits interrupt (tx failed)
+*   TX_FULL TX FIFO full flag.
+*/
+void intNrfL(){
+    spiNrf();
+    flagNrfReceive = nrf1.checkStatus();
+    if ((flagNrfReceive >>6) &0x01) { // RX_DR - RX ready
+        flagNrfReceive = 1;
+    }
+}
 
+void doNrfL(){
+    int width;
+    char data[33];
+#ifdef debug
+    printf("intNrf %02X %s \n\r",nrf1.status, nrf1.statusString());
+#endif
+    spiNrf();
+    char pipe = (nrf1.checkStatus() >> 1) & 7;
+    width= nrf1.getRxData(data);
+    data[width]='\0';
+    // Now check which pipe
+    if (pipe==0) { //The hub
+        // Check command
+        switch (data[0]){
+        case ('T'):{ // Time signal
+                time_t newTime;
+                sscanf (data,"%*c %x",&newTime);
+                set_time(newTime);
+                printf("  New Time %s\n\r", ctime(&newTime));
+                break;
+            }
+        case ('I'):{ // maxCurrent
+                sscanf (data,"%*c %x",&currentMaxL);
+                printf("  New Current %4.2f\n\r",currentMaxL);
+                break;
+            }
+        }
+    }
+    printf("==>RX Pipe %d [%d]:%s: %s\n\r",pipe, width, data, nrf1.statusString());
+    nrf1.clearStatus();
+}
+// -----------------------------------------------------------------------------
 // Initializaton
+// -----------------------------------------------------------------------------
+
+/**
+* Sets up the interrupts for the locker
+*/
+void initInteruptsLocker(){
+    nrf1IntL.fall(&intNrfL); // attach nrf
+}
+
+
 /**
 * Initialise for a locker
 */
@@ -30,7 +99,21 @@
 #ifdef debug
     printf("  Channel:%x, Hub Address %llx \n\r",channel, addrHub);
 #endif
+    // Setup nrf
 
+#ifdef debug
+    printf("Steup doNrf \n\r");
+#endif
+    spiNrf();
+    nrf1.quickRxSetup(channel, addrHub);
+#ifdef debug
+    nrf1.printDetails();
+    nrf1.checkStatus();
+    printf("Setup doNrf complete [nrf:%s]\n\r",nrf1.statusString());
+#endif
+
+    // Setup interupts
+    initInteruptsLocker();
 
 }
 // Interupt routines
@@ -39,5 +122,9 @@
 // Loop through slow routines
 
 void loopLocker(){
+    if (flagNrfReceive){
+        doNrfL();
+        flagNrfReceive = 0;
+    }
 
 }
\ No newline at end of file
--- a/main.cpp	Sun Jan 24 16:15:53 2016 +0000
+++ b/main.cpp	Thu Jan 28 14:38:45 2016 +0000
@@ -6,6 +6,7 @@
 */
 #include "mbed.h"
 #include "SDFileSystem.h"
+#include "NRF2401P.h"
 #include "utils.h"
 #include "battery.h"
 #include "hub.h"
@@ -19,11 +20,13 @@
 unsigned int sdVersion;
 int id;
 int channel;
+char txBuff[100];
 
 // Sd card
 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
 
-
+// nrf
+NRF2401P nrf1(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce)
 /**
 * Reads config file and configures the mbed
 */
@@ -62,7 +65,7 @@
             break;
         }
     case('H'): { // Hub
-            //initialiseHub(fp);
+            initialiseHub(fp);
             break;
         }
     case('L'): { // Locker
@@ -74,9 +77,9 @@
             break;
         }
     default:{
-    writeError ("Type declared in config not available.");
-    }
-        
+            writeError ("Type declared in config not available.");
+        }
+
     }
     fclose (fp);
 }
@@ -100,6 +103,27 @@
 #endif
     while (true) {
         // do slower routines
+        switch (role){
+        case('B'): { // Battery
+                loopBattery();
+                break;
+            }
+        case('H'): { // Hub
+                loopHub();
+                break;
+            }
+        case('L'): { // Locker
+                loopLocker();
+                break;
+            }
+        case('T'): { // Test
+                loopTest();
+                break;
+            }
+        default:{
+                writeError ("Type declared in config not available.");
+            }
+        }
 
     }
 }
\ No newline at end of file