The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Committer:
defrost
Date:
Wed Jun 15 00:20:18 2016 +0000
Revision:
22:93fde34d9a94
Parent:
16:919e37e5a895
Child:
26:10e8b9908631
- Green LED now toggles the Red LED because of conflict with ionQube hardware; - Added numerous debug statements

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 13:de43f28c0365 28 #define FUNCNAME "SNGC"
defrost 13:de43f28c0365 29 #include "defs.h"
defrost 1:df924e0126d1 30 // Constructor:
defrost 5:57b06b4b47c6 31 /**
defrost 5:57b06b4b47c6 32 * Constructor.
defrost 5:57b06b4b47c6 33 */
defrost 13:de43f28c0365 34 SolarNanoGrid::SolarNanoGrid(FILE *fp=NULL, Serial *pc=NULL):pc(pc),_fp(fp){
defrost 1:df924e0126d1 35 // Save the sd card pointer:
defrost 5:57b06b4b47c6 36 DBG("Solar init");
defrost 5:57b06b4b47c6 37 // Leds
defrost 5:57b06b4b47c6 38 ledRed = new DigitalOut(LED_RED,1);
defrost 22:93fde34d9a94 39 ledGreen= new DigitalOut(LED_RED,1); // The converter uses the saem DIO as the green led as an interrupt in.
defrost 5:57b06b4b47c6 40 ledBlue = new DigitalOut(LED_BLUE,1);
defrost 5:57b06b4b47c6 41 // Realtime clock
defrost 5:57b06b4b47c6 42 time_t now= time(NULL);
defrost 5:57b06b4b47c6 43 struct tm * timeInf = localtime(&now);
defrost 5:57b06b4b47c6 44 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 45
defrost 5:57b06b4b47c6 46 // Read config.ini
defrost 5:57b06b4b47c6 47 if (fscanf (fp,"%d %*c %*s",&sdVersion )!=1) ERR("Config: cannot read version");
defrost 5:57b06b4b47c6 48 if (fscanf (fp,"%u %*c %*s",&communityID )!=1) ERR("Config: cannot read community ID");
defrost 5:57b06b4b47c6 49 if (fscanf (fp,"%x %*c %*s",&id)!=1) ERR("Config: cannot read ID");
defrost 5:57b06b4b47c6 50 if (fscanf (fp,"%x %*c %*s",&chan )!=1) ERR("Locker config: cannot read channel");
defrost 1:df924e0126d1 51
defrost 5:57b06b4b47c6 52 INFO("config.ini: Version %u, Community %u ID %x Channel %x",sdVersion,communityID,id,chan);
defrost 5:57b06b4b47c6 53 spiSD();
defrost 5:57b06b4b47c6 54 mkdir("/sd/data", 777);
defrost 5:57b06b4b47c6 55 // Initialize nrf
defrost 5:57b06b4b47c6 56 ce = new DigitalOut(PTB20);
defrost 5:57b06b4b47c6 57 nrf = new NRF2401P(PTD6, PTD7, PTD5, PTD4, PTB20); // Rev E
defrost 5:57b06b4b47c6 58 //nrf1 = new NRF2401P(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) REV D // irq ptc18 on k64f
defrost 1:df924e0126d1 59
defrost 1:df924e0126d1 60
defrost 1:df924e0126d1 61 }
defrost 1:df924e0126d1 62
defrost 5:57b06b4b47c6 63 /**
defrost 5:57b06b4b47c6 64 * asks the user for the time
defrost 5:57b06b4b47c6 65 */
defrost 5:57b06b4b47c6 66
defrost 5:57b06b4b47c6 67 void SolarNanoGrid::userSetRTCpc()
defrost 1:df924e0126d1 68 {
defrost 5:57b06b4b47c6 69 // get the current time from the terminal
defrost 5:57b06b4b47c6 70 struct tm t;
defrost 5:57b06b4b47c6 71 printf("Enter current date :\n\r");
defrost 5:57b06b4b47c6 72 printf("YYYY MM DD [enter]\n\r");
defrost 5:57b06b4b47c6 73 scanf("%d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday);
defrost 5:57b06b4b47c6 74 printf("Enter current time:\n\r");
defrost 5:57b06b4b47c6 75 printf("HH MM SS [enter]\n\r");
defrost 5:57b06b4b47c6 76 scanf("%d %d %d", &t.tm_hour, &t.tm_min, &t.tm_sec);
defrost 5:57b06b4b47c6 77
defrost 5:57b06b4b47c6 78 // adjust for tm structure required values
defrost 5:57b06b4b47c6 79 t.tm_year = t.tm_year - 1900;
defrost 5:57b06b4b47c6 80 t.tm_mon = t.tm_mon - 1;
defrost 5:57b06b4b47c6 81
defrost 5:57b06b4b47c6 82 // set the time
defrost 5:57b06b4b47c6 83 set_time(mktime(&t));
defrost 5:57b06b4b47c6 84
defrost 5:57b06b4b47c6 85 }
defrost 5:57b06b4b47c6 86 /**
defrost 5:57b06b4b47c6 87 * Flushes the rx and tx buffers and resets the status.
defrost 5:57b06b4b47c6 88 */
defrost 5:57b06b4b47c6 89 void SolarNanoGrid::nrfFlush() {
epgmdm 11:87ab310924f0 90 spiNRF();
defrost 5:57b06b4b47c6 91 if(nrf!=NULL){
defrost 5:57b06b4b47c6 92 nrf->clearStatus();
defrost 5:57b06b4b47c6 93 nrf->flushRx();
defrost 5:57b06b4b47c6 94 nrf->flushTx();
defrost 5:57b06b4b47c6 95 nrf->clearStatus();
defrost 1:df924e0126d1 96 }
defrost 1:df924e0126d1 97 }
defrost 1:df924e0126d1 98
defrost 5:57b06b4b47c6 99 /**
defrost 5:57b06b4b47c6 100 * Turns SPI on for SD card
defrost 5:57b06b4b47c6 101 */
defrost 22:93fde34d9a94 102 void spiSD(void)
defrost 2:929cf7fc6998 103 {
defrost 2:929cf7fc6998 104 //sd.select();
defrost 2:929cf7fc6998 105 pin_function(PTE1 , 7); //Set SD_MISO as SPI, this is the same as the last number in those tables
defrost 2:929cf7fc6998 106 pin_function(PTD7, 1); //pin function 1 is GPIO
defrost 2:929cf7fc6998 107 return;
defrost 2:929cf7fc6998 108 }
defrost 2:929cf7fc6998 109
defrost 5:57b06b4b47c6 110 /**
defrost 5:57b06b4b47c6 111 * Turns SPI on for nrf
defrost 5:57b06b4b47c6 112 */
defrost 22:93fde34d9a94 113 void spiNRF(void)
defrost 2:929cf7fc6998 114 {
defrost 2:929cf7fc6998 115 //sd.deselect();
defrost 2:929cf7fc6998 116 pin_function(PTE1, 1); //pin function 1 is GPIO
defrost 2:929cf7fc6998 117 pin_function(PTD7, 7); //Set SD_MISO as SPI, this is the same as the last number in those tables
defrost 2:929cf7fc6998 118 return;
defrost 2:929cf7fc6998 119 }
epgmdm 11:87ab310924f0 120
epgmdm 11:87ab310924f0 121 /**
epgmdm 11:87ab310924f0 122 * Set NRF as RX and flush
epgmdm 11:87ab310924f0 123 */
epgmdm 11:87ab310924f0 124 void SolarNanoGrid::setAsRX(long long addr){
epgmdm 11:87ab310924f0 125
epgmdm 11:87ab310924f0 126 spiNRF();
epgmdm 11:87ab310924f0 127 nrf->quickRxSetup(chan, addr); // Pipe 0
epgmdm 11:87ab310924f0 128 nrf->setRadio(0x01, 0x03); // 2MB/S 0dB
epgmdm 11:87ab310924f0 129 nrfFlush();
epgmdm 11:87ab310924f0 130 }
defrost 13:de43f28c0365 131 /**
defrost 13:de43f28c0365 132 * loop
epgmdm 11:87ab310924f0 133
defrost 13:de43f28c0365 134 void SolarNanoGrid::loop(){
defrost 13:de43f28c0365 135 ;
defrost 13:de43f28c0365 136 }
defrost 13:de43f28c0365 137
epgmdm 11:87ab310924f0 138 * Sets NRF as TX and flushes the NRF
epgmdm 11:87ab310924f0 139 */
epgmdm 11:87ab310924f0 140 void SolarNanoGrid::setAsTX(long long addr){
epgmdm 11:87ab310924f0 141 DBG("setAsTx");
epgmdm 11:87ab310924f0 142 spiNRF();
epgmdm 11:87ab310924f0 143 nrf->quickTxSetup(chan, addr);
epgmdm 11:87ab310924f0 144 nrf->setRadio(0x01, 0x03); // 2MB/S 0dB
epgmdm 11:87ab310924f0 145 nrf->setTxRetry(0x0F, 0x0F);
epgmdm 11:87ab310924f0 146 nrfFlush();
epgmdm 11:87ab310924f0 147 DBG("Nrf Details:");
epgmdm 11:87ab310924f0 148 #ifdef DEBUG
epgmdm 11:87ab310924f0 149 nrf->printDetails();
epgmdm 11:87ab310924f0 150 #endif
epgmdm 11:87ab310924f0 151
defrost 16:919e37e5a895 152 }
defrost 16:919e37e5a895 153
defrost 16:919e37e5a895 154 SongSettings::SongSettings(void){
defrost 16:919e37e5a895 155 // Just set everything to null:
defrost 16:919e37e5a895 156 role = NULL;
defrost 16:919e37e5a895 157 sdVersion = NULL;
defrost 16:919e37e5a895 158 communityID = NULL;
defrost 16:919e37e5a895 159 id = NULL;
defrost 16:919e37e5a895 160 chan = NULL;
defrost 16:919e37e5a895 161 return;
defrost 16:919e37e5a895 162 }
defrost 16:919e37e5a895 163
defrost 16:919e37e5a895 164 bool SongSettings::LoadSongSettings(void){
defrost 16:919e37e5a895 165
defrost 16:919e37e5a895 166 char * name = "/sd/config.ini";
defrost 16:919e37e5a895 167 bool result = true;
defrost 16:919e37e5a895 168 INFO("Loading configuration from %s", name);
defrost 16:919e37e5a895 169 FILE *fp;
defrost 22:93fde34d9a94 170 INFO("Loading config file into settings...");
defrost 22:93fde34d9a94 171 spiSD();
defrost 16:919e37e5a895 172 fp = fopen(name, "r");
defrost 16:919e37e5a895 173 if(fp == NULL) {
defrost 16:919e37e5a895 174 ERR("Config file cannot be read, USE PC TO CREATE config.ini");
defrost 16:919e37e5a895 175 return false;
defrost 16:919e37e5a895 176 }else{
defrost 22:93fde34d9a94 177 DBG("Reading values...");
defrost 16:919e37e5a895 178 // Read config.ini
defrost 22:93fde34d9a94 179 // Role:
defrost 22:93fde34d9a94 180 DBG("Role:");
defrost 22:93fde34d9a94 181 if (fscanf (fp,"%c %*c %*s",&role )!=1){
defrost 16:919e37e5a895 182 ERR("Config: cannot read role");
defrost 16:919e37e5a895 183 result = false;
defrost 22:93fde34d9a94 184 }else{
defrost 22:93fde34d9a94 185 DBG("Read Role: %c", role);
defrost 16:919e37e5a895 186 }
defrost 22:93fde34d9a94 187 // Community ID:
defrost 22:93fde34d9a94 188 DBG("Community ID:");
defrost 22:93fde34d9a94 189 if (fscanf (fp,"%u %*c %*s",&communityID )!=1){
defrost 22:93fde34d9a94 190 ERR("Config: cannot read community ID");
defrost 22:93fde34d9a94 191 result = false;
defrost 22:93fde34d9a94 192 }else{
defrost 22:93fde34d9a94 193 DBG("Read community ID: %u", communityID);
defrost 22:93fde34d9a94 194 }
defrost 22:93fde34d9a94 195 // SD Version:
defrost 22:93fde34d9a94 196 DBG("SD Version:");
defrost 22:93fde34d9a94 197 if (fscanf (fp,"%d %*c %*s",&sdVersion )!=1){
defrost 16:919e37e5a895 198 ERR("Config: cannot read version");
defrost 16:919e37e5a895 199 result = false;
defrost 22:93fde34d9a94 200 }else{
defrost 22:93fde34d9a94 201 DBG("Read sdVersion: %d", sdVersion);
defrost 16:919e37e5a895 202 }
defrost 22:93fde34d9a94 203 // ID:
defrost 22:93fde34d9a94 204 DBG("ID:");
defrost 22:93fde34d9a94 205 if (fscanf (fp,"%x %*c %*s",&id)!=1){
defrost 16:919e37e5a895 206 ERR("Config: cannot read ID");
defrost 16:919e37e5a895 207 result = false;
defrost 22:93fde34d9a94 208 }else{
defrost 22:93fde34d9a94 209 DBG("Read read ID: %x", id);
defrost 16:919e37e5a895 210 }
defrost 22:93fde34d9a94 211 // Channel:
defrost 22:93fde34d9a94 212 DBG("Channel:");
defrost 22:93fde34d9a94 213 if (fscanf (fp,"%x %*c %*s",&chan )!=1){
defrost 16:919e37e5a895 214 ERR("Locker config: cannot read channel");
defrost 16:919e37e5a895 215 result = false;
defrost 22:93fde34d9a94 216 }else{
defrost 22:93fde34d9a94 217 DBG("Read channel: %x", chan);
defrost 16:919e37e5a895 218 }
defrost 16:919e37e5a895 219
defrost 16:919e37e5a895 220 if(result == true){
defrost 16:919e37e5a895 221 // create the log directory string, this does NOT create the directory!
defrost 22:93fde34d9a94 222 sprintf(LogDir, "/sd/data/%02x%04x", (communityID&0XFF),(id&0XFFFF));
defrost 16:919e37e5a895 223 // Print everything back:
defrost 16:919e37e5a895 224 INFO("config.ini: Version %u, Community %u ID %x Channel %x, Log Directory: %s",sdVersion, communityID, id, chan, LogDir);
defrost 16:919e37e5a895 225 }else{
defrost 16:919e37e5a895 226 ERR("Some of the settings were not loaded correctly. Perhaps %s is corrupted.", name);
defrost 16:919e37e5a895 227 }
defrost 16:919e37e5a895 228 return result;
defrost 16:919e37e5a895 229 }
defrost 16:919e37e5a895 230
defrost 16:919e37e5a895 231 }
defrost 16:919e37e5a895 232
defrost 16:919e37e5a895 233