Damien Frost / SolarNanoGrid_Field

Fork of SolarNanoGridv3 by SONG Project

Revision:
5:57b06b4b47c6
Parent:
3:350c167b9b2d
Child:
6:93ca8321f83e
--- a/SolarNanoGrid.cpp	Mon May 09 14:26:30 2016 +0000
+++ b/SolarNanoGrid.cpp	Thu Jun 02 16:41:40 2016 +0000
@@ -1,102 +1,103 @@
-#include "pinmap.h"
-#include "battery.h"
-#include "locker.h"
-#include "SolarNanoGrid.h"
-
-#define DEBUG
-#define INFOMESSAGES
-#define WARNMESSAGES
-#define ERRMESSAGES
-
-#ifdef DEBUG
-#define DBG(x, ...) printf("[SNG : DBG] "x"\r\n", ##__VA_ARGS__); 
-#else
-#define DBG(x, ...) 
-#endif
-
-#ifdef ERRMESSAGES
-#define ERR(x, ...) printf("[SNG : ERR] "x"\r\n", ##__VA_ARGS__); 
-#else
-#define ERR(x, ...) 
-#endif
-
-#ifdef WARNMESSAGES
-#define WARN(x, ...) printf("[SNG : WARN] "x"\r\n", ##__VA_ARGS__); 
-#else
-#define WARN(x, ...) 
-#endif
-
-#ifdef INFOMESSAGES
-#define INFO(x, ...) printf("[SNG : INFO] "x"\r\n", ##__VA_ARGS__); 
-#else
-#define INFO(x, ...) 
-#endif
+/**
+ *@section DESCRIPTION
+ * mbed SolarNanogrid  Library
+ *@section LICENSE
+ * Copyright (c) 2016, Malcolm McCulloch
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ * @file "SolarNanoGrid.c"
+ */
+#include <mbed.h>
+#include "SolarnanoGrid.h"
 
 // Constructor:
-SolarNanoGrid::SolarNanoGrid(SDFileSystem* sd, DigitalOut* led_1, DigitalOut* led_2){
+/**
+ * Constructor.
+ */
+SolarNanoGrid::SolarNanoGrid(FILE *fp=NULL):_fp(fp){
     // Save the sd card pointer:
-    _sd = sd;
-    
-    // initialize the SNG module:
-    char * name = "/sd/config.ini";
-    FILE *fp;
-    spiSD();
-    mkdir("/sd/binData",777);
-    mkdir("/sd/data",777);
-    mkdir("/sd/compress",777);
-
-    //Configuration
+    DBG("Solar init");
+    // Leds
+    ledRed = new DigitalOut(LED_RED,1);
+    ledGreen= new DigitalOut(LED_GREEN,1);
+    ledBlue = new DigitalOut(LED_BLUE,1);
+    // Realtime clock
+    time_t now= time(NULL);
+    struct tm * timeInf = localtime(&now);
+    INFO ("Time is now: %04d-%02d-%02d %02d:%02d:%02d \n\r",timeInf->tm_year+1900,timeInf->tm_mon+1,timeInf->tm_mday,timeInf->tm_hour, timeInf->tm_min,timeInf->tm_sec);
 
-    INFO("Reading config file...");
+    // Read config.ini
+    if (fscanf (fp,"%d %*c %*s",&sdVersion )!=1) ERR("Config: cannot read version");
+    if (fscanf (fp,"%u %*c %*s",&communityID  )!=1) ERR("Config: cannot read community ID");
+    if (fscanf (fp,"%x %*c %*s",&id)!=1) ERR("Config: cannot read ID");
+    if (fscanf (fp,"%x %*c %*s",&chan )!=1) ERR("Locker config: cannot read channel");
 
-    fp = fopen(name, "r");
-    if (fp == NULL) {
-        ERR("Config file cannot be read");
-        return;
-    }
-    if (fscanf (fp,"%c %*c %*s",&role )!=1) WARN("Config: cannot read role");
-    if (fscanf (fp,"%d %*c %*s",&sdVersion )!=1) WARN("Config: cannot read version");
-    if (fscanf (fp,"%x %*c %*s",&id )!=1) WARN("Config: cannot read id");
-    id = 1;
-
-    INFO("  Type:%c, Version %u, ID %d",role,sdVersion,id);
+    INFO("config.ini:  Version %u, Community %u ID %x Channel %x",sdVersion,communityID,id,chan);
+    spiSD();
+    mkdir("/sd/data", 777);
+    // Initialize nrf
+    ce = new DigitalOut(PTB20);
+    nrf = new NRF2401P(PTD6, PTD7, PTD5, PTD4, PTB20);       // Rev E
+    //nrf1 = new NRF2401P(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) REV D // irq ptc18 on k64f
 
 
-    switch (role) {
-        case('B'):
-        case('b'):
-            SongBat = new battery(nrf1, nrf1Int, led_1, led_2, id);
-            SongBat->initialiseBattery(fp);
-            break;
-        case('L'):
-        case('l'):
-            SongLock = new locker(nrf1, nrf1Int, led_1, led_2, id);
-            SongLock->initialiseLocker(fp);
-            break;
-    }
-    
-    return;
 }
 
-void SolarNanoGrid::loop(void)
+/**
+ * asks the user for the time
+ */
+
+void SolarNanoGrid::userSetRTCpc()
 {
-    switch (role) {
-        case('B'): { // Battery
-            SongBat->loop();
-            break;
-        }
-        case('M'): { // Master
-            // loopMaster();
-            break;
-        }
-        case('L'): { // Locker
-            SongLock->loop();
-            break;
-        }
+    // 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));
+
+}
+/**
+ * Flushes the rx and tx buffers and resets the status.
+ */
+void SolarNanoGrid::nrfFlush() {
+    spiNrf();
+    if(nrf!=NULL){
+        nrf->clearStatus();
+        nrf->flushRx();
+        nrf->flushTx();
+        nrf->clearStatus();
     }
 }
 
-// Turns SPI on for SD card
+/**
+ * Turns SPI on for SD card
+ */
 void SolarNanoGrid::spiSD(void)
 {
     //sd.select();
@@ -105,6 +106,9 @@
     return;
 }
 
+/**
+ * Turns SPI on for nrf
+ */
 void SolarNanoGrid::spiNrf(void)
 {
     //sd.deselect();
@@ -112,4 +116,3 @@
     pin_function(PTD7, 7); //Set SD_MISO as SPI, this is the same as the last number in those tables
     return;
 }
-