"ES - Configure.h" "ES - Framework.h" "OPAMP.h" "UART.h" "Inputs.h"
"ES - Configure.h" "ES - Framework.h" "OPAMP.h" "UART.h" "Inputs.h"
Module
OPAMP.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 "OPAMP.h"
#include "UART.h"
#include "Inputs.h"
/*----------------------------- Module Defines ----------------------------*/
#define SHIELD_MAX 200
#define HULL_MAX 100
#define OPAMP_UPDATE_TIME 50
#define ONE_SEC 1000
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine.They should be functions
relevant to the behavior of this state machine
*/
// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
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 InitOpamp(uint8_t Priority)
{
ES_Event_t ThisEvent;
MyPriority = Priority;
// put us into the Initial PseudoState
CurrentState = OpampInitP;
// post the initial transition event
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}
/****************************************************************************
Function
PostOpamp
Parameters
EF_Event_t ThisEvent , the event to post to the queue
Returns
boolean 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 PostOpamp(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunTemplateFSM
Parameters
ES_Event_t : the event to process
Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here
Notes
uses nested switch/case to implement the machine.
Author
J. Edward Carryer, 01/15/12, 15:23
****************************************************************************/
ES_Event_t RunOpamp(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
switch (CurrentState)
{
case OpampInitP: // If current state is initial Psedudo State
{
if (ThisEvent.EventType == ES_INIT) // only respond to ES_Init
{
// this is where you would put any actions associated with the
// transition from the initial pseudo-state into the actual
// initial state
shieldLevel = 0;
hullLevel = HULL_MAX;
}break;
case ES_TIMEOUT:
{
if(ThisEvent.EventParam == OPAMP_TIMER){
//get normalized power levels and update capacitors
powerLevels pwrLvlRaw = getPowerLevelInput();
uint16_t total = pwrLvlRaw.navigation + pwrLvlRaw.shields +
pwrLvlRaw.weapons;
if(total == 0){
pwrLvl.navigation = 21;
pwrLvl.shields = 21;
pwrLvl.weapons = 21;
}else{
pwrLvl.navigation = 64*pwrLvlRaw.navigation/total;
pwrLvl.shields = 64*pwrLvlRaw.shields/total;
pwrLvl.weapons = 64*pwrLvlRaw.weapons/total;
}
//set shieldLevels
uint16_t shieldInc = pwrLvl.shields*OPAMP_UPDATE_TIME/ONE_SEC;
if(shieldLevel + shieldInc < SHIELD_MAX){
shieldLevel += shieldInc & 255;
}else{
shieldLevel = SHIELD_MAX;
}
ES_Timer_InitTimer(OPAMP_TIMER, OPAMP_UPDATE_TIME);
}
}break;
case ES_DEAD:
{
CurrentState = OpampDead;
}break;
default:
;
} // end switch on CurrentEvent
}
break;
// repeat state pattern as required for other states
default:
;
} // end switch on Current State
return ReturnEvent;
}
/****************************************************************************
Function
QueryTemplateSM
Parameters
None
Returns
TemplateState_t The current state of the Template state machine
Description
returns the current state of the Template state machine
Notes
Author
J. Edward Carryer, 10/23/11, 19:21
****************************************************************************/
OpampState_t QueryOpamp(void)
{
return CurrentState;
}
/***************************************************************************
getter functions
***************************************************************************/
powerLevels getPowerLevels(void){
return pwrLvl;
}
uint8_t getShieldLevel(void){
return shieldLevel; //return current shield level
}
uint8_t getHullLevel(void){
return hullLevel; //return current hull level
}
/***************************************************************************
private functions
***************************************************************************/