Sei sulla pagina 1di 10

/* PlantCheckers.

c -- This file defines Plant IR reciver input pins and defines


the Light Event Checkers.
History
When
Who
-------------- --11/14/16 03:25 mep

What/Why
-------Created

*/
/*
*****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for the framework and this service
*/
#include
#include
#include
#include

"ES_Configure.h"
"ES_Framework.h"
"ES_DeferRecall.h"
"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"
"driverlib/gpio.h"
"driverlib/timer.h"
"driverlib/interrupt.h"

// Define PART_TM4C123GH6PM in project

#include "Plants.h"
#include "BITDEFS.h"
#include "SceneSM.h"
#ifndef ALL_BITS
#define ALL_BITS (0xff<<2)
#endif
#define PLANT1_INPUT BIT3HI
#define PLANT2_INPUT BIT4HI
#define PLANT3_INPUT BIT5HI
//Module variables
//************************************
//Required: Static uint16_t TimeOfLastRise1,TimeOfLastFall1
static uint16_t TimeOfLastRise1;
static uint16_t TimeOfLastFall1;
static uint16_t TimeOfLastRise2;
static uint16_t TimeOfLastFall2;
static uint16_t TimeOfLastRise3;
static uint16_t TimeOfLastFall3;
static uint16_t LastInputState1=0;
static uint16_t LastInputState2=0;
static uint16_t LastInputState3=0;
static bool Plant1State=0;
static bool Plant2State=0;
static bool Plant3State=0;
static uint8_t MyPriority;

static PState_t CurrentState;


//~~~~~~~~~~~~~~~~~~~~~~//
///SERVICE DEFINITION ///
//~~~~~~~~~~~~~~~~~~~~~~//
bool PlantsInit(uint8_t Priority){
MyPriority=Priority;
CurrentState=Plants_Init;
Input_Plant_Init();
//puts("initilalized plants");
ES_Event ThisEvent;
ThisEvent.EventType = ES_INIT;
if (ES_PostToService( MyPriority, ThisEvent) == true)
{
return true;
}else
{
return false;
}
}
bool PostPlants(ES_Event ThisEvent){
//
set up post function
return ES_PostToService(MyPriority, ThisEvent);
}
ES_Event RunPlants(ES_Event ThisEvent){
ES_Event ReturnEvent;
ReturnEvent.EventType=ES_NO_EVENT;
PState_t NextState=CurrentState;
ES_Event Event2Post;
switch (CurrentState) {
case (Plants_Init):
if(ThisEvent.EventType == ES_INIT) {
NextState = Plants_Idle;
}
break;
case(Plants_Idle): {
if ((ThisEvent.EventType==ES_GROW)&&(ThisEvent.EventParam==0))
{

NextState=Plants_Waiting_Rise;
puts("Plants_Service_GO!!!");
}
}
break;
case(Plants_Waiting_Rise):{
if(ThisEvent.EventType == ES_RISING_EDGE_PLANT_1) {
TimeOfLastRise1 = ThisEvent.EventParam;
NextState = Plants_Waiting_Fall;
//puts("plant1");
}

if(ThisEvent.EventType == ES_RISING_EDGE_PLANT_2) {
TimeOfLastRise2 = ThisEvent.EventParam;
NextState = Plants_Waiting_Fall;
//puts("plant2");

if(ThisEvent.EventType == ES_RISING_EDGE_PLANT_3) {
TimeOfLastRise3 = ThisEvent.EventParam;
NextState = Plants_Waiting_Fall;
//puts("plant3");
}
if(ThisEvent.EventType==ES_RESET){
//tells all plants that they are no longer
grown (motors must actually pull them down on ES_RESET, this assumes motor
service will do this on ES_RESET)
PlantConstantReset();
//puts("plants RESET");
NextState=Plants_Idle;
//RESET ALL PLANT RELATED STATICS!
}
}
break;
case(Plants_Waiting_Fall):{
uint8_t PlantNum = 0;
if(ThisEvent.EventType == ES_FALLING_EDGE_PLANT_1) {
TimeOfLastFall1 = ThisEvent.EventParam;
NextState = Plants_Waiting_Rise;
}
if(ThisEvent.EventType == ES_FALLING_EDGE_PLANT_2) {
TimeOfLastFall2 = ThisEvent.EventParam;
NextState = Plants_Waiting_Rise;
}
if(ThisEvent.EventType == ES_FALLING_EDGE_PLANT_3) {
TimeOfLastFall3 = ThisEvent.EventParam;
NextState = Plants_Waiting_Rise;
}
PlantNum=BeaconSpotted();
// printf("%d",PlantNum);
if (PlantNum !=0){
//puts("\n\r grow!");
//printf("%u",PlantNum);
Event2Post.EventType=ES_GROW;
Event2Post.EventParam=PlantNum;
//Post To the Motor Service responsible for raising the

plants.

// ADDED POST TO SCENESM


PostSceneSM(Event2Post);
if(Plant1State&&Plant2State&&Plant3State==1){
//puts("allplantsgrown");
NextState=Plants_Idle;
}
}
if(ThisEvent.EventType==ES_RESET){
//tells all plants that they are no longer
grown (motors must actually pull them down on ES_RESET, this assumes motor
service will do this on ES_RESET)
PlantConstantReset();
NextState=Plants_Idle;
//RESET ALL PLANT RELATED STATICS!
}
}
break;

}
CurrentState=NextState;
return ReturnEvent;
}
/****************************************************************************
Function
PlantConstantReset
Parameters
void
Returns
nothing
Description

Sets all static plant constants back to their initial values

Notes
Author

Maxwell Perham, 11/14/2016


****************************************************************************/
void PlantConstantReset(void){
TimeOfLastRise1=0;
TimeOfLastFall1=0;
TimeOfLastRise2=0;
TimeOfLastFall2=0;
TimeOfLastRise3=0;
TimeOfLastFall3=0;
LastInputState1=0;
LastInputState2=0;
LastInputState3=0;
Plant1State=false;
Plant2State=false;
Plant3State=false;
}
/****************************************************************************
Function
BeaconSpotted
Parameters
void
Returns

uint8_t PlantNum = This is 0,1,2, or 3. This number tells us which


plant has seen the
correct IR light beacon. If no plant has detected the light or all
are grown, this will
output a zero.
Description
Beacon Checker looks for our 66.6 hz signal that is being emmited by
our IR 555 timer circuit,
if we dont see this specific signal, we know that the flashlight has
not been pointed at our circuit.
Notes
Author

Maxwell Perham, 11/14/2016

****************************************************************************/
uint8_t BeaconSpotted(void){
uint16_t LastPulseWidth1;
uint16_t LastPulseWidth2;
uint16_t LastPulseWidth3;
uint8_t PlantNum = 0;
//puts("\n\r looking for the beacon gonna grow some plaaannttts");
uint16_t LowerTime=5; //assuming microseconds, this is the high time
uint16_t HigherTime=10;
//puts("\r\n beacon Spotted");
if(Plant1State==0){
//Checks the Current State of
Plant1, if it hasnt grown, it checks for an incoming signal
LastPulseWidth1=(TimeOfLastFall1 - TimeOfLastRise1);
//checking incoming signal
if(LastPulseWidth1>LowerTime &&LastPulseWidth1<HigherTime){ //is it
our signal with a high time of ~7 milliseconds?
PlantNum=1;
//first plant has seen the liiiighhht!
Plant1State=1;
//plant 1 has grown, dont try this again until plant 1 has died
//puts("plant1 LIVESSS");
}
}
if(Plant2State==0){
//Checks the Current State of Plant2,
if it hasnt grown, it checks for an incoming signal
LastPulseWidth2=(TimeOfLastFall2 - TimeOfLastRise2);
//checking incoming signal
//printf("\r\n PW2= %u",LastPulseWidth2);
//puts("made it here");
if(LastPulseWidth2>LowerTime &&LastPulseWidth2<HigherTime){ //is it
our signal with a high time of ~7 milliseconds?
PlantNum=2;
//second plant has seen the liiiighhht! Praise the sun!
Plant2State=1;
//plant 2 has grown, dont try this again until plant 2 has died
//puts("plant2 LIVESSS");
}
}
if(Plant3State==0){
//Checks the Current State of Plant3,
if it hasnt grown, it checks for an incoming signal
LastPulseWidth3=(TimeOfLastFall3 - TimeOfLastRise3);
//checking incoming signal
if(LastPulseWidth3>LowerTime &&LastPulseWidth3<HigherTime){ //is it
our signal with a high time of ~7 milliseconds?
PlantNum=3;
//Third plant is a happy plant
Plant3State=1;
//plant 3 has grown, dont try this again until plant 3 is dead
//puts("plant3 LIVESSS");

return PlantNum;
}
/****************************************************************************
Function
Input_Plant_Init
Parameters
void
Returns
nothing
Description

Sets Up pins 0,1,and 2 on port A as digital inputs

Notes
Author

Maxwell Perham, 11/14/2016


****************************************************************************/
void Input_Plant_Init(void){
HWREG(SYSCTL_RCGCGPIO)|=SYSCTL_RCGCGPIO_R0;
while ((HWREG(SYSCTL_PRGPIO)&SYSCTL_PRGPIO_R0)!=SYSCTL_PRGPIO_R0) {}
HWREG(GPIO_PORTA_BASE+GPIO_O_DEN)|=(PLANT1_INPUT|PLANT2_INPUT|
PLANT3_INPUT);
HWREG(GPIO_PORTA_BASE+GPIO_O_DIR)&=~(PLANT1_INPUT|PLANT2_INPUT|
PLANT3_INPUT);
HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA+ALL_BITS))&=~(PLANT1_INPUT|
PLANT2_INPUT|PLANT3_INPUT);
}
/****************************************************************************
Function
Input_PlantPin
Parameters
uint8_t WhichPlant
Returns
bool Hi or low?
Description

outputs desired input pin status

Notes
Author

Maxwell Perham, 11/14/2016


****************************************************************************/
bool Input_PlantPin(uint8_t WhichPlant){
if(WhichPlant==1){
uint8_t input=0x00;
//puts("looking at plant1pin");
input=(HWREG(GPIO_PORTA_BASE+
(GPIO_O_DATA+ALL_BITS))&PLANT1_INPUT);
bool output=0;
//printf(("My unsigned integer: %u \r\n"), input);
if((input != 0)){
output=1;
//puts("\r\n hi input \r\n");

}
else{output=0;
//puts("\r\n 0 input \r\n");
}
//figure out how to read pins.
return output; //pin state.
}
if(WhichPlant==2){
uint8_t input=0x00;
input=(HWREG(GPIO_PORTA_BASE+
(GPIO_O_DATA+ALL_BITS))&PLANT2_INPUT);
bool output=0;
//printf(("My unsigned integer: %u \r\n"), input);
if((input!=0)){
output=1;
//puts("\r\n hi input \r\n");
}
else{output=0;
//puts("\r\n 0 input \r\n");
}
//figure out how to read pins.
return output; //pin state.
}
if(WhichPlant==3){
uint8_t input=0x00;
input=(HWREG(GPIO_PORTA_BASE+
(GPIO_O_DATA+ALL_BITS))&PLANT3_INPUT);
bool output=0;
//printf(("My unsigned integer: %u \r\n"), input);
if((input!=0)){
output=1;
//puts("\r\n hi input \r\n");
}
else{output=0;
//puts("\r\n 0 input \r\n");
}
//figure out how to read pins.
return output; //pin state.
}
}

puts("error 379 plant service");


return 7;

//~~~~~~~~~~~~~~~~~~~~~~//
///EVENT CHECKERS BELOW///
//~~~~~~~~~~~~~~~~~~~~~~//
/****************************************************************************
Function
CheckLightPlant1Events
Parameters
void
Returns
bool = a bool indicating if event happened (true) or not (false).

Description

Event Checker for Plant 1 IR detector

Notes
Author

Maxwell Perham, 11/14/2016


****************************************************************************/
bool CheckLightPlant1Events(void){
//Takes no EventParameters, returns true if an event was posted (11/4/11 jec)
bool ReturnVal = false;
bool CurrentInputState;
ES_Event EdgeEvent;
CurrentInputState=Input_PlantPin(1); //gets status of plant 1 (pin 2 on port A)
if(CurrentInputState != LastInputState1){ //ifthe state of the plant input line
has changed
if(CurrentInputState==1){ //the current state of the input line is
high

VALUE

EdgeEvent.EventType=ES_RISING_EDGE_PLANT_1;
TimeOfLastRise1=ES_Timer_GetTime();
//printf(("Plant 1 Rise Time= %u"), TimeOfLastRise1);
EdgeEvent.EventParam=TimeOfLastRise1;
//TIMER

PostPlants(EdgeEvent);// with EventParameter of the Current


Time //Where too?
}
if(CurrentInputState==0){
EdgeEvent.EventType=ES_FALLING_EDGE_PLANT_1;
TimeOfLastFall1=ES_Timer_GetTime();
//printf(("\n\rPlant 1 Fall Time= %u"), TimeOfLastFall1);
EdgeEvent.EventParam=TimeOfLastFall1;
//TIMER
VALUE
PostPlants(EdgeEvent); //with EventParameter of the Current
Time
}
ReturnVal = true;
}
LastInputState1=CurrentInputState;
return ReturnVal;
}
/****************************************************************************
Function
CheckLightPlant2Events
Parameters
void
Returns

bool = a bool indicating if event happened (true) or not (false).

Description
Notes

Event Checker for Plant 2 IR detector

Author
Maxwell Perham, 11/14/2016
****************************************************************************/
bool CheckLightPlant2Events(void){

//Takes no EventParameters, returns true if an event was posted (11/4/11 jec)


bool ReturnVal = false;
bool CurrentInputState;
ES_Event EdgeEvent;
CurrentInputState=Input_PlantPin(2); //gets status of plant 2 (pin 1 on port A)
if(CurrentInputState != LastInputState2){ //ifthe state of the plant input line
has changed
if(CurrentInputState==1){ //the current state of the input line is
high

VALUE

EdgeEvent.EventType=ES_RISING_EDGE_PLANT_2;
TimeOfLastRise2=ES_Timer_GetTime();
//printf(("\n\r Plant 2 Rise Time= %u"), TimeOfLastRise2);
EdgeEvent.EventParam=TimeOfLastRise2;
//TIMER

PostPlants(EdgeEvent);// with EventParameter of the Current


Time //Where too?
}
if(CurrentInputState==0){
EdgeEvent.EventType=ES_FALLING_EDGE_PLANT_2;
TimeOfLastFall2=ES_Timer_GetTime();
//printf(("\n\r Plant 2 Fall Time= %u"), TimeOfLastFall2);
EdgeEvent.EventParam=TimeOfLastFall2;
//TIMER
VALUE
PostPlants(EdgeEvent);
//with
EventParameter of the Current Time
}
ReturnVal = true;
}
LastInputState2=CurrentInputState;
return ReturnVal;
}
/****************************************************************************
Function
CheckLightPlant2Events
Parameters
void
Returns
bool = a bool indicating if event happened (true) or not (false).
Description

Event Checker for Plant 2 IR detector

Notes
Author

Maxwell Perham, 11/14/2016


****************************************************************************/
bool CheckLightPlant3Events(void){
//Takes no EventParameters, returns true if an event was posted (11/4/11 jec)
bool ReturnVal = false;
bool CurrentInputState;
ES_Event EdgeEvent;
CurrentInputState=Input_PlantPin(3);

//gets status of plant 3 (pin 1 on port A)

if(CurrentInputState != LastInputState3){ //if the state of the plant input line

has changed
high

if(CurrentInputState==1){ //the current state of the input line is


EdgeEvent.EventType=ES_RISING_EDGE_PLANT_3;
TimeOfLastRise3=ES_Timer_GetTime();
//printf(("Plant 3 Rise Time= %u"), TimeOfLastRise3);
EdgeEvent.EventParam=TimeOfLastRise3;
//TIMER

VALUE

PostPlants(EdgeEvent);// with EventParameter of the Current


Time //Where too?
}
if(CurrentInputState==0){
EdgeEvent.EventType=ES_FALLING_EDGE_PLANT_3;
TimeOfLastFall3=ES_Timer_GetTime();
//printf(("\n\rPlant 1 Fall Time= %u"), TimeOfLastFall1);
EdgeEvent.EventParam=TimeOfLastFall3;
//TIMER
VALUE
PostPlants(EdgeEvent);
//with
EventParameter of the Current Time
}
ReturnVal = true;
}
LastInputState3=CurrentInputState;
return ReturnVal;
}

Potrebbero piacerti anche