The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Committer:
defrost
Date:
Fri Jun 03 14:55:12 2016 +0000
Revision:
10:30c9e8df0032
Parent:
9:541c2ae1cfec
Child:
11:87ab310924f0
- Warnings all cleared up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
defrost 5:57b06b4b47c6 1 /**
defrost 5:57b06b4b47c6 2 *@section DESCRIPTION
defrost 5:57b06b4b47c6 3 * mbed SolarNanogrid Library
defrost 5:57b06b4b47c6 4 * Locker extends SolarNanoGrid.
defrost 5:57b06b4b47c6 5 * Locker interacts with the batteries.
defrost 5:57b06b4b47c6 6 * The ID must 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 "Locker.c"
defrost 5:57b06b4b47c6 28 */
defrost 5:57b06b4b47c6 29 #include "Locker.h"
defrost 10:30c9e8df0032 30 #define FUNCNAME "LOCKER"
defrost 10:30c9e8df0032 31 #include "defs.h"
defrost 5:57b06b4b47c6 32
defrost 5:57b06b4b47c6 33 Locker::Locker(FILE* fp) :
defrost 5:57b06b4b47c6 34 SolarNanoGrid(fp) {
defrost 5:57b06b4b47c6 35 DBG("Initialize class");
defrost 5:57b06b4b47c6 36 // No other information needed from the config file
defrost 5:57b06b4b47c6 37 fclose(fp);
defrost 5:57b06b4b47c6 38
adrienBiz 9:541c2ae1cfec 39 // ***** variables initialization *****
adrienBiz 9:541c2ae1cfec 40 // Protected interrupts
adrienBiz 9:541c2ae1cfec 41 button=NULL;
adrienBiz 9:541c2ae1cfec 42 rxWatch=NULL;
adrienBiz 9:541c2ae1cfec 43 oneSecond=NULL;
adrienBiz 9:541c2ae1cfec 44
adrienBiz 9:541c2ae1cfec 45 // Protected variables: *
adrienBiz 9:541c2ae1cfec 46 /**
adrienBiz 9:541c2ae1cfec 47 * Open channel address of the locker
adrienBiz 9:541c2ae1cfec 48 */
adrienBiz 9:541c2ae1cfec 49 openAddr=0;
adrienBiz 9:541c2ae1cfec 50
adrienBiz 9:541c2ae1cfec 51 /**
adrienBiz 9:541c2ae1cfec 52 * Channel address for Utility
adrienBiz 9:541c2ae1cfec 53 */
adrienBiz 9:541c2ae1cfec 54 addrUtil=0;
adrienBiz 9:541c2ae1cfec 55 /**
adrienBiz 9:541c2ae1cfec 56 * Array of battery states
adrienBiz 9:541c2ae1cfec 57 */
adrienBiz 9:541c2ae1cfec 58 *battIn=NULL; // array of all battery states
adrienBiz 9:541c2ae1cfec 59 *battPipes=NULL; // array of RX pipes
adrienBiz 9:541c2ae1cfec 60 *temp=NULL;
adrienBiz 9:541c2ae1cfec 61 *sdBuffer=NULL;
adrienBiz 9:541c2ae1cfec 62 sdBuffPnt=0;
adrienBiz 9:541c2ae1cfec 63 battQP=0; // Battery queue pointer
adrienBiz 9:541c2ae1cfec 64 pipeQP=0; // pipe q pointer (Add 2)
adrienBiz 9:541c2ae1cfec 65 pipe = 0;
adrienBiz 9:541c2ae1cfec 66 width = 0;
adrienBiz 9:541c2ae1cfec 67
adrienBiz 9:541c2ae1cfec 68 *timeValue=NULL;
adrienBiz 9:541c2ae1cfec 69
adrienBiz 9:541c2ae1cfec 70 now=0;
adrienBiz 9:541c2ae1cfec 71 lastRxTme=0;
adrienBiz 9:541c2ae1cfec 72
adrienBiz 9:541c2ae1cfec 73 // flags
adrienBiz 9:541c2ae1cfec 74 flagRotate=false;
adrienBiz 9:541c2ae1cfec 75 flagNrf=0;
adrienBiz 9:541c2ae1cfec 76 flagOneSecond=0;
adrienBiz 9:541c2ae1cfec 77
adrienBiz 9:541c2ae1cfec 78 // ***** end of varibale initialization *****
adrienBiz 9:541c2ae1cfec 79
defrost 5:57b06b4b47c6 80 battIn = (char *) calloc(256, 1);
defrost 5:57b06b4b47c6 81 battPipes = (char *) calloc(6, 1);
defrost 5:57b06b4b47c6 82 for (int i = 0; i < 4; i++) {
defrost 5:57b06b4b47c6 83 dirNames[i] = (char *) calloc(32, 1);
defrost 5:57b06b4b47c6 84 fileNames[i] = (char *) calloc(32, 1);
defrost 5:57b06b4b47c6 85 }
defrost 5:57b06b4b47c6 86
defrost 5:57b06b4b47c6 87 temp = (char *) calloc(64, 1);
defrost 5:57b06b4b47c6 88 timeValue = (char *) calloc(32, 1);
defrost 5:57b06b4b47c6 89 sdBuffer = (char *) calloc(SDBUFFERSIZE,1);
defrost 5:57b06b4b47c6 90 doOneSecond();
defrost 5:57b06b4b47c6 91 //
defrost 5:57b06b4b47c6 92 // Setup NRF
defrost 5:57b06b4b47c6 93 //
defrost 5:57b06b4b47c6 94 openAddr = ((long long) communityID << 16) + (id & 0XFFFF);
defrost 5:57b06b4b47c6 95 addrUtil = openAddr & 0XFFFFFF0000 | 0xFE00;
defrost 5:57b06b4b47c6 96
defrost 5:57b06b4b47c6 97 DBG(" Channel:%x, Address %x Util Address %x", chan, openAddr, addrUtil);
defrost 5:57b06b4b47c6 98
adrienBiz 9:541c2ae1cfec 99 setAsRX();
defrost 5:57b06b4b47c6 100 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 101 #ifdef DEBUG
defrost 5:57b06b4b47c6 102 nrf->printDetails();
defrost 5:57b06b4b47c6 103 #endif
defrost 5:57b06b4b47c6 104
defrost 5:57b06b4b47c6 105 //
defrost 5:57b06b4b47c6 106 // Interrupts
defrost 5:57b06b4b47c6 107 //
defrost 5:57b06b4b47c6 108
defrost 5:57b06b4b47c6 109 nrfInt = new InterruptIn(PTC18);
defrost 5:57b06b4b47c6 110 nrfInt->fall(this, &Locker::intNrf); // attach nrf interrupt.
defrost 5:57b06b4b47c6 111
defrost 5:57b06b4b47c6 112 button = new InterruptIn(SW2);
defrost 5:57b06b4b47c6 113 button->fall(this, &Locker::intButton);
defrost 5:57b06b4b47c6 114
defrost 5:57b06b4b47c6 115 rxWatch = new Ticker();
defrost 5:57b06b4b47c6 116 rxWatch->attach(this, &Locker::intRxClean, 30.14151);
defrost 5:57b06b4b47c6 117
defrost 5:57b06b4b47c6 118 oneSecond = new Ticker();
defrost 5:57b06b4b47c6 119 oneSecond->attach(this, &Locker::intOneSecond, 1.0);
defrost 5:57b06b4b47c6 120
defrost 5:57b06b4b47c6 121 loop();
defrost 5:57b06b4b47c6 122 }
defrost 5:57b06b4b47c6 123
defrost 5:57b06b4b47c6 124 // Protected
defrost 5:57b06b4b47c6 125
defrost 5:57b06b4b47c6 126 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 127 // Interrupt routines
defrost 5:57b06b4b47c6 128 // ------------------------------------------------------------------------
defrost 5:57b06b4b47c6 129
defrost 5:57b06b4b47c6 130 void Locker::intOneSecond() {
defrost 5:57b06b4b47c6 131 flagOneSecond = 1;
defrost 5:57b06b4b47c6 132 }
defrost 5:57b06b4b47c6 133
defrost 5:57b06b4b47c6 134 void Locker::doOneSecond() {
defrost 5:57b06b4b47c6 135 now = time(NULL);
defrost 5:57b06b4b47c6 136 sprintf(timeValue, "T %x", now);
defrost 5:57b06b4b47c6 137 *ledGreen = !*ledGreen;
defrost 5:57b06b4b47c6 138
defrost 5:57b06b4b47c6 139 // DBG("One second: %s", timeValue);
defrost 5:57b06b4b47c6 140 }
defrost 5:57b06b4b47c6 141 /**
defrost 5:57b06b4b47c6 142 * called when the nrf creates an interrupt.
defrost 5:57b06b4b47c6 143 *
defrost 5:57b06b4b47c6 144 */
defrost 5:57b06b4b47c6 145 void Locker::intNrf() {
defrost 5:57b06b4b47c6 146
defrost 5:57b06b4b47c6 147 int bID = 0;
defrost 5:57b06b4b47c6 148 int status = 0;
defrost 5:57b06b4b47c6 149 //
defrost 5:57b06b4b47c6 150 //Get status and pipe
defrost 5:57b06b4b47c6 151 //
defrost 5:57b06b4b47c6 152 spiNrf();
defrost 5:57b06b4b47c6 153 status = nrf->checkStatus();
defrost 5:57b06b4b47c6 154 pipe = (status >> 1);
defrost 5:57b06b4b47c6 155 pipe = (pipe & 0x0007);
defrost 5:57b06b4b47c6 156
defrost 5:57b06b4b47c6 157 //
defrost 5:57b06b4b47c6 158 // Check if data received
defrost 5:57b06b4b47c6 159 //
defrost 5:57b06b4b47c6 160 if (((status >> RX_DR) & 01) == 1) { // Received a packet
defrost 5:57b06b4b47c6 161 // Get the data
defrost 5:57b06b4b47c6 162 width = nrf->getRxData(dataRx);
defrost 5:57b06b4b47c6 163 dataRx[width] = '\0';
defrost 5:57b06b4b47c6 164 //Process the acknowledge
defrost 5:57b06b4b47c6 165 if ((pipe>=2)&&(battIn[battPipes[pipe - 2]] == SENDING)) { // A file is being transferred.
defrost 5:57b06b4b47c6 166 nrf->acknowledgeData("S ", 2, pipe);
defrost 5:57b06b4b47c6 167 flagNrf = 1;
defrost 5:57b06b4b47c6 168 } else if (dataRx[0] == 'T') {
defrost 5:57b06b4b47c6 169 // Sends the time - this is updated in doOneSecond()
defrost 5:57b06b4b47c6 170 nrf->acknowledgeData(timeValue, strlen(timeValue), pipe);
defrost 5:57b06b4b47c6 171 } else {
defrost 5:57b06b4b47c6 172 nrf->acknowledgeData(dataRx, 2, pipe);
defrost 5:57b06b4b47c6 173 DBG("intNrf>%s %x",dataRx,bID);
defrost 5:57b06b4b47c6 174 }
defrost 5:57b06b4b47c6 175 if (pipe == 1) { // Open channel
defrost 5:57b06b4b47c6 176 if (dataRx[0] == 'C') { //Request for check in
defrost 5:57b06b4b47c6 177 int battID;
defrost 5:57b06b4b47c6 178 sscanf(&dataRx[2], "%x", &battID);
defrost 5:57b06b4b47c6 179 bID = battID & 0x00ff;
defrost 5:57b06b4b47c6 180 if (battIn[bID] == CHECKED_OUT) {
defrost 5:57b06b4b47c6 181 battIn[bID] = CHECKED_IN;
defrost 5:57b06b4b47c6 182 DBG("intNrf>Check in %d", bID);
defrost 5:57b06b4b47c6 183 } else {
defrost 5:57b06b4b47c6 184 WARN("Battery %04X is already checked in.", battID);
defrost 5:57b06b4b47c6 185 }
defrost 5:57b06b4b47c6 186 flagRotate = 1;
defrost 5:57b06b4b47c6 187 }
defrost 5:57b06b4b47c6 188 }else if (pipe>1) {
defrost 5:57b06b4b47c6 189 flagNrf = 1;
defrost 5:57b06b4b47c6 190 }
defrost 5:57b06b4b47c6 191
defrost 5:57b06b4b47c6 192 lastRxTme = now;
defrost 5:57b06b4b47c6 193 }
defrost 5:57b06b4b47c6 194
defrost 5:57b06b4b47c6 195 // DBG("intNRF");
defrost 5:57b06b4b47c6 196 nrf->clearStatus();
defrost 5:57b06b4b47c6 197 *ledBlue = !*ledBlue;
defrost 5:57b06b4b47c6 198
defrost 5:57b06b4b47c6 199 }
defrost 5:57b06b4b47c6 200
defrost 5:57b06b4b47c6 201 /**
defrost 5:57b06b4b47c6 202 * Responds to the received signals from Master and Batteries
defrost 5:57b06b4b47c6 203 */
defrost 5:57b06b4b47c6 204 void Locker::doNrf()
defrost 5:57b06b4b47c6 205 {
defrost 5:57b06b4b47c6 206 //DBG("doNrf>>");
defrost 5:57b06b4b47c6 207 // Now check which pipe
defrost 5:57b06b4b47c6 208 if (pipe>1) { //A battery
defrost 5:57b06b4b47c6 209 int bID = battPipes[pipe]; // get battery ID from pipe.
defrost 5:57b06b4b47c6 210 int p = pipe-2;
defrost 5:57b06b4b47c6 211 if (battIn[battPipes[p]] == SENDING) {
defrost 5:57b06b4b47c6 212 saveFile(p,dataRx,width);
defrost 5:57b06b4b47c6 213 } else {
defrost 5:57b06b4b47c6 214 switch (dataRx[0]) {
defrost 5:57b06b4b47c6 215 case ('D'): { // Directory name.
defrost 5:57b06b4b47c6 216 *ce = 0;
defrost 5:57b06b4b47c6 217 sscanf (&dataRx[2],"%s",&(dirNames[p][0]));
defrost 5:57b06b4b47c6 218 spiSD();
defrost 5:57b06b4b47c6 219 DBG("doNrf>>>Making dir %s:",dirNames[p]);
defrost 5:57b06b4b47c6 220 int ok = mkdir(dirNames[p],777);
defrost 5:57b06b4b47c6 221 spiNrf();
defrost 5:57b06b4b47c6 222 DBG("doNrf>>>Directory name[%d] = <%s> OK=%d",p,dirNames[p],ok);
defrost 5:57b06b4b47c6 223 *ce = 1;
defrost 5:57b06b4b47c6 224 break;
defrost 5:57b06b4b47c6 225 }
defrost 5:57b06b4b47c6 226 case ('F'): { // File name
defrost 5:57b06b4b47c6 227 *ce = 0;
defrost 5:57b06b4b47c6 228 strncpy(&fileNames[p][0],&dataRx[2],30);
defrost 5:57b06b4b47c6 229 // sscanf (&dataRx[2],"%20s",&fileNames[p][0]);
defrost 5:57b06b4b47c6 230 sprintf(temp,"%s/%s",dirNames[p],fileNames[p]);
defrost 5:57b06b4b47c6 231 DBG("doNrf>>> Creating File name<%s>",temp);
defrost 5:57b06b4b47c6 232 spiSD();
defrost 5:57b06b4b47c6 233 // Make sure file is created and reset
defrost 5:57b06b4b47c6 234 FILE *fp = fopen(temp,"wb");
defrost 5:57b06b4b47c6 235 fclose(fp);
defrost 5:57b06b4b47c6 236 spiNrf();
defrost 5:57b06b4b47c6 237 DBG("doNrf>>>File name[%d] = <%s>",p,fileNames[p]);
defrost 5:57b06b4b47c6 238 *ce = 1;
defrost 5:57b06b4b47c6 239 break;
defrost 5:57b06b4b47c6 240
defrost 5:57b06b4b47c6 241 }
defrost 5:57b06b4b47c6 242 case ('S'): { // File name
defrost 5:57b06b4b47c6 243 battIn[battPipes[p]] = SENDING;
defrost 5:57b06b4b47c6 244 sscanf (&dataRx[2],"%x",&lengthFile[p]);
defrost 5:57b06b4b47c6 245 sdBuffPnt=0; // set buffer to start
defrost 5:57b06b4b47c6 246 DBG("doNrf>>>File Length[%d] = <%u>",p,lengthFile[p]);
defrost 5:57b06b4b47c6 247 break;
defrost 5:57b06b4b47c6 248
defrost 5:57b06b4b47c6 249 }
defrost 5:57b06b4b47c6 250
defrost 5:57b06b4b47c6 251 }
defrost 5:57b06b4b47c6 252 }
defrost 5:57b06b4b47c6 253
defrost 5:57b06b4b47c6 254 }
defrost 5:57b06b4b47c6 255 }
defrost 5:57b06b4b47c6 256
defrost 5:57b06b4b47c6 257 /**
defrost 5:57b06b4b47c6 258 * When the button is pressed print status
defrost 5:57b06b4b47c6 259 */
defrost 5:57b06b4b47c6 260 void Locker::intButton() {
defrost 5:57b06b4b47c6 261 DBG("int Button");
defrost 5:57b06b4b47c6 262 intRxClean();
defrost 5:57b06b4b47c6 263 }
defrost 5:57b06b4b47c6 264
defrost 5:57b06b4b47c6 265 /**
defrost 5:57b06b4b47c6 266 * Cleans the receiver
defrost 5:57b06b4b47c6 267 */
defrost 5:57b06b4b47c6 268 void Locker::intRxClean() {
defrost 5:57b06b4b47c6 269 if (now - lastRxTme>60){
defrost 5:57b06b4b47c6 270 nrfFlush();
defrost 5:57b06b4b47c6 271 wait(0.01);
defrost 5:57b06b4b47c6 272 nrfFlush();
defrost 5:57b06b4b47c6 273 DBG("intRxClean < status=%x", nrf->checkStatus());
defrost 5:57b06b4b47c6 274 }
defrost 5:57b06b4b47c6 275 }
defrost 5:57b06b4b47c6 276
defrost 5:57b06b4b47c6 277
defrost 5:57b06b4b47c6 278 // Loop through slow routines
defrost 5:57b06b4b47c6 279
defrost 5:57b06b4b47c6 280 void Locker::loop(void) {
defrost 5:57b06b4b47c6 281 while (1) {
defrost 5:57b06b4b47c6 282 if (flagRotate == 1) {
defrost 5:57b06b4b47c6 283 flagRotate = 0;
defrost 5:57b06b4b47c6 284 DBG("R");
defrost 5:57b06b4b47c6 285
defrost 5:57b06b4b47c6 286 doRotate();
defrost 5:57b06b4b47c6 287 }
defrost 5:57b06b4b47c6 288 if (flagNrf == 1) {
defrost 5:57b06b4b47c6 289 flagNrf = 0;
defrost 5:57b06b4b47c6 290 doNrf();
defrost 5:57b06b4b47c6 291 }
defrost 5:57b06b4b47c6 292 if (flagOneSecond == 1) {
defrost 5:57b06b4b47c6 293 flagOneSecond = 0;
defrost 5:57b06b4b47c6 294 doOneSecond();
defrost 5:57b06b4b47c6 295 }
defrost 5:57b06b4b47c6 296
defrost 5:57b06b4b47c6 297 __WFI();
defrost 5:57b06b4b47c6 298 };
defrost 5:57b06b4b47c6 299
defrost 5:57b06b4b47c6 300 }
defrost 5:57b06b4b47c6 301 /**
defrost 5:57b06b4b47c6 302 * Rotates one battery the ON_AIR batteries.
defrost 5:57b06b4b47c6 303 */
defrost 5:57b06b4b47c6 304 void Locker::doRotate() {
defrost 5:57b06b4b47c6 305 DBG("Rotate");
defrost 5:57b06b4b47c6 306
defrost 5:57b06b4b47c6 307 // Select battery
defrost 5:57b06b4b47c6 308 for (int b = 0; b < 255; b++, battQP++) {
defrost 5:57b06b4b47c6 309 battQP %= 256;
defrost 5:57b06b4b47c6 310 //INFO(" Bat %d stat %d",battQP,battIn[battQP]);
defrost 5:57b06b4b47c6 311 if (battIn[battQP] == CHECKED_IN) {
defrost 5:57b06b4b47c6 312 pipeQP = (pipeQP) % 4;
defrost 5:57b06b4b47c6 313 for (int p = 0; p < 4; p++) {
defrost 5:57b06b4b47c6 314 // Select pipe
defrost 5:57b06b4b47c6 315
defrost 5:57b06b4b47c6 316 if ((battPipes[pipeQP] == 0)
defrost 5:57b06b4b47c6 317 && (battIn[battPipes[pipeQP]] != SENDING)) {
defrost 5:57b06b4b47c6 318 // Remove existing battery from pipes and place back in queue
defrost 5:57b06b4b47c6 319 if (battIn[battPipes[pipeQP]] == ON_AIR) {
defrost 5:57b06b4b47c6 320 battIn[battPipes[pipeQP]] = CHECKED_IN;
defrost 5:57b06b4b47c6 321 }
defrost 5:57b06b4b47c6 322 battPipes[pipeQP] = battQP; // New battery in
defrost 5:57b06b4b47c6 323 battIn[battQP] = ON_AIR;
defrost 5:57b06b4b47c6 324 // int address = (addrBattery & 0XFFFFFF00)|battQP;
defrost 5:57b06b4b47c6 325 long long address = openAddr | battQP;
defrost 5:57b06b4b47c6 326 nrf->setRxAddress(address, pipeQP + 2);
defrost 5:57b06b4b47c6 327 nrf->setPwrDown();
defrost 5:57b06b4b47c6 328 nrf->setPwrUp();
defrost 5:57b06b4b47c6 329 DBG("=====> Rotate battQ %d pipeQ %d Addr %X", battQP,
defrost 5:57b06b4b47c6 330 pipeQP, address);
defrost 5:57b06b4b47c6 331 DBG("Nrf Details:");
defrost 5:57b06b4b47c6 332 #ifdef DEBUG
defrost 5:57b06b4b47c6 333 nrf->printDetails();
defrost 5:57b06b4b47c6 334 #endif
defrost 5:57b06b4b47c6 335 break;
defrost 5:57b06b4b47c6 336 };
defrost 5:57b06b4b47c6 337 pipeQP = (pipeQP + 1) % 4;
defrost 5:57b06b4b47c6 338 }
defrost 5:57b06b4b47c6 339 break;
defrost 5:57b06b4b47c6 340 }
defrost 5:57b06b4b47c6 341
defrost 5:57b06b4b47c6 342 }
defrost 5:57b06b4b47c6 343
defrost 5:57b06b4b47c6 344 }
defrost 5:57b06b4b47c6 345 /**
defrost 5:57b06b4b47c6 346 * Take the info and saves to a file via a buffer.
defrost 5:57b06b4b47c6 347 **/
defrost 5:57b06b4b47c6 348 void Locker::saveFile (int p,char *data, int width)
defrost 5:57b06b4b47c6 349 {
defrost 5:57b06b4b47c6 350 *ledRed=!*ledRed;
defrost 5:57b06b4b47c6 351 memcpy (&sdBuffer[sdBuffPnt],data,width);
defrost 5:57b06b4b47c6 352 sdBuffPnt += width;
defrost 5:57b06b4b47c6 353 lengthFile[p] -= width;
defrost 5:57b06b4b47c6 354
defrost 5:57b06b4b47c6 355 //INFO("%d",lengthFile[p]);
defrost 5:57b06b4b47c6 356 if ( lengthFile[p] <=0) {
defrost 5:57b06b4b47c6 357 flushSDBuffer(p);
defrost 5:57b06b4b47c6 358 sdBuffPnt=0; // reset for next one
defrost 5:57b06b4b47c6 359 battIn[battPipes[p]] = ON_AIR;
defrost 5:57b06b4b47c6 360 INFO("saveFile> File saved: %s/%s ",dirNames[p], fileNames[p]);
defrost 5:57b06b4b47c6 361 *ledRed=1;
defrost 5:57b06b4b47c6 362 return;
defrost 5:57b06b4b47c6 363 }
defrost 5:57b06b4b47c6 364
defrost 5:57b06b4b47c6 365
defrost 5:57b06b4b47c6 366 if (sdBuffPnt >= SDBUFFERSIZE) {
defrost 5:57b06b4b47c6 367 DBG("Left %u",lengthFile[p]);
defrost 5:57b06b4b47c6 368 flushSDBuffer(p);
defrost 5:57b06b4b47c6 369 sdBuffPnt=0;
defrost 5:57b06b4b47c6 370
defrost 5:57b06b4b47c6 371 }
defrost 5:57b06b4b47c6 372
defrost 5:57b06b4b47c6 373 }
defrost 5:57b06b4b47c6 374 /**
defrost 5:57b06b4b47c6 375 * writes the sdbuffer to file
defrost 5:57b06b4b47c6 376 */
defrost 5:57b06b4b47c6 377 void Locker::flushSDBuffer(int p)
defrost 5:57b06b4b47c6 378 {
defrost 5:57b06b4b47c6 379 //INFO("z");
defrost 5:57b06b4b47c6 380 Timer t;
defrost 5:57b06b4b47c6 381 t.start();
defrost 5:57b06b4b47c6 382 strcpy(temp,dirNames[p]);
defrost 5:57b06b4b47c6 383 strcat(temp,"/");
defrost 5:57b06b4b47c6 384 strcat(temp,fileNames[p]);
defrost 5:57b06b4b47c6 385 //DBG("SD W %s",temp);
defrost 5:57b06b4b47c6 386 spiSD();
defrost 5:57b06b4b47c6 387 FILE *fp = fopen(temp,"ab");
defrost 5:57b06b4b47c6 388 if (fp!=NULL) {
defrost 5:57b06b4b47c6 389 fwrite(sdBuffer,1,sdBuffPnt,fp);
defrost 5:57b06b4b47c6 390 sdBuffPnt=0;
defrost 5:57b06b4b47c6 391 fclose(fp);
defrost 5:57b06b4b47c6 392 } else {
defrost 5:57b06b4b47c6 393 ;
defrost 5:57b06b4b47c6 394 }
defrost 5:57b06b4b47c6 395 spiNrf();
defrost 5:57b06b4b47c6 396 t.stop();
defrost 5:57b06b4b47c6 397 DBG("flush> Timer = %d ms %d us",t.read_ms(), t.read_us())
defrost 5:57b06b4b47c6 398 }
defrost 5:57b06b4b47c6 399
defrost 5:57b06b4b47c6 400 /**
defrost 5:57b06b4b47c6 401 * Set NRF as RX
defrost 5:57b06b4b47c6 402 */
defrost 5:57b06b4b47c6 403 void Locker::setAsRX(){
defrost 5:57b06b4b47c6 404
defrost 5:57b06b4b47c6 405 spiNrf();
defrost 5:57b06b4b47c6 406 nrf->quickRxSetup(chan, addrUtil); // Pipe 0
defrost 5:57b06b4b47c6 407 nrf->setRxAddress(openAddr, 1); //Default battery pipe address 1
defrost 5:57b06b4b47c6 408 nrf->setRadio(0x01, 0x03); // 2MB/S 0dB
defrost 5:57b06b4b47c6 409 nrfFlush();
defrost 5:57b06b4b47c6 410 }
defrost 5:57b06b4b47c6 411
defrost 5:57b06b4b47c6 412 /**
defrost 5:57b06b4b47c6 413 * Set NRF as TX
defrost 5:57b06b4b47c6 414 */
defrost 5:57b06b4b47c6 415 void Locker::setAsTX(){
defrost 5:57b06b4b47c6 416 spiNrf();
defrost 5:57b06b4b47c6 417
defrost 5:57b06b4b47c6 418 }