The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Committer:
epgmdm
Date:
Thu Jun 23 13:30:17 2016 +0000
Revision:
30:47f73b850a5e
Parent:
29:bbdbe1094a63
Child:
31:3bf76548e024
Changed way files are sent.

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 18:be77ad141fac 37 Battery::Battery(FILE* fp, Serial *pc) : SolarNanoGrid(fp,pc)
epgmdm 18:be77ad141fac 38 {
defrost 5:57b06b4b47c6 39 DBG("Initialize class");
adrienBiz 8:0acda4f2e0a8 40 // ***** Variable initialisation *****
adrienBiz 8:0acda4f2e0a8 41 // Public variable initialisation
epgmdm 11:87ab310924f0 42
epgmdm 12:d9b13f31206d 43 maxChargeRate=1.0; // Charge rate in amps
epgmdm 11:87ab310924f0 44
adrienBiz 8:0acda4f2e0a8 45 // Protected variables initialisation
adrienBiz 8:0acda4f2e0a8 46 count=0;
adrienBiz 8:0acda4f2e0a8 47 sizeRead=0;
adrienBiz 8:0acda4f2e0a8 48 fileLength=0;
adrienBiz 8:0acda4f2e0a8 49 filePointer=0;
adrienBiz 8:0acda4f2e0a8 50 fileLeft=0;
epgmdm 11:87ab310924f0 51
adrienBiz 8:0acda4f2e0a8 52 sdBuffPnt=0;
adrienBiz 8:0acda4f2e0a8 53 /**
adrienBiz 8:0acda4f2e0a8 54 * Open channel address of the locker
adrienBiz 8:0acda4f2e0a8 55 */
adrienBiz 8:0acda4f2e0a8 56 openAddr=0;
adrienBiz 8:0acda4f2e0a8 57
adrienBiz 8:0acda4f2e0a8 58 /**
adrienBiz 8:0acda4f2e0a8 59 * Private channel address of the locker
adrienBiz 8:0acda4f2e0a8 60 */
adrienBiz 8:0acda4f2e0a8 61 privateAddr=0;
adrienBiz 8:0acda4f2e0a8 62
adrienBiz 8:0acda4f2e0a8 63 countSD=0;
adrienBiz 8:0acda4f2e0a8 64
adrienBiz 8:0acda4f2e0a8 65 // Protected flags: *
adrienBiz 8:0acda4f2e0a8 66 checkedIn=0;
epgmdm 18:be77ad141fac 67 flagDirFileName=0;
adrienBiz 8:0acda4f2e0a8 68 flagGetTime=0;
adrienBiz 8:0acda4f2e0a8 69 flagNextFile=0;
adrienBiz 8:0acda4f2e0a8 70 flagSendFileName=0;
adrienBiz 8:0acda4f2e0a8 71 flagSendFileSize=0;
adrienBiz 8:0acda4f2e0a8 72 flagSendFile=0;
adrienBiz 8:0acda4f2e0a8 73 flagSendFileDone=0;
epgmdm 12:d9b13f31206d 74 flagGetMaxCharge=0;
epgmdm 12:d9b13f31206d 75 sendingFile=0;
epgmdm 18:be77ad141fac 76 flagStartFileTx=0;
defrost 17:dc19b3b39790 77 startCharge = false;
epgmdm 18:be77ad141fac 78 flagHubNotReady=0;
epgmdm 18:be77ad141fac 79 flagCheckOut=0;
defrost 28:e85a86fc8b59 80 sending=0;
adrienBiz 8:0acda4f2e0a8 81 // Protected interrupts
adrienBiz 8:0acda4f2e0a8 82 button=NULL;
adrienBiz 8:0acda4f2e0a8 83 txWatch=NULL;
adrienBiz 8:0acda4f2e0a8 84 delay=NULL;
epgmdm 11:87ab310924f0 85
adrienBiz 8:0acda4f2e0a8 86 //Private variables initialisation
adrienBiz 8:0acda4f2e0a8 87 maxRT=false;
epgmdm 11:87ab310924f0 88
adrienBiz 8:0acda4f2e0a8 89 // ***** End of variables initialisation *****
epgmdm 11:87ab310924f0 90
epgmdm 11:87ab310924f0 91 // No other information needed from the config file
epgmdm 11:87ab310924f0 92 fclose(fp);
epgmdm 11:87ab310924f0 93 //
epgmdm 11:87ab310924f0 94 // Setup variables
epgmdm 11:87ab310924f0 95 //
epgmdm 11:87ab310924f0 96 txFileName = (char*) calloc(80, 1);
epgmdm 11:87ab310924f0 97 fullTxFilePath = (char*) calloc(80, 1);
epgmdm 11:87ab310924f0 98 logDir = (char*) calloc(20, 1);
epgmdm 11:87ab310924f0 99 sdBuffer = (char*) calloc(SDBUFFERSIZE, 1);
epgmdm 11:87ab310924f0 100
epgmdm 11:87ab310924f0 101
epgmdm 11:87ab310924f0 102
epgmdm 11:87ab310924f0 103
defrost 5:57b06b4b47c6 104 //
defrost 5:57b06b4b47c6 105 // Setup NRF
defrost 5:57b06b4b47c6 106 //
defrost 5:57b06b4b47c6 107 privateAddr = ((long long)communityID <<16)+ (id &0XFFFF);
defrost 5:57b06b4b47c6 108 openAddr = privateAddr & 0xFFFFFFFF00;
defrost 5:57b06b4b47c6 109
defrost 5:57b06b4b47c6 110 DBG("Channel %x Address %llx Address Locker %llx", chan, privateAddr, openAddr);
defrost 5:57b06b4b47c6 111
epgmdm 11:87ab310924f0 112 spiNRF();
defrost 5:57b06b4b47c6 113 nrf->quickTxSetup(chan, openAddr);
defrost 5:57b06b4b47c6 114 nrf->setRadio(0x01, 0x03); // 2MB/S 0dB
defrost 5:57b06b4b47c6 115 nrf->setTxRetry(0x0F, 0x0F);
defrost 5:57b06b4b47c6 116 nrfFlush();
defrost 5:57b06b4b47c6 117 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 118 #ifdef DEBUG
defrost 5:57b06b4b47c6 119 nrf->printDetails();
defrost 5:57b06b4b47c6 120 #endif
defrost 5:57b06b4b47c6 121 //
defrost 5:57b06b4b47c6 122 // Setup logging directories
defrost 5:57b06b4b47c6 123 //
defrost 5:57b06b4b47c6 124 sprintf(logDir, "/sd/data/%02X%04X", (communityID&0XFF),(id&0XFFFF));
defrost 5:57b06b4b47c6 125 DBG(" Log directory %s", logDir);
defrost 5:57b06b4b47c6 126 spiSD();
defrost 5:57b06b4b47c6 127 mkdir("/sd/data", 777);
defrost 5:57b06b4b47c6 128 mkdir(logDir, 777);
defrost 5:57b06b4b47c6 129 DBG(" made");
defrost 5:57b06b4b47c6 130 //
defrost 5:57b06b4b47c6 131 // Setup interrupts
defrost 5:57b06b4b47c6 132 //
defrost 5:57b06b4b47c6 133 button = new InterruptIn(SW2);
defrost 5:57b06b4b47c6 134 button->fall(this, &Battery::intButton);
defrost 5:57b06b4b47c6 135
defrost 5:57b06b4b47c6 136 nrfInt = new InterruptIn(PTC18);
defrost 5:57b06b4b47c6 137 nrfInt->fall(this, &Battery::intNrf); // attach nrf interrupt.
defrost 5:57b06b4b47c6 138
defrost 5:57b06b4b47c6 139 txWatch = new Ticker ();
defrost 5:57b06b4b47c6 140 txWatch->attach(this,&Battery::retransmit,10.01);
defrost 5:57b06b4b47c6 141
defrost 5:57b06b4b47c6 142 delay = new Timeout();
defrost 5:57b06b4b47c6 143
defrost 5:57b06b4b47c6 144 loop();
defrost 5:57b06b4b47c6 145 }
defrost 5:57b06b4b47c6 146
defrost 5:57b06b4b47c6 147 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 148 // Interrupt routines
defrost 5:57b06b4b47c6 149 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 150
defrost 5:57b06b4b47c6 151 /**
defrost 5:57b06b4b47c6 152 * When the button is pressed transmit something
defrost 5:57b06b4b47c6 153 */
epgmdm 18:be77ad141fac 154 void Battery::intButton()
epgmdm 18:be77ad141fac 155 {
epgmdm 18:be77ad141fac 156 if (!maxRT) {
defrost 5:57b06b4b47c6 157 *ce = 1;
epgmdm 18:be77ad141fac 158 if(checkedIn==0) {
defrost 5:57b06b4b47c6 159 checkIn();
epgmdm 18:be77ad141fac 160 } else {
defrost 5:57b06b4b47c6 161 flagNextFile = 1;
defrost 5:57b06b4b47c6 162 }
defrost 5:57b06b4b47c6 163
defrost 5:57b06b4b47c6 164 }
defrost 5:57b06b4b47c6 165 }
defrost 5:57b06b4b47c6 166
defrost 5:57b06b4b47c6 167 /**
defrost 5:57b06b4b47c6 168 * Called when the nrf creates an interrupt.
defrost 5:57b06b4b47c6 169 *
defrost 5:57b06b4b47c6 170 */
epgmdm 18:be77ad141fac 171 void Battery::intNrf()
epgmdm 18:be77ad141fac 172 {
defrost 5:57b06b4b47c6 173 // DBG("NRF Interrupt Started.");
defrost 5:57b06b4b47c6 174 int pipe=0;
defrost 5:57b06b4b47c6 175 int status=0;
defrost 5:57b06b4b47c6 176 int width=0;
defrost 5:57b06b4b47c6 177
defrost 28:e85a86fc8b59 178 //INFO("intNrf");
defrost 28:e85a86fc8b59 179
defrost 5:57b06b4b47c6 180 //
defrost 5:57b06b4b47c6 181 //Get status and pipe
defrost 5:57b06b4b47c6 182 //
epgmdm 11:87ab310924f0 183 spiNRF();
defrost 5:57b06b4b47c6 184 status = nrf->checkStatus();
defrost 5:57b06b4b47c6 185 pipe = (status >> 1);
defrost 5:57b06b4b47c6 186 pipe = (pipe & 0x0007);
defrost 5:57b06b4b47c6 187
defrost 5:57b06b4b47c6 188 //
defrost 5:57b06b4b47c6 189 // Check if max RT is reached
defrost 5:57b06b4b47c6 190 //
defrost 29:bbdbe1094a63 191 /*if (((status >>MAX_RT)& 01) ==1) {
epgmdm 18:be77ad141fac 192 if (flagStartFileTx==1) { // Locker not ready to reacieve - wait 25s.
epgmdm 18:be77ad141fac 193 flagHubNotReady = 1;
epgmdm 18:be77ad141fac 194 nrf->clearStatus();
epgmdm 18:be77ad141fac 195 flag = &flagDirFileName;
epgmdm 18:be77ad141fac 196 delay->detach();
epgmdm 18:be77ad141fac 197 delay->attach(this, &Battery::setFlag, 25.0f);
epgmdm 18:be77ad141fac 198 } else {
epgmdm 18:be77ad141fac 199 maxRT=true;
epgmdm 18:be77ad141fac 200 DBG("MAX RT flag is set. PTB20= %d status=%x",(unsigned int) *ce,status);
epgmdm 18:be77ad141fac 201 *ce = 0;
epgmdm 18:be77ad141fac 202 DBG("PTB20= %d status=%x",(unsigned int) *ce,status);
epgmdm 18:be77ad141fac 203 nrf->clearStatus();
epgmdm 18:be77ad141fac 204 *ledRed=0;
epgmdm 18:be77ad141fac 205 }
defrost 29:bbdbe1094a63 206 }*/
defrost 5:57b06b4b47c6 207
defrost 5:57b06b4b47c6 208 //
defrost 5:57b06b4b47c6 209 // Check if data received
defrost 5:57b06b4b47c6 210 //
defrost 5:57b06b4b47c6 211 if (((status >>RX_DR)& 01) ==1) { // Received a packet
defrost 5:57b06b4b47c6 212 width= nrf->getRxData(dataRx);
defrost 5:57b06b4b47c6 213 dataRx[width]='\0';
epgmdm 18:be77ad141fac 214 if (dataRx[0]!='S') {
defrost 5:57b06b4b47c6 215 DBG("intNrf>D[p=%1d]=<%2s> ",pipe,dataRx);
defrost 5:57b06b4b47c6 216 }
defrost 5:57b06b4b47c6 217 maxRT=false;
epgmdm 18:be77ad141fac 218 flagHubNotReady = 0;
defrost 5:57b06b4b47c6 219 *ce = 1;
defrost 5:57b06b4b47c6 220 nrf->clearStatus();
defrost 5:57b06b4b47c6 221 switch (dataRx[0]) {
epgmdm 18:be77ad141fac 222 case ('C'): {
epgmdm 18:be77ad141fac 223 doCheckIn();
epgmdm 18:be77ad141fac 224 flagNextFile = 1;
epgmdm 18:be77ad141fac 225 DBG("Flag Set");
epgmdm 18:be77ad141fac 226 break;
epgmdm 18:be77ad141fac 227 }
epgmdm 18:be77ad141fac 228 case ('D'): {
epgmdm 18:be77ad141fac 229 flagStartFileTx=0; // Hub commmunicating.
epgmdm 18:be77ad141fac 230 flag = &flagSendFileName;
epgmdm 18:be77ad141fac 231 delay->detach();
epgmdm 18:be77ad141fac 232 delay->attach(this, &Battery::setFlag, 1.5);
epgmdm 18:be77ad141fac 233 break;
epgmdm 18:be77ad141fac 234 }
epgmdm 18:be77ad141fac 235 case ('F'): {
epgmdm 18:be77ad141fac 236 flag = &flagSendFileSize;
epgmdm 18:be77ad141fac 237 countSD = 0;
epgmdm 18:be77ad141fac 238 delay->detach();
epgmdm 18:be77ad141fac 239 delay->attach(this, &Battery::setFlag, 1.50);
epgmdm 18:be77ad141fac 240 break;
epgmdm 18:be77ad141fac 241 }
epgmdm 18:be77ad141fac 242 case ('O'): {
epgmdm 18:be77ad141fac 243 doCheckOut();
epgmdm 18:be77ad141fac 244 break;
epgmdm 18:be77ad141fac 245 }
epgmdm 18:be77ad141fac 246 case ('S'): {
epgmdm 18:be77ad141fac 247 flag = &flagSendFile;
defrost 28:e85a86fc8b59 248
epgmdm 18:be77ad141fac 249 delay->detach();
epgmdm 18:be77ad141fac 250 // printf("%d\n\r",countSD%16);
epgmdm 18:be77ad141fac 251 //delay->attach(this, &Battery::setFlag, 0.05);
defrost 5:57b06b4b47c6 252
epgmdm 30:47f73b850a5e 253 if (countSD % (SDBUFFERSIZE/32) == 0) {
epgmdm 18:be77ad141fac 254 delay->attach(this, &Battery::setFlag, 1.5); // wait to write to SD card
epgmdm 18:be77ad141fac 255 } else {
defrost 28:e85a86fc8b59 256 delay->attach_us(this, &Battery::setFlag, 800); // was 800
epgmdm 18:be77ad141fac 257 }
epgmdm 18:be77ad141fac 258 countSD++;
epgmdm 18:be77ad141fac 259 break;
defrost 5:57b06b4b47c6 260 }
epgmdm 18:be77ad141fac 261 case ('T'): {
epgmdm 18:be77ad141fac 262 time_t now;
epgmdm 18:be77ad141fac 263 sscanf (&dataRx[2],"%x",&now );
epgmdm 18:be77ad141fac 264 set_time(now);
epgmdm 18:be77ad141fac 265 now= time(NULL);
epgmdm 18:be77ad141fac 266 struct tm * timeInf = localtime(&now);
defrost 25:1979c129acb0 267 DBG ("Time is now: %04d-%02d-%02d %02d:%02d:%02d",timeInf->tm_year+1900,timeInf->tm_mon+1,timeInf->tm_mday,timeInf->tm_hour, timeInf->tm_min,timeInf->tm_sec);
defrost 5:57b06b4b47c6 268
epgmdm 18:be77ad141fac 269 break;
epgmdm 18:be77ad141fac 270 }
defrost 5:57b06b4b47c6 271
epgmdm 12:d9b13f31206d 272
epgmdm 18:be77ad141fac 273 case ('M'): {
defrost 28:e85a86fc8b59 274
epgmdm 18:be77ad141fac 275 sscanf (&dataRx[2],"%f",&maxChargeRate );
epgmdm 18:be77ad141fac 276 if(maxChargeRate>2.0f) {
epgmdm 18:be77ad141fac 277 maxChargeRate=2.0f;
epgmdm 18:be77ad141fac 278 }
epgmdm 18:be77ad141fac 279 if (maxChargeRate<0.0f) {
epgmdm 18:be77ad141fac 280 maxChargeRate=0.0f;
epgmdm 18:be77ad141fac 281 }
defrost 25:1979c129acb0 282 DBG ("Max charge %f",maxChargeRate);
epgmdm 18:be77ad141fac 283
epgmdm 18:be77ad141fac 284 break;
epgmdm 12:d9b13f31206d 285 }
defrost 5:57b06b4b47c6 286 }
epgmdm 12:d9b13f31206d 287
defrost 5:57b06b4b47c6 288 }
epgmdm 12:d9b13f31206d 289
defrost 5:57b06b4b47c6 290 if (((status >>TX_DS)& 01) ==1) {
defrost 5:57b06b4b47c6 291 // maxRT=false;
defrost 5:57b06b4b47c6 292 nrf->flushTx();
defrost 5:57b06b4b47c6 293 nrf->flushRx();
defrost 5:57b06b4b47c6 294 //DBG(" TX_DS");
defrost 5:57b06b4b47c6 295 }
defrost 5:57b06b4b47c6 296
defrost 5:57b06b4b47c6 297 *ledBlue = !*ledBlue;
defrost 5:57b06b4b47c6 298 *ledRed = !maxRT;
defrost 5:57b06b4b47c6 299 }
defrost 5:57b06b4b47c6 300
defrost 5:57b06b4b47c6 301 /**
defrost 5:57b06b4b47c6 302 * Transmits the data and sets up a call back to check data sent 30s later.
defrost 5:57b06b4b47c6 303 */
defrost 5:57b06b4b47c6 304
defrost 5:57b06b4b47c6 305 /**
defrost 5:57b06b4b47c6 306 * Checks to see if transmit failed and then retransmit
defrost 5:57b06b4b47c6 307 *
defrost 5:57b06b4b47c6 308 */
epgmdm 18:be77ad141fac 309 void Battery::retransmit()
epgmdm 18:be77ad141fac 310 {
defrost 5:57b06b4b47c6 311
defrost 5:57b06b4b47c6 312 if (maxRT) {
defrost 5:57b06b4b47c6 313 DBG("Retransmit");
defrost 5:57b06b4b47c6 314 *ledRed=1;
defrost 5:57b06b4b47c6 315 *ce = 1;
defrost 5:57b06b4b47c6 316 maxRT=false;
defrost 5:57b06b4b47c6 317 nrf->clearStatus();
defrost 5:57b06b4b47c6 318 nrf->flushRx();
defrost 5:57b06b4b47c6 319 nrf->retransmitData();
defrost 5:57b06b4b47c6 320 }
epgmdm 18:be77ad141fac 321 if ((!sendingFile)&&(checkedIn == 1)) {
epgmdm 12:d9b13f31206d 322 flagGetTime=1;
epgmdm 12:d9b13f31206d 323 flagGetMaxCharge=1;
epgmdm 12:d9b13f31206d 324 }
defrost 5:57b06b4b47c6 325 }
defrost 5:57b06b4b47c6 326
defrost 5:57b06b4b47c6 327 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 328 // Main routines
defrost 5:57b06b4b47c6 329 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 330
defrost 5:57b06b4b47c6 331 /**
defrost 5:57b06b4b47c6 332 * Checks in the Battery to the locker
defrost 5:57b06b4b47c6 333 * Transmits "C ID"
defrost 5:57b06b4b47c6 334 *
defrost 5:57b06b4b47c6 335 */
epgmdm 18:be77ad141fac 336 void Battery::checkIn()
epgmdm 18:be77ad141fac 337 {
defrost 5:57b06b4b47c6 338 sprintf(dataTx, "C %04X", (int) (id&0XFFFF));
defrost 5:57b06b4b47c6 339 DBG("Check into locker [%s]", dataTx);
epgmdm 11:87ab310924f0 340 spiNRF();
defrost 5:57b06b4b47c6 341 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 342 }
defrost 5:57b06b4b47c6 343
epgmdm 18:be77ad141fac 344
epgmdm 18:be77ad141fac 345 /**
epgmdm 18:be77ad141fac 346 * Checks out. Sends O %d, ID to locker
epgmdm 18:be77ad141fac 347 *
epgmdm 18:be77ad141fac 348 */
epgmdm 18:be77ad141fac 349 void Battery::checkOut()
epgmdm 18:be77ad141fac 350 {
epgmdm 18:be77ad141fac 351 sprintf(dataTx, "O %04X", (int) (id&0XFFFF));
epgmdm 18:be77ad141fac 352 DBG("Check out locker [%s]", dataTx);
epgmdm 18:be77ad141fac 353 spiNRF();
epgmdm 18:be77ad141fac 354 nrf->transmitData(dataTx, strlen(dataTx));
epgmdm 18:be77ad141fac 355 // Reset to locker public comms channel
epgmdm 18:be77ad141fac 356 }
epgmdm 18:be77ad141fac 357
epgmdm 18:be77ad141fac 358
defrost 5:57b06b4b47c6 359 /**
defrost 5:57b06b4b47c6 360 * Checks in the Battery to the locker
defrost 5:57b06b4b47c6 361 * Acknowledge received.
defrost 5:57b06b4b47c6 362 * Changes the TX pipe address.
defrost 5:57b06b4b47c6 363 *
defrost 5:57b06b4b47c6 364 */
epgmdm 18:be77ad141fac 365 void Battery::doCheckIn()
epgmdm 18:be77ad141fac 366 {
defrost 5:57b06b4b47c6 367 nrf->setTxAddress(privateAddr);
defrost 5:57b06b4b47c6 368 checkedIn = 1;
defrost 5:57b06b4b47c6 369 DBG("Checked into locker");
defrost 5:57b06b4b47c6 370 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 371 #ifdef DEBUG
defrost 5:57b06b4b47c6 372 nrf->printDetails();
defrost 5:57b06b4b47c6 373 #endif
defrost 5:57b06b4b47c6 374 }
defrost 5:57b06b4b47c6 375 /**
defrost 5:57b06b4b47c6 376 * Checks in the Battery to the locker
defrost 5:57b06b4b47c6 377 * Acknowledge received.
defrost 5:57b06b4b47c6 378 * Changes the TX pipe address.
defrost 5:57b06b4b47c6 379 *
defrost 5:57b06b4b47c6 380 */
epgmdm 18:be77ad141fac 381 void Battery::doCheckOut()
epgmdm 18:be77ad141fac 382 {
defrost 5:57b06b4b47c6 383 nrf->setTxAddress(openAddr);
defrost 5:57b06b4b47c6 384 checkedIn = 0;
defrost 5:57b06b4b47c6 385 DBG("Checked out of locker");
defrost 5:57b06b4b47c6 386 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 387 #ifdef DEBUG
defrost 5:57b06b4b47c6 388 nrf->printDetails();
defrost 5:57b06b4b47c6 389 #endif
defrost 5:57b06b4b47c6 390 }
defrost 5:57b06b4b47c6 391
defrost 5:57b06b4b47c6 392 /**
defrost 5:57b06b4b47c6 393 * Slow routines for Battery
defrost 5:57b06b4b47c6 394 */
epgmdm 18:be77ad141fac 395 void Battery::loop()
epgmdm 18:be77ad141fac 396 {
defrost 28:e85a86fc8b59 397 //INFO("In Battery Loop");
epgmdm 12:d9b13f31206d 398 *ledGreen = !checkedIn;
epgmdm 18:be77ad141fac 399
defrost 5:57b06b4b47c6 400
epgmdm 12:d9b13f31206d 401 if (flagGetTime) {
epgmdm 12:d9b13f31206d 402 flagGetTime=0;
epgmdm 12:d9b13f31206d 403 requestTime();
epgmdm 12:d9b13f31206d 404
epgmdm 12:d9b13f31206d 405 }
epgmdm 12:d9b13f31206d 406 if (flagGetMaxCharge) {
epgmdm 12:d9b13f31206d 407 flagGetMaxCharge=0;
epgmdm 12:d9b13f31206d 408 requestMaxCharge();
defrost 5:57b06b4b47c6 409
epgmdm 12:d9b13f31206d 410 }
epgmdm 12:d9b13f31206d 411 if (flagNextFile) {
epgmdm 12:d9b13f31206d 412 flagNextFile = 0;
defrost 28:e85a86fc8b59 413 sending=1;
defrost 13:de43f28c0365 414 DBG("NextFile called");
epgmdm 12:d9b13f31206d 415 requestTime();
epgmdm 12:d9b13f31206d 416 nextFileToTx();
epgmdm 18:be77ad141fac 417 if (fileLength>0) {
epgmdm 12:d9b13f31206d 418 INFO("Sending file %s.",txFileName)
epgmdm 18:be77ad141fac 419 flagDirFileName=1;
epgmdm 12:d9b13f31206d 420 } else {
epgmdm 12:d9b13f31206d 421 sendingFile=0;
epgmdm 18:be77ad141fac 422 flagStartFileTx=0;
epgmdm 12:d9b13f31206d 423 INFO("No more files to send.")
defrost 25:1979c129acb0 424 startCharge = true;
epgmdm 18:be77ad141fac 425 checkOut();
defrost 28:e85a86fc8b59 426 sending=0;
defrost 5:57b06b4b47c6 427 }
epgmdm 12:d9b13f31206d 428 }
epgmdm 18:be77ad141fac 429 if (flagDirFileName) {
epgmdm 18:be77ad141fac 430 flagDirFileName=0;
epgmdm 18:be77ad141fac 431 sendDirName();
epgmdm 18:be77ad141fac 432 }
epgmdm 12:d9b13f31206d 433 if (flagSendFileName) {
epgmdm 12:d9b13f31206d 434 flagSendFileName = 0;
epgmdm 12:d9b13f31206d 435 sendFileName();
defrost 5:57b06b4b47c6 436
epgmdm 12:d9b13f31206d 437 }
epgmdm 12:d9b13f31206d 438 if (flagSendFileSize) {
epgmdm 12:d9b13f31206d 439 flagSendFileSize = 0;
epgmdm 12:d9b13f31206d 440 sendFileSize();
epgmdm 12:d9b13f31206d 441 }
epgmdm 12:d9b13f31206d 442 if (flagSendFile) {
epgmdm 12:d9b13f31206d 443 flagSendFile = 0;
defrost 28:e85a86fc8b59 444 sending=1;
epgmdm 12:d9b13f31206d 445 sendFile();
defrost 5:57b06b4b47c6 446 }
epgmdm 12:d9b13f31206d 447 if (flagSendFileDone) {
epgmdm 12:d9b13f31206d 448 flagSendFileDone = 0;
epgmdm 12:d9b13f31206d 449 sendFileDone();
epgmdm 12:d9b13f31206d 450 INFO("File sent <%s>",txFileName);
epgmdm 12:d9b13f31206d 451 flag = &flagNextFile;
epgmdm 12:d9b13f31206d 452 delay->detach();
epgmdm 12:d9b13f31206d 453 delay->attach(this, &Battery::setFlag, 2.5);
epgmdm 12:d9b13f31206d 454 }
epgmdm 18:be77ad141fac 455 if (flagCheckOut) {
epgmdm 18:be77ad141fac 456 flagCheckOut=0;
epgmdm 18:be77ad141fac 457 doCheckOut();
epgmdm 18:be77ad141fac 458 }
defrost 28:e85a86fc8b59 459 //INFO("Out Battery Loop");
defrost 28:e85a86fc8b59 460 return;
defrost 5:57b06b4b47c6 461 }
defrost 5:57b06b4b47c6 462
defrost 5:57b06b4b47c6 463 /**
defrost 5:57b06b4b47c6 464 * sends a request for time update.
defrost 5:57b06b4b47c6 465 */
epgmdm 18:be77ad141fac 466 void Battery::requestTime()
epgmdm 18:be77ad141fac 467 {
defrost 28:e85a86fc8b59 468 if(sending==0) {
defrost 28:e85a86fc8b59 469 DBG("getTime");
defrost 28:e85a86fc8b59 470 nrf->transmitData("T ",2);
defrost 28:e85a86fc8b59 471 }
defrost 5:57b06b4b47c6 472 }
epgmdm 12:d9b13f31206d 473
epgmdm 12:d9b13f31206d 474 /**
epgmdm 12:d9b13f31206d 475 * sends a request for time update.
epgmdm 12:d9b13f31206d 476 */
epgmdm 18:be77ad141fac 477 void Battery::requestMaxCharge()
epgmdm 18:be77ad141fac 478 {
defrost 28:e85a86fc8b59 479 if(sending==0) {
defrost 28:e85a86fc8b59 480 DBG("Get Max Charge Rate");
defrost 28:e85a86fc8b59 481 nrf->transmitData("M ",2);
defrost 28:e85a86fc8b59 482 }
epgmdm 12:d9b13f31206d 483 }
defrost 5:57b06b4b47c6 484 /**
defrost 5:57b06b4b47c6 485 * Selects the next file to transmit
defrost 5:57b06b4b47c6 486 * Uses /sd/lastFileTx.txt to store the lst file transmitted
defrost 5:57b06b4b47c6 487 */
epgmdm 18:be77ad141fac 488 void Battery::nextFileToTx()
epgmdm 18:be77ad141fac 489 {
defrost 5:57b06b4b47c6 490
defrost 5:57b06b4b47c6 491 DBG("> nextTxFile");
defrost 5:57b06b4b47c6 492
defrost 5:57b06b4b47c6 493 char lastFileName[64];
defrost 5:57b06b4b47c6 494 char fileName[64];
defrost 5:57b06b4b47c6 495 char fn[40];
defrost 5:57b06b4b47c6 496 unsigned int txLength;
defrost 5:57b06b4b47c6 497
defrost 5:57b06b4b47c6 498 sdBuffPnt=0;// Tells sendFile to read from SD the first time.
defrost 5:57b06b4b47c6 499 fileLength = 0;
defrost 5:57b06b4b47c6 500 strcpy(txFileName, "");
defrost 5:57b06b4b47c6 501 spiSD();
defrost 5:57b06b4b47c6 502 // Read in last file name
defrost 5:57b06b4b47c6 503 FILE *fp = fopen("/sd/lastFileTx.txt", "r");
defrost 5:57b06b4b47c6 504 if (fp != NULL) {
defrost 5:57b06b4b47c6 505 if (fscanf(fp, "%s %u", lastFileName, &txLength) != 2) {
defrost 28:e85a86fc8b59 506 sprintf(lastFileName, "0");
defrost 5:57b06b4b47c6 507 }
defrost 5:57b06b4b47c6 508 fclose(fp);
defrost 5:57b06b4b47c6 509 } else {
defrost 28:e85a86fc8b59 510 sprintf(lastFileName, "0");
defrost 28:e85a86fc8b59 511 txLength = 0;
defrost 5:57b06b4b47c6 512 }
defrost 5:57b06b4b47c6 513 DBG("nextFileToTx>Last Filename=%s len=%u",lastFileName,txLength);
defrost 5:57b06b4b47c6 514 // Open directory and read files
defrost 5:57b06b4b47c6 515 DIR *dp;
defrost 5:57b06b4b47c6 516 struct dirent *dirp;
defrost 5:57b06b4b47c6 517 spiSD();
defrost 5:57b06b4b47c6 518 dp = opendir(logDir);
defrost 5:57b06b4b47c6 519 if (dp == NULL) {
defrost 5:57b06b4b47c6 520 DBG("nextTxFile logdir %s is NULL", logDir);
defrost 5:57b06b4b47c6 521 } else
defrost 5:57b06b4b47c6 522 //read all directory and file names in current directory into filename vector
defrost 5:57b06b4b47c6 523 while ((dirp = readdir(dp)) != NULL) {
defrost 5:57b06b4b47c6 524 DBG("nextFileToTx> WHILE lst [%s] dir [%s] %d",lastFileName, dirp->d_name,strcmp(lastFileName, dirp->d_name));
defrost 5:57b06b4b47c6 525
defrost 5:57b06b4b47c6 526 if (strcmp(lastFileName, dirp->d_name) < 0) {
defrost 5:57b06b4b47c6 527 strcpy(fileName, dirp->d_name);
defrost 5:57b06b4b47c6 528 sprintf(txFileName, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 529 fp = fopen(txFileName, "r");
defrost 5:57b06b4b47c6 530 fseek(fp, 0L, SEEK_END);
defrost 5:57b06b4b47c6 531 fileLength = ftell(fp);
defrost 5:57b06b4b47c6 532 fclose(fp);
defrost 5:57b06b4b47c6 533 DBG("CHOSEN %s old %s ", fileName, lastFileName );
defrost 5:57b06b4b47c6 534 break;
defrost 5:57b06b4b47c6 535 }
defrost 5:57b06b4b47c6 536 if (strcmp(lastFileName, dirp->d_name) == 0) {
defrost 5:57b06b4b47c6 537
defrost 5:57b06b4b47c6 538 strcpy(fileName, dirp->d_name);
defrost 5:57b06b4b47c6 539 sprintf(fn, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 540 fp = fopen(fn, "r");
defrost 5:57b06b4b47c6 541 fseek(fp, 0L, SEEK_END);
defrost 5:57b06b4b47c6 542 fileLength = ftell(fp);
defrost 5:57b06b4b47c6 543 fclose(fp);
defrost 5:57b06b4b47c6 544 if (txLength < fileLength) {
defrost 5:57b06b4b47c6 545 sprintf(txFileName, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 546 DBG("More to send is %s old %u new %u ", fileName, txLength,fileLength );
defrost 5:57b06b4b47c6 547 break;
defrost 5:57b06b4b47c6 548 }
defrost 5:57b06b4b47c6 549 }
defrost 5:57b06b4b47c6 550 // test filename and if greater than last read then choose.
defrost 5:57b06b4b47c6 551 }
defrost 5:57b06b4b47c6 552 closedir(dp);
defrost 5:57b06b4b47c6 553 if (strlen(txFileName) > 0) {
defrost 5:57b06b4b47c6 554 strcpy(txFileName, fileName);
defrost 5:57b06b4b47c6 555 sprintf(fullTxFilePath, "%s/%s", logDir, txFileName);
defrost 5:57b06b4b47c6 556 fileLeft = fileLength;
defrost 5:57b06b4b47c6 557
defrost 5:57b06b4b47c6 558 } else {
defrost 5:57b06b4b47c6 559 fileLength =0;
defrost 5:57b06b4b47c6 560 }
defrost 5:57b06b4b47c6 561 DBG("nextTxFile> Next is [%s] length %d", txFileName, fileLength);
defrost 5:57b06b4b47c6 562
defrost 5:57b06b4b47c6 563 }
epgmdm 18:be77ad141fac 564
defrost 5:57b06b4b47c6 565 /**
defrost 5:57b06b4b47c6 566 * Sends the directory name, allows the otherside to create.
defrost 5:57b06b4b47c6 567 */
defrost 5:57b06b4b47c6 568
epgmdm 18:be77ad141fac 569 void Battery::sendDirName()
epgmdm 18:be77ad141fac 570 {
epgmdm 18:be77ad141fac 571 flagStartFileTx=1;
defrost 5:57b06b4b47c6 572 sprintf(dataTx, "D %s", logDir);
defrost 5:57b06b4b47c6 573
defrost 28:e85a86fc8b59 574 DBG("send Dir Name [%s].", dataTx);
defrost 5:57b06b4b47c6 575
epgmdm 11:87ab310924f0 576 spiNRF();
defrost 5:57b06b4b47c6 577 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 578
defrost 5:57b06b4b47c6 579 }
defrost 5:57b06b4b47c6 580 /**
defrost 5:57b06b4b47c6 581 * Sends the file name, if fileLength >0
defrost 5:57b06b4b47c6 582 */
defrost 5:57b06b4b47c6 583
epgmdm 18:be77ad141fac 584 void Battery::sendFileName()
epgmdm 18:be77ad141fac 585 {
defrost 5:57b06b4b47c6 586 // delay->detach();
defrost 5:57b06b4b47c6 587 if (fileLength < 1) {
defrost 5:57b06b4b47c6 588 return;
defrost 5:57b06b4b47c6 589 }
defrost 5:57b06b4b47c6 590 sprintf(dataTx, "F %20s", txFileName);
defrost 5:57b06b4b47c6 591
defrost 5:57b06b4b47c6 592 DBG("send File Name [%s] \n\r", dataTx);
defrost 5:57b06b4b47c6 593
epgmdm 11:87ab310924f0 594 spiNRF();
defrost 5:57b06b4b47c6 595 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 596 }
defrost 5:57b06b4b47c6 597
defrost 5:57b06b4b47c6 598 /**
defrost 5:57b06b4b47c6 599 * Sends the file length to the locker - this is also the cue that the following bytes are the file.
defrost 5:57b06b4b47c6 600 */
epgmdm 18:be77ad141fac 601 void Battery::sendFileSize()
epgmdm 18:be77ad141fac 602 {
defrost 5:57b06b4b47c6 603 if (fileLength < 1) {
defrost 5:57b06b4b47c6 604 return;
defrost 5:57b06b4b47c6 605 }
epgmdm 12:d9b13f31206d 606 sendingFile=1;
defrost 5:57b06b4b47c6 607 filePointer = 0;
defrost 5:57b06b4b47c6 608 sprintf(dataTx, "S %X", fileLength);
defrost 5:57b06b4b47c6 609
defrost 5:57b06b4b47c6 610
defrost 5:57b06b4b47c6 611 DBG("send File size [%s]", dataTx);
defrost 5:57b06b4b47c6 612
epgmdm 11:87ab310924f0 613 spiNRF();
defrost 5:57b06b4b47c6 614 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 615 }
defrost 5:57b06b4b47c6 616
defrost 5:57b06b4b47c6 617 /**
defrost 5:57b06b4b47c6 618 * Sends the file length to the locker - this is also the cue that the following bytes are the file.
defrost 5:57b06b4b47c6 619 */
epgmdm 18:be77ad141fac 620 void Battery::sendFile()
epgmdm 18:be77ad141fac 621 {
epgmdm 30:47f73b850a5e 622 char * packet;
epgmdm 30:47f73b850a5e 623 unsigned int packetSize=32;
epgmdm 30:47f73b850a5e 624 spiNRF();
defrost 5:57b06b4b47c6 625
epgmdm 30:47f73b850a5e 626 while (packetSize>0) {
epgmdm 30:47f73b850a5e 627 packet = getPacket(&packetSize,fullTxFilePath);
epgmdm 30:47f73b850a5e 628 nrf->transmitData(packet, packetSize);
epgmdm 30:47f73b850a5e 629 };
epgmdm 30:47f73b850a5e 630 resetPackets();
defrost 5:57b06b4b47c6 631
defrost 5:57b06b4b47c6 632 }
defrost 5:57b06b4b47c6 633
defrost 5:57b06b4b47c6 634 /**
defrost 5:57b06b4b47c6 635 * Update the last file sent file
defrost 5:57b06b4b47c6 636 */
epgmdm 18:be77ad141fac 637 void Battery::sendFileDone()
epgmdm 18:be77ad141fac 638 {
defrost 28:e85a86fc8b59 639 DBG("Send File Done. Writing to lastFileTx.txt...");
epgmdm 18:be77ad141fac 640 if (strlen(txFileName)>0) {
defrost 5:57b06b4b47c6 641 spiSD();
defrost 5:57b06b4b47c6 642 FILE *fp = fopen("/sd/lastFileTx.txt", "w");
defrost 5:57b06b4b47c6 643 if (fp != NULL) {
defrost 28:e85a86fc8b59 644 spiSD();
defrost 5:57b06b4b47c6 645 fprintf(fp, "%s %u \n", txFileName, fileLength);
defrost 28:e85a86fc8b59 646 spiSD();
defrost 5:57b06b4b47c6 647 fclose(fp);
defrost 5:57b06b4b47c6 648 DBG("sendFileDone> updated lastFileTx.txt %s %u ", txFileName, fileLength);
epgmdm 18:be77ad141fac 649 } else {
defrost 5:57b06b4b47c6 650 INFO("sendFileDone> Cannot update file lastFiletx.txt");
defrost 5:57b06b4b47c6 651 }
epgmdm 11:87ab310924f0 652 spiNRF();
defrost 5:57b06b4b47c6 653
defrost 5:57b06b4b47c6 654 }
defrost 5:57b06b4b47c6 655
defrost 5:57b06b4b47c6 656 }
defrost 5:57b06b4b47c6 657
defrost 5:57b06b4b47c6 658 /**
defrost 5:57b06b4b47c6 659 * Sets the value pointed to by flag to 1.
defrost 5:57b06b4b47c6 660 */
epgmdm 18:be77ad141fac 661 void Battery::setFlag()
epgmdm 18:be77ad141fac 662 {
defrost 5:57b06b4b47c6 663 if (flag != NULL) {
defrost 5:57b06b4b47c6 664 *flag = 1;
defrost 5:57b06b4b47c6 665 }
defrost 5:57b06b4b47c6 666 flag = NULL;
defrost 5:57b06b4b47c6 667 }
epgmdm 12:d9b13f31206d 668
epgmdm 12:d9b13f31206d 669
epgmdm 12:d9b13f31206d 670 /**
epgmdm 12:d9b13f31206d 671 * Returns the max current that can be drawn from the system, in amps
epgmdm 12:d9b13f31206d 672 */
epgmdm 12:d9b13f31206d 673
epgmdm 18:be77ad141fac 674 float Battery::getMaxCurrent()
epgmdm 18:be77ad141fac 675 {
epgmdm 12:d9b13f31206d 676 return maxChargeRate;
epgmdm 12:d9b13f31206d 677 }
epgmdm 12:d9b13f31206d 678
epgmdm 12:d9b13f31206d 679 /**
epgmdm 12:d9b13f31206d 680 * returns a string of the log directory
epgmdm 12:d9b13f31206d 681 */
epgmdm 18:be77ad141fac 682 char* Battery::getLogDirectory()
epgmdm 18:be77ad141fac 683 {
epgmdm 12:d9b13f31206d 684
epgmdm 12:d9b13f31206d 685 return logDir;
epgmdm 12:d9b13f31206d 686 }
defrost 17:dc19b3b39790 687
defrost 17:dc19b3b39790 688 /**
defrost 17:dc19b3b39790 689 * Returns true if we are authorized to start charging.
defrost 17:dc19b3b39790 690 */
epgmdm 18:be77ad141fac 691 bool Battery::startCharging(void)
epgmdm 18:be77ad141fac 692 {
defrost 17:dc19b3b39790 693 return startCharge;
defrost 17:dc19b3b39790 694 }
defrost 17:dc19b3b39790 695
epgmdm 18:be77ad141fac 696 bool Battery::isCheckedIn(void)
epgmdm 18:be77ad141fac 697 {
epgmdm 18:be77ad141fac 698 if(checkedIn == 1) {
defrost 17:dc19b3b39790 699 return true;
epgmdm 18:be77ad141fac 700 } else {
defrost 17:dc19b3b39790 701 return false;
defrost 17:dc19b3b39790 702 }
defrost 17:dc19b3b39790 703 }
defrost 24:6aba7f16e0c6 704
defrost 24:6aba7f16e0c6 705 void Battery::disableInt(void)
defrost 24:6aba7f16e0c6 706 {
defrost 24:6aba7f16e0c6 707 // Detaches all interrupts:
defrost 24:6aba7f16e0c6 708 txWatch->detach();
defrost 24:6aba7f16e0c6 709 delay->detach();
defrost 25:1979c129acb0 710 nrf->setPwrDown();
defrost 28:e85a86fc8b59 711
defrost 24:6aba7f16e0c6 712 return;
defrost 24:6aba7f16e0c6 713 }
epgmdm 30:47f73b850a5e 714 /**
epgmdm 30:47f73b850a5e 715 * Resets the buffers to start reading the file from the beginning again.
epgmdm 30:47f73b850a5e 716 *
epgmdm 30:47f73b850a5e 717 */
epgmdm 30:47f73b850a5e 718
epgmdm 30:47f73b850a5e 719 char* Battery::resetPackets()
epgmdm 30:47f73b850a5e 720 {
epgmdm 30:47f73b850a5e 721 sdBuffPnt=0;
epgmdm 30:47f73b850a5e 722 fileLeft = fileLength;
epgmdm 30:47f73b850a5e 723 }
epgmdm 30:47f73b850a5e 724 /**
epgmdm 30:47f73b850a5e 725 * Gathers a packet from the file defined by fullTxFilePath
epgmdm 30:47f73b850a5e 726 * @param readOnly if true then it does not move the internal pointers on.
epgmdm 30:47f73b850a5e 727 */
epgmdm 30:47f73b850a5e 728 char* Battery::getPacket(unsigned int *sizePackett,char * fullTxFilePath)
epgmdm 30:47f73b850a5e 729 {
epgmdm 30:47f73b850a5e 730 //DBG("Get packet");
epgmdm 30:47f73b850a5e 731
epgmdm 30:47f73b850a5e 732 // printf("send File [%s] at %d %d\n\r",fullTxFilePath,fileLength, fileLeft );
epgmdm 30:47f73b850a5e 733
epgmdm 30:47f73b850a5e 734 // if (fileLength < 1) {
epgmdm 30:47f73b850a5e 735 // INFO("File Sent");
epgmdm 30:47f73b850a5e 736 // sdBuffPnt=0;
epgmdm 30:47f73b850a5e 737 // return;
epgmdm 30:47f73b850a5e 738 // }
epgmdm 30:47f73b850a5e 739 if (sdBuffPnt>=SDBUFFERSIZE) {
epgmdm 30:47f73b850a5e 740 sdBuffPnt=0;
epgmdm 30:47f73b850a5e 741 }
epgmdm 30:47f73b850a5e 742 if (sdBuffPnt == 0) {
epgmdm 30:47f73b850a5e 743 DBG("File %s",fullTxFilePath);
epgmdm 30:47f73b850a5e 744 spiSD();
epgmdm 30:47f73b850a5e 745 FILE *fp;
epgmdm 30:47f73b850a5e 746 fp= fopen(fullTxFilePath, "rb");
epgmdm 30:47f73b850a5e 747 wait(0.1);
epgmdm 30:47f73b850a5e 748 DBG("Open");
epgmdm 30:47f73b850a5e 749 while (fp == NULL) {
epgmdm 30:47f73b850a5e 750 WARN("sendFile> File %s not found for reading",fullTxFilePath);
epgmdm 30:47f73b850a5e 751 fp= fopen(fullTxFilePath, "rb");
epgmdm 30:47f73b850a5e 752 }
epgmdm 30:47f73b850a5e 753 spiSD();
epgmdm 30:47f73b850a5e 754 //DBG("Seek");
epgmdm 30:47f73b850a5e 755 fseek(fp, fileLength - fileLeft, SEEK_SET);
epgmdm 30:47f73b850a5e 756 //DBG("Read");
epgmdm 30:47f73b850a5e 757 sizeRead = fread(sdBuffer, 1,SDBUFFERSIZE, fp);
epgmdm 30:47f73b850a5e 758 //DBG("Read %d",sizeRead);
epgmdm 30:47f73b850a5e 759 fileLeft -= sizeRead;
epgmdm 30:47f73b850a5e 760
epgmdm 30:47f73b850a5e 761 fclose(fp);
epgmdm 30:47f73b850a5e 762 DBG("File %s %d \n\r",fullTxFilePath,sizeRead);
epgmdm 30:47f73b850a5e 763 //spiNRF();
epgmdm 30:47f73b850a5e 764
epgmdm 30:47f73b850a5e 765 }
epgmdm 30:47f73b850a5e 766
epgmdm 30:47f73b850a5e 767 unsigned int bpNow =sdBuffPnt;
epgmdm 30:47f73b850a5e 768
epgmdm 30:47f73b850a5e 769 unsigned int size=*sizePackett;
epgmdm 30:47f73b850a5e 770 if (sizeRead<size) {
epgmdm 30:47f73b850a5e 771 size=sizeRead;
epgmdm 30:47f73b850a5e 772 }
epgmdm 30:47f73b850a5e 773 if ((fileLeft==0)&&(sizeRead==0)) {
epgmdm 30:47f73b850a5e 774 sdBuffPnt=0;
epgmdm 30:47f73b850a5e 775 //flagSendFileDone=1;
epgmdm 30:47f73b850a5e 776 *sizePackett = 0;
epgmdm 30:47f73b850a5e 777 INFO("Done sending file %s",fullTxFilePath);
epgmdm 30:47f73b850a5e 778
epgmdm 30:47f73b850a5e 779 } else {
epgmdm 30:47f73b850a5e 780
epgmdm 30:47f73b850a5e 781 sizeRead -= size;
epgmdm 30:47f73b850a5e 782 //sendPacket(&(sdBuffer[sdBuffPnt]), size);
epgmdm 30:47f73b850a5e 783 sdBuffPnt+=size;
epgmdm 30:47f73b850a5e 784 //*sizePackett = size;
epgmdm 30:47f73b850a5e 785 DBG("Left to send %d",(fileLeft+sizeRead));
epgmdm 30:47f73b850a5e 786 return &(sdBuffer[bpNow]);
epgmdm 30:47f73b850a5e 787 }
epgmdm 30:47f73b850a5e 788 return NULL;
epgmdm 30:47f73b850a5e 789 }