/****************************************************************************
Module
LEDService.c
Revision
1.0.1
Description
This is a template file for implementing flat state machines under the
Gen2 Events and Services Framework.
Notes
History
When Who What/Why
-------------- --- --------
01/15/12 11:12 jec revisions for Gen2 framework
11/07/11 11:26 jec made the queue static
10/30/11 17:59 jec fixed references to CurrentEvent in RunTemplateSM()
10/23/11 18:20 jec began conversion from SMTemplate.c (02/20/07 rev)
****************************************************************************/
/*----------------------------- 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 "LEDService.h"
#include <xc.h>
#include <stdbool.h>
#include "PIC32_SPI_HAL.h"
#include "DM_Display.h"
#include "FontStuff.h"
#include "dbprintf.h"
#include "ES_DeferRecall.h"
#include "ES_CheckEvents.h"
/*----------------------------- Module Defines ----------------------------*/
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine.They should be functions
relevant to the behavior of this state machine
*/
bool PostPrintService();
/*---------------------------- Module Variables ---------------------------*/
// everybody needs a state variable, you may need others as well.
// type of state variable should match htat of enum in header file
static LEDState_t CurrentState;
// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
uint16_t score = 0;
static ES_Event_t DeferralQueue[3 + 1];
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitTemplateFSM
Parameters
uint8_t : the priorty of this service
Returns
bool, false if error in initialization, true otherwise
Description
Saves away the priority, sets up the initial transition and does any
other required initialization for this state machine
Notes
Author
J. Edward Carryer, 10/23/11, 18:55
****************************************************************************/
bool InitLEDService(uint8_t Priority)
{
ES_Event_t ThisEvent;
MyPriority = Priority;
clrScrn();
// put us into the Initial PseudoState
CurrentState = InitLState;
ES_InitDeferralQueueWith(DeferralQueue, ARRAY_SIZE(DeferralQueue));
SPI_Module_t SPI_1 = SPI_SPI1;
uint32_t clockPeriod = 10000;
if ((SPISetup_BasicConfig(SPI_1)) &&
(SPISetup_SetLeader(SPI_1, SPI_SMP_MID)) &&
(SPISetup_SetBitTime(SPI_1, clockPeriod)) &&
(SPISetup_MapSSOutput(SPI_1, SPI_RPA0)) &&
(SPISetup_MapSDOutput(SPI_1, SPI_RPA1)) &&
(SPISetup_SetClockIdleState(SPI_1, SPI_CLK_HI)) &&
(SPISetup_SetActiveEdge(SPI_1, SPI_SECOND_EDGE)) &&
(SPISetup_SetXferWidth(SPI_1, SPI_16BIT)) &&
(SPISetEnhancedBuffer(SPI_1, 1)) &&
(SPISetup_EnableSPI(SPI_1))) {
DB_printf("PIC 32 Initialized\r\n");
while(!DM_TakeInitDisplayStep());
DB_printf("Display Initialized\r\n");
CurrentState = WAITING;
} else {
DB_printf("Failed to initialize\r\n");
}
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}
bool PostLEDService(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
ES_Event_t RunLEDService(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT;
switch (CurrentState)
{
case InitLState:
{
if (ThisEvent.EventType == ES_INIT)
{
DB_printf("ES_INFIT received in LED Service \r\n");
}
break;
case WAITING: // If current state is state one
{
switch (ThisEvent.EventType)
{
case ES_MESSAGE:
{
switch(ThisEvent.EventParam)
{
case ES_DISPLAY_SCORE:
{
DM_ClearDisplayBuffer();
DM_AddChar2DisplayBuffer('S');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer('c');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer('o');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer('r');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer('e');
DM_ScrollDisplayBuffer(3);
DM_AddChar2DisplayBuffer(':');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer('0');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer('0');
ThisEvent.EventType = ES_UPDATING;
PostLEDService(ThisEvent);
CurrentState = UPDATING;
}
break;
case ES_UPDATING_SCORE:
{
score++;
DM_ClearDisplayBuffer();
DM_AddChar2DisplayBuffer('S');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer('c');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer('o');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer('r');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer('e');
DM_ScrollDisplayBuffer(3);
DM_AddChar2DisplayBuffer(':');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer((score%100)/10 + '0');
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer(score%10 + '0');
ThisEvent.EventType = ES_UPDATING;
PostLEDService(ThisEvent);
CurrentState = UPDATING;
}
break;
case ES_MESSAGE:
{
DB_printf("In MESS\r\n");
ThisEvent.EventPrint = "Game Timed Out ";
ThisEvent.EventType = ES_PRINT;
PostPrintService(ThisEvent);
}
break;
case ES_INSERT2:
{
DB_printf("In ES_INSERT2\r\n");
ThisEvent.EventPrint = " Insert 2 Coins... ";
ThisEvent.EventType = ES_PRINT;
PostPrintService(ThisEvent);
}
break;
case ES_INSERT1:
{
ThisEvent.EventPrint = "Insert 1 Coin... ";
ThisEvent.EventType = ES_PRINT;
PostPrintService(ThisEvent);
}
break;
case ES_CONGRATS:
{
DM_ClearDisplayBuffer();
ThisEvent.EventPrint = " Great job!!! ";
ThisEvent.EventType = ES_PRINT;
PostPrintService(ThisEvent);
}
break;
case ES_DEFEAT:
{
DM_ClearDisplayBuffer();
ThisEvent.EventPrint = " I've seen better... ";
ThisEvent.EventType = ES_PRINT;
PostPrintService(ThisEvent);
}
break;
default:
{
uint8_t NumCols2Scroll = 4;
DM_ScrollDisplayBuffer(NumCols2Scroll);
DM_AddChar2DisplayBuffer(ThisEvent.EventParam);
ThisEvent.EventType = ES_UPDATING;
PostLEDService(ThisEvent);
CurrentState = UPDATING;
}
break;
}
}
break;
default:
break;
} // end switch on CurrentEvent
}
break;
case UPDATING:
{
switch (ThisEvent.EventType)
{
case ES_UPDATING:
{
if(!DM_TakeDisplayUpdateStep()){
//DB_printf("updating display\r\n");
ThisEvent.EventType = ES_UPDATING;
PostLEDService(ThisEvent);
} else {
CurrentState = WAITING;
}
}
break;
default:
;
}
}
break;
default:
;
}
return ReturnEvent;
}
}
LEDState_t QueryLEDService(void)
{
return CurrentState;
}