The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

SolarNanoGrid.cpp

Committer:
defrost
Date:
2016-06-03
Revision:
7:5db9aeec9c2e
Parent:
6:93ca8321f83e
Child:
10:30c9e8df0032

File content as of revision 7:5db9aeec9c2e:

/**
 *@section DESCRIPTION
 * mbed SolarNanogrid  Library
 *@section LICENSE
 * Copyright (c) 2016, Malcolm McCulloch
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * @file "SolarNanoGrid.c"
 */
#include <mbed.h>
#include "SolarNanoGrid.h"

// Constructor:
/**
 * Constructor.
 */
SolarNanoGrid::SolarNanoGrid(FILE *fp=NULL):_fp(fp){
    // Save the sd card pointer:
    DBG("Solar init");
    // Leds
    ledRed = new DigitalOut(LED_RED,1);
    ledGreen= new DigitalOut(LED_GREEN,1);
    ledBlue = new DigitalOut(LED_BLUE,1);
    // Realtime clock
    time_t now= time(NULL);
    struct tm * timeInf = localtime(&now);
    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);

    // Read config.ini
    if (fscanf (fp,"%d %*c %*s",&sdVersion )!=1) ERR("Config: cannot read version");
    if (fscanf (fp,"%u %*c %*s",&communityID  )!=1) ERR("Config: cannot read community ID");
    if (fscanf (fp,"%x %*c %*s",&id)!=1) ERR("Config: cannot read ID");
    if (fscanf (fp,"%x %*c %*s",&chan )!=1) ERR("Locker config: cannot read channel");

    INFO("config.ini:  Version %u, Community %u ID %x Channel %x",sdVersion,communityID,id,chan);
    spiSD();
    mkdir("/sd/data", 777);
    // Initialize nrf
    ce = new DigitalOut(PTB20);
    nrf = new NRF2401P(PTD6, PTD7, PTD5, PTD4, PTB20);       // Rev E
    //nrf1 = new NRF2401P(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) REV D // irq ptc18 on k64f
    
    // initialize all of the variables:
    sdVersion=0;
    id=0;
    communityID=0;
    chan=0;
   

    *ledRed=NULL;
    *ledGreen=NULL;
    *ledBlue=NULL;
    _fp=NULL;
    nrf=NULL;



}

/**
 * asks the user for the time
 */

void SolarNanoGrid::userSetRTCpc()
{
    // get the current time from the terminal
    struct tm t;
    printf("Enter current date :\n\r");
    printf("YYYY MM DD [enter]\n\r");
    scanf("%d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday);
    printf("Enter current time:\n\r");
    printf("HH MM SS [enter]\n\r");
    scanf("%d %d %d", &t.tm_hour, &t.tm_min, &t.tm_sec);

    // adjust for tm structure required values
    t.tm_year = t.tm_year - 1900;
    t.tm_mon = t.tm_mon - 1;

    // set the time
    set_time(mktime(&t));

}
/**
 * Flushes the rx and tx buffers and resets the status.
 */
void SolarNanoGrid::nrfFlush() {
    spiNrf();
    if(nrf!=NULL){
        nrf->clearStatus();
        nrf->flushRx();
        nrf->flushTx();
        nrf->clearStatus();
    }
}

/**
 * Turns SPI on for SD card
 */
void SolarNanoGrid::spiSD(void)
{
    //sd.select();
    pin_function(PTE1 , 7); //Set SD_MISO as SPI, this is the same as the last number in those tables
    pin_function(PTD7, 1);  //pin function 1 is GPIO
    return;
}

/**
 * Turns SPI on for nrf
 */
void SolarNanoGrid::spiNrf(void)
{
    //sd.deselect();
    pin_function(PTE1, 1); //pin function 1 is GPIO
    pin_function(PTD7, 7); //Set SD_MISO as SPI, this is the same as the last number in those tables
    return;
}