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