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