working version of song control with initialization from sd card

Dependencies:   MFRC522 NRF2401P SDFileSystem SPI_TFT_ILI9341 TFT_fonts mbed

Fork of Song_Control by Malcolm McCulloch

hub.cpp

Committer:
dxyang
Date:
2016-02-21
Revision:
4:74afa8c5ffe9
Parent:
2:d1eae91343a9

File content as of revision 4:74afa8c5ffe9:

/**
* All the code associated to run the mbed as a hub.
* Link with locker
*/
#include "mbed.h"
#include "utils.h"
#include "NRF2401P.h"

#define debug 

// Flags
unsigned char flag1sH = 0;

// Variables
Ticker tick1sHub;
float currentMaxH = 1.5; // Maximum current that each cube can consume.

// tx nRF2401
extern char txBuff[];
extern NRF2401P nrf1;
extern int channel;
long long addrLcker=0xBBBBBBBBBB;
// -----------------------------------------------------------------------------
// Interupt routines
// -----------------------------------------------------------------------------
/**
* Fast interrupt routine for every 1 second
*/
void int1sH()
{
    flag1sH=1;
    // add only fast code.
}
/**
* Sends a time stamp
*/
void txTimeH()
{
#ifdef debug
    printf("Send time \n\r");
#endif
    time_t now= time(NULL);
    sprintf(txBuff,"T %X",now);
    nrf1.transmitData(txBuff,strlen(txBuff));
#ifdef debug
    printf("Tx %s [nrf:%s] \n\r", txBuff,nrf1.statusString());
#endif  
}

/**
* Sends max Current
*/
void txCurrentH()
{
#ifdef debug
    printf("Send current \n\r");
#endif
    sprintf(txBuff,"I %04X", *((int *) &currentMaxH));
    nrf1.transmitData(txBuff,strlen(txBuff));
#ifdef debug
    printf("Tx %s [nrf:%s] \n\r", txBuff,nrf1.statusString());
#endif  
}

/**
* Slow interrupt routine for every 1 second
*/
void do1sH()
{
#ifdef debug
    printf("Hub 1s \n\r");
#endif
    
    time_t now= time(NULL);
    if ((now % 60)==0){
    txTimeH();  
    }
    txCurrentH();
#ifdef debug
    printf("Tx %s [nrf:%s] \n\r", txBuff,nrf1.statusString());
#endif  
}
// -----------------------------------------------------------------------------
// Initializaton
// -----------------------------------------------------------------------------
/**
* Sets up the interrupts for the hub
*/
void initInteruptsHub(){
    tick1sHub.attach(&int1sH, 10.0);
}
/**
* asks the user for the time
*/

void setRTCpc()
{
    // 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));

}



/**
* Initialise for a locker
* fp is the config file if additonal information is needed.
*/
void initialiseHub(FILE *fp){
#ifdef debug
    printf("Initialise Hub\n\r");
#endif

    // Read in hub address and channel
    if (fscanf (fp,"%x %*c %*s",&channel )!=1) writeError("Hub config: cannot read channel");
    if (fscanf (fp,"%llx %*c %*s",&addrLcker )!=1) writeError("Hub config: cannot read hub address");

#ifdef debug
    printf("  Channel:%x, Hub Address %llx \n\r",channel, addrLcker);
#endif
    setRTCpc();

    // Setup nrf

#ifdef debug
    printf("Steup doNrf \n\r");
#endif
    spiNrf();
    nrf1.quickTxSetup(channel, addrLcker);
#ifdef debug
    nrf1.printDetails();
    nrf1.checkStatus();
    printf("Setup doNrf complete [nrf:%s]\n\r",nrf1.statusString());
#endif

    // Setup interupts
    initInteruptsHub();


}

// Interupt routines

// Loop through slow routines

void loopHub(){

    if (flag1sH){
        do1sH();
        flag1sH=0;
    }
}