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