The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Committer:
defrost
Date:
Wed Aug 03 12:51:48 2016 +0000
Revision:
35:ede6d8bad011
Parent:
34:e4a42b47c117
Child:
36:a5620262f296
- Changed default charge rate to 1.5A

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
defrost 35:ede6d8bad011 43 maxChargeRate=1.5; // 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);
defrost 32:f586ce639168 451 //flag = &flagNextFile;
defrost 32:f586ce639168 452 //delay->detach();
defrost 32:f586ce639168 453 //delay->attach(this, &Battery::setFlag, 2.5);
defrost 32:f586ce639168 454 flagNextFile = 1;
epgmdm 12:d9b13f31206d 455 }
epgmdm 18:be77ad141fac 456 if (flagCheckOut) {
epgmdm 18:be77ad141fac 457 flagCheckOut=0;
epgmdm 18:be77ad141fac 458 doCheckOut();
epgmdm 18:be77ad141fac 459 }
defrost 28:e85a86fc8b59 460 //INFO("Out Battery Loop");
defrost 28:e85a86fc8b59 461 return;
defrost 5:57b06b4b47c6 462 }
defrost 5:57b06b4b47c6 463
defrost 5:57b06b4b47c6 464 /**
defrost 5:57b06b4b47c6 465 * sends a request for time update.
defrost 5:57b06b4b47c6 466 */
epgmdm 18:be77ad141fac 467 void Battery::requestTime()
epgmdm 18:be77ad141fac 468 {
defrost 28:e85a86fc8b59 469 if(sending==0) {
defrost 28:e85a86fc8b59 470 DBG("getTime");
defrost 28:e85a86fc8b59 471 nrf->transmitData("T ",2);
defrost 28:e85a86fc8b59 472 }
defrost 5:57b06b4b47c6 473 }
epgmdm 12:d9b13f31206d 474
epgmdm 12:d9b13f31206d 475 /**
epgmdm 12:d9b13f31206d 476 * sends a request for time update.
epgmdm 12:d9b13f31206d 477 */
epgmdm 18:be77ad141fac 478 void Battery::requestMaxCharge()
epgmdm 18:be77ad141fac 479 {
defrost 28:e85a86fc8b59 480 if(sending==0) {
defrost 28:e85a86fc8b59 481 DBG("Get Max Charge Rate");
defrost 28:e85a86fc8b59 482 nrf->transmitData("M ",2);
defrost 28:e85a86fc8b59 483 }
epgmdm 12:d9b13f31206d 484 }
defrost 5:57b06b4b47c6 485 /**
defrost 5:57b06b4b47c6 486 * Selects the next file to transmit
defrost 5:57b06b4b47c6 487 * Uses /sd/lastFileTx.txt to store the lst file transmitted
defrost 5:57b06b4b47c6 488 */
epgmdm 18:be77ad141fac 489 void Battery::nextFileToTx()
epgmdm 18:be77ad141fac 490 {
defrost 5:57b06b4b47c6 491
defrost 5:57b06b4b47c6 492 DBG("> nextTxFile");
defrost 5:57b06b4b47c6 493
defrost 5:57b06b4b47c6 494 char lastFileName[64];
defrost 5:57b06b4b47c6 495 char fileName[64];
defrost 5:57b06b4b47c6 496 char fn[40];
defrost 5:57b06b4b47c6 497 unsigned int txLength;
defrost 5:57b06b4b47c6 498
defrost 5:57b06b4b47c6 499 sdBuffPnt=0;// Tells sendFile to read from SD the first time.
defrost 5:57b06b4b47c6 500 fileLength = 0;
defrost 5:57b06b4b47c6 501 strcpy(txFileName, "");
defrost 5:57b06b4b47c6 502 spiSD();
defrost 5:57b06b4b47c6 503 // Read in last file name
defrost 5:57b06b4b47c6 504 FILE *fp = fopen("/sd/lastFileTx.txt", "r");
defrost 5:57b06b4b47c6 505 if (fp != NULL) {
defrost 5:57b06b4b47c6 506 if (fscanf(fp, "%s %u", lastFileName, &txLength) != 2) {
defrost 28:e85a86fc8b59 507 sprintf(lastFileName, "0");
defrost 5:57b06b4b47c6 508 }
defrost 5:57b06b4b47c6 509 fclose(fp);
defrost 5:57b06b4b47c6 510 } else {
defrost 28:e85a86fc8b59 511 sprintf(lastFileName, "0");
defrost 28:e85a86fc8b59 512 txLength = 0;
defrost 5:57b06b4b47c6 513 }
defrost 5:57b06b4b47c6 514 DBG("nextFileToTx>Last Filename=%s len=%u",lastFileName,txLength);
defrost 5:57b06b4b47c6 515 // Open directory and read files
defrost 5:57b06b4b47c6 516 DIR *dp;
defrost 5:57b06b4b47c6 517 struct dirent *dirp;
defrost 5:57b06b4b47c6 518 spiSD();
defrost 5:57b06b4b47c6 519 dp = opendir(logDir);
defrost 5:57b06b4b47c6 520 if (dp == NULL) {
defrost 5:57b06b4b47c6 521 DBG("nextTxFile logdir %s is NULL", logDir);
defrost 5:57b06b4b47c6 522 } else
defrost 5:57b06b4b47c6 523 //read all directory and file names in current directory into filename vector
defrost 5:57b06b4b47c6 524 while ((dirp = readdir(dp)) != NULL) {
defrost 5:57b06b4b47c6 525 DBG("nextFileToTx> WHILE lst [%s] dir [%s] %d",lastFileName, dirp->d_name,strcmp(lastFileName, dirp->d_name));
defrost 5:57b06b4b47c6 526
defrost 5:57b06b4b47c6 527 if (strcmp(lastFileName, dirp->d_name) < 0) {
defrost 5:57b06b4b47c6 528 strcpy(fileName, dirp->d_name);
defrost 5:57b06b4b47c6 529 sprintf(txFileName, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 530 fp = fopen(txFileName, "r");
defrost 5:57b06b4b47c6 531 fseek(fp, 0L, SEEK_END);
defrost 5:57b06b4b47c6 532 fileLength = ftell(fp);
defrost 5:57b06b4b47c6 533 fclose(fp);
defrost 5:57b06b4b47c6 534 DBG("CHOSEN %s old %s ", fileName, lastFileName );
defrost 5:57b06b4b47c6 535 break;
defrost 5:57b06b4b47c6 536 }
defrost 5:57b06b4b47c6 537 if (strcmp(lastFileName, dirp->d_name) == 0) {
defrost 5:57b06b4b47c6 538
defrost 5:57b06b4b47c6 539 strcpy(fileName, dirp->d_name);
defrost 5:57b06b4b47c6 540 sprintf(fn, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 541 fp = fopen(fn, "r");
defrost 5:57b06b4b47c6 542 fseek(fp, 0L, SEEK_END);
defrost 5:57b06b4b47c6 543 fileLength = ftell(fp);
defrost 5:57b06b4b47c6 544 fclose(fp);
defrost 5:57b06b4b47c6 545 if (txLength < fileLength) {
defrost 5:57b06b4b47c6 546 sprintf(txFileName, "%s/%s", logDir, dirp->d_name);
defrost 5:57b06b4b47c6 547 DBG("More to send is %s old %u new %u ", fileName, txLength,fileLength );
defrost 5:57b06b4b47c6 548 break;
defrost 5:57b06b4b47c6 549 }
defrost 5:57b06b4b47c6 550 }
defrost 5:57b06b4b47c6 551 // test filename and if greater than last read then choose.
defrost 5:57b06b4b47c6 552 }
defrost 5:57b06b4b47c6 553 closedir(dp);
defrost 5:57b06b4b47c6 554 if (strlen(txFileName) > 0) {
defrost 5:57b06b4b47c6 555 strcpy(txFileName, fileName);
defrost 5:57b06b4b47c6 556 sprintf(fullTxFilePath, "%s/%s", logDir, txFileName);
defrost 5:57b06b4b47c6 557 fileLeft = fileLength;
defrost 5:57b06b4b47c6 558
defrost 5:57b06b4b47c6 559 } else {
defrost 5:57b06b4b47c6 560 fileLength =0;
defrost 5:57b06b4b47c6 561 }
defrost 5:57b06b4b47c6 562 DBG("nextTxFile> Next is [%s] length %d", txFileName, fileLength);
defrost 5:57b06b4b47c6 563
defrost 5:57b06b4b47c6 564 }
epgmdm 18:be77ad141fac 565
defrost 5:57b06b4b47c6 566 /**
defrost 5:57b06b4b47c6 567 * Sends the directory name, allows the otherside to create.
defrost 5:57b06b4b47c6 568 */
defrost 5:57b06b4b47c6 569
epgmdm 18:be77ad141fac 570 void Battery::sendDirName()
epgmdm 18:be77ad141fac 571 {
epgmdm 18:be77ad141fac 572 flagStartFileTx=1;
defrost 5:57b06b4b47c6 573 sprintf(dataTx, "D %s", logDir);
defrost 5:57b06b4b47c6 574
defrost 28:e85a86fc8b59 575 DBG("send Dir Name [%s].", dataTx);
defrost 5:57b06b4b47c6 576
epgmdm 11:87ab310924f0 577 spiNRF();
defrost 5:57b06b4b47c6 578 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 579
defrost 5:57b06b4b47c6 580 }
defrost 5:57b06b4b47c6 581 /**
defrost 5:57b06b4b47c6 582 * Sends the file name, if fileLength >0
defrost 5:57b06b4b47c6 583 */
defrost 5:57b06b4b47c6 584
epgmdm 18:be77ad141fac 585 void Battery::sendFileName()
epgmdm 18:be77ad141fac 586 {
defrost 5:57b06b4b47c6 587 // delay->detach();
defrost 5:57b06b4b47c6 588 if (fileLength < 1) {
defrost 5:57b06b4b47c6 589 return;
defrost 5:57b06b4b47c6 590 }
defrost 5:57b06b4b47c6 591 sprintf(dataTx, "F %20s", txFileName);
defrost 5:57b06b4b47c6 592
defrost 5:57b06b4b47c6 593 DBG("send File Name [%s] \n\r", dataTx);
defrost 5:57b06b4b47c6 594
epgmdm 11:87ab310924f0 595 spiNRF();
defrost 5:57b06b4b47c6 596 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 597 }
defrost 5:57b06b4b47c6 598
defrost 5:57b06b4b47c6 599 /**
defrost 5:57b06b4b47c6 600 * Sends the file length to the locker - this is also the cue that the following bytes are the file.
defrost 5:57b06b4b47c6 601 */
epgmdm 18:be77ad141fac 602 void Battery::sendFileSize()
epgmdm 18:be77ad141fac 603 {
defrost 5:57b06b4b47c6 604 if (fileLength < 1) {
defrost 5:57b06b4b47c6 605 return;
defrost 5:57b06b4b47c6 606 }
epgmdm 12:d9b13f31206d 607 sendingFile=1;
defrost 5:57b06b4b47c6 608 filePointer = 0;
defrost 5:57b06b4b47c6 609 sprintf(dataTx, "S %X", fileLength);
defrost 5:57b06b4b47c6 610
defrost 5:57b06b4b47c6 611
defrost 5:57b06b4b47c6 612 DBG("send File size [%s]", dataTx);
defrost 5:57b06b4b47c6 613
epgmdm 11:87ab310924f0 614 spiNRF();
defrost 5:57b06b4b47c6 615 nrf->transmitData(dataTx, strlen(dataTx));
defrost 5:57b06b4b47c6 616 }
defrost 5:57b06b4b47c6 617
defrost 5:57b06b4b47c6 618 /**
defrost 5:57b06b4b47c6 619 * Sends the file length to the locker - this is also the cue that the following bytes are the file.
defrost 5:57b06b4b47c6 620 */
epgmdm 18:be77ad141fac 621 void Battery::sendFile()
epgmdm 18:be77ad141fac 622 {
epgmdm 30:47f73b850a5e 623 char * packet;
epgmdm 30:47f73b850a5e 624 unsigned int packetSize=32;
epgmdm 30:47f73b850a5e 625 spiNRF();
defrost 5:57b06b4b47c6 626
epgmdm 30:47f73b850a5e 627 while (packetSize>0) {
epgmdm 30:47f73b850a5e 628 packet = getPacket(&packetSize,fullTxFilePath);
epgmdm 30:47f73b850a5e 629 nrf->transmitData(packet, packetSize);
epgmdm 30:47f73b850a5e 630 };
epgmdm 30:47f73b850a5e 631 resetPackets();
epgmdm 31:3bf76548e024 632 flagSendFileDone=1;
epgmdm 31:3bf76548e024 633 DBG("Done sending");
defrost 5:57b06b4b47c6 634
defrost 5:57b06b4b47c6 635 }
defrost 5:57b06b4b47c6 636
defrost 32:f586ce639168 637
defrost 5:57b06b4b47c6 638 /**
defrost 5:57b06b4b47c6 639 * Update the last file sent file
defrost 5:57b06b4b47c6 640 */
epgmdm 18:be77ad141fac 641 void Battery::sendFileDone()
epgmdm 18:be77ad141fac 642 {
defrost 32:f586ce639168 643 int check;
defrost 32:f586ce639168 644 if (strlen(txFileName)>0) {
defrost 32:f586ce639168 645 spiSD();
defrost 32:f586ce639168 646 FILE *fp = fopen("/sd/lastFileTx.txt", "w");
defrost 32:f586ce639168 647 if (fp != NULL) {
defrost 32:f586ce639168 648 check = fprintf(fp, "%s %u \n", txFileName, fileLength);
defrost 32:f586ce639168 649 fclose(fp);
defrost 32:f586ce639168 650 DBG("sendFileDone> updated lastFileTx.txt %s %u chk = %d", txFileName, fileLength, check);
defrost 32:f586ce639168 651 } else {
defrost 32:f586ce639168 652 INFO("sendFileDone> Cannot update file lastFiletx.txt");
defrost 32:f586ce639168 653 }
defrost 32:f586ce639168 654 spiNRF();
defrost 5:57b06b4b47c6 655
defrost 32:f586ce639168 656 }
defrost 5:57b06b4b47c6 657
defrost 5:57b06b4b47c6 658 }
defrost 5:57b06b4b47c6 659
defrost 5:57b06b4b47c6 660 /**
defrost 5:57b06b4b47c6 661 * Sets the value pointed to by flag to 1.
defrost 5:57b06b4b47c6 662 */
epgmdm 18:be77ad141fac 663 void Battery::setFlag()
epgmdm 18:be77ad141fac 664 {
defrost 5:57b06b4b47c6 665 if (flag != NULL) {
defrost 5:57b06b4b47c6 666 *flag = 1;
defrost 5:57b06b4b47c6 667 }
defrost 5:57b06b4b47c6 668 flag = NULL;
defrost 5:57b06b4b47c6 669 }
epgmdm 12:d9b13f31206d 670
epgmdm 12:d9b13f31206d 671
epgmdm 12:d9b13f31206d 672 /**
epgmdm 12:d9b13f31206d 673 * Returns the max current that can be drawn from the system, in amps
epgmdm 12:d9b13f31206d 674 */
epgmdm 12:d9b13f31206d 675
epgmdm 18:be77ad141fac 676 float Battery::getMaxCurrent()
epgmdm 18:be77ad141fac 677 {
epgmdm 12:d9b13f31206d 678 return maxChargeRate;
epgmdm 12:d9b13f31206d 679 }
epgmdm 12:d9b13f31206d 680
epgmdm 12:d9b13f31206d 681 /**
epgmdm 12:d9b13f31206d 682 * returns a string of the log directory
epgmdm 12:d9b13f31206d 683 */
epgmdm 18:be77ad141fac 684 char* Battery::getLogDirectory()
epgmdm 18:be77ad141fac 685 {
epgmdm 12:d9b13f31206d 686
epgmdm 12:d9b13f31206d 687 return logDir;
epgmdm 12:d9b13f31206d 688 }
defrost 17:dc19b3b39790 689
defrost 17:dc19b3b39790 690 /**
defrost 17:dc19b3b39790 691 * Returns true if we are authorized to start charging.
defrost 17:dc19b3b39790 692 */
epgmdm 18:be77ad141fac 693 bool Battery::startCharging(void)
epgmdm 18:be77ad141fac 694 {
defrost 17:dc19b3b39790 695 return startCharge;
defrost 17:dc19b3b39790 696 }
defrost 17:dc19b3b39790 697
epgmdm 18:be77ad141fac 698 bool Battery::isCheckedIn(void)
epgmdm 18:be77ad141fac 699 {
epgmdm 18:be77ad141fac 700 if(checkedIn == 1) {
defrost 17:dc19b3b39790 701 return true;
epgmdm 18:be77ad141fac 702 } else {
defrost 17:dc19b3b39790 703 return false;
defrost 17:dc19b3b39790 704 }
defrost 17:dc19b3b39790 705 }
defrost 24:6aba7f16e0c6 706
defrost 24:6aba7f16e0c6 707 void Battery::disableInt(void)
defrost 24:6aba7f16e0c6 708 {
defrost 24:6aba7f16e0c6 709 // Detaches all interrupts:
defrost 24:6aba7f16e0c6 710 txWatch->detach();
defrost 24:6aba7f16e0c6 711 delay->detach();
defrost 25:1979c129acb0 712 nrf->setPwrDown();
defrost 28:e85a86fc8b59 713
defrost 24:6aba7f16e0c6 714 return;
defrost 24:6aba7f16e0c6 715 }
epgmdm 30:47f73b850a5e 716 /**
epgmdm 30:47f73b850a5e 717 * Resets the buffers to start reading the file from the beginning again.
epgmdm 30:47f73b850a5e 718 *
epgmdm 30:47f73b850a5e 719 */
epgmdm 30:47f73b850a5e 720
epgmdm 31:3bf76548e024 721 void Battery::resetPackets()
epgmdm 30:47f73b850a5e 722 {
epgmdm 30:47f73b850a5e 723 sdBuffPnt=0;
epgmdm 30:47f73b850a5e 724 fileLeft = fileLength;
epgmdm 30:47f73b850a5e 725 }
epgmdm 30:47f73b850a5e 726 /**
epgmdm 30:47f73b850a5e 727 * Gathers a packet from the file defined by fullTxFilePath
epgmdm 30:47f73b850a5e 728 * @param readOnly if true then it does not move the internal pointers on.
epgmdm 30:47f73b850a5e 729 */
epgmdm 30:47f73b850a5e 730 char* Battery::getPacket(unsigned int *sizePackett,char * fullTxFilePath)
epgmdm 30:47f73b850a5e 731 {
epgmdm 30:47f73b850a5e 732 //DBG("Get packet");
epgmdm 30:47f73b850a5e 733
epgmdm 30:47f73b850a5e 734 // printf("send File [%s] at %d %d\n\r",fullTxFilePath,fileLength, fileLeft );
epgmdm 30:47f73b850a5e 735
epgmdm 30:47f73b850a5e 736 // if (fileLength < 1) {
epgmdm 30:47f73b850a5e 737 // INFO("File Sent");
epgmdm 30:47f73b850a5e 738 // sdBuffPnt=0;
epgmdm 30:47f73b850a5e 739 // return;
epgmdm 30:47f73b850a5e 740 // }
epgmdm 30:47f73b850a5e 741 if (sdBuffPnt>=SDBUFFERSIZE) {
epgmdm 30:47f73b850a5e 742 sdBuffPnt=0;
epgmdm 30:47f73b850a5e 743 }
epgmdm 30:47f73b850a5e 744 if (sdBuffPnt == 0) {
epgmdm 30:47f73b850a5e 745 DBG("File %s",fullTxFilePath);
epgmdm 30:47f73b850a5e 746 spiSD();
epgmdm 30:47f73b850a5e 747 FILE *fp;
epgmdm 30:47f73b850a5e 748 fp= fopen(fullTxFilePath, "rb");
epgmdm 30:47f73b850a5e 749 wait(0.1);
epgmdm 30:47f73b850a5e 750 DBG("Open");
epgmdm 30:47f73b850a5e 751 while (fp == NULL) {
epgmdm 30:47f73b850a5e 752 WARN("sendFile> File %s not found for reading",fullTxFilePath);
epgmdm 30:47f73b850a5e 753 fp= fopen(fullTxFilePath, "rb");
epgmdm 30:47f73b850a5e 754 }
epgmdm 30:47f73b850a5e 755 spiSD();
epgmdm 30:47f73b850a5e 756 //DBG("Seek");
epgmdm 30:47f73b850a5e 757 fseek(fp, fileLength - fileLeft, SEEK_SET);
epgmdm 30:47f73b850a5e 758 //DBG("Read");
epgmdm 30:47f73b850a5e 759 sizeRead = fread(sdBuffer, 1,SDBUFFERSIZE, fp);
epgmdm 30:47f73b850a5e 760 //DBG("Read %d",sizeRead);
epgmdm 30:47f73b850a5e 761 fileLeft -= sizeRead;
epgmdm 30:47f73b850a5e 762
epgmdm 30:47f73b850a5e 763 fclose(fp);
epgmdm 30:47f73b850a5e 764 DBG("File %s %d \n\r",fullTxFilePath,sizeRead);
epgmdm 30:47f73b850a5e 765 //spiNRF();
epgmdm 30:47f73b850a5e 766
epgmdm 30:47f73b850a5e 767 }
epgmdm 30:47f73b850a5e 768
epgmdm 30:47f73b850a5e 769 unsigned int bpNow =sdBuffPnt;
epgmdm 30:47f73b850a5e 770
epgmdm 30:47f73b850a5e 771 unsigned int size=*sizePackett;
epgmdm 30:47f73b850a5e 772 if (sizeRead<size) {
epgmdm 30:47f73b850a5e 773 size=sizeRead;
epgmdm 30:47f73b850a5e 774 }
epgmdm 30:47f73b850a5e 775 if ((fileLeft==0)&&(sizeRead==0)) {
epgmdm 30:47f73b850a5e 776 sdBuffPnt=0;
epgmdm 30:47f73b850a5e 777 //flagSendFileDone=1;
epgmdm 30:47f73b850a5e 778 *sizePackett = 0;
epgmdm 30:47f73b850a5e 779 INFO("Done sending file %s",fullTxFilePath);
epgmdm 30:47f73b850a5e 780
epgmdm 30:47f73b850a5e 781 } else {
epgmdm 30:47f73b850a5e 782
epgmdm 30:47f73b850a5e 783 sizeRead -= size;
epgmdm 30:47f73b850a5e 784 //sendPacket(&(sdBuffer[sdBuffPnt]), size);
epgmdm 30:47f73b850a5e 785 sdBuffPnt+=size;
epgmdm 30:47f73b850a5e 786 //*sizePackett = size;
epgmdm 30:47f73b850a5e 787 DBG("Left to send %d",(fileLeft+sizeRead));
epgmdm 30:47f73b850a5e 788 return &(sdBuffer[bpNow]);
epgmdm 30:47f73b850a5e 789 }
epgmdm 30:47f73b850a5e 790 return NULL;
epgmdm 30:47f73b850a5e 791 }