/****************************************************************************
Module
GenSeqService.c
Revision
1.0.1
Description
This is a template file for implementing a simple service under the
Gen2 Events and Services Framework.
Notes
History
When Who What/Why
-------------- --- --------
01/16/12 09:58 jec began conversion from TemplateFSM.c
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for this state machine as well as any machines at the
next lower level in the hierarchy that are sub-machines to this machine
*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "GenerateSequenceService.h"
#include "dbprintf.h"
#include "PWM_PIC32.h"
#include "GameFSM.h"
#include <time.h>
/*----------------------------- Module Defines ----------------------------*/
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this service.They should be functions
relevant to the behavior of this service
*/
/*---------------------------- Module Variables ---------------------------*/
// with the introduction of Gen2, we need a module level Priority variable
static uint8_t MyPriority;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitTemplateService
Parameters
uint8_t : the priorty of this service
Returns
bool, false if error in initialization, true otherwise
Description
Saves away the priority, and does any
other required initialization for this service
Notes
Author
J. Edward Carryer, 01/16/12, 10:00
****************************************************************************/
bool InitGenSeqService(uint8_t Priority)
{
ES_Event_t ThisEvent;
MyPriority = Priority;
PWMSetup_BasicConfig(4);
PWMSetup_AssignChannelToTimer(1,_Timer2_);
PWMSetup_AssignChannelToTimer(2,_Timer2_);
PWMSetup_AssignChannelToTimer(3,_Timer2_);
PWMSetup_AssignChannelToTimer(4,_Timer2_);
PWMSetup_SetPeriodOnTimer(25000,_Timer2_);
PWMSetup_MapChannelToOutputPin(1,PWM_RPB3);
PWMSetup_MapChannelToOutputPin(2,PWM_RPB5);
PWMSetup_MapChannelToOutputPin(3,PWM_RPA3);
PWMSetup_MapChannelToOutputPin(4,PWM_RPA4);
// post the initial transition event
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}
/****************************************************************************
Function
PostTemplateService
Parameters
EF_Event_t ThisEvent ,the event to post to the queue
Returns
bool false if the Enqueue operation failed, true otherwise
Description
Posts an event to this state machine's queue
Notes
Author
J. Edward Carryer, 10/23/11, 19:25
****************************************************************************/
bool PostGenSeqService(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunTemplateService
Parameters
ES_Event_t : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here
Notes
Author
J. Edward Carryer, 01/15/12, 15:23
****************************************************************************/
ES_Event_t RunGenSeqService(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
static uint8_t size;
static int sequence[10];
static int user_sequence[10];
static uint8_t counter;
static int tracker = 0;
if(ThisEvent.EventType == PLAY_SEQUENCE){
size = ThisEvent.EventParam;
srand(ES_Timer_GetTime());
generateSeq(sequence,user_sequence,size,1,5);
counter = 0;
PWMOperate_SetPulseWidthOnChannel(6500,1);
PWMOperate_SetPulseWidthOnChannel(6500,2);
PWMOperate_SetPulseWidthOnChannel(6500,3);
PWMOperate_SetPulseWidthOnChannel(6500,4);
ES_Timer_InitTimer(GENSEQ_TIMER, 1000);
}
if(ThisEvent.EventType == ES_TIMEOUT){
if(counter < (size*2))
{
if (tracker%2 == 0){
if (sequence[counter] == 5){
LATAbits.LATA2 = 1;
DB_printf("LED ON: %d\n",(int)sequence[counter]);
} else {
PWMOperate_SetPulseWidthOnChannel(1500,sequence[counter]);
DB_printf("Servo move on: %d\n",(int)sequence[counter]);
}
} else {
if (sequence[counter] == 5)
{
LATAbits.LATA2 = 0;
//DB_printf("LED OFF: %d\n",(int)sequence[counter]);
}
else {
PWMOperate_SetPulseWidthOnChannel(6300,sequence[counter-1]);
//DB_printf("Servo move off: %d\n",(int)sequence[counter-1]);
}
ThisEvent.EventType = EXPECTED_SEQUENCE;
ThisEvent.EventParam = sequence[counter];
PostGameFSM(ThisEvent);
}
//DB_printf("Counter Val = %d\n",(int)counter);
if (counter != (size*2 - 1))
{
ES_Timer_InitTimer(GENSEQ_TIMER, 650);
}
}
counter++;
tracker++;
}
return ReturnEvent;
}
/***************************************************************************
private functions
***************************************************************************/
void generateSeq(int *sequence,int *user_sequence,int size,int min,int max)
{
uint16_t value;
for(int i = 0; i < (size*2); i++){
value = (rand() % (max - min + 1)) + min;
sequence[i] = value;
sequence[i+1] = value;
user_sequence[i/2] = value;
i++;
}
}
/*------------------------------- Footnotes -------------------------------*/
/*------------------------------ End of file ------------------------------*/