The field version of the solarnano grid on the ionQubes
Fork of SolarNanoGridv3 by
Battery/Battery.cpp@25:1979c129acb0, 2016-06-15 (annotated)
- Committer:
- defrost
- Date:
- Wed Jun 15 01:53:07 2016 +0000
- Revision:
- 25:1979c129acb0
- Parent:
- 24:6aba7f16e0c6
- Child:
- 28:e85a86fc8b59
- Added the power down command for the nrf module in the disableInt() function; - Added another spot where the startCharge flag is set to true; - Some minor formatting
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 | 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; |
epgmdm | 11:87ab310924f0 | 80 | |
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 | 5:57b06b4b47c6 | 178 | // |
defrost | 5:57b06b4b47c6 | 179 | //Get status and pipe |
defrost | 5:57b06b4b47c6 | 180 | // |
epgmdm | 11:87ab310924f0 | 181 | spiNRF(); |
defrost | 5:57b06b4b47c6 | 182 | status = nrf->checkStatus(); |
defrost | 5:57b06b4b47c6 | 183 | pipe = (status >> 1); |
defrost | 5:57b06b4b47c6 | 184 | pipe = (pipe & 0x0007); |
defrost | 5:57b06b4b47c6 | 185 | |
defrost | 5:57b06b4b47c6 | 186 | // |
defrost | 5:57b06b4b47c6 | 187 | // Check if max RT is reached |
defrost | 5:57b06b4b47c6 | 188 | // |
defrost | 5:57b06b4b47c6 | 189 | if (((status >>MAX_RT)& 01) ==1) { |
epgmdm | 18:be77ad141fac | 190 | if (flagStartFileTx==1) { // Locker not ready to reacieve - wait 25s. |
epgmdm | 18:be77ad141fac | 191 | flagHubNotReady = 1; |
epgmdm | 18:be77ad141fac | 192 | nrf->clearStatus(); |
epgmdm | 18:be77ad141fac | 193 | flag = &flagDirFileName; |
epgmdm | 18:be77ad141fac | 194 | delay->detach(); |
epgmdm | 18:be77ad141fac | 195 | delay->attach(this, &Battery::setFlag, 25.0f); |
epgmdm | 18:be77ad141fac | 196 | } else { |
epgmdm | 18:be77ad141fac | 197 | maxRT=true; |
epgmdm | 18:be77ad141fac | 198 | DBG("MAX RT flag is set. PTB20= %d status=%x",(unsigned int) *ce,status); |
epgmdm | 18:be77ad141fac | 199 | *ce = 0; |
epgmdm | 18:be77ad141fac | 200 | DBG("PTB20= %d status=%x",(unsigned int) *ce,status); |
epgmdm | 18:be77ad141fac | 201 | nrf->clearStatus(); |
epgmdm | 18:be77ad141fac | 202 | *ledRed=0; |
epgmdm | 18:be77ad141fac | 203 | } |
defrost | 5:57b06b4b47c6 | 204 | } |
defrost | 5:57b06b4b47c6 | 205 | |
defrost | 5:57b06b4b47c6 | 206 | // |
defrost | 5:57b06b4b47c6 | 207 | // Check if data received |
defrost | 5:57b06b4b47c6 | 208 | // |
defrost | 5:57b06b4b47c6 | 209 | if (((status >>RX_DR)& 01) ==1) { // Received a packet |
defrost | 5:57b06b4b47c6 | 210 | width= nrf->getRxData(dataRx); |
defrost | 5:57b06b4b47c6 | 211 | dataRx[width]='\0'; |
epgmdm | 18:be77ad141fac | 212 | if (dataRx[0]!='S') { |
defrost | 5:57b06b4b47c6 | 213 | DBG("intNrf>D[p=%1d]=<%2s> ",pipe,dataRx); |
defrost | 5:57b06b4b47c6 | 214 | } |
defrost | 5:57b06b4b47c6 | 215 | maxRT=false; |
epgmdm | 18:be77ad141fac | 216 | flagHubNotReady = 0; |
defrost | 5:57b06b4b47c6 | 217 | *ce = 1; |
defrost | 5:57b06b4b47c6 | 218 | nrf->clearStatus(); |
defrost | 5:57b06b4b47c6 | 219 | switch (dataRx[0]) { |
epgmdm | 18:be77ad141fac | 220 | case ('C'): { |
epgmdm | 18:be77ad141fac | 221 | doCheckIn(); |
epgmdm | 18:be77ad141fac | 222 | flagNextFile = 1; |
epgmdm | 18:be77ad141fac | 223 | DBG("Flag Set"); |
epgmdm | 18:be77ad141fac | 224 | break; |
epgmdm | 18:be77ad141fac | 225 | } |
epgmdm | 18:be77ad141fac | 226 | case ('D'): { |
epgmdm | 18:be77ad141fac | 227 | flagStartFileTx=0; // Hub commmunicating. |
epgmdm | 18:be77ad141fac | 228 | flag = &flagSendFileName; |
epgmdm | 18:be77ad141fac | 229 | delay->detach(); |
epgmdm | 18:be77ad141fac | 230 | delay->attach(this, &Battery::setFlag, 1.5); |
epgmdm | 18:be77ad141fac | 231 | break; |
epgmdm | 18:be77ad141fac | 232 | } |
epgmdm | 18:be77ad141fac | 233 | case ('F'): { |
epgmdm | 18:be77ad141fac | 234 | flag = &flagSendFileSize; |
epgmdm | 18:be77ad141fac | 235 | countSD = 0; |
epgmdm | 18:be77ad141fac | 236 | delay->detach(); |
epgmdm | 18:be77ad141fac | 237 | delay->attach(this, &Battery::setFlag, 1.50); |
epgmdm | 18:be77ad141fac | 238 | break; |
epgmdm | 18:be77ad141fac | 239 | } |
epgmdm | 18:be77ad141fac | 240 | case ('O'): { |
epgmdm | 18:be77ad141fac | 241 | doCheckOut(); |
epgmdm | 18:be77ad141fac | 242 | break; |
epgmdm | 18:be77ad141fac | 243 | } |
epgmdm | 18:be77ad141fac | 244 | case ('S'): { |
epgmdm | 18:be77ad141fac | 245 | flag = &flagSendFile; |
epgmdm | 18:be77ad141fac | 246 | delay->detach(); |
epgmdm | 18:be77ad141fac | 247 | // printf("%d\n\r",countSD%16); |
epgmdm | 18:be77ad141fac | 248 | //delay->attach(this, &Battery::setFlag, 0.05); |
defrost | 5:57b06b4b47c6 | 249 | |
epgmdm | 18:be77ad141fac | 250 | if (countSD % (SDBUFFERSIZE/32) == 0) { |
epgmdm | 18:be77ad141fac | 251 | delay->attach(this, &Battery::setFlag, 1.5); // wait to write to SD card |
epgmdm | 18:be77ad141fac | 252 | } else { |
epgmdm | 18:be77ad141fac | 253 | delay->attach_us(this, &Battery::setFlag, 800); |
epgmdm | 18:be77ad141fac | 254 | } |
epgmdm | 18:be77ad141fac | 255 | countSD++; |
epgmdm | 18:be77ad141fac | 256 | break; |
defrost | 5:57b06b4b47c6 | 257 | } |
epgmdm | 18:be77ad141fac | 258 | case ('T'): { |
epgmdm | 18:be77ad141fac | 259 | time_t now; |
epgmdm | 18:be77ad141fac | 260 | sscanf (&dataRx[2],"%x",&now ); |
epgmdm | 18:be77ad141fac | 261 | set_time(now); |
epgmdm | 18:be77ad141fac | 262 | now= time(NULL); |
epgmdm | 18:be77ad141fac | 263 | struct tm * timeInf = localtime(&now); |
defrost | 25:1979c129acb0 | 264 | 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 | 265 | |
epgmdm | 18:be77ad141fac | 266 | break; |
epgmdm | 18:be77ad141fac | 267 | } |
defrost | 5:57b06b4b47c6 | 268 | |
epgmdm | 12:d9b13f31206d | 269 | |
epgmdm | 18:be77ad141fac | 270 | case ('M'): { |
epgmdm | 18:be77ad141fac | 271 | time_t now; |
epgmdm | 18:be77ad141fac | 272 | sscanf (&dataRx[2],"%f",&maxChargeRate ); |
epgmdm | 18:be77ad141fac | 273 | if(maxChargeRate>2.0f) { |
epgmdm | 18:be77ad141fac | 274 | maxChargeRate=2.0f; |
epgmdm | 18:be77ad141fac | 275 | } |
epgmdm | 18:be77ad141fac | 276 | if (maxChargeRate<0.0f) { |
epgmdm | 18:be77ad141fac | 277 | maxChargeRate=0.0f; |
epgmdm | 18:be77ad141fac | 278 | } |
defrost | 25:1979c129acb0 | 279 | DBG ("Max charge %f",maxChargeRate); |
epgmdm | 18:be77ad141fac | 280 | |
epgmdm | 18:be77ad141fac | 281 | break; |
epgmdm | 12:d9b13f31206d | 282 | } |
defrost | 5:57b06b4b47c6 | 283 | } |
epgmdm | 12:d9b13f31206d | 284 | |
defrost | 5:57b06b4b47c6 | 285 | } |
epgmdm | 12:d9b13f31206d | 286 | |
defrost | 5:57b06b4b47c6 | 287 | if (((status >>TX_DS)& 01) ==1) { |
defrost | 5:57b06b4b47c6 | 288 | // maxRT=false; |
defrost | 5:57b06b4b47c6 | 289 | nrf->flushTx(); |
defrost | 5:57b06b4b47c6 | 290 | nrf->flushRx(); |
defrost | 5:57b06b4b47c6 | 291 | //DBG(" TX_DS"); |
defrost | 5:57b06b4b47c6 | 292 | } |
defrost | 5:57b06b4b47c6 | 293 | |
defrost | 5:57b06b4b47c6 | 294 | *ledBlue = !*ledBlue; |
defrost | 5:57b06b4b47c6 | 295 | *ledRed = !maxRT; |
defrost | 5:57b06b4b47c6 | 296 | } |
defrost | 5:57b06b4b47c6 | 297 | |
defrost | 5:57b06b4b47c6 | 298 | /** |
defrost | 5:57b06b4b47c6 | 299 | * Transmits the data and sets up a call back to check data sent 30s later. |
defrost | 5:57b06b4b47c6 | 300 | */ |
defrost | 5:57b06b4b47c6 | 301 | |
defrost | 5:57b06b4b47c6 | 302 | /** |
defrost | 5:57b06b4b47c6 | 303 | * Checks to see if transmit failed and then retransmit |
defrost | 5:57b06b4b47c6 | 304 | * |
defrost | 5:57b06b4b47c6 | 305 | */ |
epgmdm | 18:be77ad141fac | 306 | void Battery::retransmit() |
epgmdm | 18:be77ad141fac | 307 | { |
defrost | 5:57b06b4b47c6 | 308 | |
defrost | 5:57b06b4b47c6 | 309 | if (maxRT) { |
defrost | 5:57b06b4b47c6 | 310 | DBG("Retransmit"); |
defrost | 5:57b06b4b47c6 | 311 | *ledRed=1; |
defrost | 5:57b06b4b47c6 | 312 | *ce = 1; |
defrost | 5:57b06b4b47c6 | 313 | maxRT=false; |
defrost | 5:57b06b4b47c6 | 314 | nrf->clearStatus(); |
defrost | 5:57b06b4b47c6 | 315 | nrf->flushRx(); |
defrost | 5:57b06b4b47c6 | 316 | nrf->retransmitData(); |
defrost | 5:57b06b4b47c6 | 317 | } |
epgmdm | 18:be77ad141fac | 318 | if ((!sendingFile)&&(checkedIn == 1)) { |
epgmdm | 12:d9b13f31206d | 319 | flagGetTime=1; |
epgmdm | 12:d9b13f31206d | 320 | flagGetMaxCharge=1; |
epgmdm | 12:d9b13f31206d | 321 | } |
defrost | 5:57b06b4b47c6 | 322 | } |
defrost | 5:57b06b4b47c6 | 323 | |
defrost | 5:57b06b4b47c6 | 324 | // ------------------------------------------------------------------------ |
defrost | 5:57b06b4b47c6 | 325 | // Main routines |
defrost | 5:57b06b4b47c6 | 326 | // ------------------------------------------------------------------------ |
defrost | 5:57b06b4b47c6 | 327 | |
defrost | 5:57b06b4b47c6 | 328 | /** |
defrost | 5:57b06b4b47c6 | 329 | * Checks in the Battery to the locker |
defrost | 5:57b06b4b47c6 | 330 | * Transmits "C ID" |
defrost | 5:57b06b4b47c6 | 331 | * |
defrost | 5:57b06b4b47c6 | 332 | */ |
epgmdm | 18:be77ad141fac | 333 | void Battery::checkIn() |
epgmdm | 18:be77ad141fac | 334 | { |
defrost | 5:57b06b4b47c6 | 335 | sprintf(dataTx, "C %04X", (int) (id&0XFFFF)); |
defrost | 5:57b06b4b47c6 | 336 | DBG("Check into locker [%s]", dataTx); |
epgmdm | 11:87ab310924f0 | 337 | spiNRF(); |
defrost | 5:57b06b4b47c6 | 338 | nrf->transmitData(dataTx, strlen(dataTx)); |
defrost | 5:57b06b4b47c6 | 339 | } |
defrost | 5:57b06b4b47c6 | 340 | |
epgmdm | 18:be77ad141fac | 341 | |
epgmdm | 18:be77ad141fac | 342 | /** |
epgmdm | 18:be77ad141fac | 343 | * Checks out. Sends O %d, ID to locker |
epgmdm | 18:be77ad141fac | 344 | * |
epgmdm | 18:be77ad141fac | 345 | */ |
epgmdm | 18:be77ad141fac | 346 | void Battery::checkOut() |
epgmdm | 18:be77ad141fac | 347 | { |
epgmdm | 18:be77ad141fac | 348 | sprintf(dataTx, "O %04X", (int) (id&0XFFFF)); |
epgmdm | 18:be77ad141fac | 349 | DBG("Check out locker [%s]", dataTx); |
epgmdm | 18:be77ad141fac | 350 | spiNRF(); |
epgmdm | 18:be77ad141fac | 351 | nrf->transmitData(dataTx, strlen(dataTx)); |
epgmdm | 18:be77ad141fac | 352 | // Reset to locker public comms channel |
epgmdm | 18:be77ad141fac | 353 | } |
epgmdm | 18:be77ad141fac | 354 | |
epgmdm | 18:be77ad141fac | 355 | |
defrost | 5:57b06b4b47c6 | 356 | /** |
defrost | 5:57b06b4b47c6 | 357 | * Checks in the Battery to the locker |
defrost | 5:57b06b4b47c6 | 358 | * Acknowledge received. |
defrost | 5:57b06b4b47c6 | 359 | * Changes the TX pipe address. |
defrost | 5:57b06b4b47c6 | 360 | * |
defrost | 5:57b06b4b47c6 | 361 | */ |
epgmdm | 18:be77ad141fac | 362 | void Battery::doCheckIn() |
epgmdm | 18:be77ad141fac | 363 | { |
defrost | 5:57b06b4b47c6 | 364 | nrf->setTxAddress(privateAddr); |
defrost | 5:57b06b4b47c6 | 365 | checkedIn = 1; |
defrost | 5:57b06b4b47c6 | 366 | DBG("Checked into locker"); |
defrost | 5:57b06b4b47c6 | 367 | DBG("Nrf Details:"); |
defrost | 5:57b06b4b47c6 | 368 | #ifdef DEBUG |
defrost | 5:57b06b4b47c6 | 369 | nrf->printDetails(); |
defrost | 5:57b06b4b47c6 | 370 | #endif |
defrost | 5:57b06b4b47c6 | 371 | } |
defrost | 5:57b06b4b47c6 | 372 | /** |
defrost | 5:57b06b4b47c6 | 373 | * Checks in the Battery to the locker |
defrost | 5:57b06b4b47c6 | 374 | * Acknowledge received. |
defrost | 5:57b06b4b47c6 | 375 | * Changes the TX pipe address. |
defrost | 5:57b06b4b47c6 | 376 | * |
defrost | 5:57b06b4b47c6 | 377 | */ |
epgmdm | 18:be77ad141fac | 378 | void Battery::doCheckOut() |
epgmdm | 18:be77ad141fac | 379 | { |
defrost | 5:57b06b4b47c6 | 380 | nrf->setTxAddress(openAddr); |
defrost | 5:57b06b4b47c6 | 381 | checkedIn = 0; |
defrost | 5:57b06b4b47c6 | 382 | DBG("Checked out of locker"); |
defrost | 5:57b06b4b47c6 | 383 | DBG("Nrf Details:"); |
defrost | 5:57b06b4b47c6 | 384 | #ifdef DEBUG |
defrost | 5:57b06b4b47c6 | 385 | nrf->printDetails(); |
defrost | 5:57b06b4b47c6 | 386 | #endif |
defrost | 5:57b06b4b47c6 | 387 | } |
defrost | 5:57b06b4b47c6 | 388 | |
defrost | 5:57b06b4b47c6 | 389 | /** |
defrost | 5:57b06b4b47c6 | 390 | * Slow routines for Battery |
defrost | 5:57b06b4b47c6 | 391 | */ |
epgmdm | 18:be77ad141fac | 392 | void Battery::loop() |
epgmdm | 18:be77ad141fac | 393 | { |
epgmdm | 12:d9b13f31206d | 394 | |
epgmdm | 12:d9b13f31206d | 395 | *ledGreen = !checkedIn; |
epgmdm | 18:be77ad141fac | 396 | |
defrost | 5:57b06b4b47c6 | 397 | |
epgmdm | 12:d9b13f31206d | 398 | if (flagGetTime) { |
epgmdm | 12:d9b13f31206d | 399 | flagGetTime=0; |
epgmdm | 12:d9b13f31206d | 400 | requestTime(); |
epgmdm | 12:d9b13f31206d | 401 | |
epgmdm | 12:d9b13f31206d | 402 | } |
epgmdm | 12:d9b13f31206d | 403 | if (flagGetMaxCharge) { |
epgmdm | 12:d9b13f31206d | 404 | flagGetMaxCharge=0; |
epgmdm | 12:d9b13f31206d | 405 | requestMaxCharge(); |
defrost | 5:57b06b4b47c6 | 406 | |
epgmdm | 12:d9b13f31206d | 407 | } |
epgmdm | 12:d9b13f31206d | 408 | if (flagNextFile) { |
epgmdm | 12:d9b13f31206d | 409 | flagNextFile = 0; |
defrost | 13:de43f28c0365 | 410 | DBG("NextFile called"); |
epgmdm | 12:d9b13f31206d | 411 | requestTime(); |
epgmdm | 12:d9b13f31206d | 412 | nextFileToTx(); |
epgmdm | 18:be77ad141fac | 413 | if (fileLength>0) { |
epgmdm | 12:d9b13f31206d | 414 | INFO("Sending file %s.",txFileName) |
epgmdm | 18:be77ad141fac | 415 | flagDirFileName=1; |
epgmdm | 12:d9b13f31206d | 416 | } else { |
epgmdm | 12:d9b13f31206d | 417 | sendingFile=0; |
epgmdm | 18:be77ad141fac | 418 | flagStartFileTx=0; |
epgmdm | 12:d9b13f31206d | 419 | INFO("No more files to send.") |
defrost | 25:1979c129acb0 | 420 | startCharge = true; |
epgmdm | 18:be77ad141fac | 421 | checkOut(); |
defrost | 5:57b06b4b47c6 | 422 | } |
epgmdm | 12:d9b13f31206d | 423 | } |
epgmdm | 18:be77ad141fac | 424 | if (flagDirFileName) { |
epgmdm | 18:be77ad141fac | 425 | flagDirFileName=0; |
epgmdm | 18:be77ad141fac | 426 | sendDirName(); |
epgmdm | 18:be77ad141fac | 427 | } |
epgmdm | 12:d9b13f31206d | 428 | if (flagSendFileName) { |
epgmdm | 12:d9b13f31206d | 429 | flagSendFileName = 0; |
epgmdm | 12:d9b13f31206d | 430 | sendFileName(); |
defrost | 5:57b06b4b47c6 | 431 | |
epgmdm | 12:d9b13f31206d | 432 | } |
epgmdm | 12:d9b13f31206d | 433 | if (flagSendFileSize) { |
epgmdm | 12:d9b13f31206d | 434 | flagSendFileSize = 0; |
epgmdm | 12:d9b13f31206d | 435 | sendFileSize(); |
epgmdm | 12:d9b13f31206d | 436 | } |
epgmdm | 12:d9b13f31206d | 437 | if (flagSendFile) { |
epgmdm | 12:d9b13f31206d | 438 | flagSendFile = 0; |
epgmdm | 12:d9b13f31206d | 439 | sendFile(); |
defrost | 5:57b06b4b47c6 | 440 | } |
epgmdm | 12:d9b13f31206d | 441 | if (flagSendFileDone) { |
epgmdm | 12:d9b13f31206d | 442 | flagSendFileDone = 0; |
epgmdm | 12:d9b13f31206d | 443 | sendFileDone(); |
epgmdm | 12:d9b13f31206d | 444 | INFO("File sent <%s>",txFileName); |
epgmdm | 12:d9b13f31206d | 445 | flag = &flagNextFile; |
epgmdm | 12:d9b13f31206d | 446 | delay->detach(); |
epgmdm | 12:d9b13f31206d | 447 | delay->attach(this, &Battery::setFlag, 2.5); |
epgmdm | 12:d9b13f31206d | 448 | } |
epgmdm | 18:be77ad141fac | 449 | if (flagCheckOut) { |
epgmdm | 18:be77ad141fac | 450 | flagCheckOut=0; |
epgmdm | 18:be77ad141fac | 451 | doCheckOut(); |
epgmdm | 18:be77ad141fac | 452 | } |
epgmdm | 12:d9b13f31206d | 453 | |
defrost | 5:57b06b4b47c6 | 454 | } |
defrost | 5:57b06b4b47c6 | 455 | |
defrost | 5:57b06b4b47c6 | 456 | /** |
defrost | 5:57b06b4b47c6 | 457 | * sends a request for time update. |
defrost | 5:57b06b4b47c6 | 458 | */ |
epgmdm | 18:be77ad141fac | 459 | void Battery::requestTime() |
epgmdm | 18:be77ad141fac | 460 | { |
defrost | 5:57b06b4b47c6 | 461 | DBG("getTime"); |
defrost | 5:57b06b4b47c6 | 462 | nrf->transmitData("T ",2); |
defrost | 5:57b06b4b47c6 | 463 | } |
epgmdm | 12:d9b13f31206d | 464 | |
epgmdm | 12:d9b13f31206d | 465 | /** |
epgmdm | 12:d9b13f31206d | 466 | * sends a request for time update. |
epgmdm | 12:d9b13f31206d | 467 | */ |
epgmdm | 18:be77ad141fac | 468 | void Battery::requestMaxCharge() |
epgmdm | 18:be77ad141fac | 469 | { |
defrost | 22:93fde34d9a94 | 470 | DBG("Get Max Charge Rate"); |
epgmdm | 12:d9b13f31206d | 471 | nrf->transmitData("M ",2); |
epgmdm | 12:d9b13f31206d | 472 | } |
defrost | 5:57b06b4b47c6 | 473 | /** |
defrost | 5:57b06b4b47c6 | 474 | * Selects the next file to transmit |
defrost | 5:57b06b4b47c6 | 475 | * Uses /sd/lastFileTx.txt to store the lst file transmitted |
defrost | 5:57b06b4b47c6 | 476 | */ |
epgmdm | 18:be77ad141fac | 477 | void Battery::nextFileToTx() |
epgmdm | 18:be77ad141fac | 478 | { |
defrost | 5:57b06b4b47c6 | 479 | |
defrost | 5:57b06b4b47c6 | 480 | DBG("> nextTxFile"); |
defrost | 5:57b06b4b47c6 | 481 | |
defrost | 5:57b06b4b47c6 | 482 | char lastFileName[64]; |
defrost | 5:57b06b4b47c6 | 483 | char fileName[64]; |
defrost | 5:57b06b4b47c6 | 484 | char fn[40]; |
defrost | 5:57b06b4b47c6 | 485 | unsigned int txLength; |
defrost | 5:57b06b4b47c6 | 486 | |
defrost | 5:57b06b4b47c6 | 487 | sdBuffPnt=0;// Tells sendFile to read from SD the first time. |
defrost | 5:57b06b4b47c6 | 488 | fileLength = 0; |
defrost | 5:57b06b4b47c6 | 489 | strcpy(txFileName, ""); |
defrost | 5:57b06b4b47c6 | 490 | spiSD(); |
defrost | 5:57b06b4b47c6 | 491 | // Read in last file name |
defrost | 5:57b06b4b47c6 | 492 | FILE *fp = fopen("/sd/lastFileTx.txt", "r"); |
defrost | 5:57b06b4b47c6 | 493 | if (fp != NULL) { |
defrost | 5:57b06b4b47c6 | 494 | if (fscanf(fp, "%s %u", lastFileName, &txLength) != 2) { |
defrost | 5:57b06b4b47c6 | 495 | sprintf(lastFileName, "a"); |
defrost | 5:57b06b4b47c6 | 496 | } |
defrost | 5:57b06b4b47c6 | 497 | fclose(fp); |
defrost | 5:57b06b4b47c6 | 498 | } else { |
defrost | 5:57b06b4b47c6 | 499 | sprintf(lastFileName, "a"); |
defrost | 5:57b06b4b47c6 | 500 | } |
defrost | 5:57b06b4b47c6 | 501 | DBG("nextFileToTx>Last Filename=%s len=%u",lastFileName,txLength); |
defrost | 5:57b06b4b47c6 | 502 | // Open directory and read files |
defrost | 5:57b06b4b47c6 | 503 | DIR *dp; |
defrost | 5:57b06b4b47c6 | 504 | struct dirent *dirp; |
defrost | 5:57b06b4b47c6 | 505 | spiSD(); |
defrost | 5:57b06b4b47c6 | 506 | dp = opendir(logDir); |
defrost | 5:57b06b4b47c6 | 507 | if (dp == NULL) { |
defrost | 5:57b06b4b47c6 | 508 | DBG("nextTxFile logdir %s is NULL", logDir); |
defrost | 5:57b06b4b47c6 | 509 | } else |
defrost | 5:57b06b4b47c6 | 510 | //read all directory and file names in current directory into filename vector |
defrost | 5:57b06b4b47c6 | 511 | while ((dirp = readdir(dp)) != NULL) { |
defrost | 5:57b06b4b47c6 | 512 | DBG("nextFileToTx> WHILE lst [%s] dir [%s] %d",lastFileName, dirp->d_name,strcmp(lastFileName, dirp->d_name)); |
defrost | 5:57b06b4b47c6 | 513 | |
defrost | 5:57b06b4b47c6 | 514 | if (strcmp(lastFileName, dirp->d_name) < 0) { |
defrost | 5:57b06b4b47c6 | 515 | strcpy(fileName, dirp->d_name); |
defrost | 5:57b06b4b47c6 | 516 | sprintf(txFileName, "%s/%s", logDir, dirp->d_name); |
defrost | 5:57b06b4b47c6 | 517 | fp = fopen(txFileName, "r"); |
defrost | 5:57b06b4b47c6 | 518 | fseek(fp, 0L, SEEK_END); |
defrost | 5:57b06b4b47c6 | 519 | fileLength = ftell(fp); |
defrost | 5:57b06b4b47c6 | 520 | fclose(fp); |
defrost | 5:57b06b4b47c6 | 521 | DBG("CHOSEN %s old %s ", fileName, lastFileName ); |
defrost | 5:57b06b4b47c6 | 522 | break; |
defrost | 5:57b06b4b47c6 | 523 | } |
defrost | 5:57b06b4b47c6 | 524 | if (strcmp(lastFileName, dirp->d_name) == 0) { |
defrost | 5:57b06b4b47c6 | 525 | |
defrost | 5:57b06b4b47c6 | 526 | strcpy(fileName, dirp->d_name); |
defrost | 5:57b06b4b47c6 | 527 | sprintf(fn, "%s/%s", logDir, dirp->d_name); |
defrost | 5:57b06b4b47c6 | 528 | fp = fopen(fn, "r"); |
defrost | 5:57b06b4b47c6 | 529 | fseek(fp, 0L, SEEK_END); |
defrost | 5:57b06b4b47c6 | 530 | fileLength = ftell(fp); |
defrost | 5:57b06b4b47c6 | 531 | fclose(fp); |
defrost | 5:57b06b4b47c6 | 532 | if (txLength < fileLength) { |
defrost | 5:57b06b4b47c6 | 533 | sprintf(txFileName, "%s/%s", logDir, dirp->d_name); |
defrost | 5:57b06b4b47c6 | 534 | DBG("More to send is %s old %u new %u ", fileName, txLength,fileLength ); |
defrost | 5:57b06b4b47c6 | 535 | break; |
defrost | 5:57b06b4b47c6 | 536 | } |
defrost | 5:57b06b4b47c6 | 537 | } |
defrost | 5:57b06b4b47c6 | 538 | // test filename and if greater than last read then choose. |
defrost | 5:57b06b4b47c6 | 539 | } |
defrost | 5:57b06b4b47c6 | 540 | closedir(dp); |
defrost | 5:57b06b4b47c6 | 541 | if (strlen(txFileName) > 0) { |
defrost | 5:57b06b4b47c6 | 542 | strcpy(txFileName, fileName); |
defrost | 5:57b06b4b47c6 | 543 | sprintf(fullTxFilePath, "%s/%s", logDir, txFileName); |
defrost | 5:57b06b4b47c6 | 544 | fileLeft = fileLength; |
defrost | 5:57b06b4b47c6 | 545 | |
defrost | 5:57b06b4b47c6 | 546 | } else { |
defrost | 5:57b06b4b47c6 | 547 | fileLength =0; |
defrost | 5:57b06b4b47c6 | 548 | } |
defrost | 5:57b06b4b47c6 | 549 | DBG("nextTxFile> Next is [%s] length %d", txFileName, fileLength); |
defrost | 5:57b06b4b47c6 | 550 | |
defrost | 5:57b06b4b47c6 | 551 | } |
epgmdm | 18:be77ad141fac | 552 | |
defrost | 5:57b06b4b47c6 | 553 | /** |
defrost | 5:57b06b4b47c6 | 554 | * Sends the directory name, allows the otherside to create. |
defrost | 5:57b06b4b47c6 | 555 | */ |
defrost | 5:57b06b4b47c6 | 556 | |
epgmdm | 18:be77ad141fac | 557 | void Battery::sendDirName() |
epgmdm | 18:be77ad141fac | 558 | { |
epgmdm | 18:be77ad141fac | 559 | flagStartFileTx=1; |
defrost | 5:57b06b4b47c6 | 560 | sprintf(dataTx, "D %s", logDir); |
defrost | 5:57b06b4b47c6 | 561 | |
defrost | 5:57b06b4b47c6 | 562 | DBG("send Dir Name [%s].\n\r", dataTx); |
defrost | 5:57b06b4b47c6 | 563 | |
epgmdm | 11:87ab310924f0 | 564 | spiNRF(); |
defrost | 5:57b06b4b47c6 | 565 | nrf->transmitData(dataTx, strlen(dataTx)); |
defrost | 5:57b06b4b47c6 | 566 | |
defrost | 5:57b06b4b47c6 | 567 | } |
defrost | 5:57b06b4b47c6 | 568 | /** |
defrost | 5:57b06b4b47c6 | 569 | * Sends the file name, if fileLength >0 |
defrost | 5:57b06b4b47c6 | 570 | */ |
defrost | 5:57b06b4b47c6 | 571 | |
epgmdm | 18:be77ad141fac | 572 | void Battery::sendFileName() |
epgmdm | 18:be77ad141fac | 573 | { |
defrost | 5:57b06b4b47c6 | 574 | // delay->detach(); |
defrost | 5:57b06b4b47c6 | 575 | if (fileLength < 1) { |
defrost | 5:57b06b4b47c6 | 576 | return; |
defrost | 5:57b06b4b47c6 | 577 | } |
defrost | 5:57b06b4b47c6 | 578 | sprintf(dataTx, "F %20s", txFileName); |
defrost | 5:57b06b4b47c6 | 579 | |
defrost | 5:57b06b4b47c6 | 580 | DBG("send File Name [%s] \n\r", dataTx); |
defrost | 5:57b06b4b47c6 | 581 | |
epgmdm | 11:87ab310924f0 | 582 | spiNRF(); |
defrost | 5:57b06b4b47c6 | 583 | nrf->transmitData(dataTx, strlen(dataTx)); |
defrost | 5:57b06b4b47c6 | 584 | } |
defrost | 5:57b06b4b47c6 | 585 | |
defrost | 5:57b06b4b47c6 | 586 | /** |
defrost | 5:57b06b4b47c6 | 587 | * Sends the file length to the locker - this is also the cue that the following bytes are the file. |
defrost | 5:57b06b4b47c6 | 588 | */ |
epgmdm | 18:be77ad141fac | 589 | void Battery::sendFileSize() |
epgmdm | 18:be77ad141fac | 590 | { |
defrost | 5:57b06b4b47c6 | 591 | if (fileLength < 1) { |
defrost | 5:57b06b4b47c6 | 592 | return; |
defrost | 5:57b06b4b47c6 | 593 | } |
epgmdm | 12:d9b13f31206d | 594 | sendingFile=1; |
defrost | 5:57b06b4b47c6 | 595 | filePointer = 0; |
defrost | 5:57b06b4b47c6 | 596 | sprintf(dataTx, "S %X", fileLength); |
defrost | 5:57b06b4b47c6 | 597 | |
defrost | 5:57b06b4b47c6 | 598 | |
defrost | 5:57b06b4b47c6 | 599 | DBG("send File size [%s]", dataTx); |
defrost | 5:57b06b4b47c6 | 600 | |
epgmdm | 11:87ab310924f0 | 601 | spiNRF(); |
defrost | 5:57b06b4b47c6 | 602 | nrf->transmitData(dataTx, strlen(dataTx)); |
defrost | 5:57b06b4b47c6 | 603 | } |
defrost | 5:57b06b4b47c6 | 604 | |
defrost | 5:57b06b4b47c6 | 605 | /** |
defrost | 5:57b06b4b47c6 | 606 | * Sends the file length to the locker - this is also the cue that the following bytes are the file. |
defrost | 5:57b06b4b47c6 | 607 | */ |
epgmdm | 18:be77ad141fac | 608 | void Battery::sendFile() |
epgmdm | 18:be77ad141fac | 609 | { |
defrost | 5:57b06b4b47c6 | 610 | |
defrost | 5:57b06b4b47c6 | 611 | // printf("send File [%s] at %d %d\n\r",fullTxFilePath,fileLength, fileLeft ); |
defrost | 5:57b06b4b47c6 | 612 | |
epgmdm | 11:87ab310924f0 | 613 | // if (fileLength < 1) { |
epgmdm | 11:87ab310924f0 | 614 | // INFO("File Sent"); |
epgmdm | 11:87ab310924f0 | 615 | // sdBuffPnt=0; |
epgmdm | 11:87ab310924f0 | 616 | // return; |
epgmdm | 11:87ab310924f0 | 617 | // } |
epgmdm | 18:be77ad141fac | 618 | if (sdBuffPnt>=SDBUFFERSIZE) { |
defrost | 5:57b06b4b47c6 | 619 | sdBuffPnt=0; |
defrost | 5:57b06b4b47c6 | 620 | } |
defrost | 5:57b06b4b47c6 | 621 | if (sdBuffPnt == 0) { |
defrost | 5:57b06b4b47c6 | 622 | spiSD(); |
defrost | 5:57b06b4b47c6 | 623 | FILE *fp = fopen(fullTxFilePath, "rb"); |
defrost | 5:57b06b4b47c6 | 624 | if (fp == NULL) { |
epgmdm | 11:87ab310924f0 | 625 | spiNRF(); |
defrost | 5:57b06b4b47c6 | 626 | INFO("sendFile> File %s not found for reading",fullTxFilePath); |
defrost | 5:57b06b4b47c6 | 627 | return; |
defrost | 5:57b06b4b47c6 | 628 | } |
defrost | 5:57b06b4b47c6 | 629 | spiSD(); |
defrost | 5:57b06b4b47c6 | 630 | fseek(fp, fileLength - fileLeft, SEEK_SET); |
defrost | 5:57b06b4b47c6 | 631 | sizeRead = fread(sdBuffer, 1,SDBUFFERSIZE, fp); |
defrost | 5:57b06b4b47c6 | 632 | fileLeft -= sizeRead; |
defrost | 5:57b06b4b47c6 | 633 | fclose(fp); |
defrost | 5:57b06b4b47c6 | 634 | // printf("File %s %d \n\r",fullTxFilePath,sizeRead); |
epgmdm | 11:87ab310924f0 | 635 | spiNRF(); |
defrost | 5:57b06b4b47c6 | 636 | |
defrost | 5:57b06b4b47c6 | 637 | } |
defrost | 5:57b06b4b47c6 | 638 | |
defrost | 5:57b06b4b47c6 | 639 | if ((count++) % 256 ==0) { |
defrost | 5:57b06b4b47c6 | 640 | DBG("W %d %d", fileLeft, sizeRead); |
defrost | 5:57b06b4b47c6 | 641 | } |
defrost | 5:57b06b4b47c6 | 642 | int size=32; |
epgmdm | 18:be77ad141fac | 643 | if (sizeRead<32) { |
defrost | 5:57b06b4b47c6 | 644 | size=sizeRead; |
defrost | 5:57b06b4b47c6 | 645 | } |
epgmdm | 18:be77ad141fac | 646 | if ((fileLeft==0)&&(sizeRead==0)) { |
defrost | 5:57b06b4b47c6 | 647 | sdBuffPnt=0; |
defrost | 5:57b06b4b47c6 | 648 | flagSendFileDone=1; |
defrost | 5:57b06b4b47c6 | 649 | DBG("Done sending"); |
defrost | 17:dc19b3b39790 | 650 | startCharge = true; |
epgmdm | 18:be77ad141fac | 651 | } else { |
defrost | 5:57b06b4b47c6 | 652 | sizeRead -= size; |
defrost | 5:57b06b4b47c6 | 653 | nrf->transmitData(&(sdBuffer[sdBuffPnt]), size); |
defrost | 5:57b06b4b47c6 | 654 | sdBuffPnt+=size; |
defrost | 5:57b06b4b47c6 | 655 | } |
defrost | 5:57b06b4b47c6 | 656 | |
defrost | 5:57b06b4b47c6 | 657 | |
defrost | 5:57b06b4b47c6 | 658 | } |
defrost | 5:57b06b4b47c6 | 659 | |
defrost | 5:57b06b4b47c6 | 660 | /** |
defrost | 5:57b06b4b47c6 | 661 | * Update the last file sent file |
defrost | 5:57b06b4b47c6 | 662 | */ |
epgmdm | 18:be77ad141fac | 663 | void Battery::sendFileDone() |
epgmdm | 18:be77ad141fac | 664 | { |
epgmdm | 18:be77ad141fac | 665 | if (strlen(txFileName)>0) { |
defrost | 5:57b06b4b47c6 | 666 | spiSD(); |
defrost | 5:57b06b4b47c6 | 667 | FILE *fp = fopen("/sd/lastFileTx.txt", "w"); |
defrost | 5:57b06b4b47c6 | 668 | if (fp != NULL) { |
defrost | 5:57b06b4b47c6 | 669 | fprintf(fp, "%s %u \n", txFileName, fileLength); |
defrost | 5:57b06b4b47c6 | 670 | fclose(fp); |
defrost | 5:57b06b4b47c6 | 671 | DBG("sendFileDone> updated lastFileTx.txt %s %u ", txFileName, fileLength); |
epgmdm | 18:be77ad141fac | 672 | } else { |
defrost | 5:57b06b4b47c6 | 673 | INFO("sendFileDone> Cannot update file lastFiletx.txt"); |
defrost | 5:57b06b4b47c6 | 674 | } |
epgmdm | 11:87ab310924f0 | 675 | spiNRF(); |
defrost | 5:57b06b4b47c6 | 676 | |
defrost | 5:57b06b4b47c6 | 677 | } |
defrost | 5:57b06b4b47c6 | 678 | |
defrost | 5:57b06b4b47c6 | 679 | } |
defrost | 5:57b06b4b47c6 | 680 | |
defrost | 5:57b06b4b47c6 | 681 | /** |
defrost | 5:57b06b4b47c6 | 682 | * Sets the value pointed to by flag to 1. |
defrost | 5:57b06b4b47c6 | 683 | */ |
epgmdm | 18:be77ad141fac | 684 | void Battery::setFlag() |
epgmdm | 18:be77ad141fac | 685 | { |
defrost | 5:57b06b4b47c6 | 686 | if (flag != NULL) { |
defrost | 5:57b06b4b47c6 | 687 | *flag = 1; |
defrost | 5:57b06b4b47c6 | 688 | } |
defrost | 5:57b06b4b47c6 | 689 | flag = NULL; |
defrost | 5:57b06b4b47c6 | 690 | } |
epgmdm | 12:d9b13f31206d | 691 | |
epgmdm | 12:d9b13f31206d | 692 | |
epgmdm | 12:d9b13f31206d | 693 | /** |
epgmdm | 12:d9b13f31206d | 694 | * Returns the max current that can be drawn from the system, in amps |
epgmdm | 12:d9b13f31206d | 695 | */ |
epgmdm | 12:d9b13f31206d | 696 | |
epgmdm | 18:be77ad141fac | 697 | float Battery::getMaxCurrent() |
epgmdm | 18:be77ad141fac | 698 | { |
epgmdm | 12:d9b13f31206d | 699 | return maxChargeRate; |
epgmdm | 12:d9b13f31206d | 700 | } |
epgmdm | 12:d9b13f31206d | 701 | |
epgmdm | 12:d9b13f31206d | 702 | /** |
epgmdm | 12:d9b13f31206d | 703 | * returns a string of the log directory |
epgmdm | 12:d9b13f31206d | 704 | */ |
epgmdm | 18:be77ad141fac | 705 | char* Battery::getLogDirectory() |
epgmdm | 18:be77ad141fac | 706 | { |
epgmdm | 12:d9b13f31206d | 707 | |
epgmdm | 12:d9b13f31206d | 708 | return logDir; |
epgmdm | 12:d9b13f31206d | 709 | } |
defrost | 17:dc19b3b39790 | 710 | |
defrost | 17:dc19b3b39790 | 711 | /** |
defrost | 17:dc19b3b39790 | 712 | * Returns true if we are authorized to start charging. |
defrost | 17:dc19b3b39790 | 713 | */ |
epgmdm | 18:be77ad141fac | 714 | bool Battery::startCharging(void) |
epgmdm | 18:be77ad141fac | 715 | { |
defrost | 17:dc19b3b39790 | 716 | return startCharge; |
defrost | 17:dc19b3b39790 | 717 | } |
defrost | 17:dc19b3b39790 | 718 | |
epgmdm | 18:be77ad141fac | 719 | bool Battery::isCheckedIn(void) |
epgmdm | 18:be77ad141fac | 720 | { |
epgmdm | 18:be77ad141fac | 721 | if(checkedIn == 1) { |
defrost | 17:dc19b3b39790 | 722 | return true; |
epgmdm | 18:be77ad141fac | 723 | } else { |
defrost | 17:dc19b3b39790 | 724 | return false; |
defrost | 17:dc19b3b39790 | 725 | } |
defrost | 17:dc19b3b39790 | 726 | } |
defrost | 24:6aba7f16e0c6 | 727 | |
defrost | 24:6aba7f16e0c6 | 728 | void Battery::disableInt(void) |
defrost | 24:6aba7f16e0c6 | 729 | { |
defrost | 24:6aba7f16e0c6 | 730 | // Detaches all interrupts: |
defrost | 24:6aba7f16e0c6 | 731 | txWatch->detach(); |
defrost | 24:6aba7f16e0c6 | 732 | delay->detach(); |
defrost | 25:1979c129acb0 | 733 | nrf->setPwrDown(); |
defrost | 24:6aba7f16e0c6 | 734 | |
defrost | 24:6aba7f16e0c6 | 735 | return; |
defrost | 24:6aba7f16e0c6 | 736 | } |