The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Committer:
defrost
Date:
Tue Sep 06 06:08:56 2016 +0000
Revision:
36:a5620262f296
Parent:
28:e85a86fc8b59
Turned off the charge rate update from hub

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 27:512cf6199b1d 187 // SD Version:
defrost 27:512cf6199b1d 188 DBG("SD Version:");
defrost 27:512cf6199b1d 189 if (fscanf (fp,"%d %*c %*s",&sdVersion )!=1){
defrost 27:512cf6199b1d 190 ERR("Config: cannot read version");
defrost 27:512cf6199b1d 191 result = false;
defrost 27:512cf6199b1d 192 }else{
defrost 27:512cf6199b1d 193 DBG("Read sdVersion: %d", sdVersion);
defrost 27:512cf6199b1d 194 }
defrost 22:93fde34d9a94 195 // Community ID:
defrost 22:93fde34d9a94 196 DBG("Community ID:");
defrost 22:93fde34d9a94 197 if (fscanf (fp,"%u %*c %*s",&communityID )!=1){
defrost 22:93fde34d9a94 198 ERR("Config: cannot read community ID");
defrost 22:93fde34d9a94 199 result = false;
defrost 22:93fde34d9a94 200 }else{
defrost 22:93fde34d9a94 201 DBG("Read community ID: %u", communityID);
defrost 22:93fde34d9a94 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 28:e85a86fc8b59 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 26:10e8b9908631 233
defrost 26:10e8b9908631 234 void doNewConfig(Serial *pc)
defrost 26:10e8b9908631 235 {
defrost 26:10e8b9908631 236 // H : Role
defrost 26:10e8b9908631 237 // 300 : Version
defrost 26:10e8b9908631 238 // 2540001 : Community_ID
defrost 26:10e8b9908631 239 // FF00 : ID
defrost 26:10e8b9908631 240 // 5a : Channel
defrost 26:10e8b9908631 241
defrost 26:10e8b9908631 242 char * name = "/sd/config.ini";
defrost 26:10e8b9908631 243 char buffer[32];
defrost 26:10e8b9908631 244 char CommunityIDbuff[32];
defrost 26:10e8b9908631 245 char IDbuff[32];
defrost 26:10e8b9908631 246 char channelbuff[32];
defrost 26:10e8b9908631 247 char Role;
defrost 26:10e8b9908631 248 FILE *fp;
defrost 27:512cf6199b1d 249 INFO("");
defrost 26:10e8b9908631 250 INFO("***********************************");
defrost 26:10e8b9908631 251 INFO("*** Creating a new config file. ***");
defrost 26:10e8b9908631 252 INFO("***********************************");
defrost 26:10e8b9908631 253 INFO("");
defrost 26:10e8b9908631 254 fp = fopen(name, "w");
defrost 26:10e8b9908631 255 //Empty buffer
defrost 26:10e8b9908631 256 while (pc->readable()) {
defrost 26:10e8b9908631 257 pc->getc();
defrost 26:10e8b9908631 258 }
defrost 26:10e8b9908631 259
defrost 26:10e8b9908631 260 char resp='k';
defrost 26:10e8b9908631 261
defrost 26:10e8b9908631 262 do {
defrost 26:10e8b9908631 263 INFO("Enter Role (B-Battery, H-Hmi, L-Locker, U-Utility, X-Exit:");
defrost 26:10e8b9908631 264 buffer[0] = 'k';
defrost 26:10e8b9908631 265 while(1){
defrost 26:10e8b9908631 266 // Get a user input:
defrost 26:10e8b9908631 267 pc->scanf("%c",buffer);
defrost 26:10e8b9908631 268 // Check the input:
defrost 26:10e8b9908631 269 if ((buffer[0]=='X') ||(buffer[0]=='x')) {
defrost 26:10e8b9908631 270 INFO("Exiting...");
defrost 26:10e8b9908631 271 return;
defrost 26:10e8b9908631 272 }
defrost 26:10e8b9908631 273 if((buffer[0]=='B')||(buffer[0]=='b')||(buffer[0]=='H')||(buffer[0]=='h')||(buffer[0]=='L')||(buffer[0]=='l')||(buffer[0]=='U')||(buffer[0]=='u')){
defrost 26:10e8b9908631 274 break;
defrost 26:10e8b9908631 275 }
defrost 26:10e8b9908631 276 // If we get here, the input sucks:
defrost 26:10e8b9908631 277 INFO("Invalid selection, please enter the Role (B-Battery, H-Hmi, L-Locker, U-Utility, X-Exit:");
defrost 26:10e8b9908631 278 }
defrost 26:10e8b9908631 279 // Save the result:
defrost 26:10e8b9908631 280 Role = buffer[0];
defrost 26:10e8b9908631 281 INFO("Role = %c, selected.", Role);
defrost 26:10e8b9908631 282 INFO("");
defrost 26:10e8b9908631 283
defrost 26:10e8b9908631 284 // Get the Community ID:
defrost 26:10e8b9908631 285 INFO("Enter community ID (Decimal int: e.g. 2540001):");
defrost 26:10e8b9908631 286 pc->scanf("%s",CommunityIDbuff);
defrost 26:10e8b9908631 287 INFO("Community ID= %s",CommunityIDbuff);
defrost 26:10e8b9908631 288 INFO("");
defrost 26:10e8b9908631 289
defrost 26:10e8b9908631 290 // Get the ID:
defrost 26:10e8b9908631 291 INFO("Enter ID: (Hex: Locker (2 Bytes) Pod(1 Byte) Battery number (1Byte) e.g 01a1)");
defrost 26:10e8b9908631 292 pc->scanf("%s",IDbuff);
defrost 26:10e8b9908631 293 INFO("Locker ID= %s",IDbuff);
defrost 26:10e8b9908631 294 INFO("");
defrost 26:10e8b9908631 295
defrost 26:10e8b9908631 296 // Get the Channel:
defrost 26:10e8b9908631 297 INFO("Enter Channel: (default: 5a)");
defrost 26:10e8b9908631 298 pc->scanf("%s",channelbuff);
defrost 26:10e8b9908631 299 INFO("Channel %s",channelbuff);
defrost 26:10e8b9908631 300 INFO("");
defrost 26:10e8b9908631 301
defrost 26:10e8b9908631 302 // Display the results:
defrost 26:10e8b9908631 303 INFO("The new configuration file is:");
defrost 26:10e8b9908631 304 INFO("");
defrost 26:10e8b9908631 305 INFO("%c : Role",Role);
defrost 26:10e8b9908631 306 INFO("300 : Version");
defrost 26:10e8b9908631 307 INFO("%s : CommunityID",CommunityIDbuff);
defrost 26:10e8b9908631 308 INFO("%s : ID",IDbuff);
defrost 26:10e8b9908631 309 INFO("%s : channel",channelbuff);
defrost 26:10e8b9908631 310 INFO("");
defrost 26:10e8b9908631 311 //Save the result to a file:
defrost 26:10e8b9908631 312 INFO("Saving the new file to: %s...", name);
defrost 26:10e8b9908631 313 fp = fopen(name, "w");
defrost 27:512cf6199b1d 314 fprintf (fp,"%c : Role\r\n",Role);
defrost 27:512cf6199b1d 315 fprintf (fp,"300 : Version\r\n");
defrost 26:10e8b9908631 316 fprintf (fp,"%s : CommunityID\r\n",CommunityIDbuff);
defrost 26:10e8b9908631 317 fprintf (fp,"%s : ID\r\n",IDbuff);
defrost 26:10e8b9908631 318 fprintf (fp,"%s : channel\r\n",channelbuff);
defrost 26:10e8b9908631 319 fclose(fp);
defrost 26:10e8b9908631 320 INFO("Config file updated.");
defrost 26:10e8b9908631 321
defrost 26:10e8b9908631 322 // Ask if we want to continue:
defrost 26:10e8b9908631 323 INFO("OK? (y/n)");
defrost 26:10e8b9908631 324 pc->scanf("%s",&buffer);
defrost 26:10e8b9908631 325 resp = buffer[0];
defrost 26:10e8b9908631 326 INFO("%c", resp);
defrost 26:10e8b9908631 327
defrost 26:10e8b9908631 328 } while(resp!='y' and resp!='Y');
defrost 26:10e8b9908631 329
defrost 26:10e8b9908631 330 INFO("Exiting...");
defrost 26:10e8b9908631 331 INFO("");
defrost 26:10e8b9908631 332 return;
defrost 26:10e8b9908631 333 }
defrost 16:919e37e5a895 334