The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Committer:
defrost
Date:
Fri Jun 10 14:17:38 2016 +0000
Revision:
13:de43f28c0365
Parent:
12:d9b13f31206d
Child:
17:dc19b3b39790
- File transmission is starting to work, it still does not complete.; - Fixed all of the warnings in the code. Many of them were related to 'regular' string format specifiers being used with 'long' variables. %x -> %llx.

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 * Battery extends SolarNanoGrid.
defrost 5:57b06b4b47c6 5 * Battery interacts with the lockers.
defrost 5:57b06b4b47c6 6 * The ID must NOT end in 00.
defrost 5:57b06b4b47c6 7 *@section LICENSE
defrost 5:57b06b4b47c6 8 * Copyright (c) 2016, Malcolm McCulloch
defrost 5:57b06b4b47c6 9 *
defrost 5:57b06b4b47c6 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
defrost 5:57b06b4b47c6 11 * of this software and associated documentation files (the "Software"), to deal
defrost 5:57b06b4b47c6 12 * in the Software without restriction, including without limitation the rights
defrost 5:57b06b4b47c6 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
defrost 5:57b06b4b47c6 14 * copies of the Software, and to permit persons to whom the Software is
defrost 5:57b06b4b47c6 15 * furnished to do so, subject to the following conditions:
defrost 5:57b06b4b47c6 16 *
defrost 5:57b06b4b47c6 17 * The above copyright notice and this permission notice shall be included in
defrost 5:57b06b4b47c6 18 * all copies or substantial portions of the Software.
defrost 5:57b06b4b47c6 19 *
defrost 5:57b06b4b47c6 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
defrost 5:57b06b4b47c6 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
defrost 5:57b06b4b47c6 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
defrost 5:57b06b4b47c6 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
defrost 5:57b06b4b47c6 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
defrost 5:57b06b4b47c6 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
defrost 5:57b06b4b47c6 26 * THE SOFTWARE.
defrost 5:57b06b4b47c6 27 * @file "Battery.c"
defrost 5:57b06b4b47c6 28 */
defrost 5:57b06b4b47c6 29 #include "Battery.h"
defrost 10:30c9e8df0032 30 #define FUNCNAME "BATTERY"
defrost 10:30c9e8df0032 31 #include "defs.h"
defrost 5:57b06b4b47c6 32
defrost 5:57b06b4b47c6 33 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 34 // Constructors
defrost 5:57b06b4b47c6 35 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 36
epgmdm 11:87ab310924f0 37 Battery::Battery(FILE* fp, Serial *pc) : SolarNanoGrid(fp,pc){
defrost 5:57b06b4b47c6 38 DBG("Initialize class");
adrienBiz 8:0acda4f2e0a8 39 // ***** Variable initialisation *****
adrienBiz 8:0acda4f2e0a8 40 // Public variable initialisation
epgmdm 11:87ab310924f0 41
epgmdm 12:d9b13f31206d 42 maxChargeRate=1.0; // Charge rate in amps
epgmdm 11:87ab310924f0 43
adrienBiz 8:0acda4f2e0a8 44 // Protected variables initialisation
adrienBiz 8:0acda4f2e0a8 45 count=0;
adrienBiz 8:0acda4f2e0a8 46 sizeRead=0;
adrienBiz 8:0acda4f2e0a8 47 fileLength=0;
adrienBiz 8:0acda4f2e0a8 48 filePointer=0;
adrienBiz 8:0acda4f2e0a8 49 fileLeft=0;
epgmdm 11:87ab310924f0 50
adrienBiz 8:0acda4f2e0a8 51 sdBuffPnt=0;
adrienBiz 8:0acda4f2e0a8 52 /**
adrienBiz 8:0acda4f2e0a8 53 * Open channel address of the locker
adrienBiz 8:0acda4f2e0a8 54 */
adrienBiz 8:0acda4f2e0a8 55 openAddr=0;
adrienBiz 8:0acda4f2e0a8 56
adrienBiz 8:0acda4f2e0a8 57 /**
adrienBiz 8:0acda4f2e0a8 58 * Private channel address of the locker
adrienBiz 8:0acda4f2e0a8 59 */
adrienBiz 8:0acda4f2e0a8 60 privateAddr=0;
adrienBiz 8:0acda4f2e0a8 61
adrienBiz 8:0acda4f2e0a8 62 countSD=0;
adrienBiz 8:0acda4f2e0a8 63
adrienBiz 8:0acda4f2e0a8 64 // Protected flags: *
adrienBiz 8:0acda4f2e0a8 65 checkedIn=0;
adrienBiz 8:0acda4f2e0a8 66 flagGetTime=0;
adrienBiz 8:0acda4f2e0a8 67 flagNextFile=0;
adrienBiz 8:0acda4f2e0a8 68 flagSendFileName=0;
adrienBiz 8:0acda4f2e0a8 69 flagSendFileSize=0;
adrienBiz 8:0acda4f2e0a8 70 flagSendFile=0;
adrienBiz 8:0acda4f2e0a8 71 flagSendFileDone=0;
epgmdm 12:d9b13f31206d 72 flagGetMaxCharge=0;
epgmdm 12:d9b13f31206d 73 sendingFile=0;
epgmdm 11:87ab310924f0 74
adrienBiz 8:0acda4f2e0a8 75 // Protected interrupts
adrienBiz 8:0acda4f2e0a8 76 button=NULL;
adrienBiz 8:0acda4f2e0a8 77 txWatch=NULL;
adrienBiz 8:0acda4f2e0a8 78 delay=NULL;
epgmdm 11:87ab310924f0 79
adrienBiz 8:0acda4f2e0a8 80 //Private variables initialisation
adrienBiz 8:0acda4f2e0a8 81 maxRT=false;
epgmdm 11:87ab310924f0 82
adrienBiz 8:0acda4f2e0a8 83 // ***** End of variables initialisation *****
epgmdm 11:87ab310924f0 84
epgmdm 11:87ab310924f0 85 // No other information needed from the config file
epgmdm 11:87ab310924f0 86 fclose(fp);
epgmdm 11:87ab310924f0 87 //
epgmdm 11:87ab310924f0 88 // Setup variables
epgmdm 11:87ab310924f0 89 //
epgmdm 11:87ab310924f0 90 txFileName = (char*) calloc(80, 1);
epgmdm 11:87ab310924f0 91 fullTxFilePath = (char*) calloc(80, 1);
epgmdm 11:87ab310924f0 92 logDir = (char*) calloc(20, 1);
epgmdm 11:87ab310924f0 93 sdBuffer = (char*) calloc(SDBUFFERSIZE, 1);
epgmdm 11:87ab310924f0 94
epgmdm 11:87ab310924f0 95
epgmdm 11:87ab310924f0 96
epgmdm 11:87ab310924f0 97
defrost 5:57b06b4b47c6 98 //
defrost 5:57b06b4b47c6 99 // Setup NRF
defrost 5:57b06b4b47c6 100 //
defrost 5:57b06b4b47c6 101 privateAddr = ((long long)communityID <<16)+ (id &0XFFFF);
defrost 5:57b06b4b47c6 102 openAddr = privateAddr & 0xFFFFFFFF00;
defrost 5:57b06b4b47c6 103
defrost 5:57b06b4b47c6 104 DBG("Channel %x Address %llx Address Locker %llx", chan, privateAddr, openAddr);
defrost 5:57b06b4b47c6 105
epgmdm 11:87ab310924f0 106 spiNRF();
defrost 5:57b06b4b47c6 107 nrf->quickTxSetup(chan, openAddr);
defrost 5:57b06b4b47c6 108 nrf->setRadio(0x01, 0x03); // 2MB/S 0dB
defrost 5:57b06b4b47c6 109 nrf->setTxRetry(0x0F, 0x0F);
defrost 5:57b06b4b47c6 110 nrfFlush();
defrost 5:57b06b4b47c6 111 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 112 #ifdef DEBUG
defrost 5:57b06b4b47c6 113 nrf->printDetails();
defrost 5:57b06b4b47c6 114 #endif
defrost 5:57b06b4b47c6 115 //
defrost 5:57b06b4b47c6 116 // Setup logging directories
defrost 5:57b06b4b47c6 117 //
defrost 5:57b06b4b47c6 118 sprintf(logDir, "/sd/data/%02X%04X", (communityID&0XFF),(id&0XFFFF));
defrost 5:57b06b4b47c6 119 DBG(" Log directory %s", logDir);
defrost 5:57b06b4b47c6 120 spiSD();
defrost 5:57b06b4b47c6 121 mkdir("/sd/data", 777);
defrost 5:57b06b4b47c6 122 mkdir(logDir, 777);
defrost 5:57b06b4b47c6 123 DBG(" made");
defrost 5:57b06b4b47c6 124 //
defrost 5:57b06b4b47c6 125 // Setup interrupts
defrost 5:57b06b4b47c6 126 //
defrost 5:57b06b4b47c6 127 button = new InterruptIn(SW2);
defrost 5:57b06b4b47c6 128 button->fall(this, &Battery::intButton);
defrost 5:57b06b4b47c6 129
defrost 5:57b06b4b47c6 130 nrfInt = new InterruptIn(PTC18);
defrost 5:57b06b4b47c6 131 nrfInt->fall(this, &Battery::intNrf); // attach nrf interrupt.
defrost 5:57b06b4b47c6 132
defrost 5:57b06b4b47c6 133 txWatch = new Ticker ();
defrost 5:57b06b4b47c6 134 txWatch->attach(this,&Battery::retransmit,10.01);
defrost 5:57b06b4b47c6 135
defrost 5:57b06b4b47c6 136 delay = new Timeout();
defrost 5:57b06b4b47c6 137
defrost 5:57b06b4b47c6 138 loop();
defrost 5:57b06b4b47c6 139 }
defrost 5:57b06b4b47c6 140
defrost 5:57b06b4b47c6 141 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 142 // Interrupt routines
defrost 5:57b06b4b47c6 143 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 144
defrost 5:57b06b4b47c6 145 /**
defrost 5:57b06b4b47c6 146 * When the button is pressed transmit something
defrost 5:57b06b4b47c6 147 */
defrost 5:57b06b4b47c6 148 void Battery::intButton(){
defrost 5:57b06b4b47c6 149 if (!maxRT){
defrost 5:57b06b4b47c6 150 *ce = 1;
defrost 5:57b06b4b47c6 151 if(checkedIn==0){
defrost 5:57b06b4b47c6 152 checkIn();
defrost 5:57b06b4b47c6 153 } else{
defrost 5:57b06b4b47c6 154 flagNextFile = 1;
defrost 5:57b06b4b47c6 155 }
defrost 5:57b06b4b47c6 156
defrost 5:57b06b4b47c6 157 }
defrost 5:57b06b4b47c6 158 }
defrost 5:57b06b4b47c6 159
defrost 5:57b06b4b47c6 160 /**
defrost 5:57b06b4b47c6 161 * Called when the nrf creates an interrupt.
defrost 5:57b06b4b47c6 162 *
defrost 5:57b06b4b47c6 163 */
defrost 5:57b06b4b47c6 164 void Battery::intNrf(){
defrost 5:57b06b4b47c6 165 // DBG("NRF Interrupt Started.");
defrost 5:57b06b4b47c6 166 int pipe=0;
defrost 5:57b06b4b47c6 167 int status=0;
defrost 5:57b06b4b47c6 168 int width=0;
defrost 5:57b06b4b47c6 169
defrost 5:57b06b4b47c6 170 //
defrost 5:57b06b4b47c6 171 //Get status and pipe
defrost 5:57b06b4b47c6 172 //
epgmdm 11:87ab310924f0 173 spiNRF();
defrost 5:57b06b4b47c6 174 status = nrf->checkStatus();
defrost 5:57b06b4b47c6 175 pipe = (status >> 1);
defrost 5:57b06b4b47c6 176 pipe = (pipe & 0x0007);
defrost 5:57b06b4b47c6 177
defrost 5:57b06b4b47c6 178 //
defrost 5:57b06b4b47c6 179 // Check if max RT is reached
defrost 5:57b06b4b47c6 180 //
defrost 5:57b06b4b47c6 181 if (((status >>MAX_RT)& 01) ==1) {
defrost 5:57b06b4b47c6 182 maxRT=true;
defrost 13:de43f28c0365 183 DBG("MAX RT flag is set. PTB20= %d status=%x",(unsigned int) *ce,status);
defrost 5:57b06b4b47c6 184 *ce = 0;
defrost 13:de43f28c0365 185 DBG("PTB20= %d status=%x",(unsigned int) *ce,status);
defrost 5:57b06b4b47c6 186 nrf->clearStatus();
defrost 5:57b06b4b47c6 187 *ledRed=0;
defrost 5:57b06b4b47c6 188 }
defrost 5:57b06b4b47c6 189
defrost 5:57b06b4b47c6 190 //
defrost 5:57b06b4b47c6 191 // Check if data received
defrost 5:57b06b4b47c6 192 //
defrost 5:57b06b4b47c6 193 if (((status >>RX_DR)& 01) ==1) { // Received a packet
defrost 5:57b06b4b47c6 194 width= nrf->getRxData(dataRx);
defrost 5:57b06b4b47c6 195 dataRx[width]='\0';
defrost 5:57b06b4b47c6 196 if (dataRx[0]!='S'){
defrost 5:57b06b4b47c6 197 DBG("intNrf>D[p=%1d]=<%2s> ",pipe,dataRx);
defrost 5:57b06b4b47c6 198 }
defrost 5:57b06b4b47c6 199 maxRT=false;
defrost 5:57b06b4b47c6 200 *ce = 1;
defrost 5:57b06b4b47c6 201 nrf->clearStatus();
defrost 5:57b06b4b47c6 202 switch (dataRx[0]) {
defrost 5:57b06b4b47c6 203 case ('C'):{
defrost 5:57b06b4b47c6 204 doCheckIn();
defrost 5:57b06b4b47c6 205 flagNextFile = 1;
defrost 13:de43f28c0365 206 DBG("Flag Set");
defrost 5:57b06b4b47c6 207 break;}
defrost 5:57b06b4b47c6 208 case ('D'):{
defrost 5:57b06b4b47c6 209 flag = &flagSendFileName;
defrost 5:57b06b4b47c6 210 delay->detach();
defrost 5:57b06b4b47c6 211 delay->attach(this, &Battery::setFlag, 1.5);
defrost 5:57b06b4b47c6 212 break;}
defrost 5:57b06b4b47c6 213 case ('F'):{
defrost 5:57b06b4b47c6 214 flag = &flagSendFileSize;
defrost 5:57b06b4b47c6 215 countSD = 0;
defrost 5:57b06b4b47c6 216 delay->detach();
defrost 5:57b06b4b47c6 217 delay->attach(this, &Battery::setFlag, 1.50);
defrost 5:57b06b4b47c6 218 break;}
defrost 5:57b06b4b47c6 219 case ('S'):{
defrost 5:57b06b4b47c6 220 flag = &flagSendFile;
defrost 5:57b06b4b47c6 221 delay->detach();
defrost 5:57b06b4b47c6 222 // printf("%d\n\r",countSD%16);
defrost 5:57b06b4b47c6 223 //delay->attach(this, &Battery::setFlag, 0.05);
defrost 5:57b06b4b47c6 224
defrost 5:57b06b4b47c6 225 if (countSD % (SDBUFFERSIZE/32) == 0) {
defrost 5:57b06b4b47c6 226 delay->attach(this, &Battery::setFlag, 1.5); // wait to write to SD card
defrost 5:57b06b4b47c6 227 } else {
defrost 5:57b06b4b47c6 228 delay->attach_us(this, &Battery::setFlag, 800);
defrost 5:57b06b4b47c6 229 }
defrost 5:57b06b4b47c6 230 countSD++;
defrost 5:57b06b4b47c6 231 break;}
defrost 5:57b06b4b47c6 232 case ('T'):{
defrost 5:57b06b4b47c6 233 time_t now;
defrost 5:57b06b4b47c6 234 sscanf (&dataRx[2],"%x",&now );
defrost 5:57b06b4b47c6 235 set_time(now);
defrost 5:57b06b4b47c6 236 now= time(NULL);
defrost 5:57b06b4b47c6 237 struct tm * timeInf = localtime(&now);
defrost 5:57b06b4b47c6 238 DBG ("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 5:57b06b4b47c6 239
defrost 5:57b06b4b47c6 240 break;}
defrost 5:57b06b4b47c6 241
epgmdm 12:d9b13f31206d 242
epgmdm 12:d9b13f31206d 243 case ('M'):{
epgmdm 12:d9b13f31206d 244 time_t now;
epgmdm 12:d9b13f31206d 245 sscanf (&dataRx[2],"%f",&maxChargeRate );
defrost 13:de43f28c0365 246 if(maxChargeRate>2.0f){
defrost 13:de43f28c0365 247 maxChargeRate=2.0f;
epgmdm 12:d9b13f31206d 248 }
defrost 13:de43f28c0365 249 if (maxChargeRate<0.0f){
defrost 13:de43f28c0365 250 maxChargeRate=0.0f;
epgmdm 12:d9b13f31206d 251 }
epgmdm 12:d9b13f31206d 252 DBG ("Max charge %f \n\r",maxChargeRate);
epgmdm 12:d9b13f31206d 253
epgmdm 12:d9b13f31206d 254 break;}
defrost 5:57b06b4b47c6 255 }
epgmdm 12:d9b13f31206d 256
defrost 5:57b06b4b47c6 257 }
epgmdm 12:d9b13f31206d 258
defrost 5:57b06b4b47c6 259 if (((status >>TX_DS)& 01) ==1) {
defrost 5:57b06b4b47c6 260 // maxRT=false;
defrost 5:57b06b4b47c6 261 nrf->flushTx();
defrost 5:57b06b4b47c6 262 nrf->flushRx();
defrost 5:57b06b4b47c6 263 //DBG(" TX_DS");
defrost 5:57b06b4b47c6 264 }
defrost 5:57b06b4b47c6 265
defrost 5:57b06b4b47c6 266 *ledBlue = !*ledBlue;
defrost 5:57b06b4b47c6 267 *ledRed = !maxRT;
defrost 5:57b06b4b47c6 268 }
defrost 5:57b06b4b47c6 269
defrost 5:57b06b4b47c6 270 /**
defrost 5:57b06b4b47c6 271 * Transmits the data and sets up a call back to check data sent 30s later.
defrost 5:57b06b4b47c6 272 */
defrost 5:57b06b4b47c6 273
defrost 5:57b06b4b47c6 274 /**
defrost 5:57b06b4b47c6 275 * Checks to see if transmit failed and then retransmit
defrost 5:57b06b4b47c6 276 *
defrost 5:57b06b4b47c6 277 */
defrost 5:57b06b4b47c6 278 void Battery::retransmit(){
defrost 5:57b06b4b47c6 279
defrost 5:57b06b4b47c6 280 if (maxRT) {
defrost 5:57b06b4b47c6 281 DBG("Retransmit");
defrost 5:57b06b4b47c6 282 *ledRed=1;
defrost 5:57b06b4b47c6 283 *ce = 1;
defrost 5:57b06b4b47c6 284 maxRT=false;
defrost 5:57b06b4b47c6 285 nrf->clearStatus();
defrost 5:57b06b4b47c6 286 nrf->flushRx();
defrost 5:57b06b4b47c6 287 nrf->retransmitData();
defrost 5:57b06b4b47c6 288 }
epgmdm 12:d9b13f31206d 289 if ((!sendingFile)&&(checkedIn == 1)){
epgmdm 12:d9b13f31206d 290 flagGetTime=1;
epgmdm 12:d9b13f31206d 291 flagGetMaxCharge=1;
epgmdm 12:d9b13f31206d 292 }
defrost 5:57b06b4b47c6 293 }
defrost 5:57b06b4b47c6 294
defrost 5:57b06b4b47c6 295 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 296 // Main routines
defrost 5:57b06b4b47c6 297 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 298
defrost 5:57b06b4b47c6 299 /**
defrost 5:57b06b4b47c6 300 * Checks in the Battery to the locker
defrost 5:57b06b4b47c6 301 * Transmits "C ID"
defrost 5:57b06b4b47c6 302 *
defrost 5:57b06b4b47c6 303 */
defrost 5:57b06b4b47c6 304 void Battery::checkIn() {
defrost 5:57b06b4b47c6 305 sprintf(dataTx, "C %04X", (int) (id&0XFFFF));
defrost 5:57b06b4b47c6 306 DBG("Check into locker [%s]", dataTx);
epgmdm 11:87ab310924f0 307 spiNRF();
defrost 5:57b06b4b47c6 308 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 309 }
defrost 5:57b06b4b47c6 310
defrost 5:57b06b4b47c6 311 /**
defrost 5:57b06b4b47c6 312 * Checks in the Battery to the locker
defrost 5:57b06b4b47c6 313 * Acknowledge received.
defrost 5:57b06b4b47c6 314 * Changes the TX pipe address.
defrost 5:57b06b4b47c6 315 *
defrost 5:57b06b4b47c6 316 */
defrost 5:57b06b4b47c6 317 void Battery::doCheckIn() {
defrost 5:57b06b4b47c6 318 nrf->setTxAddress(privateAddr);
defrost 5:57b06b4b47c6 319 checkedIn = 1;
defrost 5:57b06b4b47c6 320 DBG("Checked into locker");
defrost 5:57b06b4b47c6 321 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 322 #ifdef DEBUG
defrost 5:57b06b4b47c6 323 nrf->printDetails();
defrost 5:57b06b4b47c6 324 #endif
defrost 5:57b06b4b47c6 325 }
defrost 5:57b06b4b47c6 326 /**
defrost 5:57b06b4b47c6 327 * Checks in the Battery to the locker
defrost 5:57b06b4b47c6 328 * Acknowledge received.
defrost 5:57b06b4b47c6 329 * Changes the TX pipe address.
defrost 5:57b06b4b47c6 330 *
defrost 5:57b06b4b47c6 331 */
defrost 5:57b06b4b47c6 332 void Battery::doCheckOut() {
defrost 5:57b06b4b47c6 333 nrf->setTxAddress(openAddr);
defrost 5:57b06b4b47c6 334 checkedIn = 0;
defrost 5:57b06b4b47c6 335 DBG("Checked out of locker");
defrost 5:57b06b4b47c6 336 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 337 #ifdef DEBUG
defrost 5:57b06b4b47c6 338 nrf->printDetails();
defrost 5:57b06b4b47c6 339 #endif
defrost 5:57b06b4b47c6 340 }
defrost 5:57b06b4b47c6 341
defrost 5:57b06b4b47c6 342 /**
defrost 5:57b06b4b47c6 343 * Slow routines for Battery
defrost 5:57b06b4b47c6 344 */
defrost 5:57b06b4b47c6 345 void Battery::loop() {
epgmdm 12:d9b13f31206d 346
epgmdm 12:d9b13f31206d 347 *ledGreen = !checkedIn;
defrost 13:de43f28c0365 348
defrost 5:57b06b4b47c6 349
epgmdm 12:d9b13f31206d 350 if (flagGetTime) {
epgmdm 12:d9b13f31206d 351 flagGetTime=0;
epgmdm 12:d9b13f31206d 352 requestTime();
epgmdm 12:d9b13f31206d 353
epgmdm 12:d9b13f31206d 354 }
epgmdm 12:d9b13f31206d 355 if (flagGetMaxCharge) {
epgmdm 12:d9b13f31206d 356 flagGetMaxCharge=0;
epgmdm 12:d9b13f31206d 357 requestMaxCharge();
defrost 5:57b06b4b47c6 358
epgmdm 12:d9b13f31206d 359 }
epgmdm 12:d9b13f31206d 360 if (flagNextFile) {
epgmdm 12:d9b13f31206d 361 flagNextFile = 0;
defrost 13:de43f28c0365 362 DBG("NextFile called");
epgmdm 12:d9b13f31206d 363 requestTime();
epgmdm 12:d9b13f31206d 364 nextFileToTx();
epgmdm 12:d9b13f31206d 365 if (fileLength>0){
epgmdm 12:d9b13f31206d 366 INFO("Sending file %s.",txFileName)
epgmdm 12:d9b13f31206d 367 sendDirName();
epgmdm 12:d9b13f31206d 368 } else {
epgmdm 12:d9b13f31206d 369 sendingFile=0;
epgmdm 12:d9b13f31206d 370 INFO("No more files to send.")
defrost 5:57b06b4b47c6 371 }
epgmdm 12:d9b13f31206d 372 }
epgmdm 12:d9b13f31206d 373 if (flagSendFileName) {
epgmdm 12:d9b13f31206d 374 flagSendFileName = 0;
epgmdm 12:d9b13f31206d 375 sendFileName();
defrost 5:57b06b4b47c6 376
epgmdm 12:d9b13f31206d 377 }
epgmdm 12:d9b13f31206d 378 if (flagSendFileSize) {
epgmdm 12:d9b13f31206d 379 flagSendFileSize = 0;
epgmdm 12:d9b13f31206d 380 sendFileSize();
epgmdm 12:d9b13f31206d 381 }
epgmdm 12:d9b13f31206d 382 if (flagSendFile) {
epgmdm 12:d9b13f31206d 383 flagSendFile = 0;
epgmdm 12:d9b13f31206d 384 sendFile();
defrost 5:57b06b4b47c6 385 }
epgmdm 12:d9b13f31206d 386 if (flagSendFileDone) {
epgmdm 12:d9b13f31206d 387 flagSendFileDone = 0;
epgmdm 12:d9b13f31206d 388 sendFileDone();
epgmdm 12:d9b13f31206d 389 INFO("File sent <%s>",txFileName);
epgmdm 12:d9b13f31206d 390 flag = &flagNextFile;
epgmdm 12:d9b13f31206d 391 delay->detach();
epgmdm 12:d9b13f31206d 392 delay->attach(this, &Battery::setFlag, 2.5);
epgmdm 12:d9b13f31206d 393 }
epgmdm 12:d9b13f31206d 394
defrost 5:57b06b4b47c6 395 }
defrost 5:57b06b4b47c6 396
defrost 5:57b06b4b47c6 397 /**
defrost 5:57b06b4b47c6 398 * sends a request for time update.
defrost 5:57b06b4b47c6 399 */
epgmdm 12:d9b13f31206d 400 void Battery::requestTime() {
defrost 5:57b06b4b47c6 401 DBG("getTime");
defrost 5:57b06b4b47c6 402 nrf->transmitData("T ",2);
defrost 5:57b06b4b47c6 403 }
epgmdm 12:d9b13f31206d 404
epgmdm 12:d9b13f31206d 405 /**
epgmdm 12:d9b13f31206d 406 * sends a request for time update.
epgmdm 12:d9b13f31206d 407 */
epgmdm 12:d9b13f31206d 408 void Battery::requestMaxCharge() {
epgmdm 12:d9b13f31206d 409 DBG("getTime");
epgmdm 12:d9b13f31206d 410 nrf->transmitData("M ",2);
epgmdm 12:d9b13f31206d 411 }
defrost 5:57b06b4b47c6 412 /**
defrost 5:57b06b4b47c6 413 * Selects the next file to transmit
defrost 5:57b06b4b47c6 414 * Uses /sd/lastFileTx.txt to store the lst file transmitted
defrost 5:57b06b4b47c6 415 */
defrost 5:57b06b4b47c6 416 void Battery::nextFileToTx() {
defrost 5:57b06b4b47c6 417
defrost 5:57b06b4b47c6 418 DBG("> nextTxFile");
defrost 5:57b06b4b47c6 419
defrost 5:57b06b4b47c6 420 char lastFileName[64];
defrost 5:57b06b4b47c6 421 char fileName[64];
defrost 5:57b06b4b47c6 422 char fn[40];
defrost 5:57b06b4b47c6 423 unsigned int txLength;
defrost 5:57b06b4b47c6 424
defrost 5:57b06b4b47c6 425 sdBuffPnt=0;// Tells sendFile to read from SD the first time.
defrost 5:57b06b4b47c6 426 fileLength = 0;
defrost 5:57b06b4b47c6 427 strcpy(txFileName, "");
defrost 5:57b06b4b47c6 428 spiSD();
defrost 5:57b06b4b47c6 429 // Read in last file name
defrost 5:57b06b4b47c6 430 FILE *fp = fopen("/sd/lastFileTx.txt", "r");
defrost 5:57b06b4b47c6 431 if (fp != NULL) {
defrost 5:57b06b4b47c6 432 if (fscanf(fp, "%s %u", lastFileName, &txLength) != 2) {
defrost 5:57b06b4b47c6 433 sprintf(lastFileName, "a");
defrost 5:57b06b4b47c6 434 }
defrost 5:57b06b4b47c6 435 fclose(fp);
defrost 5:57b06b4b47c6 436 } else {
defrost 5:57b06b4b47c6 437 sprintf(lastFileName, "a");
defrost 5:57b06b4b47c6 438 }
defrost 5:57b06b4b47c6 439 DBG("nextFileToTx>Last Filename=%s len=%u",lastFileName,txLength);
defrost 5:57b06b4b47c6 440 // Open directory and read files
defrost 5:57b06b4b47c6 441 DIR *dp;
defrost 5:57b06b4b47c6 442 struct dirent *dirp;
defrost 5:57b06b4b47c6 443 spiSD();
defrost 5:57b06b4b47c6 444 dp = opendir(logDir);
defrost 5:57b06b4b47c6 445 if (dp == NULL) {
defrost 5:57b06b4b47c6 446 DBG("nextTxFile logdir %s is NULL", logDir);
defrost 5:57b06b4b47c6 447 } else
defrost 5:57b06b4b47c6 448 //read all directory and file names in current directory into filename vector
defrost 5:57b06b4b47c6 449 while ((dirp = readdir(dp)) != NULL) {
defrost 5:57b06b4b47c6 450 DBG("nextFileToTx> WHILE lst [%s] dir [%s] %d",lastFileName, dirp->d_name,strcmp(lastFileName, dirp->d_name));
defrost 5:57b06b4b47c6 451
defrost 5:57b06b4b47c6 452 if (strcmp(lastFileName, dirp->d_name) < 0) {
defrost 5:57b06b4b47c6 453 strcpy(fileName, dirp->d_name);
defrost 5:57b06b4b47c6 454 sprintf(txFileName, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 455 fp = fopen(txFileName, "r");
defrost 5:57b06b4b47c6 456 fseek(fp, 0L, SEEK_END);
defrost 5:57b06b4b47c6 457 fileLength = ftell(fp);
defrost 5:57b06b4b47c6 458 fclose(fp);
defrost 5:57b06b4b47c6 459 DBG("CHOSEN %s old %s ", fileName, lastFileName );
defrost 5:57b06b4b47c6 460 break;
defrost 5:57b06b4b47c6 461 }
defrost 5:57b06b4b47c6 462 if (strcmp(lastFileName, dirp->d_name) == 0) {
defrost 5:57b06b4b47c6 463
defrost 5:57b06b4b47c6 464 strcpy(fileName, dirp->d_name);
defrost 5:57b06b4b47c6 465 sprintf(fn, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 466 fp = fopen(fn, "r");
defrost 5:57b06b4b47c6 467 fseek(fp, 0L, SEEK_END);
defrost 5:57b06b4b47c6 468 fileLength = ftell(fp);
defrost 5:57b06b4b47c6 469 fclose(fp);
defrost 5:57b06b4b47c6 470 if (txLength < fileLength) {
defrost 5:57b06b4b47c6 471 sprintf(txFileName, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 472 DBG("More to send is %s old %u new %u ", fileName, txLength,fileLength );
defrost 5:57b06b4b47c6 473 break;
defrost 5:57b06b4b47c6 474 }
defrost 5:57b06b4b47c6 475 }
defrost 5:57b06b4b47c6 476 // test filename and if greater than last read then choose.
defrost 5:57b06b4b47c6 477 }
defrost 5:57b06b4b47c6 478 closedir(dp);
defrost 5:57b06b4b47c6 479 if (strlen(txFileName) > 0) {
defrost 5:57b06b4b47c6 480 strcpy(txFileName, fileName);
defrost 5:57b06b4b47c6 481 sprintf(fullTxFilePath, "%s/%s", logDir, txFileName);
defrost 5:57b06b4b47c6 482 fileLeft = fileLength;
defrost 5:57b06b4b47c6 483
defrost 5:57b06b4b47c6 484 } else {
defrost 5:57b06b4b47c6 485 fileLength =0;
defrost 5:57b06b4b47c6 486 }
defrost 5:57b06b4b47c6 487 DBG("nextTxFile> Next is [%s] length %d", txFileName, fileLength);
defrost 5:57b06b4b47c6 488
defrost 5:57b06b4b47c6 489 }
defrost 5:57b06b4b47c6 490 /**
defrost 5:57b06b4b47c6 491 * Sends the directory name, allows the otherside to create.
defrost 5:57b06b4b47c6 492 */
defrost 5:57b06b4b47c6 493
defrost 5:57b06b4b47c6 494 void Battery::sendDirName() {
defrost 5:57b06b4b47c6 495 sprintf(dataTx, "D %s", logDir);
defrost 5:57b06b4b47c6 496
defrost 5:57b06b4b47c6 497 DBG("send Dir Name [%s].\n\r", dataTx);
defrost 5:57b06b4b47c6 498
epgmdm 11:87ab310924f0 499 spiNRF();
defrost 5:57b06b4b47c6 500 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 501
defrost 5:57b06b4b47c6 502 }
defrost 5:57b06b4b47c6 503 /**
defrost 5:57b06b4b47c6 504 * Sends the file name, if fileLength >0
defrost 5:57b06b4b47c6 505 */
defrost 5:57b06b4b47c6 506
defrost 5:57b06b4b47c6 507 void Battery::sendFileName() {
defrost 5:57b06b4b47c6 508 // delay->detach();
defrost 5:57b06b4b47c6 509 if (fileLength < 1) {
defrost 5:57b06b4b47c6 510 return;
defrost 5:57b06b4b47c6 511 }
defrost 5:57b06b4b47c6 512 sprintf(dataTx, "F %20s", txFileName);
defrost 5:57b06b4b47c6 513
defrost 5:57b06b4b47c6 514 DBG("send File Name [%s] \n\r", dataTx);
defrost 5:57b06b4b47c6 515
epgmdm 11:87ab310924f0 516 spiNRF();
defrost 5:57b06b4b47c6 517 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 518 }
defrost 5:57b06b4b47c6 519
defrost 5:57b06b4b47c6 520 /**
defrost 5:57b06b4b47c6 521 * Sends the file length to the locker - this is also the cue that the following bytes are the file.
defrost 5:57b06b4b47c6 522 */
defrost 5:57b06b4b47c6 523 void Battery::sendFileSize() {
defrost 5:57b06b4b47c6 524 if (fileLength < 1) {
defrost 5:57b06b4b47c6 525 return;
defrost 5:57b06b4b47c6 526 }
epgmdm 12:d9b13f31206d 527 sendingFile=1;
defrost 5:57b06b4b47c6 528 filePointer = 0;
defrost 5:57b06b4b47c6 529 sprintf(dataTx, "S %X", fileLength);
defrost 5:57b06b4b47c6 530
defrost 5:57b06b4b47c6 531
defrost 5:57b06b4b47c6 532 DBG("send File size [%s]", dataTx);
defrost 5:57b06b4b47c6 533
epgmdm 11:87ab310924f0 534 spiNRF();
defrost 5:57b06b4b47c6 535 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 536 }
defrost 5:57b06b4b47c6 537
defrost 5:57b06b4b47c6 538 /**
defrost 5:57b06b4b47c6 539 * Sends the file length to the locker - this is also the cue that the following bytes are the file.
defrost 5:57b06b4b47c6 540 */
defrost 5:57b06b4b47c6 541 void Battery::sendFile() {
defrost 5:57b06b4b47c6 542
defrost 5:57b06b4b47c6 543 // printf("send File [%s] at %d %d\n\r",fullTxFilePath,fileLength, fileLeft );
defrost 5:57b06b4b47c6 544
epgmdm 11:87ab310924f0 545 // if (fileLength < 1) {
epgmdm 11:87ab310924f0 546 // INFO("File Sent");
epgmdm 11:87ab310924f0 547 // sdBuffPnt=0;
epgmdm 11:87ab310924f0 548 // return;
epgmdm 11:87ab310924f0 549 // }
defrost 5:57b06b4b47c6 550 if (sdBuffPnt>=SDBUFFERSIZE){
defrost 5:57b06b4b47c6 551 sdBuffPnt=0;
defrost 5:57b06b4b47c6 552 }
defrost 5:57b06b4b47c6 553 if (sdBuffPnt == 0) {
defrost 5:57b06b4b47c6 554 spiSD();
defrost 5:57b06b4b47c6 555 FILE *fp = fopen(fullTxFilePath, "rb");
defrost 5:57b06b4b47c6 556 if (fp == NULL) {
epgmdm 11:87ab310924f0 557 spiNRF();
defrost 5:57b06b4b47c6 558 INFO("sendFile> File %s not found for reading",fullTxFilePath);
defrost 5:57b06b4b47c6 559 return;
defrost 5:57b06b4b47c6 560 }
defrost 5:57b06b4b47c6 561 spiSD();
defrost 5:57b06b4b47c6 562 fseek(fp, fileLength - fileLeft, SEEK_SET);
defrost 5:57b06b4b47c6 563 sizeRead = fread(sdBuffer, 1,SDBUFFERSIZE, fp);
defrost 5:57b06b4b47c6 564 fileLeft -= sizeRead;
defrost 5:57b06b4b47c6 565 fclose(fp);
defrost 5:57b06b4b47c6 566 // printf("File %s %d \n\r",fullTxFilePath,sizeRead);
epgmdm 11:87ab310924f0 567 spiNRF();
defrost 5:57b06b4b47c6 568
defrost 5:57b06b4b47c6 569 }
defrost 5:57b06b4b47c6 570
defrost 5:57b06b4b47c6 571 if ((count++) % 256 ==0) {
defrost 5:57b06b4b47c6 572 DBG("W %d %d", fileLeft, sizeRead);
defrost 5:57b06b4b47c6 573 }
defrost 5:57b06b4b47c6 574 int size=32;
defrost 5:57b06b4b47c6 575 if (sizeRead<32){
defrost 5:57b06b4b47c6 576 size=sizeRead;
defrost 5:57b06b4b47c6 577 }
defrost 5:57b06b4b47c6 578 if ((fileLeft==0)&&(sizeRead==0)){
defrost 5:57b06b4b47c6 579 sdBuffPnt=0;
defrost 5:57b06b4b47c6 580 flagSendFileDone=1;
defrost 5:57b06b4b47c6 581 DBG("Done sending");
defrost 5:57b06b4b47c6 582 }else{
defrost 5:57b06b4b47c6 583 sizeRead -= size;
defrost 5:57b06b4b47c6 584 nrf->transmitData(&(sdBuffer[sdBuffPnt]), size);
defrost 5:57b06b4b47c6 585 sdBuffPnt+=size;
defrost 5:57b06b4b47c6 586 }
defrost 5:57b06b4b47c6 587
defrost 5:57b06b4b47c6 588
defrost 5:57b06b4b47c6 589 }
defrost 5:57b06b4b47c6 590
defrost 5:57b06b4b47c6 591 /**
defrost 5:57b06b4b47c6 592 * Update the last file sent file
defrost 5:57b06b4b47c6 593 */
defrost 5:57b06b4b47c6 594 void Battery::sendFileDone() {
defrost 5:57b06b4b47c6 595 if (strlen(txFileName)>0){
defrost 5:57b06b4b47c6 596 spiSD();
defrost 5:57b06b4b47c6 597 FILE *fp = fopen("/sd/lastFileTx.txt", "w");
defrost 5:57b06b4b47c6 598 if (fp != NULL) {
defrost 5:57b06b4b47c6 599 fprintf(fp, "%s %u \n", txFileName, fileLength);
defrost 5:57b06b4b47c6 600 fclose(fp);
defrost 5:57b06b4b47c6 601 DBG("sendFileDone> updated lastFileTx.txt %s %u ", txFileName, fileLength);
defrost 5:57b06b4b47c6 602 }else{
defrost 5:57b06b4b47c6 603 INFO("sendFileDone> Cannot update file lastFiletx.txt");
defrost 5:57b06b4b47c6 604 }
epgmdm 11:87ab310924f0 605 spiNRF();
defrost 5:57b06b4b47c6 606
defrost 5:57b06b4b47c6 607 }
defrost 5:57b06b4b47c6 608
defrost 5:57b06b4b47c6 609 }
defrost 5:57b06b4b47c6 610
defrost 5:57b06b4b47c6 611 /**
defrost 5:57b06b4b47c6 612 * Sets the value pointed to by flag to 1.
defrost 5:57b06b4b47c6 613 */
defrost 5:57b06b4b47c6 614 void Battery::setFlag() {
defrost 5:57b06b4b47c6 615 if (flag != NULL) {
defrost 5:57b06b4b47c6 616 *flag = 1;
defrost 5:57b06b4b47c6 617 }
defrost 5:57b06b4b47c6 618 flag = NULL;
defrost 5:57b06b4b47c6 619 }
epgmdm 12:d9b13f31206d 620
epgmdm 12:d9b13f31206d 621
epgmdm 12:d9b13f31206d 622 /**
epgmdm 12:d9b13f31206d 623 * Returns the max current that can be drawn from the system, in amps
epgmdm 12:d9b13f31206d 624 */
epgmdm 12:d9b13f31206d 625
epgmdm 12:d9b13f31206d 626 float Battery::getMaxCurrent(){
epgmdm 12:d9b13f31206d 627 return maxChargeRate;
epgmdm 12:d9b13f31206d 628 }
epgmdm 12:d9b13f31206d 629
epgmdm 12:d9b13f31206d 630 /**
epgmdm 12:d9b13f31206d 631 * returns a string of the log directory
epgmdm 12:d9b13f31206d 632 */
epgmdm 12:d9b13f31206d 633 char* Battery::getLogDirectory(){
epgmdm 12:d9b13f31206d 634
epgmdm 12:d9b13f31206d 635 return logDir;
epgmdm 12:d9b13f31206d 636 }