Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SolarNanoGridv3 by
SolarNanoGrid.cpp@16:919e37e5a895, 2016-06-11 (annotated)
- Committer:
- defrost
- Date:
- Sat Jun 11 19:01:24 2016 +0000
- Revision:
- 16:919e37e5a895
- Parent:
- 13:de43f28c0365
- Child:
- 22:93fde34d9a94
- Added a class to store useful information: role/id/etc..
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 | *@section LICENSE |
defrost | 5:57b06b4b47c6 | 5 | * Copyright (c) 2016, Malcolm McCulloch |
defrost | 5:57b06b4b47c6 | 6 | * |
defrost | 5:57b06b4b47c6 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
defrost | 5:57b06b4b47c6 | 8 | * of this software and associated documentation files (the "Software"), to deal |
defrost | 5:57b06b4b47c6 | 9 | * in the Software without restriction, including without limitation the rights |
defrost | 5:57b06b4b47c6 | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
defrost | 5:57b06b4b47c6 | 11 | * copies of the Software, and to permit persons to whom the Software is |
defrost | 5:57b06b4b47c6 | 12 | * furnished to do so, subject to the following conditions: |
defrost | 5:57b06b4b47c6 | 13 | * |
defrost | 5:57b06b4b47c6 | 14 | * The above copyright notice and this permission notice shall be included in |
defrost | 5:57b06b4b47c6 | 15 | * all copies or substantial portions of the Software. |
defrost | 5:57b06b4b47c6 | 16 | * |
defrost | 5:57b06b4b47c6 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
defrost | 5:57b06b4b47c6 | 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
defrost | 5:57b06b4b47c6 | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
defrost | 5:57b06b4b47c6 | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
defrost | 5:57b06b4b47c6 | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
defrost | 5:57b06b4b47c6 | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
defrost | 5:57b06b4b47c6 | 23 | * THE SOFTWARE. |
defrost | 5:57b06b4b47c6 | 24 | * @file "SolarNanoGrid.c" |
defrost | 5:57b06b4b47c6 | 25 | */ |
defrost | 5:57b06b4b47c6 | 26 | #include <mbed.h> |
defrost | 6:93ca8321f83e | 27 | #include "SolarNanoGrid.h" |
defrost | 13:de43f28c0365 | 28 | #define FUNCNAME "SNGC" |
defrost | 13:de43f28c0365 | 29 | #include "defs.h" |
defrost | 1:df924e0126d1 | 30 | // Constructor: |
defrost | 5:57b06b4b47c6 | 31 | /** |
defrost | 5:57b06b4b47c6 | 32 | * Constructor. |
defrost | 5:57b06b4b47c6 | 33 | */ |
defrost | 13:de43f28c0365 | 34 | SolarNanoGrid::SolarNanoGrid(FILE *fp=NULL, Serial *pc=NULL):pc(pc),_fp(fp){ |
defrost | 1:df924e0126d1 | 35 | // Save the sd card pointer: |
defrost | 5:57b06b4b47c6 | 36 | DBG("Solar init"); |
defrost | 5:57b06b4b47c6 | 37 | // Leds |
defrost | 5:57b06b4b47c6 | 38 | ledRed = new DigitalOut(LED_RED,1); |
defrost | 5:57b06b4b47c6 | 39 | ledGreen= new DigitalOut(LED_GREEN,1); |
defrost | 5:57b06b4b47c6 | 40 | ledBlue = new DigitalOut(LED_BLUE,1); |
defrost | 5:57b06b4b47c6 | 41 | // Realtime clock |
defrost | 5:57b06b4b47c6 | 42 | time_t now= time(NULL); |
defrost | 5:57b06b4b47c6 | 43 | struct tm * timeInf = localtime(&now); |
defrost | 5:57b06b4b47c6 | 44 | INFO ("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 | 1:df924e0126d1 | 45 | |
defrost | 5:57b06b4b47c6 | 46 | // Read config.ini |
defrost | 5:57b06b4b47c6 | 47 | if (fscanf (fp,"%d %*c %*s",&sdVersion )!=1) ERR("Config: cannot read version"); |
defrost | 5:57b06b4b47c6 | 48 | if (fscanf (fp,"%u %*c %*s",&communityID )!=1) ERR("Config: cannot read community ID"); |
defrost | 5:57b06b4b47c6 | 49 | if (fscanf (fp,"%x %*c %*s",&id)!=1) ERR("Config: cannot read ID"); |
defrost | 5:57b06b4b47c6 | 50 | if (fscanf (fp,"%x %*c %*s",&chan )!=1) ERR("Locker config: cannot read channel"); |
defrost | 1:df924e0126d1 | 51 | |
defrost | 5:57b06b4b47c6 | 52 | INFO("config.ini: Version %u, Community %u ID %x Channel %x",sdVersion,communityID,id,chan); |
defrost | 5:57b06b4b47c6 | 53 | spiSD(); |
defrost | 5:57b06b4b47c6 | 54 | mkdir("/sd/data", 777); |
defrost | 5:57b06b4b47c6 | 55 | // Initialize nrf |
defrost | 5:57b06b4b47c6 | 56 | ce = new DigitalOut(PTB20); |
defrost | 5:57b06b4b47c6 | 57 | nrf = new NRF2401P(PTD6, PTD7, PTD5, PTD4, PTB20); // Rev E |
defrost | 5:57b06b4b47c6 | 58 | //nrf1 = new NRF2401P(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) REV D // irq ptc18 on k64f |
defrost | 1:df924e0126d1 | 59 | |
defrost | 1:df924e0126d1 | 60 | |
defrost | 1:df924e0126d1 | 61 | } |
defrost | 1:df924e0126d1 | 62 | |
defrost | 5:57b06b4b47c6 | 63 | /** |
defrost | 5:57b06b4b47c6 | 64 | * asks the user for the time |
defrost | 5:57b06b4b47c6 | 65 | */ |
defrost | 5:57b06b4b47c6 | 66 | |
defrost | 5:57b06b4b47c6 | 67 | void SolarNanoGrid::userSetRTCpc() |
defrost | 1:df924e0126d1 | 68 | { |
defrost | 5:57b06b4b47c6 | 69 | // get the current time from the terminal |
defrost | 5:57b06b4b47c6 | 70 | struct tm t; |
defrost | 5:57b06b4b47c6 | 71 | printf("Enter current date :\n\r"); |
defrost | 5:57b06b4b47c6 | 72 | printf("YYYY MM DD [enter]\n\r"); |
defrost | 5:57b06b4b47c6 | 73 | scanf("%d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday); |
defrost | 5:57b06b4b47c6 | 74 | printf("Enter current time:\n\r"); |
defrost | 5:57b06b4b47c6 | 75 | printf("HH MM SS [enter]\n\r"); |
defrost | 5:57b06b4b47c6 | 76 | scanf("%d %d %d", &t.tm_hour, &t.tm_min, &t.tm_sec); |
defrost | 5:57b06b4b47c6 | 77 | |
defrost | 5:57b06b4b47c6 | 78 | // adjust for tm structure required values |
defrost | 5:57b06b4b47c6 | 79 | t.tm_year = t.tm_year - 1900; |
defrost | 5:57b06b4b47c6 | 80 | t.tm_mon = t.tm_mon - 1; |
defrost | 5:57b06b4b47c6 | 81 | |
defrost | 5:57b06b4b47c6 | 82 | // set the time |
defrost | 5:57b06b4b47c6 | 83 | set_time(mktime(&t)); |
defrost | 5:57b06b4b47c6 | 84 | |
defrost | 5:57b06b4b47c6 | 85 | } |
defrost | 5:57b06b4b47c6 | 86 | /** |
defrost | 5:57b06b4b47c6 | 87 | * Flushes the rx and tx buffers and resets the status. |
defrost | 5:57b06b4b47c6 | 88 | */ |
defrost | 5:57b06b4b47c6 | 89 | void SolarNanoGrid::nrfFlush() { |
epgmdm | 11:87ab310924f0 | 90 | spiNRF(); |
defrost | 5:57b06b4b47c6 | 91 | if(nrf!=NULL){ |
defrost | 5:57b06b4b47c6 | 92 | nrf->clearStatus(); |
defrost | 5:57b06b4b47c6 | 93 | nrf->flushRx(); |
defrost | 5:57b06b4b47c6 | 94 | nrf->flushTx(); |
defrost | 5:57b06b4b47c6 | 95 | nrf->clearStatus(); |
defrost | 1:df924e0126d1 | 96 | } |
defrost | 1:df924e0126d1 | 97 | } |
defrost | 1:df924e0126d1 | 98 | |
defrost | 5:57b06b4b47c6 | 99 | /** |
defrost | 5:57b06b4b47c6 | 100 | * Turns SPI on for SD card |
defrost | 5:57b06b4b47c6 | 101 | */ |
defrost | 2:929cf7fc6998 | 102 | void SolarNanoGrid::spiSD(void) |
defrost | 2:929cf7fc6998 | 103 | { |
defrost | 2:929cf7fc6998 | 104 | //sd.select(); |
defrost | 2:929cf7fc6998 | 105 | pin_function(PTE1 , 7); //Set SD_MISO as SPI, this is the same as the last number in those tables |
defrost | 2:929cf7fc6998 | 106 | pin_function(PTD7, 1); //pin function 1 is GPIO |
defrost | 2:929cf7fc6998 | 107 | return; |
defrost | 2:929cf7fc6998 | 108 | } |
defrost | 2:929cf7fc6998 | 109 | |
defrost | 5:57b06b4b47c6 | 110 | /** |
defrost | 5:57b06b4b47c6 | 111 | * Turns SPI on for nrf |
defrost | 5:57b06b4b47c6 | 112 | */ |
epgmdm | 11:87ab310924f0 | 113 | void SolarNanoGrid::spiNRF(void) |
defrost | 2:929cf7fc6998 | 114 | { |
defrost | 2:929cf7fc6998 | 115 | //sd.deselect(); |
defrost | 2:929cf7fc6998 | 116 | pin_function(PTE1, 1); //pin function 1 is GPIO |
defrost | 2:929cf7fc6998 | 117 | pin_function(PTD7, 7); //Set SD_MISO as SPI, this is the same as the last number in those tables |
defrost | 2:929cf7fc6998 | 118 | return; |
defrost | 2:929cf7fc6998 | 119 | } |
epgmdm | 11:87ab310924f0 | 120 | |
epgmdm | 11:87ab310924f0 | 121 | /** |
epgmdm | 11:87ab310924f0 | 122 | * Set NRF as RX and flush |
epgmdm | 11:87ab310924f0 | 123 | */ |
epgmdm | 11:87ab310924f0 | 124 | void SolarNanoGrid::setAsRX(long long addr){ |
epgmdm | 11:87ab310924f0 | 125 | |
epgmdm | 11:87ab310924f0 | 126 | spiNRF(); |
epgmdm | 11:87ab310924f0 | 127 | nrf->quickRxSetup(chan, addr); // Pipe 0 |
epgmdm | 11:87ab310924f0 | 128 | nrf->setRadio(0x01, 0x03); // 2MB/S 0dB |
epgmdm | 11:87ab310924f0 | 129 | nrfFlush(); |
epgmdm | 11:87ab310924f0 | 130 | } |
defrost | 13:de43f28c0365 | 131 | /** |
defrost | 13:de43f28c0365 | 132 | * loop |
epgmdm | 11:87ab310924f0 | 133 | |
defrost | 13:de43f28c0365 | 134 | void SolarNanoGrid::loop(){ |
defrost | 13:de43f28c0365 | 135 | ; |
defrost | 13:de43f28c0365 | 136 | } |
defrost | 13:de43f28c0365 | 137 | |
epgmdm | 11:87ab310924f0 | 138 | * Sets NRF as TX and flushes the NRF |
epgmdm | 11:87ab310924f0 | 139 | */ |
epgmdm | 11:87ab310924f0 | 140 | void SolarNanoGrid::setAsTX(long long addr){ |
epgmdm | 11:87ab310924f0 | 141 | DBG("setAsTx"); |
epgmdm | 11:87ab310924f0 | 142 | spiNRF(); |
epgmdm | 11:87ab310924f0 | 143 | nrf->quickTxSetup(chan, addr); |
epgmdm | 11:87ab310924f0 | 144 | nrf->setRadio(0x01, 0x03); // 2MB/S 0dB |
epgmdm | 11:87ab310924f0 | 145 | nrf->setTxRetry(0x0F, 0x0F); |
epgmdm | 11:87ab310924f0 | 146 | nrfFlush(); |
epgmdm | 11:87ab310924f0 | 147 | DBG("Nrf Details:"); |
epgmdm | 11:87ab310924f0 | 148 | #ifdef DEBUG |
epgmdm | 11:87ab310924f0 | 149 | nrf->printDetails(); |
epgmdm | 11:87ab310924f0 | 150 | #endif |
epgmdm | 11:87ab310924f0 | 151 | |
defrost | 16:919e37e5a895 | 152 | } |
defrost | 16:919e37e5a895 | 153 | |
defrost | 16:919e37e5a895 | 154 | SongSettings::SongSettings(void){ |
defrost | 16:919e37e5a895 | 155 | // Just set everything to null: |
defrost | 16:919e37e5a895 | 156 | role = NULL; |
defrost | 16:919e37e5a895 | 157 | sdVersion = NULL; |
defrost | 16:919e37e5a895 | 158 | communityID = NULL; |
defrost | 16:919e37e5a895 | 159 | id = NULL; |
defrost | 16:919e37e5a895 | 160 | chan = NULL; |
defrost | 16:919e37e5a895 | 161 | return; |
defrost | 16:919e37e5a895 | 162 | } |
defrost | 16:919e37e5a895 | 163 | |
defrost | 16:919e37e5a895 | 164 | bool SongSettings::LoadSongSettings(void){ |
defrost | 16:919e37e5a895 | 165 | |
defrost | 16:919e37e5a895 | 166 | char * name = "/sd/config.ini"; |
defrost | 16:919e37e5a895 | 167 | bool result = true; |
defrost | 16:919e37e5a895 | 168 | INFO("Loading configuration from %s", name); |
defrost | 16:919e37e5a895 | 169 | FILE *fp; |
defrost | 16:919e37e5a895 | 170 | INFO("Reading config file..."); |
defrost | 16:919e37e5a895 | 171 | fp = fopen(name, "r"); |
defrost | 16:919e37e5a895 | 172 | if(fp == NULL) { |
defrost | 16:919e37e5a895 | 173 | ERR("Config file cannot be read, USE PC TO CREATE config.ini"); |
defrost | 16:919e37e5a895 | 174 | return false; |
defrost | 16:919e37e5a895 | 175 | }else{ |
defrost | 16:919e37e5a895 | 176 | // Read config.ini |
defrost | 16:919e37e5a895 | 177 | if (fscanf (fp,"%c %*c %*s",role )!=1){ |
defrost | 16:919e37e5a895 | 178 | ERR("Config: cannot read role"); |
defrost | 16:919e37e5a895 | 179 | result = false; |
defrost | 16:919e37e5a895 | 180 | } |
defrost | 16:919e37e5a895 | 181 | if (fscanf (fp,"%d %*c %*s",sdVersion )!=1){ |
defrost | 16:919e37e5a895 | 182 | ERR("Config: cannot read version"); |
defrost | 16:919e37e5a895 | 183 | result = false; |
defrost | 16:919e37e5a895 | 184 | } |
defrost | 16:919e37e5a895 | 185 | if (fscanf (fp,"%u %*c %*s",communityID )!=1){ |
defrost | 16:919e37e5a895 | 186 | ERR("Config: cannot read community ID"); |
defrost | 16:919e37e5a895 | 187 | result = false; |
defrost | 16:919e37e5a895 | 188 | } |
defrost | 16:919e37e5a895 | 189 | if (fscanf (fp,"%x %*c %*s",id)!=1){ |
defrost | 16:919e37e5a895 | 190 | ERR("Config: cannot read ID"); |
defrost | 16:919e37e5a895 | 191 | result = false; |
defrost | 16:919e37e5a895 | 192 | } |
defrost | 16:919e37e5a895 | 193 | if (fscanf (fp,"%x %*c %*s",chan )!=1){ |
defrost | 16:919e37e5a895 | 194 | ERR("Locker config: cannot read channel"); |
defrost | 16:919e37e5a895 | 195 | result = false; |
defrost | 16:919e37e5a895 | 196 | } |
defrost | 16:919e37e5a895 | 197 | |
defrost | 16:919e37e5a895 | 198 | if(result == true){ |
defrost | 16:919e37e5a895 | 199 | // create the log directory string, this does NOT create the directory! |
defrost | 16:919e37e5a895 | 200 | sprintf(LogDir, "/sd/data/%02X%04X", (communityID&0XFF),(id&0XFFFF)); |
defrost | 16:919e37e5a895 | 201 | // Print everything back: |
defrost | 16:919e37e5a895 | 202 | INFO("config.ini: Version %u, Community %u ID %x Channel %x, Log Directory: %s",sdVersion, communityID, id, chan, LogDir); |
defrost | 16:919e37e5a895 | 203 | }else{ |
defrost | 16:919e37e5a895 | 204 | ERR("Some of the settings were not loaded correctly. Perhaps %s is corrupted.", name); |
defrost | 16:919e37e5a895 | 205 | } |
defrost | 16:919e37e5a895 | 206 | return result; |
defrost | 16:919e37e5a895 | 207 | } |
defrost | 16:919e37e5a895 | 208 | |
defrost | 16:919e37e5a895 | 209 | } |
defrost | 16:919e37e5a895 | 210 | |
defrost | 16:919e37e5a895 | 211 |