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
Diff: locker.cpp
- Revision:
- 2:d1eae91343a9
- Parent:
- 1:c2232b1eaf31
--- a/locker.cpp Sun Jan 24 16:15:53 2016 +0000 +++ b/locker.cpp Thu Jan 28 14:38:45 2016 +0000 @@ -5,16 +5,85 @@ */ #include "mbed.h" #include "utils.h" +#include "NRF2401P.h" #define debug // Flags +unsigned char flagNrfReceive = 0; // Variables +char nrfStateL; +float currentMaxL = 1.5; // Maximum current that each cube can consume. + // tx nRF2401 long long addrHub=0xBBBBBBBBBB; +extern NRF2401P nrf1; extern int channel; +InterruptIn nrf1IntL(PTC18); // IRQ of nRF24 + +// ----------------------------------------------------------------------------- +// Interupt routines +// ----------------------------------------------------------------------------- +/** +* Interrupt from nrf +* +* RX_DR Data Ready RX FIFO interrupt. +* TX_DS Data Sent TX FIFO interrupt. Asserted when packet transmitted on TX. +* - - If AUTO_ACK is activated, this bit is set high only when ACK is received. +* MAX_RT Maximum number of TX retransmits interrupt (tx failed) +* TX_FULL TX FIFO full flag. +*/ +void intNrfL(){ + spiNrf(); + flagNrfReceive = nrf1.checkStatus(); + if ((flagNrfReceive >>6) &0x01) { // RX_DR - RX ready + flagNrfReceive = 1; + } +} +void doNrfL(){ + int width; + char data[33]; +#ifdef debug + printf("intNrf %02X %s \n\r",nrf1.status, nrf1.statusString()); +#endif + spiNrf(); + char pipe = (nrf1.checkStatus() >> 1) & 7; + width= nrf1.getRxData(data); + data[width]='\0'; + // Now check which pipe + if (pipe==0) { //The hub + // Check command + switch (data[0]){ + case ('T'):{ // Time signal + time_t newTime; + sscanf (data,"%*c %x",&newTime); + set_time(newTime); + printf(" New Time %s\n\r", ctime(&newTime)); + break; + } + case ('I'):{ // maxCurrent + sscanf (data,"%*c %x",¤tMaxL); + printf(" New Current %4.2f\n\r",currentMaxL); + break; + } + } + } + printf("==>RX Pipe %d [%d]:%s: %s\n\r",pipe, width, data, nrf1.statusString()); + nrf1.clearStatus(); +} +// ----------------------------------------------------------------------------- // Initializaton +// ----------------------------------------------------------------------------- + +/** +* Sets up the interrupts for the locker +*/ +void initInteruptsLocker(){ + nrf1IntL.fall(&intNrfL); // attach nrf +} + + /** * Initialise for a locker */ @@ -30,7 +99,21 @@ #ifdef debug printf(" Channel:%x, Hub Address %llx \n\r",channel, addrHub); #endif + // Setup nrf +#ifdef debug + printf("Steup doNrf \n\r"); +#endif + spiNrf(); + nrf1.quickRxSetup(channel, addrHub); +#ifdef debug + nrf1.printDetails(); + nrf1.checkStatus(); + printf("Setup doNrf complete [nrf:%s]\n\r",nrf1.statusString()); +#endif + + // Setup interupts + initInteruptsLocker(); } // Interupt routines @@ -39,5 +122,9 @@ // Loop through slow routines void loopLocker(){ + if (flagNrfReceive){ + doNrfL(); + flagNrfReceive = 0; + } } \ No newline at end of file