/* 
* classCanApplication.h
*
* Created: 07/02/2020 14:36:42
* Author: Ledo
* Using CAn Bus to comunicate with the boards 
* "Can Motor Board" and "Can IO Board" from
* www.Ledoelectronics.com
* and WAGO PLC 750-337
*/


#ifndef __CLASSCANAPPLICATION_H__
#define __CLASSCANAPPLICATION_H__

#include "classCan.h"
#include "classGraphics.h"
#include "classAD7843.h"
#include "classKeyboard.h"
#define CAN_COMM_TXMB_ID             0
#define CAN_TX_PRIO                  14

#define CAN_COMM_RXMB_IO_ID          1
#define CAN_COMM_RXMB_WAGO_ID        3
#define CAN_COMM_RXMB_MOTOR_ID       5

#define CAN_NODES                    3
#define CAN_MOTOR_ID                 1541
#define CAN_IO_ID                    1537


/****************************************************
* WAGO 750-337 FIELD BUS CONTRLER
****************************************************/
#define WAGO_ID                        2 //Jumper setting
#define CAN_WAGO_RXSDO_COB_ID          1536 + WAGO_ID
#define CAN_WAGO_TXSDO_COB_ID          1408 + WAGO_ID
#define CAN_WAGO_RXPDO1_COB_ID         512 + WAGO_ID
#define CAN_WAGO_TXPDO1_COB_ID         384 + WAGO_ID

#define WAGO_750_337_INPUTS_INDEX      0x2000
#define WAGO_750_337_OUTPUTS_INDEX     0x2100
//CAN Open nODE Cmds
#define WAGO_GO_START                  0x01
#define WAGO_GO_STOP                   0x02
#define WAGO_GO_PREOPERATIONAL         0x80
#define WAGO_GO_RESET_NODE             0x81
#define WAGO_GO_RESET_COMMUNICATION    0x82
//CAN Open Node Status
#define WAGO_INITIALIZING              0x00
#define WAGO_STOPED                    0x04
#define WAGO_OPERATIONAL               0x05
#define WAGO_PREOPERATIONAL            0x7f


/** CAN frame max data length */
#define MAX_CAN_FRAME_DATA_LEN       8


/* Union para convertir Int32 to byte array */
typedef union UL2B
{
	uint32_t data32;
	uint8_t bytes[4];
}UL2B;

typedef enum WAGO_CMD
{
	NodeStart = 1,
	NodeStop,
	NodeReset,
	InputsReadSDO,
	InputsReadPDO,
	OutputSetSDO,
	OutputSetPDO
}WAGO_CMD;

typedef struct WAGO_INPUTS
{
	bool I0;
	bool I1;
	bool I2;
	bool I3;
	bool I4;
	bool I5;
	bool I6;
	bool I7;
	bool I8;
	bool I9;
	bool I10;
	bool I11;
	uint8_t inputs0;
	uint8_t inputs1;

}WAGO_INPUTS;

typedef struct WAGO_OUTPUTS
{
	bool Q0;
	bool Q1;
	bool Q2;
	bool Q3;
	bool Q4;
	bool Q5;
	bool Q6;
	bool Q7;
	uint8_t outputs;

}WAGO_OUTPUTS;

typedef enum CAN_IO_CMD
{
	Outs_set = 1,
	Outs_clr,
	Outs_update_all,
	Read_IO_vars
}CAN_IO_CMD;

/* Mapeado de las entradas digitales */
typedef enum INPUTS
{
	INPUT0 = (1 << 0),
	INPUT1 = (1 << 1),
	INPUT2 = (1 << 2),
	INPUT3 = (1 << 3),
	INPUT4 = (1 << 4),
	INPUT5 = (1 << 5),
	INPUT6 = (1 << 6),
	INPUT7 = (1 << 7),
	INPUT8 = (1 << 8),
	INPUT9 = (1 << 9),
	INPUT10 = (1 << 10),
	INPUT11 = (1 << 11),
	INPUT12 = (1 << 12),
	INPUT13 = (1 << 13),
	INPUT14 = (1 << 14),
	INPUT15 = (1 << 15)

}INPUTS;

/* Mapeado de las salidas digitales */
typedef enum OUTS
{
	OUT0 = (1 << 0),
	OUT1 = (1 << 1),
	OUT2 = (1 << 2),
	OUT3 = (1 << 3),
	OUT4 = (1 << 4),
	OUT5 = (1 << 5),
	OUT6 = (1 << 6),
	OUT7 = (1 << 7),
	OUT8 = (1 << 8),
	OUT9 = (1 << 9),
	OUT10 = (1 << 10),
	OUT11 = (1 << 11),
	OUT12 = (1 << 12),
	OUT13 = (1 << 13),
	OUT14 = (1 << 14),
	OUT15 = (1 << 15),
	OUT16 = (1 << 16)
	
}OUTPUTS;

typedef struct INPUTS_STATE
{
	bool bInput0;
	bool bInput1;
	bool bInput2;
	bool bInput3;
	bool bInput4;
	bool bInput5;
	bool bInput6;
	bool bInput7;
	bool bInput8;
	bool bInput9;
	bool bInput10;
	bool bInput11;
	bool bInput12;
	bool bInput13;
	bool bInput14;
	bool bInput15;
	uint8_t PuertoA;
	uint8_t PuertoB;

}INPUTS_STATE;

typedef struct OUTPUTS_STATE
{
	bool bOutput0;
	bool bOutput1;
	bool bOutput2;
	bool bOutput3;
	bool bOutput4;
	bool bOutput5;
	bool bOutput6;
	bool bOutput7;
	bool bOutput8;
	bool bOutput9;
	bool bOutput10;
	bool bOutput11;
	bool bOutput12;
	bool bOutput13;
	bool bOutput14;
	bool bOutput15;
	bool bOutput16;
	uint8_t PuertoC;
	uint8_t PuertoD;
	uint8_t PuertoR;
	
}OUTPUTS_STATE;

typedef enum CAN_MOTOR_CMD
{
	Motor_On = 1,
	Motor_Off,
	Rpm_set,
	Power_set,
	Dir_left,
	Dir_right,
	Brake_On,
	Brake_Off,
	Set_params,
	Read_vars
}CAN_MOTOR_CMD;

typedef struct MOTOR
{
	bool Enable;
	bool Brake;
	bool Dir;
	volatile uint8_t Speed;
	uint8_t Power;
	uint16_t T;
	bool Hall;
	bool CanBus_Ok;
}MOTOR;




extern classCan myCan;
extern classGraphics Gui;
extern classAD7843 Touch;
extern classKeyboard Keys;
class classCanApplication
{
//variables
public:
       can_mb_conf_t tx_mailbox;
       can_mb_conf_t io_rx_mailbox;
	   can_mb_conf_t wago_rx_mailbox;
       can_mb_conf_t motor_rx_mailbox;
	   CAN_IO_CMD CAN_IO_Cmd;
	   volatile INPUTS_STATE InputsState;
       OUTPUTS_STATE OutsStateSet;
       OUTPUTS_STATE OutsStateRead;
	   uint32_t Outputs_mask;
	   CAN_MOTOR_CMD CAN_Motor_Cmd;
	   uint8_t Motor_cmdData;
	   MOTOR MotorRead;
	   WAGO_INPUTS Wago_inputs;
	   WAGO_OUTPUTS Wago_outputs;
	   WAGO_CMD WagoCmd;
	   uint8_t wago_sIndex;
       uint16_t wago_data;
protected:
private:
      UL2B ul2byte;
      uint8_t CAN_ring_counter;
	  bool wagoScreen;
	  bool ioScreen;
	  bool motorScreen;
	  bool motor_speed_set;

//functions
public:
	classCanApplication();
	~classCanApplication();
	//Application Functions
	void Can_initialize(void);
	void decode_can_msg_motor(void);
	void decode_can_msg_IO(void);
	void decode_can_msg_WAGO(void);
	void CAN_Loop(void);
    void wagoPage_load(void);
	void ioPage_load(void);
    void motorPage_load(void);

protected:
private:
	classCanApplication( const classCanApplication &c );
	classCanApplication& operator=( const classCanApplication &c );
	void send_cmd2_motor(CAN_MOTOR_CMD cmd,uint8_t data);
	void send_cmd2_IO(CAN_IO_CMD cmd, uint32_t mask);
	void send_cmd2_Wago(WAGO_CMD cmd,uint8_t sIndex, uint8_t data);
    void drawBox(int num,int x, int y, int width,int height,bool state);
	void drawButton(char *string, int16_t x, int16_t y,int width,
	int heigh,uint16_t forecolor,
	uint16_t backcolor,uint16_t bcolor);
	void wagoPage_Update(void);
	void ioPage_Update(void);
	void motorPage_Update(void);
	bool outputBoxPressed(int x, int y);
    bool runKey(void);
    bool stopKey(void);
    bool rwKey(void);
    bool speedInput(void);
    bool speedKey(void);
}; //classCanApplication

#endif //__CLASSCANAPPLICATION_H__
