// File: Main.h //------------------------------------------------------------------------------ // Author: Giovanni de Sanctis // Email: info@lateral-technologies.com //------------------------------------------------------------------------------ // Date: Dec 2018 //------------------------------------------------------------------------------ #include "mcc_generated_files/mcc.h" #include "Power.h" #include "JDY08.h" #include "Moves.h" #include "Terminal.h" #include "RF.h" #define T_PWR (5*60*50) //5 min * 60 sec * 1/20ms #define N_RND_MOVES 9 //Number of random moves taken from the list //~30*(9-1) = 4 minutes total duration. void main(void) { uint8_t isRFver; uint8_t movCntA=0; uint8_t movCntB=0; uint8_t movCntC=0; uint8_t movCntD=0; uint8_t movCntRnd = 0; uint8_t movRndPtr = 0; uint8_t ledCntA=0; uint8_t ledCntB=0; uint8_t ledCntC=0; AckEnum lastMoveAck=ACK_WAITING; AckEnum lastLedsAck=ACK_WAITING; uint16_t tPwr = T_PWR; //Power off timeout if no commands received uint16_t tRnd = 0; //Timeout for random moves //Initialise the device SYSTEM_Initialize(); powerBootstrap(); //Keeps power enable high after button is released movesInit(); //Check if an RF receiver is connected RC4_SetDigitalInput(); //Unlock peripheral select PPSLOCK=0x55; //Magic sequence PPSLOCK=0xAA; PPSLOCK=0x00; //Reset PPSLOCKED bit RC4PPS = 0x00; //RC4-> I/O //Re-lock peripheral select PPSLOCK=0x55; //Magic sequence PPSLOCK=0xAA; PPSLOCK=0x01; //Set PPSLOCKED bit uint8_t i0=0; uint8_t i1=0; __delay_ms(500); for (uint8_t i=0; i<250; i++) { if (RC3_GetValue()) i1++; else i0++; __delay_us(1000); } //If noise was coming from the RF input then RF receiver is connected isRFver = (uint8_t)((i0>2) && (i1>2)); //if (isRFver) #ifdef IS_RF_VER { initRF(); //Inits the RF module LED_RED_On(); //Blink Red LED twice to inform RF mode is active __delay_ms(100); LED_RED_Off(); __delay_ms(100); LED_RED_On(); __delay_ms(100); LED_RED_Off(); setBluePattern(LED_BLUE_CNNCTD); // Enable the Global Interrupts INTERRUPT_GlobalInterruptEnable(); // Enable the Peripheral Interrupts INTERRUPT_PeripheralInterruptEnable(); while(1) { uint8_t rfCode = rfGetNewCode(); switch (rfCode) { case RF_A: if (lastMoveAck==ACK_RUNNING) break; setMove(moveGrpSlow[movCntA++]); if (movCntA>=MAX_GRP_SLOW) movCntA=0; break; case RF_B: if (lastMoveAck==ACK_RUNNING) break; setMove(moveGrpFast[movCntB++]); if (movCntB>=MAX_GRP_FAST) movCntB=0; break; case RF_C: if (lastMoveAck==ACK_RUNNING) break; setMove(moveGrpTrem[movCntC++]); if (movCntC>=MAX_GRP_TREM) movCntC=0; break; case RF_D: if (lastMoveAck==ACK_RUNNING) break; setMove(moveGrpErec[movCntD++]); if (movCntD>=MAX_GRP_EREC) movCntD=0; break; case RF_LONG_A: //If any other pattern was running then turn off (first pattern of grp A) //if (ACK_RUNNING == lastLedsAck) setLEDs(ledsGrpA[ledCntA++]); if (ledCntA>=MAX_LED_GRPA) ledCntA=0; break; case RF_LONG_B: ledCntA = 1; setLEDs(ledsGrpB[ledCntB++]); if (ledCntB>=MAX_LED_GRPB) ledCntB=0; break; case RF_LONG_C: ledCntA = 1; setLEDs(ledsGrpC[ledCntC++]); if (ledCntC>=MAX_LED_GRPC) ledCntC=0; break; case RF_LONG_D: //Random moves from first 2 groups if (movCntRnd) movCntRnd = 0; else movCntRnd = N_RND_MOVES; tRnd = 0; break; } wait20ms(); lastMoveAck=updateMove(); lastLedsAck=updateLEDs(); if ((lastMoveAck == ACK_WAITING) && (movCntRnd)) { if (tRnd) tRnd--; else { tRnd = (uint16_t)moveRndGrpT[movRndPtr] * 50; setMove(moveRndGrp[movRndPtr]); if (movRndPtrEUSART:TX; //Re-lock peripheral select PPSLOCK=0x55; //Magic sequence PPSLOCK=0xAA; PPSLOCK=0x01; //Set PPSLOCKED bit TMR1_StopTimer(); //Timer 1 not used in BT mode // Enable the Global Interrupts INTERRUPT_GlobalInterruptEnable(); // Enable the Peripheral Interrupts INTERRUPT_PeripheralInterruptEnable(); initJDY08(); //Inits the bluetooth module //setAuto(1,5,240,2); while (1) { AckEnum ack; //Update move and leds (if running) and send a message at the end ack = updateMove(); ack |= updateLEDs(); terminal(ack); checkPowerButton(); //Read power button to power down the system wait20ms(); } } //} #endif