The field version of the solarnano grid on the ionQubes
Fork of SolarNanoGridv3 by
Utility/Utility.cpp@36:a5620262f296, 2016-09-06 (annotated)
- Committer:
- defrost
- Date:
- Tue Sep 06 06:08:56 2016 +0000
- Revision:
- 36:a5620262f296
- Parent:
- 13:de43f28c0365
Turned off the charge rate update from hub
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 | * Utility extends SolarNanoGrid. |
defrost | 5:57b06b4b47c6 | 5 | * Utility Manages the agri-equipment and the GPRS . |
defrost | 5:57b06b4b47c6 | 6 | * The ID must be FE 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. |
epgmdm | 11:87ab310924f0 | 27 | * @file "Utility.cpp" |
epgmdm | 11:87ab310924f0 | 28 | */ |
epgmdm | 11:87ab310924f0 | 29 | #include "Utility.h" |
epgmdm | 11:87ab310924f0 | 30 | #define FUNCNAME "UTILITY" |
epgmdm | 11:87ab310924f0 | 31 | #include "defs.h" |
epgmdm | 11:87ab310924f0 | 32 | Utility::Utility(FILE* fp,Serial *pc) : |
epgmdm | 11:87ab310924f0 | 33 | SolarNanoGrid(fp,pc) { |
epgmdm | 11:87ab310924f0 | 34 | DBG("Initialize class Utility"); |
epgmdm | 11:87ab310924f0 | 35 | // |
epgmdm | 11:87ab310924f0 | 36 | // Flags |
epgmdm | 11:87ab310924f0 | 37 | // |
epgmdm | 11:87ab310924f0 | 38 | flagNrf=0; |
epgmdm | 11:87ab310924f0 | 39 | flagOneSecond=0; |
epgmdm | 11:87ab310924f0 | 40 | |
epgmdm | 11:87ab310924f0 | 41 | // |
epgmdm | 11:87ab310924f0 | 42 | // Variables |
epgmdm | 11:87ab310924f0 | 43 | // |
epgmdm | 11:87ab310924f0 | 44 | |
epgmdm | 11:87ab310924f0 | 45 | |
epgmdm | 11:87ab310924f0 | 46 | sdBuffer = (char *) calloc(SDBUFFERSIZE,1); |
epgmdm | 11:87ab310924f0 | 47 | timeValue = (char *) calloc(32, 1); |
epgmdm | 11:87ab310924f0 | 48 | doOneSecond(); |
epgmdm | 11:87ab310924f0 | 49 | // |
epgmdm | 11:87ab310924f0 | 50 | // Setup relays |
epgmdm | 11:87ab310924f0 | 51 | // |
epgmdm | 11:87ab310924f0 | 52 | husker = new DigitalOut(PTD1,0); |
epgmdm | 11:87ab310924f0 | 53 | incubator = new DigitalOut(PTD3,0); |
epgmdm | 11:87ab310924f0 | 54 | posho = new DigitalOut(PTD2,0); |
epgmdm | 11:87ab310924f0 | 55 | |
epgmdm | 11:87ab310924f0 | 56 | // |
epgmdm | 11:87ab310924f0 | 57 | // Setup NRF |
epgmdm | 11:87ab310924f0 | 58 | // |
epgmdm | 11:87ab310924f0 | 59 | baseAddr = ((long long) communityID << 16) + (id & 0XFFFF); |
epgmdm | 11:87ab310924f0 | 60 | utilityAddr = baseAddr & 0XFFFFFF0000 | 0xFE00; |
defrost | 13:de43f28c0365 | 61 | DBG(" Channel:%x, Address %llx Util Address %llx", chan, baseAddr, utilityAddr); |
epgmdm | 11:87ab310924f0 | 62 | setAsRX(utilityAddr); |
epgmdm | 11:87ab310924f0 | 63 | |
epgmdm | 11:87ab310924f0 | 64 | // |
epgmdm | 11:87ab310924f0 | 65 | // Interrupts |
epgmdm | 11:87ab310924f0 | 66 | // |
epgmdm | 11:87ab310924f0 | 67 | |
epgmdm | 11:87ab310924f0 | 68 | nrfInt = new InterruptIn(PTC18); |
epgmdm | 11:87ab310924f0 | 69 | nrfInt->fall(this, &Utility::intNrf); // attach nrf interrupt. |
epgmdm | 11:87ab310924f0 | 70 | |
epgmdm | 11:87ab310924f0 | 71 | button = new InterruptIn(SW2); |
epgmdm | 11:87ab310924f0 | 72 | button->fall(this, &Utility::intButton); |
epgmdm | 11:87ab310924f0 | 73 | |
epgmdm | 11:87ab310924f0 | 74 | |
epgmdm | 11:87ab310924f0 | 75 | rxWatch = new Ticker(); |
epgmdm | 11:87ab310924f0 | 76 | rxWatch->attach(this, &Utility::intRxClean, 30.14151); |
epgmdm | 11:87ab310924f0 | 77 | |
epgmdm | 11:87ab310924f0 | 78 | oneSecond = new Ticker(); |
epgmdm | 11:87ab310924f0 | 79 | oneSecond->attach(this, &Utility::intOneSecond, 1.0); |
epgmdm | 11:87ab310924f0 | 80 | |
epgmdm | 11:87ab310924f0 | 81 | } |
epgmdm | 11:87ab310924f0 | 82 | |
epgmdm | 11:87ab310924f0 | 83 | // ------------------------------------------------------------------------ |
epgmdm | 11:87ab310924f0 | 84 | // Interrupt routines |
epgmdm | 11:87ab310924f0 | 85 | // ------------------------------------------------------------------------ |
epgmdm | 11:87ab310924f0 | 86 | /** |
epgmdm | 11:87ab310924f0 | 87 | * When the button is pressed print status |
epgmdm | 11:87ab310924f0 | 88 | */ |
epgmdm | 11:87ab310924f0 | 89 | void Utility::intButton() { |
epgmdm | 11:87ab310924f0 | 90 | DBG("int Button"); |
epgmdm | 11:87ab310924f0 | 91 | intRxClean(); |
epgmdm | 11:87ab310924f0 | 92 | } |
epgmdm | 11:87ab310924f0 | 93 | |
epgmdm | 11:87ab310924f0 | 94 | void Utility::intOneSecond() { |
epgmdm | 11:87ab310924f0 | 95 | flagOneSecond = 1; |
epgmdm | 11:87ab310924f0 | 96 | } |
epgmdm | 11:87ab310924f0 | 97 | |
epgmdm | 11:87ab310924f0 | 98 | void Utility::doOneSecond() { |
epgmdm | 11:87ab310924f0 | 99 | now = time(NULL); |
epgmdm | 11:87ab310924f0 | 100 | sprintf(timeValue, "T %x", now); |
epgmdm | 11:87ab310924f0 | 101 | *ledGreen = !*ledGreen; |
epgmdm | 11:87ab310924f0 | 102 | // DBG("One second: %s", timeValue); |
epgmdm | 11:87ab310924f0 | 103 | } |
epgmdm | 11:87ab310924f0 | 104 | |
epgmdm | 11:87ab310924f0 | 105 | /** |
epgmdm | 11:87ab310924f0 | 106 | * Responds to the received signals from HMI |
epgmdm | 11:87ab310924f0 | 107 | */ |
epgmdm | 11:87ab310924f0 | 108 | void Utility::doNrf() |
epgmdm | 11:87ab310924f0 | 109 | { |
epgmdm | 11:87ab310924f0 | 110 | |
epgmdm | 11:87ab310924f0 | 111 | //DBG("doNrf>>"); |
epgmdm | 11:87ab310924f0 | 112 | // Now check which pipe |
epgmdm | 11:87ab310924f0 | 113 | if (pipe>1) { //A battery |
epgmdm | 11:87ab310924f0 | 114 | switch (dataRx[0]) { |
epgmdm | 11:87ab310924f0 | 115 | case ('D'): { // Directory name. |
epgmdm | 11:87ab310924f0 | 116 | *ce = 0; |
epgmdm | 11:87ab310924f0 | 117 | //sscanf (&dataRx[2],"%s",&(dirNames[p][0])); |
epgmdm | 11:87ab310924f0 | 118 | break; |
epgmdm | 11:87ab310924f0 | 119 | } |
epgmdm | 11:87ab310924f0 | 120 | |
epgmdm | 11:87ab310924f0 | 121 | } |
epgmdm | 11:87ab310924f0 | 122 | |
epgmdm | 11:87ab310924f0 | 123 | } |
epgmdm | 11:87ab310924f0 | 124 | } |
epgmdm | 11:87ab310924f0 | 125 | |
defrost | 13:de43f28c0365 | 126 | void Utility::loop(){ |
defrost | 13:de43f28c0365 | 127 | return; |
defrost | 13:de43f28c0365 | 128 | } |
defrost | 13:de43f28c0365 | 129 | |
epgmdm | 11:87ab310924f0 | 130 | |
epgmdm | 11:87ab310924f0 | 131 | /** |
epgmdm | 11:87ab310924f0 | 132 | * called when the nrf creates an interrupt. |
epgmdm | 11:87ab310924f0 | 133 | * |
defrost | 5:57b06b4b47c6 | 134 | */ |
epgmdm | 11:87ab310924f0 | 135 | void Utility::intNrf() { |
epgmdm | 11:87ab310924f0 | 136 | |
epgmdm | 11:87ab310924f0 | 137 | int bID = 0; |
epgmdm | 11:87ab310924f0 | 138 | int status = 0; |
epgmdm | 11:87ab310924f0 | 139 | // |
epgmdm | 11:87ab310924f0 | 140 | //Get status and pipe |
epgmdm | 11:87ab310924f0 | 141 | // |
epgmdm | 11:87ab310924f0 | 142 | spiNRF(); |
epgmdm | 11:87ab310924f0 | 143 | status = nrf->checkStatus(); |
epgmdm | 11:87ab310924f0 | 144 | pipe = (status >> 1); |
epgmdm | 11:87ab310924f0 | 145 | pipe = (pipe & 0x0007); |
epgmdm | 11:87ab310924f0 | 146 | |
epgmdm | 11:87ab310924f0 | 147 | // |
epgmdm | 11:87ab310924f0 | 148 | // Check if data received |
epgmdm | 11:87ab310924f0 | 149 | // |
epgmdm | 11:87ab310924f0 | 150 | if (((status >> RX_DR) & 01) == 1) { // Received a packet |
epgmdm | 11:87ab310924f0 | 151 | // Get the data |
epgmdm | 11:87ab310924f0 | 152 | width = nrf->getRxData(dataRx); |
epgmdm | 11:87ab310924f0 | 153 | dataRx[width] = '\0'; |
epgmdm | 11:87ab310924f0 | 154 | //Process the acknowledge |
epgmdm | 11:87ab310924f0 | 155 | // if ((pipe>=2)&&(battIn[battPipes[pipe - 2]] == SENDING)) { // A file is being transferred. |
epgmdm | 11:87ab310924f0 | 156 | // nrf->acknowledgeData("S ", 2, pipe); |
epgmdm | 11:87ab310924f0 | 157 | // flagNrf = 1; |
epgmdm | 11:87ab310924f0 | 158 | // } else |
epgmdm | 11:87ab310924f0 | 159 | if (dataRx[0] == 'T') { |
epgmdm | 11:87ab310924f0 | 160 | // Sends the time - this is updated in doOneSecond() |
epgmdm | 11:87ab310924f0 | 161 | nrf->acknowledgeData(timeValue, strlen(timeValue), pipe); |
epgmdm | 11:87ab310924f0 | 162 | } else { |
epgmdm | 11:87ab310924f0 | 163 | nrf->acknowledgeData(dataRx, 2, pipe); |
epgmdm | 11:87ab310924f0 | 164 | DBG("intNrf>%s %x",dataRx,bID); |
epgmdm | 11:87ab310924f0 | 165 | } |
epgmdm | 11:87ab310924f0 | 166 | // Process data |
epgmdm | 11:87ab310924f0 | 167 | if (pipe == 0) { // Utility channel |
epgmdm | 11:87ab310924f0 | 168 | switch(dataRx[0]){ |
epgmdm | 11:87ab310924f0 | 169 | case 'h':{ |
epgmdm | 11:87ab310924f0 | 170 | if (dataRx[1] == 's'){ |
epgmdm | 11:87ab310924f0 | 171 | turnHuskerOn(1); |
epgmdm | 11:87ab310924f0 | 172 | *ledBlue=0; |
epgmdm | 11:87ab310924f0 | 173 | }else if (dataRx[1] == 'x'){ |
epgmdm | 11:87ab310924f0 | 174 | turnHuskerOn(0); |
epgmdm | 11:87ab310924f0 | 175 | *ledBlue=1; |
epgmdm | 11:87ab310924f0 | 176 | } |
epgmdm | 11:87ab310924f0 | 177 | break; |
epgmdm | 11:87ab310924f0 | 178 | } |
epgmdm | 11:87ab310924f0 | 179 | case 'i':{ |
epgmdm | 11:87ab310924f0 | 180 | if (dataRx[1] == 's'){ |
epgmdm | 11:87ab310924f0 | 181 | turnIncubatorOn(1); |
epgmdm | 11:87ab310924f0 | 182 | *ledBlue=0; |
epgmdm | 11:87ab310924f0 | 183 | }else if (dataRx[1] == 'x'){ |
epgmdm | 11:87ab310924f0 | 184 | turnIncubatorOn(0); |
epgmdm | 11:87ab310924f0 | 185 | *ledBlue=1; |
epgmdm | 11:87ab310924f0 | 186 | } |
epgmdm | 11:87ab310924f0 | 187 | break; |
epgmdm | 11:87ab310924f0 | 188 | } |
epgmdm | 11:87ab310924f0 | 189 | case 'p':{ |
epgmdm | 11:87ab310924f0 | 190 | if (dataRx[1] == 's'){ |
epgmdm | 11:87ab310924f0 | 191 | turnPoshoOn(1); |
epgmdm | 11:87ab310924f0 | 192 | *ledBlue=0; |
epgmdm | 11:87ab310924f0 | 193 | }else if (dataRx[1] == 'x'){ |
epgmdm | 11:87ab310924f0 | 194 | turnPoshoOn(0); |
epgmdm | 11:87ab310924f0 | 195 | *ledBlue=1; |
epgmdm | 11:87ab310924f0 | 196 | } |
epgmdm | 11:87ab310924f0 | 197 | break; |
epgmdm | 11:87ab310924f0 | 198 | } |
epgmdm | 11:87ab310924f0 | 199 | } |
epgmdm | 11:87ab310924f0 | 200 | |
epgmdm | 11:87ab310924f0 | 201 | }else if (pipe>1) { |
epgmdm | 11:87ab310924f0 | 202 | flagNrf = 1; |
epgmdm | 11:87ab310924f0 | 203 | } |
epgmdm | 11:87ab310924f0 | 204 | |
epgmdm | 11:87ab310924f0 | 205 | lastRxTme = now; |
epgmdm | 11:87ab310924f0 | 206 | |
epgmdm | 11:87ab310924f0 | 207 | } |
epgmdm | 11:87ab310924f0 | 208 | |
epgmdm | 11:87ab310924f0 | 209 | DBG("intNRF> int rx"); |
epgmdm | 11:87ab310924f0 | 210 | nrf->clearStatus(); |
epgmdm | 11:87ab310924f0 | 211 | *ledBlue = !*ledBlue; |
epgmdm | 11:87ab310924f0 | 212 | |
epgmdm | 11:87ab310924f0 | 213 | } |
epgmdm | 11:87ab310924f0 | 214 | |
epgmdm | 11:87ab310924f0 | 215 | /** |
epgmdm | 11:87ab310924f0 | 216 | * Cleans the receiver |
epgmdm | 11:87ab310924f0 | 217 | */ |
epgmdm | 11:87ab310924f0 | 218 | void Utility::intRxClean() { |
epgmdm | 11:87ab310924f0 | 219 | if (now - lastRxTme>60){ |
epgmdm | 11:87ab310924f0 | 220 | nrfFlush(); |
epgmdm | 11:87ab310924f0 | 221 | wait(0.01); |
epgmdm | 11:87ab310924f0 | 222 | nrfFlush(); |
epgmdm | 11:87ab310924f0 | 223 | DBG("intRxClean < status=%x", nrf->checkStatus()); |
epgmdm | 11:87ab310924f0 | 224 | } |
epgmdm | 11:87ab310924f0 | 225 | } |
epgmdm | 11:87ab310924f0 | 226 | /** |
epgmdm | 11:87ab310924f0 | 227 | * Turns the Husker on |
epgmdm | 11:87ab310924f0 | 228 | */ |
epgmdm | 11:87ab310924f0 | 229 | void Utility::turnHuskerOn(int on){ |
epgmdm | 11:87ab310924f0 | 230 | DBG("Turn husker %d",on); |
epgmdm | 11:87ab310924f0 | 231 | *husker=on; |
epgmdm | 11:87ab310924f0 | 232 | } |
epgmdm | 11:87ab310924f0 | 233 | /** |
epgmdm | 11:87ab310924f0 | 234 | * Turns the Incubator on |
epgmdm | 11:87ab310924f0 | 235 | */ |
epgmdm | 11:87ab310924f0 | 236 | void Utility::turnIncubatorOn(int on){ |
epgmdm | 11:87ab310924f0 | 237 | DBG("Turn incubator %d",on); |
epgmdm | 11:87ab310924f0 | 238 | *incubator = on; |
epgmdm | 11:87ab310924f0 | 239 | } |
epgmdm | 11:87ab310924f0 | 240 | |
epgmdm | 11:87ab310924f0 | 241 | /** |
epgmdm | 11:87ab310924f0 | 242 | * Turns the Posho on |
epgmdm | 11:87ab310924f0 | 243 | */ |
epgmdm | 11:87ab310924f0 | 244 | void Utility::turnPoshoOn(int on){ |
epgmdm | 11:87ab310924f0 | 245 | DBG("Turn posho %d",on); |
epgmdm | 11:87ab310924f0 | 246 | *posho = on; |
epgmdm | 11:87ab310924f0 | 247 | } |