Skip to content

Instantly share code, notes, and snippets.

@michaellin
Created December 1, 2015 04:22
Show Gist options
  • Save michaellin/041285f7cfc2f3ce8cfe to your computer and use it in GitHub Desktop.
Save michaellin/041285f7cfc2f3ce8cfe to your computer and use it in GitHub Desktop.
/***Service module for the main state machine of our project***/
// Libraries to Include
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "PWM12Tiva.h"
#include "SR_for_LCD.h"
#include "LCDMod.h"
#include "LCDService.h"
#include "UncleEdSM.h"
#include "LCDMessage.h"
#include "BucketService.h"
#include "WrenchService.h"
#include "Audio1.h"
#include "Audio2.h"
#include "TivaFunctions.h"
#include "PipeSegment.h"
// ALL_BITS offset variable
#define ALL_BITS (0xff<<2)
// Gameplay variables
#define GoodScore 40
#define GAME_TIME 60000
// Static Variables
static uint8_t MyPriority;
static uint8_t CurrentLEDState;
static EdState_t CurrentState;
static uint8_t CurrentCelebState;
static uint8_t CelebCounter;
static uint8_t HourGlassPosition;
static uint8_t score;
static uint8_t ScoreCounter;
/**************************************************************
Function: InitUncleEd
Takes: 8 bit integer (priority number)
Returns: true or false
Purpose: Passes in priority number, initializes Tiva pins, and moves into state machine.
/*************************************************************/
bool InitUncleEd (uint8_t Priority){
// Initialize ThisEvent
ES_Event ThisEvent;
//Initialize MyPriority static variable with passed in parameter
MyPriority = Priority;
//Initialize CurrentLEDState = low
CurrentLEDState = 0;
//Initialize HourGlassPosition = low
HourGlassPosition = 0;
// Initialize bit 3 on port A
HWREG(GPIO_PORTA_BASE+GPIO_O_DEN) |= (GPIO_PIN_3);
HWREG(GPIO_PORTA_BASE+GPIO_O_DIR) |= (GPIO_PIN_3);
// Initialize bit 3 on port B (5V line)
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= (GPIO_PIN_3);
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) |= (GPIO_PIN_3);
// Set CurrentState to InitModeUncleEd
CurrentState = InitModeUncleEd;
// Post ES_Init event to UncleEd service
ThisEvent.EventType = ES_INIT;
PostUncleEd(ThisEvent);
return true;
}
/**************************************************************
Function: PostUncleEd
Takes: ES_Event
Returns: true or false
Purpose: Posts events to this service's queue.
/*************************************************************/
bool PostUncleEd(ES_Event ThisEvent){
return ES_PostToService(MyPriority, ThisEvent);
}
/**************************************************************
Function: RunUncleEd
Takes: ES_Event
Returns: ES_Event (ES_NO_EVENT)
Purpose: Runs Uncle Ed state machine.
/*************************************************************/
ES_Event RunUncleEd(ES_Event ThisEvent){
switch (CurrentState){
case InitModeUncleEd:
if (ThisEvent.EventType == ES_INIT){
// Move into ArmedMode state
CurrentState = ArmedMode;
//Set Ed Timer to 10 ms
ES_Timer_InitTimer(ED_TIMER,10);
}
break;
case ArmedMode:
if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == ED_TIMER)){
//Send tunes to audio services
ThisEvent.EventType = ES_PLAY_TUNE;
ThisEvent.EventParam = ARMedMode;
PostAudio1(ThisEvent);
PostAudio2(ThisEvent);
//Set 5V line low
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) &= (BIT3LO);
//Command Ed's Arm Servo to point ed's arm towards start button
PWM_TIVA_SetPeriod(25000,1);
PWM_TIVA_SetPulseWidth(1150,2);
//Set StartButtonLED timer to 750 ms
ES_Timer_InitTimer(START_BUTTON_TIMER,750);
}
else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == START_BUTTON_TIMER)){
if (CurrentLEDState == 1){
//Set Start Button LED pin low (A3 low)
HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA+ALL_BITS)) &= (BIT3LO);
//Set CurrentLEDState = low
CurrentLEDState = 0;
//Set StartButtonLED timer to 750 ms
ES_Timer_InitTimer(START_BUTTON_TIMER,750);
}
else { // CurrentLEDState is 0 (low)
//Set Start Button LED pin high (A3 high)
HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA+ALL_BITS)) |= (BIT3HI);
//Set CurrentLEDState = high
CurrentLEDState = 1;
//Set StartButtonLED timer to 750 ms
ES_Timer_InitTimer(START_BUTTON_TIMER,750);
}
}
else if (ThisEvent.EventType == ES_START_BUTTON_DBDOWN){
// Post debounced start button to all services
ES_PostAll(ThisEvent);
// Send tunes to audio services
ThisEvent.EventType = ES_PLAY_TUNE;
ThisEvent.EventParam = StartButton;
PostAudio1(ThisEvent);
PostAudio2(ThisEvent);
// Reset ScoreCounter
ScoreCounter = 0;
//Set Ed Timer to GAME_TIME
ES_Timer_InitTimer(ED_TIMER,GAME_TIME);
if (HourGlassPosition == 0){
//Command HourGlass servo to rotate to high position (0 deg)
PWM_TIVA_SetPeriod(25000,1);
PWM_TIVA_SetPulseWidth(700,3);
//Set HourGlassPosition = high
HourGlassPosition = 1;
}
else { // HourGlassPosition = 1 (high)
//Command HourGlass servo to rotate to low position (180 deg)
PWM_TIVA_SetPeriod(25000,1);
PWM_TIVA_SetPulseWidth(3000,3);
//Set HourGlassPosition = low
HourGlassPosition = 0;
}
//Set Pipe Timer to 16 seconds
ES_Timer_InitTimer(PIPE_TIMER,16000);
//Set Start Button LED pin low
HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA+ALL_BITS)) &= (BIT3LO);
//Set CurrentState = PlayingGame
CurrentState = PlayingGame;
}
break;
case PlayingGame:
//printf("Playing Game");
if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == ED_TIMER)){
// Post GAMEOVER event to all relevant services
ThisEvent.EventType=ES_GAMEOVER;
PostBucketService(ThisEvent); //Added this in
PostWrenchService(ThisEvent);
PostPipeSegment(ThisEvent);
PostAudio1(ThisEvent);
PostAudio2(ThisEvent);
// Store the cumulated score in a static variable
score = ScoreCounter;
// Is score good enough for celebration mode?
if (score<=GoodScore){
// Move to CelebrationMode state
CurrentState = CelebrationMode1;
// Set timer to 10 ms
ES_Timer_InitTimer(ED_TIMER,10);
}
else {
// Move to FailureMode state
CurrentState = FailureMode1;
// Set timer to 10 ms
ES_Timer_InitTimer(ED_TIMER,10);
}
//Command Ed Arm servo to point at LCD
PWM_TIVA_SetPeriod(25000,1);
PWM_TIVA_SetPulseWidth(700,2);
}
else if (ThisEvent.EventType == ES_LOST_POINT){
// Update score on LCD every time a point is lost
ScoreCounter++;
// Send message to LCD via LCDMessage service
ThisEvent.EventType = ES_START_MESSAGE;
ThisEvent.EventParam = ScoreCounter;
PostLCDMessage(ThisEvent);
}
break;
case CelebrationMode1:
//printf("Celebration Mode");
if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == ED_TIMER)){
// Send message to LCD via LCDMessage service
ThisEvent.EventType = ES_CELEB_MESSAGE;
ThisEvent.EventParam = ScoreCounter;
PostLCDMessage(ThisEvent);
//Set 5V line high
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) |= (BIT3HI);
// Send tunes to audio services
ThisEvent.EventType = ES_PLAY_TUNE;
ThisEvent.EventParam = CelebrationMode;
PostAudio1(ThisEvent);
PostAudio2(ThisEvent);
// Set timer to a half second
ES_Timer_InitTimer(START_BUTTON_TIMER,500);
// Set CurrentCelebState = Red
CurrentCelebState = 1;
// Set CelebCounter to 0
CelebCounter = 0;
// Set timer to 30 seconds for celebration mode
ES_Timer_InitTimer(ED_TIMER,30000);
}
else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == START_BUTTON_TIMER)){
if (CelebCounter < 63){ // if 30 seconds haven't passed yet
if (CurrentCelebState == 1){ // if CurrentCelebState is red
//Set LightShowREDLED pin low
LCD_RedLEDOff();
//Set LightShowBLUELED pin high
LCD_BlueLEDOn();
//Command ed's arm to point at the start button
PWM_TIVA_SetPeriod(25000,1);
PWM_TIVA_SetPulseWidth(2000,2);
//Set StartButtonLED timer to a half second minus time taken to move servo (roughly 4 ms)
ES_Timer_InitTimer(START_BUTTON_TIMER,446);
//Set CurrentCelebState to blue (blue = 1)
CurrentCelebState = 0;
}
else {
//Set LightShowBLUELED low
LCD_BlueLEDOff();
//Set LightShowREDLED high
LCD_RedLEDOn();
//Command ed's arm to point at the LCD
PWM_TIVA_SetPeriod(25000,1);
PWM_TIVA_SetPulseWidth(700,2);
//Set StartButtonLED timer to a half second minus time taken to move servo (roughly 4 ms)
ES_Timer_InitTimer(START_BUTTON_TIMER,446);
//Set CurrentCelebState to red (red = 0)
CurrentCelebState = 1;
}
// Increment CelebCounter
CelebCounter++;
}
else { // 30 seconds have passed
// Turn off LEDs
LCD_BlueLEDOff();
LCD_RedLEDOff();
// Move to Ready2Arm state
CurrentState = Ready2Arm;
}
}
break;
case FailureMode1:
if ((ThisEvent.EventType == ES_TIMEOUT) &&(ThisEvent.EventParam == ED_TIMER)){
//Send failure event to LCDMessage service
ThisEvent.EventType = ES_FAIL_MESSAGE;
ThisEvent.EventParam = ScoreCounter;
PostLCDMessage(ThisEvent);
// Send tunes to audio services
ThisEvent.EventType = ES_PLAY_TUNE;
ThisEvent.EventParam = FailureMode;
PostAudio1(ThisEvent);
PostAudio2(ThisEvent);
// Set Timer to 10 seconds for failure mode
ES_Timer_InitTimer(ED_TIMER,10000);
// Move to Ready2Arm state
CurrentState = Ready2Arm;
}
break;
case Ready2Arm:
if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == ED_TIMER)){
// Move to ArmedMode state
CurrentState = ArmedMode;
//Set timer ro 10 ms
ES_Timer_InitTimer(ED_TIMER,10);
}
break;
}
// Return ES_NO_EVENT
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT;
return ReturnEvent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment