/* Microchip Technology Inc. and its subsidiaries.  You may use this software 
 * and any derivatives exclusively with Microchip products. 
 * 
 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS".  NO WARRANTIES, WHETHER 
 * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 
 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 
 * PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION 
 * WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION. 
 *
 * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, 
 * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND 
 * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS 
 * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE 
 * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS 
 * IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF 
 * ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
 *
 * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE 
 * TERMS. 
 */

#ifndef APPLICATION_H
#define	APPLICATION_H
#include "mcc_generated_files/mtouch/mtouch_button.h"
#ifdef	__cplusplus
extern "C" {
#endif

//IR receiver Address
#define IR_RX_ADDRESS   (uint8_t)0xAA  

//motor state
#define RUNNING 1
#define HALTED 0

//commands to be sent to the IR receiver for DC motor control
typedef enum  {
NONE,    
MOTOR_START,
MOTOR_STOP,
SPEED_0 = 0x81,
SPEED_1,   
SPEED_2,
SPEED_3, 
SPEED_4,
SPEED_5,
SPEED_6         
}e_commands;

//number of data bits for sending command using NEC IR protocol
#define MAXCOMMANDBITS 32
#define MAXCOMMANDBITS_1 31

//used in reversing the bits from MSb to LSb 
#define UPPER_NIBBLE 0xF0
#define LOWER_NIBBLE 0x0F
#define BIT7632 0xCC
#define BIT5410 0x33
#define BIT7531 0xAA
#define BIT6420 0x55

// DSM modulation source
#define SRC_SW_BIT 0x01    
#define SRC_CLC1_OUT 0x10
#define SRC_CLC5_OUT 0x14
    
#define BIT31_HIGH 0x80000000UL   
#define ZERO 0

#define MARK_DURATION 2// used for counting timer 0 overflows for sending MARK of 9 ms duration= 2 timer 0 overflows of 4.5ms each 
    
//used for setting motor speed according to the slider position
#define SLIDER_COUNT_MIN 20
#define SLIDER_COUNT_1 50
#define SLIDER_COUNT_2 86
#define SLIDER_COUNT_3 129
#define SLIDER_COUNT_4 172
#define SLIDER_COUNT_MAX 215  

//Application task states
typedef enum {
    GET_EVENT,
    PREPARE_CMD,
    PREPARE_IR_NEC_DATA,                
    SEND_DATA_OVER_IR,
    COMM_ERR    
}e_app_state; 

//IR data transmission states
typedef enum {
START_TMR0,
START_BIT_MARK,
START_BIT_SPACE,
LOGIC_1,
LOGIC_0,
STOP_BIT,
STOP_FRAME       
}e_IrTxState;

typedef enum {
  ERROR = 0xFF,
  SUCCESS = 1        
}e_status;  
    

typedef enum {
    KEY_NO_ACTION,
    KEY_PRESSED,
    KEY_RELEASED,                
    MAX_KEY_STATE
}e_key_state;

typedef enum {
    NO_KEY,
    SW1,
    SW2,
    SLIDER,
    MAX_KEYS
}e_key;

typedef struct {
    e_key key;
    e_key_state keyStatus;
    uint16_t sliderADC_Val;
}key_t;  

/**
   @Param
    button number
   @Returns
    none
   @Description
    callback function for button press 
 *  Currently it does nothing the code can be added if required
   @Example 
    ProcessButtonTouch(button1);   
 */    
void ProcessButtonTouch(enum mtouch_button_names button);

/**
   @Param
    none
   @Returns
    button number
   @Description
    callback function for button release
    if button 1 is released then motor start command is sent and button LED will be turned ON
    if button 2 is released then motor stop command is sent and button LED will be turned ON
   @Example
    ProcessButtonRelease(button1);
 */
void ProcessButtonRelease(enum mtouch_button_names button);

/**
   @Param
    none
   @Returns
    button number
   @Description
    callback function for slider change
    slider is used to increment or decrement the motor speed
   @Example    
 */
void ProcessSliderChange(enum mtouch_slider_names slider);

/**
 * @Param
    none
 * @Returns
    none
 * @Description
     main application function being executed in while loop
 * check for button touch or slider change event, prepare command according to the event,
 * prepare 32 bit data as per NEC IR protocol, send IR frame for the command using DSM
 * @Example
    ApplicationTask();
 */    
void ApplicationTask(void);

#ifdef	__cplusplus
}
#endif

#endif	/* APPLICATION_H */

