Damien Frost / SolarNanoGrid_Field

Fork of SolarNanoGridv3 by SONG Project

Committer:
defrost
Date:
Fri Jun 03 14:55:12 2016 +0000
Revision:
10:30c9e8df0032
Parent:
7:5db9aeec9c2e
Child:
11:87ab310924f0
- Warnings all cleared up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
defrost 5:57b06b4b47c6 1 /**
defrost 5:57b06b4b47c6 2 *@section DESCRIPTION
defrost 5:57b06b4b47c6 3 * mbed SolarNanogrid Library
defrost 5:57b06b4b47c6 4 *@section LICENSE
defrost 5:57b06b4b47c6 5 * Copyright (c) 2016, Malcolm McCulloch
defrost 5:57b06b4b47c6 6 *
defrost 5:57b06b4b47c6 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
defrost 5:57b06b4b47c6 8 * of this software and associated documentation files (the "Software"), to deal
defrost 5:57b06b4b47c6 9 * in the Software without restriction, including without limitation the rights
defrost 5:57b06b4b47c6 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
defrost 5:57b06b4b47c6 11 * copies of the Software, and to permit persons to whom the Software is
defrost 5:57b06b4b47c6 12 * furnished to do so, subject to the following conditions:
defrost 5:57b06b4b47c6 13 *
defrost 5:57b06b4b47c6 14 * The above copyright notice and this permission notice shall be included in
defrost 5:57b06b4b47c6 15 * all copies or substantial portions of the Software.
defrost 5:57b06b4b47c6 16 *
defrost 5:57b06b4b47c6 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
defrost 5:57b06b4b47c6 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
defrost 5:57b06b4b47c6 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
defrost 5:57b06b4b47c6 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
defrost 5:57b06b4b47c6 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
defrost 5:57b06b4b47c6 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
defrost 5:57b06b4b47c6 23 * THE SOFTWARE.
defrost 5:57b06b4b47c6 24 * @file "SolarNanoGrid.c"
defrost 5:57b06b4b47c6 25 */
defrost 5:57b06b4b47c6 26 #include <mbed.h>
defrost 6:93ca8321f83e 27 #include "SolarNanoGrid.h"
defrost 10:30c9e8df0032 28 #define FUNCNAME "SNGC"
defrost 10:30c9e8df0032 29 #include "defs.h"
defrost 1:df924e0126d1 30
defrost 1:df924e0126d1 31 // Constructor:
defrost 5:57b06b4b47c6 32 /**
defrost 5:57b06b4b47c6 33 * Constructor.
defrost 5:57b06b4b47c6 34 */
defrost 5:57b06b4b47c6 35 SolarNanoGrid::SolarNanoGrid(FILE *fp=NULL):_fp(fp){
defrost 1:df924e0126d1 36 // Save the sd card pointer:
defrost 5:57b06b4b47c6 37 DBG("Solar init");
defrost 5:57b06b4b47c6 38 // Leds
defrost 5:57b06b4b47c6 39 ledRed = new DigitalOut(LED_RED,1);
defrost 5:57b06b4b47c6 40 ledGreen= new DigitalOut(LED_GREEN,1);
defrost 5:57b06b4b47c6 41 ledBlue = new DigitalOut(LED_BLUE,1);
defrost 5:57b06b4b47c6 42 // Realtime clock
defrost 5:57b06b4b47c6 43 time_t now= time(NULL);
defrost 5:57b06b4b47c6 44 struct tm * timeInf = localtime(&now);
defrost 5:57b06b4b47c6 45 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);
defrost 1:df924e0126d1 46
defrost 5:57b06b4b47c6 47 // Read config.ini
defrost 5:57b06b4b47c6 48 if (fscanf (fp,"%d %*c %*s",&sdVersion )!=1) ERR("Config: cannot read version");
defrost 5:57b06b4b47c6 49 if (fscanf (fp,"%u %*c %*s",&communityID )!=1) ERR("Config: cannot read community ID");
defrost 5:57b06b4b47c6 50 if (fscanf (fp,"%x %*c %*s",&id)!=1) ERR("Config: cannot read ID");
defrost 5:57b06b4b47c6 51 if (fscanf (fp,"%x %*c %*s",&chan )!=1) ERR("Locker config: cannot read channel");
defrost 1:df924e0126d1 52
defrost 5:57b06b4b47c6 53 INFO("config.ini: Version %u, Community %u ID %x Channel %x",sdVersion,communityID,id,chan);
defrost 5:57b06b4b47c6 54 spiSD();
defrost 5:57b06b4b47c6 55 mkdir("/sd/data", 777);
defrost 5:57b06b4b47c6 56 // Initialize nrf
defrost 5:57b06b4b47c6 57 ce = new DigitalOut(PTB20);
defrost 5:57b06b4b47c6 58 nrf = new NRF2401P(PTD6, PTD7, PTD5, PTD4, PTB20); // Rev E
defrost 5:57b06b4b47c6 59 //nrf1 = new NRF2401P(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) REV D // irq ptc18 on k64f
defrost 7:5db9aeec9c2e 60
defrost 7:5db9aeec9c2e 61 // initialize all of the variables:
defrost 7:5db9aeec9c2e 62 sdVersion=0;
defrost 7:5db9aeec9c2e 63 id=0;
defrost 7:5db9aeec9c2e 64 communityID=0;
defrost 7:5db9aeec9c2e 65 chan=0;
defrost 7:5db9aeec9c2e 66
defrost 7:5db9aeec9c2e 67
defrost 7:5db9aeec9c2e 68 *ledRed=NULL;
defrost 7:5db9aeec9c2e 69 *ledGreen=NULL;
defrost 7:5db9aeec9c2e 70 *ledBlue=NULL;
defrost 7:5db9aeec9c2e 71 _fp=NULL;
defrost 7:5db9aeec9c2e 72 nrf=NULL;
defrost 7:5db9aeec9c2e 73
defrost 1:df924e0126d1 74
defrost 1:df924e0126d1 75
defrost 1:df924e0126d1 76 }
defrost 1:df924e0126d1 77
defrost 5:57b06b4b47c6 78 /**
defrost 5:57b06b4b47c6 79 * asks the user for the time
defrost 5:57b06b4b47c6 80 */
defrost 5:57b06b4b47c6 81
defrost 5:57b06b4b47c6 82 void SolarNanoGrid::userSetRTCpc()
defrost 1:df924e0126d1 83 {
defrost 5:57b06b4b47c6 84 // get the current time from the terminal
defrost 5:57b06b4b47c6 85 struct tm t;
defrost 5:57b06b4b47c6 86 printf("Enter current date :\n\r");
defrost 5:57b06b4b47c6 87 printf("YYYY MM DD [enter]\n\r");
defrost 5:57b06b4b47c6 88 scanf("%d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday);
defrost 5:57b06b4b47c6 89 printf("Enter current time:\n\r");
defrost 5:57b06b4b47c6 90 printf("HH MM SS [enter]\n\r");
defrost 5:57b06b4b47c6 91 scanf("%d %d %d", &t.tm_hour, &t.tm_min, &t.tm_sec);
defrost 5:57b06b4b47c6 92
defrost 5:57b06b4b47c6 93 // adjust for tm structure required values
defrost 5:57b06b4b47c6 94 t.tm_year = t.tm_year - 1900;
defrost 5:57b06b4b47c6 95 t.tm_mon = t.tm_mon - 1;
defrost 5:57b06b4b47c6 96
defrost 5:57b06b4b47c6 97 // set the time
defrost 5:57b06b4b47c6 98 set_time(mktime(&t));
defrost 5:57b06b4b47c6 99
defrost 5:57b06b4b47c6 100 }
defrost 5:57b06b4b47c6 101 /**
defrost 5:57b06b4b47c6 102 * Flushes the rx and tx buffers and resets the status.
defrost 5:57b06b4b47c6 103 */
defrost 5:57b06b4b47c6 104 void SolarNanoGrid::nrfFlush() {
defrost 5:57b06b4b47c6 105 spiNrf();
defrost 5:57b06b4b47c6 106 if(nrf!=NULL){
defrost 5:57b06b4b47c6 107 nrf->clearStatus();
defrost 5:57b06b4b47c6 108 nrf->flushRx();
defrost 5:57b06b4b47c6 109 nrf->flushTx();
defrost 5:57b06b4b47c6 110 nrf->clearStatus();
defrost 1:df924e0126d1 111 }
defrost 1:df924e0126d1 112 }
defrost 1:df924e0126d1 113
defrost 5:57b06b4b47c6 114 /**
defrost 5:57b06b4b47c6 115 * Turns SPI on for SD card
defrost 5:57b06b4b47c6 116 */
defrost 2:929cf7fc6998 117 void SolarNanoGrid::spiSD(void)
defrost 2:929cf7fc6998 118 {
defrost 2:929cf7fc6998 119 //sd.select();
defrost 2:929cf7fc6998 120 pin_function(PTE1 , 7); //Set SD_MISO as SPI, this is the same as the last number in those tables
defrost 2:929cf7fc6998 121 pin_function(PTD7, 1); //pin function 1 is GPIO
defrost 2:929cf7fc6998 122 return;
defrost 2:929cf7fc6998 123 }
defrost 2:929cf7fc6998 124
defrost 5:57b06b4b47c6 125 /**
defrost 5:57b06b4b47c6 126 * Turns SPI on for nrf
defrost 5:57b06b4b47c6 127 */
defrost 2:929cf7fc6998 128 void SolarNanoGrid::spiNrf(void)
defrost 2:929cf7fc6998 129 {
defrost 2:929cf7fc6998 130 //sd.deselect();
defrost 2:929cf7fc6998 131 pin_function(PTE1, 1); //pin function 1 is GPIO
defrost 2:929cf7fc6998 132 pin_function(PTD7, 7); //Set SD_MISO as SPI, this is the same as the last number in those tables
defrost 2:929cf7fc6998 133 return;
defrost 2:929cf7fc6998 134 }