The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Committer:
defrost
Date:
Thu Jun 02 16:43:53 2016 +0000
Revision:
6:93ca8321f83e
Parent:
5:57b06b4b47c6
Child:
7:5db9aeec9c2e
- Latest version from Malcolm

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 *@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 1:df924e0126d1 28
defrost 1:df924e0126d1 29 // Constructor:
defrost 5:57b06b4b47c6 30 /**
defrost 5:57b06b4b47c6 31 * Constructor.
defrost 5:57b06b4b47c6 32 */
defrost 5:57b06b4b47c6 33 SolarNanoGrid::SolarNanoGrid(FILE *fp=NULL):_fp(fp){
defrost 1:df924e0126d1 34 // Save the sd card pointer:
defrost 5:57b06b4b47c6 35 DBG("Solar init");
defrost 5:57b06b4b47c6 36 // Leds
defrost 5:57b06b4b47c6 37 ledRed = new DigitalOut(LED_RED,1);
defrost 5:57b06b4b47c6 38 ledGreen= new DigitalOut(LED_GREEN,1);
defrost 5:57b06b4b47c6 39 ledBlue = new DigitalOut(LED_BLUE,1);
defrost 5:57b06b4b47c6 40 // Realtime clock
defrost 5:57b06b4b47c6 41 time_t now= time(NULL);
defrost 5:57b06b4b47c6 42 struct tm * timeInf = localtime(&now);
defrost 5:57b06b4b47c6 43 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 44
defrost 5:57b06b4b47c6 45 // Read config.ini
defrost 5:57b06b4b47c6 46 if (fscanf (fp,"%d %*c %*s",&sdVersion )!=1) ERR("Config: cannot read version");
defrost 5:57b06b4b47c6 47 if (fscanf (fp,"%u %*c %*s",&communityID )!=1) ERR("Config: cannot read community ID");
defrost 5:57b06b4b47c6 48 if (fscanf (fp,"%x %*c %*s",&id)!=1) ERR("Config: cannot read ID");
defrost 5:57b06b4b47c6 49 if (fscanf (fp,"%x %*c %*s",&chan )!=1) ERR("Locker config: cannot read channel");
defrost 1:df924e0126d1 50
defrost 5:57b06b4b47c6 51 INFO("config.ini: Version %u, Community %u ID %x Channel %x",sdVersion,communityID,id,chan);
defrost 5:57b06b4b47c6 52 spiSD();
defrost 5:57b06b4b47c6 53 mkdir("/sd/data", 777);
defrost 5:57b06b4b47c6 54 // Initialize nrf
defrost 5:57b06b4b47c6 55 ce = new DigitalOut(PTB20);
defrost 5:57b06b4b47c6 56 nrf = new NRF2401P(PTD6, PTD7, PTD5, PTD4, PTB20); // Rev E
defrost 5:57b06b4b47c6 57 //nrf1 = new NRF2401P(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) REV D // irq ptc18 on k64f
defrost 1:df924e0126d1 58
defrost 1:df924e0126d1 59
defrost 1:df924e0126d1 60 }
defrost 1:df924e0126d1 61
defrost 5:57b06b4b47c6 62 /**
defrost 5:57b06b4b47c6 63 * asks the user for the time
defrost 5:57b06b4b47c6 64 */
defrost 5:57b06b4b47c6 65
defrost 5:57b06b4b47c6 66 void SolarNanoGrid::userSetRTCpc()
defrost 1:df924e0126d1 67 {
defrost 5:57b06b4b47c6 68 // get the current time from the terminal
defrost 5:57b06b4b47c6 69 struct tm t;
defrost 5:57b06b4b47c6 70 printf("Enter current date :\n\r");
defrost 5:57b06b4b47c6 71 printf("YYYY MM DD [enter]\n\r");
defrost 5:57b06b4b47c6 72 scanf("%d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday);
defrost 5:57b06b4b47c6 73 printf("Enter current time:\n\r");
defrost 5:57b06b4b47c6 74 printf("HH MM SS [enter]\n\r");
defrost 5:57b06b4b47c6 75 scanf("%d %d %d", &t.tm_hour, &t.tm_min, &t.tm_sec);
defrost 5:57b06b4b47c6 76
defrost 5:57b06b4b47c6 77 // adjust for tm structure required values
defrost 5:57b06b4b47c6 78 t.tm_year = t.tm_year - 1900;
defrost 5:57b06b4b47c6 79 t.tm_mon = t.tm_mon - 1;
defrost 5:57b06b4b47c6 80
defrost 5:57b06b4b47c6 81 // set the time
defrost 5:57b06b4b47c6 82 set_time(mktime(&t));
defrost 5:57b06b4b47c6 83
defrost 5:57b06b4b47c6 84 }
defrost 5:57b06b4b47c6 85 /**
defrost 5:57b06b4b47c6 86 * Flushes the rx and tx buffers and resets the status.
defrost 5:57b06b4b47c6 87 */
defrost 5:57b06b4b47c6 88 void SolarNanoGrid::nrfFlush() {
defrost 5:57b06b4b47c6 89 spiNrf();
defrost 5:57b06b4b47c6 90 if(nrf!=NULL){
defrost 5:57b06b4b47c6 91 nrf->clearStatus();
defrost 5:57b06b4b47c6 92 nrf->flushRx();
defrost 5:57b06b4b47c6 93 nrf->flushTx();
defrost 5:57b06b4b47c6 94 nrf->clearStatus();
defrost 1:df924e0126d1 95 }
defrost 1:df924e0126d1 96 }
defrost 1:df924e0126d1 97
defrost 5:57b06b4b47c6 98 /**
defrost 5:57b06b4b47c6 99 * Turns SPI on for SD card
defrost 5:57b06b4b47c6 100 */
defrost 2:929cf7fc6998 101 void SolarNanoGrid::spiSD(void)
defrost 2:929cf7fc6998 102 {
defrost 2:929cf7fc6998 103 //sd.select();
defrost 2:929cf7fc6998 104 pin_function(PTE1 , 7); //Set SD_MISO as SPI, this is the same as the last number in those tables
defrost 2:929cf7fc6998 105 pin_function(PTD7, 1); //pin function 1 is GPIO
defrost 2:929cf7fc6998 106 return;
defrost 2:929cf7fc6998 107 }
defrost 2:929cf7fc6998 108
defrost 5:57b06b4b47c6 109 /**
defrost 5:57b06b4b47c6 110 * Turns SPI on for nrf
defrost 5:57b06b4b47c6 111 */
defrost 2:929cf7fc6998 112 void SolarNanoGrid::spiNrf(void)
defrost 2:929cf7fc6998 113 {
defrost 2:929cf7fc6998 114 //sd.deselect();
defrost 2:929cf7fc6998 115 pin_function(PTE1, 1); //pin function 1 is GPIO
defrost 2:929cf7fc6998 116 pin_function(PTD7, 7); //Set SD_MISO as SPI, this is the same as the last number in those tables
defrost 2:929cf7fc6998 117 return;
defrost 2:929cf7fc6998 118 }