Damien Frost / SolarNanoGrid_Field

Fork of SolarNanoGridv3 by SONG Project

Committer:
epgmdm
Date:
Wed Jun 08 22:12:52 2016 +0000
Revision:
11:87ab310924f0
Parent:
Utility.cpp@5:57b06b4b47c6
Child:
13:de43f28c0365
Utility 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 * 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;
epgmdm 11:87ab310924f0 61 DBG(" Channel:%x, Address %x Util Address %x", 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
epgmdm 11:87ab310924f0 126
epgmdm 11:87ab310924f0 127 /**
epgmdm 11:87ab310924f0 128 * called when the nrf creates an interrupt.
epgmdm 11:87ab310924f0 129 *
defrost 5:57b06b4b47c6 130 */
epgmdm 11:87ab310924f0 131 void Utility::intNrf() {
epgmdm 11:87ab310924f0 132
epgmdm 11:87ab310924f0 133 int bID = 0;
epgmdm 11:87ab310924f0 134 int status = 0;
epgmdm 11:87ab310924f0 135 //
epgmdm 11:87ab310924f0 136 //Get status and pipe
epgmdm 11:87ab310924f0 137 //
epgmdm 11:87ab310924f0 138 spiNRF();
epgmdm 11:87ab310924f0 139 status = nrf->checkStatus();
epgmdm 11:87ab310924f0 140 pipe = (status >> 1);
epgmdm 11:87ab310924f0 141 pipe = (pipe & 0x0007);
epgmdm 11:87ab310924f0 142
epgmdm 11:87ab310924f0 143 //
epgmdm 11:87ab310924f0 144 // Check if data received
epgmdm 11:87ab310924f0 145 //
epgmdm 11:87ab310924f0 146 if (((status >> RX_DR) & 01) == 1) { // Received a packet
epgmdm 11:87ab310924f0 147 // Get the data
epgmdm 11:87ab310924f0 148 width = nrf->getRxData(dataRx);
epgmdm 11:87ab310924f0 149 dataRx[width] = '\0';
epgmdm 11:87ab310924f0 150 //Process the acknowledge
epgmdm 11:87ab310924f0 151 // if ((pipe>=2)&&(battIn[battPipes[pipe - 2]] == SENDING)) { // A file is being transferred.
epgmdm 11:87ab310924f0 152 // nrf->acknowledgeData("S ", 2, pipe);
epgmdm 11:87ab310924f0 153 // flagNrf = 1;
epgmdm 11:87ab310924f0 154 // } else
epgmdm 11:87ab310924f0 155 if (dataRx[0] == 'T') {
epgmdm 11:87ab310924f0 156 // Sends the time - this is updated in doOneSecond()
epgmdm 11:87ab310924f0 157 nrf->acknowledgeData(timeValue, strlen(timeValue), pipe);
epgmdm 11:87ab310924f0 158 } else {
epgmdm 11:87ab310924f0 159 nrf->acknowledgeData(dataRx, 2, pipe);
epgmdm 11:87ab310924f0 160 DBG("intNrf>%s %x",dataRx,bID);
epgmdm 11:87ab310924f0 161 }
epgmdm 11:87ab310924f0 162 // Process data
epgmdm 11:87ab310924f0 163 if (pipe == 0) { // Utility channel
epgmdm 11:87ab310924f0 164 switch(dataRx[0]){
epgmdm 11:87ab310924f0 165 case 'h':{
epgmdm 11:87ab310924f0 166 if (dataRx[1] == 's'){
epgmdm 11:87ab310924f0 167 turnHuskerOn(1);
epgmdm 11:87ab310924f0 168 *ledBlue=0;
epgmdm 11:87ab310924f0 169 }else if (dataRx[1] == 'x'){
epgmdm 11:87ab310924f0 170 turnHuskerOn(0);
epgmdm 11:87ab310924f0 171 *ledBlue=1;
epgmdm 11:87ab310924f0 172 }
epgmdm 11:87ab310924f0 173 break;
epgmdm 11:87ab310924f0 174 }
epgmdm 11:87ab310924f0 175 case 'i':{
epgmdm 11:87ab310924f0 176 if (dataRx[1] == 's'){
epgmdm 11:87ab310924f0 177 turnIncubatorOn(1);
epgmdm 11:87ab310924f0 178 *ledBlue=0;
epgmdm 11:87ab310924f0 179 }else if (dataRx[1] == 'x'){
epgmdm 11:87ab310924f0 180 turnIncubatorOn(0);
epgmdm 11:87ab310924f0 181 *ledBlue=1;
epgmdm 11:87ab310924f0 182 }
epgmdm 11:87ab310924f0 183 break;
epgmdm 11:87ab310924f0 184 }
epgmdm 11:87ab310924f0 185 case 'p':{
epgmdm 11:87ab310924f0 186 if (dataRx[1] == 's'){
epgmdm 11:87ab310924f0 187 turnPoshoOn(1);
epgmdm 11:87ab310924f0 188 *ledBlue=0;
epgmdm 11:87ab310924f0 189 }else if (dataRx[1] == 'x'){
epgmdm 11:87ab310924f0 190 turnPoshoOn(0);
epgmdm 11:87ab310924f0 191 *ledBlue=1;
epgmdm 11:87ab310924f0 192 }
epgmdm 11:87ab310924f0 193 break;
epgmdm 11:87ab310924f0 194 }
epgmdm 11:87ab310924f0 195 }
epgmdm 11:87ab310924f0 196
epgmdm 11:87ab310924f0 197 }else if (pipe>1) {
epgmdm 11:87ab310924f0 198 flagNrf = 1;
epgmdm 11:87ab310924f0 199 }
epgmdm 11:87ab310924f0 200
epgmdm 11:87ab310924f0 201 lastRxTme = now;
epgmdm 11:87ab310924f0 202
epgmdm 11:87ab310924f0 203 }
epgmdm 11:87ab310924f0 204
epgmdm 11:87ab310924f0 205 DBG("intNRF> int rx");
epgmdm 11:87ab310924f0 206 nrf->clearStatus();
epgmdm 11:87ab310924f0 207 *ledBlue = !*ledBlue;
epgmdm 11:87ab310924f0 208
epgmdm 11:87ab310924f0 209 }
epgmdm 11:87ab310924f0 210
epgmdm 11:87ab310924f0 211 /**
epgmdm 11:87ab310924f0 212 * Cleans the receiver
epgmdm 11:87ab310924f0 213 */
epgmdm 11:87ab310924f0 214 void Utility::intRxClean() {
epgmdm 11:87ab310924f0 215 if (now - lastRxTme>60){
epgmdm 11:87ab310924f0 216 nrfFlush();
epgmdm 11:87ab310924f0 217 wait(0.01);
epgmdm 11:87ab310924f0 218 nrfFlush();
epgmdm 11:87ab310924f0 219 DBG("intRxClean < status=%x", nrf->checkStatus());
epgmdm 11:87ab310924f0 220 }
epgmdm 11:87ab310924f0 221 }
epgmdm 11:87ab310924f0 222 /**
epgmdm 11:87ab310924f0 223 * Turns the Husker on
epgmdm 11:87ab310924f0 224 */
epgmdm 11:87ab310924f0 225 void Utility::turnHuskerOn(int on){
epgmdm 11:87ab310924f0 226 DBG("Turn husker %d",on);
epgmdm 11:87ab310924f0 227 *husker=on;
epgmdm 11:87ab310924f0 228 }
epgmdm 11:87ab310924f0 229 /**
epgmdm 11:87ab310924f0 230 * Turns the Incubator on
epgmdm 11:87ab310924f0 231 */
epgmdm 11:87ab310924f0 232 void Utility::turnIncubatorOn(int on){
epgmdm 11:87ab310924f0 233 DBG("Turn incubator %d",on);
epgmdm 11:87ab310924f0 234 *incubator = on;
epgmdm 11:87ab310924f0 235 }
epgmdm 11:87ab310924f0 236
epgmdm 11:87ab310924f0 237 /**
epgmdm 11:87ab310924f0 238 * Turns the Posho on
epgmdm 11:87ab310924f0 239 */
epgmdm 11:87ab310924f0 240 void Utility::turnPoshoOn(int on){
epgmdm 11:87ab310924f0 241 DBG("Turn posho %d",on);
epgmdm 11:87ab310924f0 242 *posho = on;
epgmdm 11:87ab310924f0 243 }