The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Revision:
11:87ab310924f0
Parent:
10:30c9e8df0032
Child:
13:de43f28c0365
--- a/Locker/Locker.cpp	Fri Jun 03 14:55:12 2016 +0000
+++ b/Locker/Locker.cpp	Wed Jun 08 22:12:52 2016 +0000
@@ -30,17 +30,12 @@
 #define FUNCNAME "LOCKER"
 #include "defs.h"
 
-Locker::Locker(FILE* fp) :
-SolarNanoGrid(fp) {
-    DBG("Initialize class");
-    // No other information needed from the config file
-    fclose(fp);
+Locker::Locker(FILE* fp,Serial *pc) :
+SolarNanoGrid(fp,pc) {
+    DBG("Initialize class locker");
+
 
     // ***** variables initialization *****
-    // Protected interrupts 
-    button=NULL;
-    rxWatch=NULL;
-    oneSecond=NULL;
 
     // Protected variables: *
     /**
@@ -55,18 +50,11 @@
     /**
      * Array of battery states
      */
-    *battIn=NULL; // array of all battery states
-    *battPipes=NULL; // array of RX pipes
-    *temp=NULL;
-    *sdBuffer=NULL;
     sdBuffPnt=0;
     battQP=0; // Battery queue pointer
     pipeQP=0; // pipe q pointer (Add 2)
     pipe = 0;
     width = 0;
-
-    *timeValue=NULL;
-
     now=0;
     lastRxTme=0;
 
@@ -74,9 +62,13 @@
     flagRotate=false;
     flagNrf=0;
     flagOneSecond=0;
-    
+
     // ***** end of varibale initialization *****
 
+    // No other information needed from the config file
+    fclose(fp);
+    DBG("Close file");
+
     battIn = (char *) calloc(256, 1);
     battPipes = (char *) calloc(6, 1);
     for (int i = 0; i < 4; i++) {
@@ -92,15 +84,38 @@
     // Setup NRF
     //
     openAddr = ((long long) communityID << 16) + (id & 0XFFFF);
-    addrUtil = openAddr & 0XFFFFFF0000 | 0xFE00;
+    addrUtil = (openAddr & 0XFFFFFF0000) | 0xFE00;
 
     DBG("  Channel:%x, Address %x Util Address %x", chan, openAddr, addrUtil);
 
-    setAsRX();
+    setAsRX(addrUtil);
+    nrf->setRxAddress(openAddr, 1); //Default battery pipe address 1
+
     DBG("Nrf Details:");
 #ifdef DEBUG
     nrf->printDetails();
 #endif
+    //
+    //Pods
+    //
+    pods[0]=new DigitalOut(PTE24,0);
+    pods[1]=new DigitalOut(PTE25,0);
+    pods[2]=new DigitalOut(PTD1,0);
+    pods[3]=new DigitalOut(PTD3,0);
+    pods[4]=new DigitalOut(PTD2,0);
+    pods[5]=new DigitalOut(PTD0,0);
+    pods[6]=new DigitalOut(PTC4,0);
+    pods[7]=new DigitalOut(PTE26,0);
+    pods[8]=new DigitalOut(PTC5,0);
+    pods[9]=new DigitalOut(PTC7,0);
+    pods[10]=new DigitalOut(PTC0,0);
+    pods[11]=new DigitalOut(PTC9,0);
+    pods[12]=new DigitalOut(PTC8,0);
+    pods[13]=new DigitalOut(PTC1,0);
+    pods[14]=new DigitalOut(PTB19,0);
+    pods[15]=new DigitalOut(PTB18,0);
+
+
 
     //
     // Interrupts
@@ -149,7 +164,7 @@
     //
     //Get status and pipe
     //
-    spiNrf();
+    spiNRF();
     status = nrf->checkStatus();
     pipe = (status >> 1);
     pipe = (pipe & 0x0007);
@@ -173,6 +188,7 @@
             DBG("intNrf>%s %x",dataRx,bID);
         }
         if (pipe == 1) { // Open channel
+            DBG("intNrf>Pipe 1  %s",dataRx);
             if (dataRx[0] == 'C') { //Request for check in
                 int battID;
                 sscanf(&dataRx[2], "%x", &battID);
@@ -184,15 +200,30 @@
                     WARN("Battery %04X is already checked in.", battID);
                 }
                 flagRotate = 1;
+            }else  if (dataRx[0] == 'b') { //Battery drop off
+                DBG("intNrf>User check in %s",dataRx);
+                sscanf (&dataRx[2], "%x %x", &userIDDrop, &numBatDrop);
+                int pod = (userIDDrop&0XF0)>>4;
+                if (dataRx[1] == 'd'){
+                    turnPodOn(pod,1);
+                    *ledRed=0;
+                }
+                if (dataRx[1] == 'p'){
+                    turnPodOn(pod,0);
+                    *ledRed=1;
+                }
+
+
             }
         }else if (pipe>1) {
             flagNrf = 1;
         }
 
         lastRxTme = now;
+
     }
 
-    //  DBG("intNRF");
+    DBG("intNRF> int rx");
     nrf->clearStatus();
     *ledBlue = !*ledBlue;
 
@@ -218,7 +249,7 @@
                 spiSD();
                 DBG("doNrf>>>Making dir %s:",dirNames[p]);
                 int ok = mkdir(dirNames[p],777);
-                spiNrf();
+                spiNRF();
                 DBG("doNrf>>>Directory name[%d] = <%s> OK=%d",p,dirNames[p],ok);
                 *ce = 1;
                 break;
@@ -233,7 +264,7 @@
                 // Make sure file is created and reset
                 FILE *fp = fopen(temp,"wb");
                 fclose(fp);
-                spiNrf();
+                spiNRF();
                 DBG("doNrf>>>File name[%d] = <%s>",p,fileNames[p]);
                 *ce = 1;
                 break;
@@ -392,27 +423,35 @@
     } else {
         ;
     }
-    spiNrf();
+    spiNRF();
     t.stop();
     DBG("flush> Timer = %d ms %d us",t.read_ms(), t.read_us())
 }
 
-/**
- * Set NRF as RX
- */
-void Locker::setAsRX(){
-
-    spiNrf();
-    nrf->quickRxSetup(chan, addrUtil); // Pipe 0
-    nrf->setRxAddress(openAddr, 1); //Default battery pipe address 1
-    nrf->setRadio(0x01, 0x03); // 2MB/S  0dB
-    nrfFlush();
-}
+///**
+// * Set NRF as RX
+// */
+//void Locker::setAsRX(){
+//
+//  spiNRF();
+//  nrf->quickRxSetup(chan, addrUtil); // Pipe 0
+//
+//  nrf->setRadio(0x01, 0x03); // 2MB/S  0dB
+//  nrfFlush();
+//}
+//
+///**
+// * Set NRF as TX
+// */
+//void Locker::setAsTX(){
+//  spiNRF();
+//
+//}
 
 /**
- * Set NRF as TX
+ * Turns a pod power on
  */
-void Locker::setAsTX(){
-    spiNrf();
-
+void Locker::turnPodOn(int pod,int on){
+    DBG("Pod %d on %d",pod,on);
+    pods[pod]->write(on);
 }