You are on page 1of 6

/****************************************************************************

Module
TemplateService.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
*/
// This module
#include "LEDService.h"

// Hardware
#include <xc.h>

// Event & Services Framework


#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_Port.h"
#include "terminal.h"
#include "dbprintf.h"
#include "ES_DeferRecall.h"

//SPI Stuff
#include "PIC32_SPI_HAL.h"
#include "DM_Display.h"
//Font stuff
#include "bitdefs.h"
#include "FontStuff.h"

#include <string.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 ---------------------------*/


static LEDState_t CurrentState;
// with the introduction of Gen2, we need a module level Priority variable
static uint8_t MyPriority;
static ES_Event_t DeferralQueue[3 + 1];
static uint32_t data = 0x00;
static uint32_t pattern = 0b11111111111111111111111111111111;
static uint32_t bullet[8] =
{0b011110000,0b111111100,0b111111110,0b111111111,0b111111110,0b111111100,0b01111000
0,0};
static char endMessage[8] = "Score:";
static uint8_t score = 0;
static char scoreChar[3];
static uint8_t messageIndex;

/*------------------------------ 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 InitLEDService(uint8_t Priority)
{
ES_Event_t ThisEvent;

MyPriority = Priority;
CurrentState = InitLEDState;
puts ("\rStarting LED Service\r");
DB_printf ("Press any key to post key-stroke events to LED Service\r\n");
DB_printf ("Press any key to add character to display\r\n");
ES_InitDeferralQueueWith(DeferralQueue, ARRAY_SIZE(DeferralQueue));
// 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 PostLEDService(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 RunLEDService(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
switch (CurrentState)
{
case InitLEDState:
{
if (ThisEvent.EventType == ES_INIT)
{
while (false == DM_TakeInitDisplayStep());
puts ("LED Service:");
DB_printf ("\rES_INIT received in LED Service\r\n");
CurrentState = Waiting;
} //end if
} //end InitPState case
break;

case Waiting:
{
switch (ThisEvent.EventType)
{
// case ES_NEW_KEY:
// {
// printf("ES_NEW_KEY received with -> %c <- in Service 1\r\n",
// (char)ThisEvent.EventParam);
// DM_ScrollDisplayBuffer(4);
// DM_AddChar2DisplayBuffer(ThisEvent.EventParam);
// CurrentState = Writing;
// ThisEvent.EventType = ES_SEND;
// PostLEDService(ThisEvent);
// } //end New key case
// break;

break;
case ES_UPDATE_COL:
{
uint8_t WhichRow;
uint32_t shift[8];
data = (data << 1) | 0b01; //Creates a right to left progression
for (WhichRow = 0; WhichRow <= 7; WhichRow++)
{
shift[WhichRow] = (data & bullet[WhichRow]) << (31 -
ThisEvent.EventParam);
}
for (WhichRow = 0; WhichRow <= 7; WhichRow++)
{
DM_PutDataIntoBufferRow(shift[WhichRow], WhichRow);
}
CurrentState = Writing;
ThisEvent.EventType = ES_SEND;
PostLEDService(ThisEvent);
}
break;

case ES_BEGIN:
{
uint8_t WhichRow;
for (WhichRow = 0; WhichRow <= 7; WhichRow++)
{
DM_PutDataIntoBufferRow(0, WhichRow);
}
CurrentState = Writing;
ThisEvent.EventType = ES_SEND;
PostLEDService(ThisEvent);

//disable scrolling message


ES_Timer_StopTimer(SCROLL_TIMER);
}
break;

case ES_ACTIVITY_TIMEOUT:
{
data = 0x00; //Reset display
ES_Timer_InitTimer(SCROLL_TIMER, 1000); //Reset Scroll Timer
messageIndex = 0;
sprintf(scoreChar, "%d", score); //Convert score to char array
printf("%c",scoreChar); //double checking

strcat(endMessage,scoreChar);

}
break;

case ES_ADD_SCORE:
{
score = score + ThisEvent.EventParam;
}
break;
case ES_TIMEOUT:
{
uint8_t messageSize = strlen(endMessage);

if (messageIndex < messageSize)


{
DM_ScrollDisplayBuffer(4);
DM_AddChar2DisplayBuffer(endMessage[messageIndex]);
ThisEvent.EventType = ES_SEND;
PostLEDService(ThisEvent);
messageIndex++;
ES_Timer_InitTimer(SCROLL_TIMER, 1000); //Reset Scroll Timer
CurrentState = Writing;
}

}
break;

default:
{}
break;
} //end Event switch
} //end Waiting case
break;
case Writing:
{
switch (ThisEvent.EventType)
{
case ES_SEND:
{
if (false == DM_TakeDisplayUpdateStep())
{
PostLEDService(ThisEvent);
}
else
{
CurrentState = Waiting;
ES_RecallEvents(MyPriority, DeferralQueue);
}
} //end Send case
break;

case ES_NEW_KEY:
{
ES_DeferEvent(DeferralQueue, ThisEvent);
}
break;

case ES_UPDATE_COL:
{
ES_DeferEvent(DeferralQueue, ThisEvent);
}
break;

case ES_ADD_SCORE:
{
ES_DeferEvent(DeferralQueue, ThisEvent);
}
break;
default:
;
} //end Event switch
} //end Writing case
break;
default:
;
} //end State switch
return ReturnEvent;
}

/***************************************************************************
private functions
***************************************************************************/

/*------------------------------- Footnotes -------------------------------*/


/*------------------------------ End of file ------------------------------*/

You might also like