You are on page 1of 3

//SenseWind.c - Module to read analog input from the windmill.

//
Checks state of analog input upon receiving a timer-based event.
/*----------------------------- Include Files -----------------------------*/
/* include header files for the framework and this service
*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include

"inc/hw_memmap.h"
"inc/hw_types.h"
"inc/hw_gpio.h"
"inc/hw_sysctl.h"
"driverlib/sysctl.h"
"driverlib/pin_map.h" // Define PART_TM4C123GH6PM in project
"driverlib/gpio.h"
"driverlib/timer.h"
"driverlib/interrupt.h"

//Include header file for needed files in this module


#include "SenseWind.h"
#include "ReadAnalogInputs.h"
#include "SceneSM.h"
#include "Audio.h"
#include "WeatherService.h"
//Additional includes and definitions
#include "BITDEFS.H"
#include "termio.h"
#define ALL_BITS (0xff<<2) //declare ALL_BITS macro to access all 8 bits on
the port at once
//Module-specific definitions
//******************************************************
#define WINDPORT SYSCTL_RCGCGPIO_R4
#define WINDHI BIT0HI
#define WINDLO BIT0LO
//******************************************************
#define
#define
#define
#define
#define
#define

checkTime 250 //check wind speed every 50ms, or 20 times/second


AnalogNoWind 3688
AnalogMaxWind 1400
MaxWindValue 255
minWindChange 100
lightningWindThreshold 240

//Module-specific variables
//******************************************************
static uint8_t MyPriority;
static WindSMState_t CurrentState;
static uint16_t LastWindValue = AnalogNoWind;
//may want to create other variables to store some time history of the wind

//******************************************************
bool InitializeWindReader (uint8_t Priority){
MyPriority = Priority; //set the service priority
CurrentState = BeforeWindScene; //set initial state to the inactive,
before wind scene state
return true; //assume no errors
}
bool PostWindReaderService( ES_Event ThisEvent){
return ES_PostToService(MyPriority,ThisEvent);
}
ES_Event readWindSpeed(ES_Event ThisEvent){
WindSMState_t NextState = CurrentState;
ES_Event returnValue;
returnValue.EventType= ES_NO_EVENT; //assume no errors
ES_Event newEvent;
switch(CurrentState){
case (BeforeWindScene):{
if(ThisEvent.EventType == ES_OPEN){
//printf("Wind reader received open event in
BWS, next state is windscene");
NextState = WindScene; //set the state to the
active WindScene state
ES_Timer_InitTimer(WIND_READ_TIMER, checkTime);
}
break;
}
case (WindScene):{
uint32_t currentPinState;
uint16_t CurrentWind;
if((ThisEvent.EventType == ES_TIMEOUT) &
(ThisEvent.EventParam == WIND_READ_TIMER)){
currentPinState =
returnAnalogInputValue(WINDHI);
if(currentPinState > AnalogNoWind){ //if wind
is slower than the minimum
currentPinState = AnalogNoWind; //set
it to the minimum
}
if(currentPinState < AnalogMaxWind){ //if wind
is faster than the maximum
currentPinState = AnalogMaxWind; //set
it to the maximum
}
uint16_t windDiff = abs(currentPinStateLastWindValue); //this is to ensure enough of a change has occurred to post a
wind event
CurrentWind = (((float)AnalogNoWind currentPinState))/(AnalogNoWind-AnalogMaxWind)*MaxWindValue; //The current
wind speed out of roughly 255. cast as a float so the division yields a
decimal and not just 0 or 1

new line below

//find the value when motor is off and add a


if(windDiff>minWindChange){
printf("\r\nwindDiff: %d\r\n",

windDiff);
currentPinState);
an ES_WIND event

printf("/r/nanalog: %d/r/n",
newEvent.EventType = ES_WIND; //create

newEvent.EventParam = CurrentWind;
//with a parameter of the analog pin (CHANGE TO A WIND SPEED)
//printf("Wind event with speed
%d/n/r", newEvent.EventParam);
PostSceneSM(newEvent);
LastWindValue = currentPinState;
if(CurrentWind >
lightningWindThreshold){ //if the wind is strong enough to cause lightning
ES_Event lightning;
lightning.EventType =
ES_LIGHTNING;
PostWeatherService(lightning);
PostAudioService(lightning);
}
}
ES_Timer_InitTimer(WIND_READ_TIMER, checkTime);
//start a new timer for the next check
NextState = WindScene; //set the state to the
active WindScene state
}
if(ThisEvent.EventType == ES_GROW){ //if we've finished
the wind scene, go back to the inactive state
//printf("Reset to BWS by grow");
NextState = BeforeWindScene;
}
if(ThisEvent.EventType == ES_RESET){ //if we've reset,
go back to the inactive state
NextState = BeforeWindScene;
//printf("Reset to BWS by reset");
}
break;
}
}
CurrentState = NextState;

return returnValue;

You might also like