Vous êtes sur la page 1sur 4

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

Module
WeatherService.c
Revision
1.0.1
Description
This is the first service for the Lights Service under the
Gen2 Events and Services Framework. (a simple service)
Notes
History
When
Who
-------------- --11/15/16 21:12 lbh

What/Why
-------ME218A Project

****************************************************************************/
/*----------------------------- 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 "LightsService.h"
#include "PWM10Tiva.h"
#include "string.h"
#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"
"driverlib/gpio.h"

// Define PART_TM4C123GH6PM in project

/*----------------------------- Module Defines ----------------------------*/


// these times assume a 1.000mS/tick timing
#define ONE_SEC 976
#define HALF_SEC (ONE_SEC/2)
#define TWO_SEC (ONE_SEC*2)
#define FIVE_SEC (ONE_SEC*5)
//number of lightning flashes
#define TotalLightningFlashes 3
//Rename lightning pin
#define LightningPin GPIO_PIN_3
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this service.They should be functions
relevant to the behavior of this service
*/
/*---------------------------- Module Variables ---------------------------*/
// with the introduction of Gen2, we need a module level Priority variable
/* create new macro to always access all 8 bits */

#define ALL_BITS (0xff<<2)


//Data private to the module: MorseString, the arrays LegalChars and MorseCode
static uint8_t MyPriority;
// add a deferral queue for up to 3 pending deferrals +1 to allow for overhead
static ES_Event DeferralQueue[3+1];
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitWeatherService
Parameters
uint8_t : the priority 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
Latifah Hamzah, 16 Nov 2016
****************************************************************************/
bool InitWeatherService (uint8_t Priority)
{
// initialize deferral queue for testing Deferral function
ES_InitDeferralQueueWith( DeferralQueue, ARRAY_SIZE(DeferralQueue) );
//Initialize the MyPriority variable with the passed in parameter.
MyPriority = Priority;
//Enable Port B (clock)
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R1;
//waits for clock to be ready
while ((HWREG(SYSCTL_PRGPIO) & SYSCTL_PRGPIO_R1) != SYSCTL_PRGPIO_R1);
//assigns bit 3, Port B as a digital I/O line
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= LightningPin;
//sets bit 3, Port B as an output - it controls the lightning
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) |= LightningPin;
//start with lightning off
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~LightningPin;
//sets up timer for lightning
void _HW_Timer_Init(TimerRate_t Rate);
uint16_t ES_Timer_GetTime(void);
return true;
}
/****************************************************************************
Function
PostWeatherService
Parameters
ES_Event 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
Latifah Hamzah, 16 Nov 2016
****************************************************************************/
bool PostWeatherService( ES_Event ThisEvent )
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunWeatherService
Parameters
ES_Event : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here
Notes
Author
Latifah Hamzah, 16 Nov 2016
****************************************************************************/
ES_Event RunWeatherService(ES_Event ThisEvent){
//EventType is one of: ES_RESET, ES_OPEN, ES_WIND, ES_WELCOME
//Parameter will be the strength of the wind. Other parameters ignored / not
used.
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
//number of lightning flashes that have occurred, initialised to zero
static uint8_t LightningFlash = 0;
//if this event is a wind event
if (ThisEvent.EventType == ES_WIND) {
//
sets fraction of light dimming as a function of wind parameter
WeatherModulator = (255-ThisEvent.EventParam)/ThisEvent.EventParam;
}
//if this event is a lightning event
if (ThisEvent.EventType == ES_LIGHTNING) {
//
while there have not been the requisite number of lightning flashes
while (LightningFlash <= TotalLightningFlashes){
//
if lightning pin is low
if ((HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) &=
LightningPin) == 0){
//
set lightning on timer as a function of number of lightning
flash
ES_Timer_InitTimer(LightningTimer, LightningFlash*10);
//
turn lightning on
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) |=
LightningPin;
//
increment lightningcount
LightningFlash ++;
}
else { //if lightning pin is high
//
set lightning off timer as a function of number of lightning
flash

//
~LightningPin;

ES_Timer_InitTimer(LightningTimer, LightningFlash*100);
turn lightning off
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) &=
}

}
return ReturnEvent;
}

Vous aimerez peut-être aussi