/* 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

#ifdef	__cplusplus
extern "C" {
#endif

#include "mcc_generated_files/mcc.h"
   

#define START_TIME      0xD2F0U//timer 1/CCP count for 13.5mS
#define START_TIME_MIN  0xB000U //timer 1/CCP count for 11.256mS
#define START_TIME_MAX  0xDF00U//timer 1/CCP count for 14.272mS
#define BIT_TIME_ERROR  0x0300U//timer 1/CCP count for 192 us
#define LOGIC_0         0x1194U//timer 1/CCP count for 1.125mS
#define LOGIC_1         0x2328U//timer 1/CCP count for 2.25mS    
#define BIT_TIME_MAX    0x2600U//timer 1/CCP count for 4.432mS   
   
#define ZERO            (uint8_t)0
    
//commands received for DC motor control
typedef enum  {
    NONE,    
    CMD_START,
    CMD_STOP,
    SPEED_CHANGE = 0x80,
    CMD_SPEED0 = 0x81,
    CMD_SPEED1,   
    CMD_SPEED2,
    CMD_SPEED3, 
    CMD_SPEED4,
    CMD_SPEED5,
    CMD_SPEED6         
}e_commands;

//macros for motor state
#define RUNNING (uint8_t)1
#define HALTED  (uint8_t)0

//macros for motor voltage    
#define MOTORVoltage5V
//#define MOTORVoltage12V
    
#ifdef   MOTORVoltage12V 
#define MIN_SPEED 100
#define MAX_SPEED 399
#define SPEED0    0
#define SPEED1    100   
#define SPEED2    160   
#define SPEED3    220   
#define SPEED4    280   
#define SPEED5    340   
#define SPEED6    400   
#endif 

#ifdef   MOTORVoltage5V 
#define MIN_SPEED 300U
#define MAX_SPEED 600U
#endif 

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

//number of data bits in the command received as per NEC IR protocol    
#define DATA_BIT_COUNT  (uint8_t)32

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

//Application task states
typedef enum  {
GET_EVENT,
DETECT_START,
CAPTURE_DATA_BITS,
DECODE_CMD,
CONTROL_ACTION,
INIT_FOR_NEXT_FRAME        
}e_app_states;

typedef enum {
    CMD_DECODED,
    DATA_BIT_ERR,       
    RX_ADDR_ERR,
    RX_CMD_ERR    
}e_cmd_status;
    
typedef union {
    uint32_t data;
    struct {
        uint8_t nCommand;
        uint8_t command;
        uint8_t nAddress;
        uint8_t address;
    };
}IR_DATA;

/**
 * @Param
    none
 * @Returns
    none
 * @Description
     main application function being executed in while loop
 * @Example
    ApplicationTask();
 */
void ApplicationTask(void);

#ifdef	__cplusplus
}
#endif

#endif	/* APPLICATION_H */

