The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Committer:
epgmdm
Date:
Wed Jun 08 22:12:52 2016 +0000
Revision:
11:87ab310924f0
Parent:
10:30c9e8df0032
Child:
12:d9b13f31206d
Utility 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 * 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
adrienBiz 8:0acda4f2e0a8 42 maxChargeRate=0.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 11:87ab310924f0 72
adrienBiz 8:0acda4f2e0a8 73 // Protected interrupts
adrienBiz 8:0acda4f2e0a8 74 button=NULL;
adrienBiz 8:0acda4f2e0a8 75 txWatch=NULL;
adrienBiz 8:0acda4f2e0a8 76 delay=NULL;
epgmdm 11:87ab310924f0 77
adrienBiz 8:0acda4f2e0a8 78 //Private variables initialisation
adrienBiz 8:0acda4f2e0a8 79 maxRT=false;
epgmdm 11:87ab310924f0 80
adrienBiz 8:0acda4f2e0a8 81 // ***** End of variables initialisation *****
epgmdm 11:87ab310924f0 82
epgmdm 11:87ab310924f0 83 // No other information needed from the config file
epgmdm 11:87ab310924f0 84 fclose(fp);
epgmdm 11:87ab310924f0 85 //
epgmdm 11:87ab310924f0 86 // Setup variables
epgmdm 11:87ab310924f0 87 //
epgmdm 11:87ab310924f0 88 txFileName = (char*) calloc(80, 1);
epgmdm 11:87ab310924f0 89 fullTxFilePath = (char*) calloc(80, 1);
epgmdm 11:87ab310924f0 90 logDir = (char*) calloc(20, 1);
epgmdm 11:87ab310924f0 91 sdBuffer = (char*) calloc(SDBUFFERSIZE, 1);
epgmdm 11:87ab310924f0 92
epgmdm 11:87ab310924f0 93
epgmdm 11:87ab310924f0 94
epgmdm 11:87ab310924f0 95
defrost 5:57b06b4b47c6 96 //
defrost 5:57b06b4b47c6 97 // Setup NRF
defrost 5:57b06b4b47c6 98 //
defrost 5:57b06b4b47c6 99 privateAddr = ((long long)communityID <<16)+ (id &0XFFFF);
defrost 5:57b06b4b47c6 100 openAddr = privateAddr & 0xFFFFFFFF00;
defrost 5:57b06b4b47c6 101
defrost 5:57b06b4b47c6 102 DBG("Channel %x Address %llx Address Locker %llx", chan, privateAddr, openAddr);
defrost 5:57b06b4b47c6 103
epgmdm 11:87ab310924f0 104 spiNRF();
defrost 5:57b06b4b47c6 105 nrf->quickTxSetup(chan, openAddr);
defrost 5:57b06b4b47c6 106 nrf->setRadio(0x01, 0x03); // 2MB/S 0dB
defrost 5:57b06b4b47c6 107 nrf->setTxRetry(0x0F, 0x0F);
defrost 5:57b06b4b47c6 108 nrfFlush();
defrost 5:57b06b4b47c6 109 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 110 #ifdef DEBUG
defrost 5:57b06b4b47c6 111 nrf->printDetails();
defrost 5:57b06b4b47c6 112 #endif
defrost 5:57b06b4b47c6 113 //
defrost 5:57b06b4b47c6 114 // Setup logging directories
defrost 5:57b06b4b47c6 115 //
defrost 5:57b06b4b47c6 116 sprintf(logDir, "/sd/data/%02X%04X", (communityID&0XFF),(id&0XFFFF));
defrost 5:57b06b4b47c6 117 DBG(" Log directory %s", logDir);
defrost 5:57b06b4b47c6 118 spiSD();
defrost 5:57b06b4b47c6 119 mkdir("/sd/data", 777);
defrost 5:57b06b4b47c6 120 mkdir(logDir, 777);
defrost 5:57b06b4b47c6 121 DBG(" made");
defrost 5:57b06b4b47c6 122 //
defrost 5:57b06b4b47c6 123 // Setup interrupts
defrost 5:57b06b4b47c6 124 //
defrost 5:57b06b4b47c6 125 button = new InterruptIn(SW2);
defrost 5:57b06b4b47c6 126 button->fall(this, &Battery::intButton);
defrost 5:57b06b4b47c6 127
defrost 5:57b06b4b47c6 128 nrfInt = new InterruptIn(PTC18);
defrost 5:57b06b4b47c6 129 nrfInt->fall(this, &Battery::intNrf); // attach nrf interrupt.
defrost 5:57b06b4b47c6 130
defrost 5:57b06b4b47c6 131 txWatch = new Ticker ();
defrost 5:57b06b4b47c6 132 txWatch->attach(this,&Battery::retransmit,10.01);
defrost 5:57b06b4b47c6 133
defrost 5:57b06b4b47c6 134 delay = new Timeout();
defrost 5:57b06b4b47c6 135
defrost 5:57b06b4b47c6 136 loop();
defrost 5:57b06b4b47c6 137 }
defrost 5:57b06b4b47c6 138
defrost 5:57b06b4b47c6 139 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 140 // Interrupt routines
defrost 5:57b06b4b47c6 141 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 142
defrost 5:57b06b4b47c6 143 /**
defrost 5:57b06b4b47c6 144 * When the button is pressed transmit something
defrost 5:57b06b4b47c6 145 */
defrost 5:57b06b4b47c6 146 void Battery::intButton(){
defrost 5:57b06b4b47c6 147 if (!maxRT){
defrost 5:57b06b4b47c6 148 *ce = 1;
defrost 5:57b06b4b47c6 149 if(checkedIn==0){
defrost 5:57b06b4b47c6 150 checkIn();
defrost 5:57b06b4b47c6 151 } else{
defrost 5:57b06b4b47c6 152 flagNextFile = 1;
defrost 5:57b06b4b47c6 153 }
defrost 5:57b06b4b47c6 154
defrost 5:57b06b4b47c6 155 }
defrost 5:57b06b4b47c6 156 }
defrost 5:57b06b4b47c6 157
defrost 5:57b06b4b47c6 158 /**
defrost 5:57b06b4b47c6 159 * Called when the nrf creates an interrupt.
defrost 5:57b06b4b47c6 160 *
defrost 5:57b06b4b47c6 161 */
defrost 5:57b06b4b47c6 162 void Battery::intNrf(){
defrost 5:57b06b4b47c6 163 // DBG("NRF Interrupt Started.");
defrost 5:57b06b4b47c6 164 int pipe=0;
defrost 5:57b06b4b47c6 165 int status=0;
defrost 5:57b06b4b47c6 166 int width=0;
defrost 5:57b06b4b47c6 167
defrost 5:57b06b4b47c6 168 //
defrost 5:57b06b4b47c6 169 //Get status and pipe
defrost 5:57b06b4b47c6 170 //
epgmdm 11:87ab310924f0 171 spiNRF();
defrost 5:57b06b4b47c6 172 status = nrf->checkStatus();
defrost 5:57b06b4b47c6 173 pipe = (status >> 1);
defrost 5:57b06b4b47c6 174 pipe = (pipe & 0x0007);
defrost 5:57b06b4b47c6 175
defrost 5:57b06b4b47c6 176 //
defrost 5:57b06b4b47c6 177 // Check if max RT is reached
defrost 5:57b06b4b47c6 178 //
defrost 5:57b06b4b47c6 179 if (((status >>MAX_RT)& 01) ==1) {
defrost 5:57b06b4b47c6 180 maxRT=true;
defrost 5:57b06b4b47c6 181 DBG("MAX RT flag is set.PTB20= %d status=%x",*ce,status);
defrost 5:57b06b4b47c6 182 *ce = 0;
defrost 5:57b06b4b47c6 183 DBG("PTB20= %d status=%x",*ce,status);
defrost 5:57b06b4b47c6 184 nrf->clearStatus();
defrost 5:57b06b4b47c6 185 *ledRed=0;
defrost 5:57b06b4b47c6 186 }
defrost 5:57b06b4b47c6 187
defrost 5:57b06b4b47c6 188 //
defrost 5:57b06b4b47c6 189 // Check if data received
defrost 5:57b06b4b47c6 190 //
defrost 5:57b06b4b47c6 191 if (((status >>RX_DR)& 01) ==1) { // Received a packet
defrost 5:57b06b4b47c6 192 width= nrf->getRxData(dataRx);
defrost 5:57b06b4b47c6 193 dataRx[width]='\0';
defrost 5:57b06b4b47c6 194 if (dataRx[0]!='S'){
defrost 5:57b06b4b47c6 195 DBG("intNrf>D[p=%1d]=<%2s> ",pipe,dataRx);
defrost 5:57b06b4b47c6 196 }
defrost 5:57b06b4b47c6 197 maxRT=false;
defrost 5:57b06b4b47c6 198 *ce = 1;
defrost 5:57b06b4b47c6 199 nrf->clearStatus();
defrost 5:57b06b4b47c6 200 switch (dataRx[0]) {
defrost 5:57b06b4b47c6 201 case ('C'):{
defrost 5:57b06b4b47c6 202 doCheckIn();
defrost 5:57b06b4b47c6 203 flagNextFile = 1;
defrost 5:57b06b4b47c6 204 break;}
defrost 5:57b06b4b47c6 205 case ('D'):{
defrost 5:57b06b4b47c6 206 flag = &flagSendFileName;
defrost 5:57b06b4b47c6 207 delay->detach();
defrost 5:57b06b4b47c6 208 delay->attach(this, &Battery::setFlag, 1.5);
defrost 5:57b06b4b47c6 209 break;}
defrost 5:57b06b4b47c6 210 case ('F'):{
defrost 5:57b06b4b47c6 211 flag = &flagSendFileSize;
defrost 5:57b06b4b47c6 212 countSD = 0;
defrost 5:57b06b4b47c6 213 delay->detach();
defrost 5:57b06b4b47c6 214 delay->attach(this, &Battery::setFlag, 1.50);
defrost 5:57b06b4b47c6 215 break;}
defrost 5:57b06b4b47c6 216 case ('S'):{
defrost 5:57b06b4b47c6 217 flag = &flagSendFile;
defrost 5:57b06b4b47c6 218 delay->detach();
defrost 5:57b06b4b47c6 219 // printf("%d\n\r",countSD%16);
defrost 5:57b06b4b47c6 220 //delay->attach(this, &Battery::setFlag, 0.05);
defrost 5:57b06b4b47c6 221
defrost 5:57b06b4b47c6 222 if (countSD % (SDBUFFERSIZE/32) == 0) {
defrost 5:57b06b4b47c6 223 delay->attach(this, &Battery::setFlag, 1.5); // wait to write to SD card
defrost 5:57b06b4b47c6 224 } else {
defrost 5:57b06b4b47c6 225 delay->attach_us(this, &Battery::setFlag, 800);
defrost 5:57b06b4b47c6 226 }
defrost 5:57b06b4b47c6 227 countSD++;
defrost 5:57b06b4b47c6 228 break;}
defrost 5:57b06b4b47c6 229 case ('T'):{
defrost 5:57b06b4b47c6 230 time_t now;
defrost 5:57b06b4b47c6 231 sscanf (&dataRx[2],"%x",&now );
defrost 5:57b06b4b47c6 232 set_time(now);
defrost 5:57b06b4b47c6 233 now= time(NULL);
defrost 5:57b06b4b47c6 234 struct tm * timeInf = localtime(&now);
defrost 5:57b06b4b47c6 235 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 236
defrost 5:57b06b4b47c6 237 break;}
defrost 5:57b06b4b47c6 238
defrost 5:57b06b4b47c6 239 }
defrost 5:57b06b4b47c6 240 }
defrost 5:57b06b4b47c6 241 if (((status >>TX_DS)& 01) ==1) {
defrost 5:57b06b4b47c6 242 // maxRT=false;
defrost 5:57b06b4b47c6 243 nrf->flushTx();
defrost 5:57b06b4b47c6 244 nrf->flushRx();
defrost 5:57b06b4b47c6 245 //DBG(" TX_DS");
defrost 5:57b06b4b47c6 246 }
defrost 5:57b06b4b47c6 247
defrost 5:57b06b4b47c6 248 *ledBlue = !*ledBlue;
defrost 5:57b06b4b47c6 249 *ledRed = !maxRT;
defrost 5:57b06b4b47c6 250 }
defrost 5:57b06b4b47c6 251
defrost 5:57b06b4b47c6 252 /**
defrost 5:57b06b4b47c6 253 * Transmits the data and sets up a call back to check data sent 30s later.
defrost 5:57b06b4b47c6 254 */
defrost 5:57b06b4b47c6 255
defrost 5:57b06b4b47c6 256 /**
defrost 5:57b06b4b47c6 257 * Checks to see if transmit failed and then retransmit
defrost 5:57b06b4b47c6 258 *
defrost 5:57b06b4b47c6 259 */
defrost 5:57b06b4b47c6 260 void Battery::retransmit(){
defrost 5:57b06b4b47c6 261
defrost 5:57b06b4b47c6 262 if (maxRT) {
defrost 5:57b06b4b47c6 263 DBG("Retransmit");
defrost 5:57b06b4b47c6 264 *ledRed=1;
defrost 5:57b06b4b47c6 265 *ce = 1;
defrost 5:57b06b4b47c6 266 maxRT=false;
defrost 5:57b06b4b47c6 267 nrf->clearStatus();
defrost 5:57b06b4b47c6 268 nrf->flushRx();
defrost 5:57b06b4b47c6 269 nrf->retransmitData();
defrost 5:57b06b4b47c6 270 }
defrost 5:57b06b4b47c6 271 }
defrost 5:57b06b4b47c6 272
defrost 5:57b06b4b47c6 273 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 274 // Main routines
defrost 5:57b06b4b47c6 275 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 276
defrost 5:57b06b4b47c6 277 /**
defrost 5:57b06b4b47c6 278 * Checks in the Battery to the locker
defrost 5:57b06b4b47c6 279 * Transmits "C ID"
defrost 5:57b06b4b47c6 280 *
defrost 5:57b06b4b47c6 281 */
defrost 5:57b06b4b47c6 282 void Battery::checkIn() {
defrost 5:57b06b4b47c6 283 sprintf(dataTx, "C %04X", (int) (id&0XFFFF));
defrost 5:57b06b4b47c6 284 DBG("Check into locker [%s]", dataTx);
epgmdm 11:87ab310924f0 285 spiNRF();
defrost 5:57b06b4b47c6 286 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 287 }
defrost 5:57b06b4b47c6 288
defrost 5:57b06b4b47c6 289 /**
defrost 5:57b06b4b47c6 290 * Checks in the Battery to the locker
defrost 5:57b06b4b47c6 291 * Acknowledge received.
defrost 5:57b06b4b47c6 292 * Changes the TX pipe address.
defrost 5:57b06b4b47c6 293 *
defrost 5:57b06b4b47c6 294 */
defrost 5:57b06b4b47c6 295 void Battery::doCheckIn() {
defrost 5:57b06b4b47c6 296 nrf->setTxAddress(privateAddr);
defrost 5:57b06b4b47c6 297 checkedIn = 1;
defrost 5:57b06b4b47c6 298 DBG("Checked into locker");
defrost 5:57b06b4b47c6 299 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 300 #ifdef DEBUG
defrost 5:57b06b4b47c6 301 nrf->printDetails();
defrost 5:57b06b4b47c6 302 #endif
defrost 5:57b06b4b47c6 303 }
defrost 5:57b06b4b47c6 304 /**
defrost 5:57b06b4b47c6 305 * Checks in the Battery to the locker
defrost 5:57b06b4b47c6 306 * Acknowledge received.
defrost 5:57b06b4b47c6 307 * Changes the TX pipe address.
defrost 5:57b06b4b47c6 308 *
defrost 5:57b06b4b47c6 309 */
defrost 5:57b06b4b47c6 310 void Battery::doCheckOut() {
defrost 5:57b06b4b47c6 311 nrf->setTxAddress(openAddr);
defrost 5:57b06b4b47c6 312 checkedIn = 0;
defrost 5:57b06b4b47c6 313 DBG("Checked out of locker");
defrost 5:57b06b4b47c6 314 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 315 #ifdef DEBUG
defrost 5:57b06b4b47c6 316 nrf->printDetails();
defrost 5:57b06b4b47c6 317 #endif
defrost 5:57b06b4b47c6 318 }
defrost 5:57b06b4b47c6 319
defrost 5:57b06b4b47c6 320 /**
defrost 5:57b06b4b47c6 321 * Slow routines for Battery
defrost 5:57b06b4b47c6 322 */
defrost 5:57b06b4b47c6 323 void Battery::loop() {
defrost 5:57b06b4b47c6 324 while (1) {
defrost 5:57b06b4b47c6 325 *ledGreen = !checkedIn;
defrost 5:57b06b4b47c6 326
defrost 5:57b06b4b47c6 327 if (flagGetTime) {
defrost 5:57b06b4b47c6 328 flagGetTime=0;
defrost 5:57b06b4b47c6 329 getTime();
defrost 5:57b06b4b47c6 330
defrost 5:57b06b4b47c6 331 }
defrost 5:57b06b4b47c6 332 if (flagNextFile) {
defrost 5:57b06b4b47c6 333 flagNextFile = 0;
defrost 5:57b06b4b47c6 334 getTime();
defrost 5:57b06b4b47c6 335 nextFileToTx();
defrost 5:57b06b4b47c6 336 if (fileLength>0){
defrost 5:57b06b4b47c6 337 INFO("Sending file %s.",txFileName)
epgmdm 11:87ab310924f0 338 sendDirName();
defrost 5:57b06b4b47c6 339 } else {
defrost 5:57b06b4b47c6 340 INFO("No more files to send.")
defrost 5:57b06b4b47c6 341 }
defrost 5:57b06b4b47c6 342 }
defrost 5:57b06b4b47c6 343 if (flagSendFileName) {
defrost 5:57b06b4b47c6 344 flagSendFileName = 0;
defrost 5:57b06b4b47c6 345 sendFileName();
defrost 5:57b06b4b47c6 346
defrost 5:57b06b4b47c6 347 }
defrost 5:57b06b4b47c6 348 if (flagSendFileSize) {
defrost 5:57b06b4b47c6 349 flagSendFileSize = 0;
defrost 5:57b06b4b47c6 350 sendFileSize();
defrost 5:57b06b4b47c6 351 }
defrost 5:57b06b4b47c6 352 if (flagSendFile) {
defrost 5:57b06b4b47c6 353 flagSendFile = 0;
defrost 5:57b06b4b47c6 354 sendFile();
defrost 5:57b06b4b47c6 355 }
defrost 5:57b06b4b47c6 356 if (flagSendFileDone) {
defrost 5:57b06b4b47c6 357 flagSendFileDone = 0;
defrost 5:57b06b4b47c6 358 sendFileDone();
defrost 5:57b06b4b47c6 359 INFO("File sent <%s>",txFileName);
defrost 5:57b06b4b47c6 360 flag = &flagNextFile;
defrost 5:57b06b4b47c6 361 delay->detach();
defrost 5:57b06b4b47c6 362 delay->attach(this, &Battery::setFlag, 2.5);
defrost 5:57b06b4b47c6 363 }
defrost 5:57b06b4b47c6 364 __WFI();
defrost 5:57b06b4b47c6 365 }
defrost 5:57b06b4b47c6 366 }
defrost 5:57b06b4b47c6 367
defrost 5:57b06b4b47c6 368 /**
defrost 5:57b06b4b47c6 369 * sends a request for time update.
defrost 5:57b06b4b47c6 370 */
defrost 5:57b06b4b47c6 371 void Battery::getTime() {
defrost 5:57b06b4b47c6 372 DBG("getTime");
defrost 5:57b06b4b47c6 373 nrf->transmitData("T ",2);
defrost 5:57b06b4b47c6 374 }
defrost 5:57b06b4b47c6 375 /**
defrost 5:57b06b4b47c6 376 * Selects the next file to transmit
defrost 5:57b06b4b47c6 377 * Uses /sd/lastFileTx.txt to store the lst file transmitted
defrost 5:57b06b4b47c6 378 */
defrost 5:57b06b4b47c6 379 void Battery::nextFileToTx() {
defrost 5:57b06b4b47c6 380
defrost 5:57b06b4b47c6 381 DBG("> nextTxFile");
defrost 5:57b06b4b47c6 382
defrost 5:57b06b4b47c6 383 char lastFileName[64];
defrost 5:57b06b4b47c6 384 char fileName[64];
defrost 5:57b06b4b47c6 385 char fn[40];
defrost 5:57b06b4b47c6 386 unsigned int txLength;
defrost 5:57b06b4b47c6 387
defrost 5:57b06b4b47c6 388 sdBuffPnt=0;// Tells sendFile to read from SD the first time.
defrost 5:57b06b4b47c6 389 fileLength = 0;
defrost 5:57b06b4b47c6 390 strcpy(txFileName, "");
defrost 5:57b06b4b47c6 391 spiSD();
defrost 5:57b06b4b47c6 392 // Read in last file name
defrost 5:57b06b4b47c6 393 FILE *fp = fopen("/sd/lastFileTx.txt", "r");
defrost 5:57b06b4b47c6 394 if (fp != NULL) {
defrost 5:57b06b4b47c6 395 if (fscanf(fp, "%s %u", lastFileName, &txLength) != 2) {
defrost 5:57b06b4b47c6 396 sprintf(lastFileName, "a");
defrost 5:57b06b4b47c6 397 }
defrost 5:57b06b4b47c6 398 fclose(fp);
defrost 5:57b06b4b47c6 399 } else {
defrost 5:57b06b4b47c6 400 sprintf(lastFileName, "a");
defrost 5:57b06b4b47c6 401 }
defrost 5:57b06b4b47c6 402 DBG("nextFileToTx>Last Filename=%s len=%u",lastFileName,txLength);
defrost 5:57b06b4b47c6 403 // Open directory and read files
defrost 5:57b06b4b47c6 404 DIR *dp;
defrost 5:57b06b4b47c6 405 struct dirent *dirp;
defrost 5:57b06b4b47c6 406 spiSD();
defrost 5:57b06b4b47c6 407 dp = opendir(logDir);
defrost 5:57b06b4b47c6 408 if (dp == NULL) {
defrost 5:57b06b4b47c6 409 DBG("nextTxFile logdir %s is NULL", logDir);
defrost 5:57b06b4b47c6 410 } else
defrost 5:57b06b4b47c6 411 //read all directory and file names in current directory into filename vector
defrost 5:57b06b4b47c6 412 while ((dirp = readdir(dp)) != NULL) {
defrost 5:57b06b4b47c6 413 DBG("nextFileToTx> WHILE lst [%s] dir [%s] %d",lastFileName, dirp->d_name,strcmp(lastFileName, dirp->d_name));
defrost 5:57b06b4b47c6 414
defrost 5:57b06b4b47c6 415 if (strcmp(lastFileName, dirp->d_name) < 0) {
defrost 5:57b06b4b47c6 416 strcpy(fileName, dirp->d_name);
defrost 5:57b06b4b47c6 417 sprintf(txFileName, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 418 fp = fopen(txFileName, "r");
defrost 5:57b06b4b47c6 419 fseek(fp, 0L, SEEK_END);
defrost 5:57b06b4b47c6 420 fileLength = ftell(fp);
defrost 5:57b06b4b47c6 421 fclose(fp);
defrost 5:57b06b4b47c6 422 DBG("CHOSEN %s old %s ", fileName, lastFileName );
defrost 5:57b06b4b47c6 423 break;
defrost 5:57b06b4b47c6 424 }
defrost 5:57b06b4b47c6 425 if (strcmp(lastFileName, dirp->d_name) == 0) {
defrost 5:57b06b4b47c6 426
defrost 5:57b06b4b47c6 427 strcpy(fileName, dirp->d_name);
defrost 5:57b06b4b47c6 428 sprintf(fn, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 429 fp = fopen(fn, "r");
defrost 5:57b06b4b47c6 430 fseek(fp, 0L, SEEK_END);
defrost 5:57b06b4b47c6 431 fileLength = ftell(fp);
defrost 5:57b06b4b47c6 432 fclose(fp);
defrost 5:57b06b4b47c6 433 if (txLength < fileLength) {
defrost 5:57b06b4b47c6 434 sprintf(txFileName, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 435 DBG("More to send is %s old %u new %u ", fileName, txLength,fileLength );
defrost 5:57b06b4b47c6 436 break;
defrost 5:57b06b4b47c6 437 }
defrost 5:57b06b4b47c6 438 }
defrost 5:57b06b4b47c6 439 // test filename and if greater than last read then choose.
defrost 5:57b06b4b47c6 440 }
defrost 5:57b06b4b47c6 441 closedir(dp);
defrost 5:57b06b4b47c6 442 if (strlen(txFileName) > 0) {
defrost 5:57b06b4b47c6 443 strcpy(txFileName, fileName);
defrost 5:57b06b4b47c6 444 sprintf(fullTxFilePath, "%s/%s", logDir, txFileName);
defrost 5:57b06b4b47c6 445 fileLeft = fileLength;
defrost 5:57b06b4b47c6 446
defrost 5:57b06b4b47c6 447 } else {
defrost 5:57b06b4b47c6 448 fileLength =0;
defrost 5:57b06b4b47c6 449 }
defrost 5:57b06b4b47c6 450 DBG("nextTxFile> Next is [%s] length %d", txFileName, fileLength);
defrost 5:57b06b4b47c6 451
defrost 5:57b06b4b47c6 452 }
defrost 5:57b06b4b47c6 453 /**
defrost 5:57b06b4b47c6 454 * Sends the directory name, allows the otherside to create.
defrost 5:57b06b4b47c6 455 */
defrost 5:57b06b4b47c6 456
defrost 5:57b06b4b47c6 457 void Battery::sendDirName() {
defrost 5:57b06b4b47c6 458 sprintf(dataTx, "D %s", logDir);
defrost 5:57b06b4b47c6 459
defrost 5:57b06b4b47c6 460 DBG("send Dir Name [%s].\n\r", dataTx);
defrost 5:57b06b4b47c6 461
epgmdm 11:87ab310924f0 462 spiNRF();
defrost 5:57b06b4b47c6 463 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 464
defrost 5:57b06b4b47c6 465 }
defrost 5:57b06b4b47c6 466 /**
defrost 5:57b06b4b47c6 467 * Sends the file name, if fileLength >0
defrost 5:57b06b4b47c6 468 */
defrost 5:57b06b4b47c6 469
defrost 5:57b06b4b47c6 470 void Battery::sendFileName() {
defrost 5:57b06b4b47c6 471 // delay->detach();
defrost 5:57b06b4b47c6 472 if (fileLength < 1) {
defrost 5:57b06b4b47c6 473 return;
defrost 5:57b06b4b47c6 474 }
defrost 5:57b06b4b47c6 475 sprintf(dataTx, "F %20s", txFileName);
defrost 5:57b06b4b47c6 476
defrost 5:57b06b4b47c6 477 DBG("send File Name [%s] \n\r", dataTx);
defrost 5:57b06b4b47c6 478
epgmdm 11:87ab310924f0 479 spiNRF();
defrost 5:57b06b4b47c6 480 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 481 }
defrost 5:57b06b4b47c6 482
defrost 5:57b06b4b47c6 483 /**
defrost 5:57b06b4b47c6 484 * Sends the file length to the locker - this is also the cue that the following bytes are the file.
defrost 5:57b06b4b47c6 485 */
defrost 5:57b06b4b47c6 486 void Battery::sendFileSize() {
defrost 5:57b06b4b47c6 487 if (fileLength < 1) {
defrost 5:57b06b4b47c6 488 return;
defrost 5:57b06b4b47c6 489 }
defrost 5:57b06b4b47c6 490 filePointer = 0;
defrost 5:57b06b4b47c6 491 sprintf(dataTx, "S %X", fileLength);
defrost 5:57b06b4b47c6 492
defrost 5:57b06b4b47c6 493
defrost 5:57b06b4b47c6 494 DBG("send File size [%s]", dataTx);
defrost 5:57b06b4b47c6 495
epgmdm 11:87ab310924f0 496 spiNRF();
defrost 5:57b06b4b47c6 497 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 498 }
defrost 5:57b06b4b47c6 499
defrost 5:57b06b4b47c6 500 /**
defrost 5:57b06b4b47c6 501 * Sends the file length to the locker - this is also the cue that the following bytes are the file.
defrost 5:57b06b4b47c6 502 */
defrost 5:57b06b4b47c6 503 void Battery::sendFile() {
defrost 5:57b06b4b47c6 504
defrost 5:57b06b4b47c6 505 // printf("send File [%s] at %d %d\n\r",fullTxFilePath,fileLength, fileLeft );
defrost 5:57b06b4b47c6 506
epgmdm 11:87ab310924f0 507 // if (fileLength < 1) {
epgmdm 11:87ab310924f0 508 // INFO("File Sent");
epgmdm 11:87ab310924f0 509 // sdBuffPnt=0;
epgmdm 11:87ab310924f0 510 // return;
epgmdm 11:87ab310924f0 511 // }
defrost 5:57b06b4b47c6 512 if (sdBuffPnt>=SDBUFFERSIZE){
defrost 5:57b06b4b47c6 513 sdBuffPnt=0;
defrost 5:57b06b4b47c6 514 }
defrost 5:57b06b4b47c6 515 if (sdBuffPnt == 0) {
defrost 5:57b06b4b47c6 516 spiSD();
defrost 5:57b06b4b47c6 517 FILE *fp = fopen(fullTxFilePath, "rb");
defrost 5:57b06b4b47c6 518 if (fp == NULL) {
epgmdm 11:87ab310924f0 519 spiNRF();
defrost 5:57b06b4b47c6 520 INFO("sendFile> File %s not found for reading",fullTxFilePath);
defrost 5:57b06b4b47c6 521 return;
defrost 5:57b06b4b47c6 522 }
defrost 5:57b06b4b47c6 523 spiSD();
defrost 5:57b06b4b47c6 524 fseek(fp, fileLength - fileLeft, SEEK_SET);
defrost 5:57b06b4b47c6 525 sizeRead = fread(sdBuffer, 1,SDBUFFERSIZE, fp);
defrost 5:57b06b4b47c6 526 fileLeft -= sizeRead;
defrost 5:57b06b4b47c6 527 fclose(fp);
defrost 5:57b06b4b47c6 528 // printf("File %s %d \n\r",fullTxFilePath,sizeRead);
epgmdm 11:87ab310924f0 529 spiNRF();
defrost 5:57b06b4b47c6 530
defrost 5:57b06b4b47c6 531 }
defrost 5:57b06b4b47c6 532
defrost 5:57b06b4b47c6 533 if ((count++) % 256 ==0) {
defrost 5:57b06b4b47c6 534 DBG("W %d %d", fileLeft, sizeRead);
defrost 5:57b06b4b47c6 535 }
defrost 5:57b06b4b47c6 536 int size=32;
defrost 5:57b06b4b47c6 537 if (sizeRead<32){
defrost 5:57b06b4b47c6 538 size=sizeRead;
defrost 5:57b06b4b47c6 539 }
defrost 5:57b06b4b47c6 540 if ((fileLeft==0)&&(sizeRead==0)){
defrost 5:57b06b4b47c6 541 sdBuffPnt=0;
defrost 5:57b06b4b47c6 542 flagSendFileDone=1;
defrost 5:57b06b4b47c6 543 DBG("Done sending");
defrost 5:57b06b4b47c6 544 }else{
defrost 5:57b06b4b47c6 545 sizeRead -= size;
defrost 5:57b06b4b47c6 546 nrf->transmitData(&(sdBuffer[sdBuffPnt]), size);
defrost 5:57b06b4b47c6 547 sdBuffPnt+=size;
defrost 5:57b06b4b47c6 548 }
defrost 5:57b06b4b47c6 549
defrost 5:57b06b4b47c6 550
defrost 5:57b06b4b47c6 551 }
defrost 5:57b06b4b47c6 552
defrost 5:57b06b4b47c6 553 /**
defrost 5:57b06b4b47c6 554 * Update the last file sent file
defrost 5:57b06b4b47c6 555 */
defrost 5:57b06b4b47c6 556 void Battery::sendFileDone() {
defrost 5:57b06b4b47c6 557 if (strlen(txFileName)>0){
defrost 5:57b06b4b47c6 558 spiSD();
defrost 5:57b06b4b47c6 559 FILE *fp = fopen("/sd/lastFileTx.txt", "w");
defrost 5:57b06b4b47c6 560 if (fp != NULL) {
defrost 5:57b06b4b47c6 561 fprintf(fp, "%s %u \n", txFileName, fileLength);
defrost 5:57b06b4b47c6 562 fclose(fp);
defrost 5:57b06b4b47c6 563 DBG("sendFileDone> updated lastFileTx.txt %s %u ", txFileName, fileLength);
defrost 5:57b06b4b47c6 564 }else{
defrost 5:57b06b4b47c6 565 INFO("sendFileDone> Cannot update file lastFiletx.txt");
defrost 5:57b06b4b47c6 566 }
epgmdm 11:87ab310924f0 567 spiNRF();
defrost 5:57b06b4b47c6 568
defrost 5:57b06b4b47c6 569 }
defrost 5:57b06b4b47c6 570
defrost 5:57b06b4b47c6 571 }
defrost 5:57b06b4b47c6 572
defrost 5:57b06b4b47c6 573 /**
defrost 5:57b06b4b47c6 574 * Sets the value pointed to by flag to 1.
defrost 5:57b06b4b47c6 575 */
defrost 5:57b06b4b47c6 576 void Battery::setFlag() {
defrost 5:57b06b4b47c6 577 if (flag != NULL) {
defrost 5:57b06b4b47c6 578 *flag = 1;
defrost 5:57b06b4b47c6 579 }
defrost 5:57b06b4b47c6 580 flag = NULL;
defrost 5:57b06b4b47c6 581 }