/*
 * relay.h
 *
 *  Created on: Jul 16, 2010
 *      Author: epics
 */

#ifndef RELAY_H_
#define RELAY_H_

extern volatile uint8_t relayThresholdEnable_flag;
extern volatile uint8_t relayThresholdCurrentState;

extern volatile uint16_t relayThresholdHigh;/*the maximum gas pressure*/
extern volatile uint16_t relayThresholdLow;/*the minimum gas pressure*/
extern volatile uint8_t relayThresholdInputChannel; /*channel of atmel ADC to get value from*/
extern volatile uint8_t relayThresholdOutputPin; /*pin on PORTA to switch value for relay*/
extern volatile uint8_t relayThresholdLedPin;
extern volatile uint8_t relayThresholdReportCurrentStatusEnable_flag;

extern volatile uint8_t relayThresholds_valid[2];
extern volatile uint8_t relayThresholdRelayValue;
extern volatile uint8_t relayThresholdRelaySetValue;
extern volatile uint8_t relayThresholdOutOfBoundsTransitLock_flag;
extern volatile uint8_t relayThresholdsInBoundsPolarity;
extern volatile uint8_t relayThresholdsOutOfBoundsPolarity;
extern volatile uint8_t relayThresholdsExternalPolarityPinPos;
extern volatile uint8_t relayThresholdsExternalPolarityPort;
extern volatile uint8_t relayThresholdsExternalPolarity;
extern volatile uint8_t relayThresholdInvertPolarity_flag;

enum relayThresholdEnum
{
      relayThreshold_LOW=0,
      relayThreshold_HIGH,
      relayThreshold_MAXIMUM_INDEX
};

extern const char* relayThresholdStateStrings[] PROGMEM;
enum relayThresholdStates
{
   relayThresholdState_IDLE,
   relayThresholdState_BELOW_LOW_THRESHOLD,
   relayThresholdState_INBETWEEN,
   relayThresholdState_ABOVE_HIGH_THRESHOLD,
   relayThresholdState_UNDEFINED,
   relayThresholdState_MAXIMUM_NUMBER
};

extern const char* relayThresholdCommandKeywords[] PROGMEM;
enum relayThresholdCommandKeyNumber
{
      relayThresholdCommandKeyNumber_CURRENT_STATE = 0,
      relayThresholdCommandKeyNumber_CURRENT_VALUE,
      relayThresholdCommandKeyNumber_THR_HIGH,
      relayThresholdCommandKeyNumber_THR_LOW,
      relayThresholdCommandKeyNumber_ENABLE,
      relayThresholdCommandKeyNumber_INPUT_CHANNEL,
      relayThresholdCommandKeyNumber_OUTPUT_PIN,
      relayThresholdCommandKeyNumber_LED_PIN,
      relayThresholdCommandKeyNumber_REPORT_STATE_ENABLE,
      relayThresholdCommandKeyNumber_RELAY_VALUE,
      relayThresholdCommandKeyNumber_THRESHOLDS,
      relayThresholdCommandKeyNumber_STATUS,
      relayThresholdCommandKeyNumber_OUT_OF_BOUNDS_TRANSIT_LOCK,
      relayThresholdCommandKeyNumber_IN_BOUNDS_POLARITY,
      relayThresholdCommandKeyNumber_OUT_OF_BOUNDS_POLARITY,
      relayThresholdCommandKeyNumber_INVERT_POLARITY_LOGIC,
      relayThresholdCommandKeyNumber_EXTERNAL_IN_BOUNDS_POLARITY,
      relayThresholdCommandKeyNumber_EXTERNAL_IN_BOUNDS_PIN_POS,
      relayThresholdCommandKeyNumber_EXTERNAL_IN_BOUNDS_PORT,
      relayThresholdCommandKeyNumber_POLARITY,
      relayThresholdCommandKeyNumber_POLARITY_REINIT,
      relayThresholdCommandKeyNumber_MAXIMUM_NUMBER
};

void relayThreshold(struct uartStruct *ptr_uartStruct);
void relayThresholdMiscSubCommands(struct uartStruct *ptr_uartStruct, int16_t index );

uint8_t relayThresholdInit(void);
uint8_t relayThresholdInBoundPolarityInit(void);
void relayThresholdDetermineStateAndTriggerRelay(void);
void relayThresholdsReportCurrentStatus(void);

uint8_t relayThresholdsSetHighThreshold(uint16_t value);
uint8_t relayThresholdsSetLowThreshold(uint16_t value);
uint8_t relayThresholdsSetEnableFlag(uint8_t value);
uint8_t relayThresholdsSetInputChannel(uint8_t value);
uint8_t relayThresholdsSetOutputPin(uint8_t value);
uint8_t relayThresholdsSetLedPin(uint8_t value);
uint8_t relayThresholdsSetReportCurrentStatusEnableFlag(uint8_t value);
uint8_t relayThresholdsSetRelayValue(uint8_t value);
uint8_t relayThresholdsSetRelaySetValue(uint8_t value);
uint8_t relayThresholdsSetOutOfBoundsTransitLockFlag(uint8_t value);

uint8_t relayThresholdsSetExternalPolarityPinPos(uint8_t value);
uint8_t relayThresholdsSetInvertPolarityFlag(uint8_t value);
uint8_t relayThresholdsSetExternalPolarityPort(uint8_t value);

uint16_t relayThresholdsGetHighThreshold(void);
uint16_t relayThresholdsGetLowThreshold(void);
uint8_t relayThresholdsGetEnableFlag(void);
uint8_t relayThresholdsGetInputChannel(void);
uint8_t relayThresholdsGetOutputPin(void);
uint8_t relayThresholdsGetLedPin(void);
uint8_t relayThresholdsGetCurrentState(void);
uint32_t relayThresholdsGetCurrentValue(void);
uint8_t relayThresholdsGetReportStateEnableFlag(void);
uint8_t relayThresholdsGetRelayValue(void);
uint8_t relayThresholdsGetRelaySetValue(void);
uint8_t relayThresholdsGetOutOfBoundsTransitLockFlag(void);

uint8_t relayThresholdsGetInBoundsPolarity(void);
uint8_t relayThresholdsGetOutOfBoundsPolarity(void);
uint8_t relayThresholdsGetExternalPolarityPinPos(void);
uint8_t relayThresholdsGetExternalPolarityPort(void);
uint8_t relayThresholdsGetExternalPolarity(void);
uint8_t relayThresholdsGetInvertPolarityFlag(void);

#endif /* RELAY_H_ */
