The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

SolarNanoGrid.cpp

Committer:
defrost
Date:
2016-06-11
Revision:
16:919e37e5a895
Parent:
13:de43f28c0365
Child:
22:93fde34d9a94

File content as of revision 16:919e37e5a895:

/**
 *@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"
#define FUNCNAME "SNGC"
#include "defs.h"
// Constructor:
/**
 * Constructor.
 */
SolarNanoGrid::SolarNanoGrid(FILE *fp=NULL, Serial *pc=NULL):pc(pc),_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


}

/**
 * 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;
}

/**
 * Set NRF as RX and flush
 */
void SolarNanoGrid::setAsRX(long long addr){

    spiNRF();
    nrf->quickRxSetup(chan, addr); // Pipe 0
    nrf->setRadio(0x01, 0x03); // 2MB/S  0dB
    nrfFlush();
}
/**
 * loop

void SolarNanoGrid::loop(){
    ;
    }

 * Sets NRF as TX and flushes the NRF
 */
void  SolarNanoGrid::setAsTX(long long addr){
    DBG("setAsTx");
    spiNRF();
    nrf->quickTxSetup(chan, addr);
    nrf->setRadio(0x01, 0x03); // 2MB/S  0dB
    nrf->setTxRetry(0x0F, 0x0F);
    nrfFlush();
    DBG("Nrf Details:");
#ifdef DEBUG
    nrf->printDetails();
#endif

}

SongSettings::SongSettings(void){
    // Just set everything to null:
    role = NULL;
    sdVersion = NULL;
    communityID = NULL;
    id = NULL;
    chan = NULL;
    return;
}

bool SongSettings::LoadSongSettings(void){
    
    char * name = "/sd/config.ini";
    bool result = true;
    INFO("Loading configuration from %s", name);
    FILE *fp;
    INFO("Reading config file...");
    fp = fopen(name, "r");
    if(fp == NULL) {
        ERR("Config file cannot be read, USE PC TO CREATE config.ini");
        return false;
    }else{
        // Read config.ini
        if (fscanf (fp,"%c %*c %*s",role )!=1){
            ERR("Config: cannot read role");
            result = false;
        }
        if (fscanf (fp,"%d %*c %*s",sdVersion )!=1){
            ERR("Config: cannot read version");
            result = false;
        }
        if (fscanf (fp,"%u %*c %*s",communityID  )!=1){
            ERR("Config: cannot read community ID");
            result = false;
        }
        if (fscanf (fp,"%x %*c %*s",id)!=1){
            ERR("Config: cannot read ID");
            result = false;
        }
        if (fscanf (fp,"%x %*c %*s",chan )!=1){
            ERR("Locker config: cannot read channel");
            result = false;
        }
        
        if(result == true){
            // create the log directory string, this does NOT create the directory!
            sprintf(LogDir, "/sd/data/%02X%04X", (communityID&0XFF),(id&0XFFFF));
            // Print everything back:
            INFO("config.ini:  Version %u, Community %u ID %x Channel %x, Log Directory: %s",sdVersion, communityID, id, chan, LogDir);
        }else{
            ERR("Some of the settings were not loaded correctly. Perhaps %s is corrupted.", name);
        }        
        return result;
    }
    
}