426 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			426 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | #ifndef _IOT_LIGHT_CTRL_DRV_V_H_
 | ||
|  | #define _IOT_LIGHT_CTRL_DRV_V_H_
 | ||
|  | 
 | ||
|  | #define iot_light_get_cmd(frm)  (((uint32_t)(*((uint8_t *)(frm)))) & 0xFF)
 | ||
|  | 
 | ||
|  | /* Channel code. Channel all supported only. */ | ||
|  | #define IOT_LIGHT_CHAN_A      0x13
 | ||
|  | #define IOT_LIGHT_CHAN_B      0x14
 | ||
|  | #define IOT_LIGHT_CHAN_ALL    0x11
 | ||
|  | 
 | ||
|  | /* Device type for LED controller */ | ||
|  | #define DEV_TYPE_LED_040W    0x02
 | ||
|  | #define DEV_TYPE_LED_060W    0x06
 | ||
|  | #define DEV_TYPE_LED_075W    0x0A
 | ||
|  | #define DEV_TYPE_LED_080W    0x12
 | ||
|  | #define DEV_TYPE_LED_090W    0x15
 | ||
|  | #define DEV_TYPE_LED_100W    0x19
 | ||
|  | #define DEV_TYPE_LED_120W    0x1D
 | ||
|  | #define DEV_TYPE_LED_150W    0x27
 | ||
|  | #define DEV_TYPE_LED_185W    0x38
 | ||
|  | #define DEV_TYPE_LED_240W    0x45
 | ||
|  | #define DEV_TYPE_LED_260W    0x48
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |   * @brief Command list for led light controller. Command from PLC to STAs to | ||
|  |   * control led light controller. | ||
|  |   */ | ||
|  | enum _iot_light_command_list_e { | ||
|  |     /*
 | ||
|  |      * Get params like light level, ADC data, power status. | ||
|  |      */ | ||
|  |     CMD_GET_PARAM              = 0x01, | ||
|  |     /*
 | ||
|  |      * Control the A & B led. | ||
|  |      * 0x00 ~ 0xC8 stand for light level from 0 to 100%. | ||
|  |      */ | ||
|  |     CMD_SET_LIGHT_LEVEL_AB     = 0x11, | ||
|  |     /*
 | ||
|  |      * Control the A or B or A & B led in given group. | ||
|  |      * 0x00 ~ 0xC8 stand for light level from 0 to 100%. | ||
|  |      */ | ||
|  |     CMD_SET_LIGHT_LEVEL_GRP    = 0x12, | ||
|  |     /*
 | ||
|  |      * Control the A led. | ||
|  |      * 0x00 ~ 0xC8 stand for light level from 0 to 100%. | ||
|  |      */ | ||
|  |     CMD_SET_LIGHT_LEVEL_A      = 0x13, | ||
|  |     /*
 | ||
|  |      * Control the B led. | ||
|  |      * 0x00 ~ 0xC8 stand for light level from 0 to 100%. | ||
|  |      */ | ||
|  |     CMD_SET_LIGHT_LEVEL_B      = 0x14, | ||
|  |     /*
 | ||
|  |      * Set the group number current controller belongs to. | ||
|  |      */ | ||
|  |     CMD_SET_GROUP              = 0x15, | ||
|  |     /*
 | ||
|  |      * Commands for private used. | ||
|  |      */ | ||
|  |     CMD_PRIVATE_COMMAND        = 0x51, | ||
|  |     /*
 | ||
|  |      * ack for REPORT_POWER_STATE from cco. | ||
|  |      */ | ||
|  |     CMD_REPORT_POWER_STATE_ACK = 0x82, | ||
|  |     /* set correction slope and intercept */ | ||
|  |     CMD_SET_CORRECTION_DATA    = 0xA1, | ||
|  |     /* get correction parameter */ | ||
|  |     CMD_GET_CORRECTION_PARAM   = 0xA2, | ||
|  |     /* set correction parameter */ | ||
|  |     CMD_SET_CORRECTION_PARAM   = 0xA3, | ||
|  | }; | ||
|  | 
 | ||
|  | /**
 | ||
|  |   * @brief Response list for commands in enum _iot_light_command_list_e. | ||
|  |   */ | ||
|  | enum _iot_light_response_list_e { | ||
|  |     /* response code for CMD_GET_PARAM */ | ||
|  |     RESP_GET_PARAM            = 0x81, | ||
|  |     /* response code for CMD_SET_LIGHT_LEVEL_AB */ | ||
|  |     RESP_SET_LIGHT_LEVEL_AB   = 0x91, | ||
|  |     /* response code for CMD_SET_LIGHT_LEVEL_A */ | ||
|  |     RESP_SET_LIGHT_LEVEL_A    = 0x83, | ||
|  |     /* response code for CMD_SET_LIGHT_LEVEL_B */ | ||
|  |     RESP_SET_LIGHT_LEVEL_B    = 0x84, | ||
|  |     /* response code for CMD_SET_GROUP */ | ||
|  |     RESP_SET_GROUP            = 0x95, | ||
|  |     /* response code for CMD_PRIVATE_COMMAND */ | ||
|  |     RESP_PRIVATE_COMMAND      = 0xD1, | ||
|  |     /* report power state to cco */ | ||
|  |     REPORT_POWER_STATE        = 0x2, | ||
|  |     /* set correction data response for CMD_SET_CORRECTION_DATA */ | ||
|  |     RESP_SET_CORRECTION_DATA  = 0x8A, | ||
|  |     /* get correction parameter response for CMD_GET_CORRECTION_PARAM */ | ||
|  |     RESP_GET_CORRECTION_PARAM = 0x8B, | ||
|  |     /* set correction parameter response for CMD_SET_CORRECTION_PARAM */ | ||
|  |     RESP_SET_CORRECTION_PARAM = 0x8C, | ||
|  | }; | ||
|  | 
 | ||
|  | /* reserved bytes on the head and tail when sub-command alloc response packet. */ | ||
|  | #define CMD_PRIVATE_RESPONSE_RESV_BY_HEAD   2 /* OPCODE && SUBOPCODE */
 | ||
|  | #define CMD_PRIVATE_RESPONSE_RESV_BY_TAIL   1 /* CHECKSUM */
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |   * @brief Subfn for CMD_PRIVATE_COMMAND/RESP_PRIVATE_COMMAND. | ||
|  |   */ | ||
|  | enum _iot_light_sub_command_private_command_e { | ||
|  |     /* Get current of leakage. */ | ||
|  |     SUBCMD_GET_LEAKAGE_CURRENT              = 0x00, | ||
|  |     /* Get device infor. */ | ||
|  |     SUBCMD_GET_DEV_INFO                     = 0x01, | ||
|  | }; | ||
|  | 
 | ||
|  | 
 | ||
|  | /* set correction data use subfn */ | ||
|  | enum _iot_light_sub_command_set_correction_data_e { | ||
|  |     SUBCMD_SET_COR_DATA_IN_POWER            = 0x01, | ||
|  |     SUBCMD_SET_COR_DATA_IN_VOLTAGE          = 0x02, | ||
|  |     SUBCMD_SET_COR_DATA_IN_CURRENT          = 0x03, | ||
|  |     SUBCMD_SET_COR_DATA_OUTPUT_CURRENT      = 0x04 | ||
|  | }; | ||
|  | 
 | ||
|  | /* get correction param use subfn */ | ||
|  | enum _iot_light_sub_command_get_correction_param_e { | ||
|  |     SUBCMD_GET_COR_PARAM_METER              = 0x01 | ||
|  | }; | ||
|  | 
 | ||
|  | /* set correction param use subfn */ | ||
|  | enum _iot_light_sub_command_set_correction_param_e { | ||
|  |     SUBCMD_SET_COR_PARAM_LIGHT_LEVEL        = 0x01 | ||
|  | }; | ||
|  | 
 | ||
|  | /* remote platform send percent of brightness is 0.5% for one step. 200/2 = 100 */ | ||
|  | #define IOT_LIGHT_BRIGHTNESS_RAW_LEVEL_MAX          200
 | ||
|  | 
 | ||
|  | #define IOT_LIGHT_BRIGHTNESS_SHUTDOWN_THRESHOLD     50 /* less than 30% */
 | ||
|  | #define IOT_LIGHT_BRIGHTNESS_OPENLOOP_THRESHOLD     \
 | ||
|  |     IOT_LIGHT_BRIGHTNESS_SHUTDOWN_THRESHOLD | ||
|  | #define IOT_LIGHT_BRIGHTNESS_MAX_LEVEL_THRESHOLD    100 /* 100% */
 | ||
|  | #define IOT_LIGHT_BRIGHTNESS_DEFAULT        \
 | ||
|  |     IOT_LIGHT_BRIGHTNESS_MAX_LEVEL_THRESHOLD /* max level as default */ | ||
|  | 
 | ||
|  | #define IOT_LIGHT_CORRECTION_PARAM_SIZE    (10) /* table size */
 | ||
|  | 
 | ||
|  | /* Command & response frames defination. */ | ||
|  | #pragma pack(push)
 | ||
|  | #pragma pack(1)
 | ||
|  | 
 | ||
|  | /* Command frames for enum _iot_light_driver_command_e. */ | ||
|  | 
 | ||
|  | /* frame format for command CMD_GET_PARAM */ | ||
|  | typedef struct _iot_light_cmd_frame_get_param_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t data; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_cmd_frm_get_param_t; | ||
|  | 
 | ||
|  | /* frame format for command CMD_SET_LIGHT_LEVEL_AB */ | ||
|  | typedef struct _iot_light_cmd_frame_set_light_level_ab_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t level; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_cmd_frm_set_light_level_ab_t; | ||
|  | 
 | ||
|  | /* frame format for command CMD_SET_LIGHT_LEVEL_GRP */ | ||
|  | typedef struct _iot_light_cmd_frame_set_light_level_group_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t group; | ||
|  |     uint8_t channel; | ||
|  |     uint8_t level; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_cmd_frm_set_light_level_grp_t; | ||
|  | 
 | ||
|  | /* frame format for command CMD_SET_LIGHT_LEVEL_A */ | ||
|  | typedef struct _iot_light_cmd_frame_set_light_level_a_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t level; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_cmd_frm_set_light_level_a_t; | ||
|  | 
 | ||
|  | /* frame format for command CMD_SET_LIGHT_LEVEL_B */ | ||
|  | typedef struct _iot_light_cmd_frame_set_light_level_b_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t level; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_cmd_frm_set_light_level_b_t; | ||
|  | 
 | ||
|  | /* frame format for command CMD_SET_GROUP */ | ||
|  | typedef struct _iot_light_cmd_frame_set_group_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t group; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_cmd_frm_set_group_t; | ||
|  | 
 | ||
|  | /* frame format for command CMD_PRIVATE_COMMAND */ | ||
|  | typedef struct _iot_light_cmd_private_command_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t sub_opcode; | ||
|  |     uint8_t data[0]; | ||
|  | } iot_cmd_frm_private_command_t; | ||
|  | 
 | ||
|  | #define IOT_LIGHT_MIN_FRAME_LEN 3
 | ||
|  | 
 | ||
|  | /* Response frames for enum _iot_light_driver_response_e */ | ||
|  | /* frame format for response RESP_GET_PARAM */ | ||
|  | typedef struct _iot_light_resp_frame_get_param_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t device_type; | ||
|  |     int8_t temperature; | ||
|  |     uint8_t voltage_in_h; | ||
|  |     uint8_t voltage_in_l; | ||
|  |     uint8_t current_in_h; | ||
|  |     uint8_t current_in_l; | ||
|  |     uint8_t voltage_out_h; | ||
|  |     uint8_t voltage_out_l; | ||
|  |     uint8_t current_out_h; | ||
|  |     uint8_t current_out_l; | ||
|  |     uint8_t power_in_h; | ||
|  |     uint8_t power_in_l; | ||
|  |     uint8_t factor; | ||
|  |     uint8_t reserved0; | ||
|  |     uint8_t level; | ||
|  |     uint8_t reserved1; | ||
|  |     uint8_t power_state_h; | ||
|  |     uint8_t power_state_l; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_resp_frm_get_param_t; | ||
|  | 
 | ||
|  | /* report power state frames */ | ||
|  | /* frame format for response REPORT_POWER_STATE */ | ||
|  | typedef struct _iot_light_report_power_state_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t power_state_h; | ||
|  |     uint8_t power_state_l; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_report_power_state_t; | ||
|  | 
 | ||
|  | /* frame format for command CMD_REPORT_POWER_STATE_ACK */ | ||
|  | typedef struct _iot_light_cmd_frame_report_ack_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t result; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_cmd_frame_report_ack_t; | ||
|  | 
 | ||
|  | /* frame format for response RESP_SET_LIGHT_LEVEL_AB */ | ||
|  | typedef struct _iot_light_resp_frame_set_light_level_ab_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t level; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_resp_frm_set_light_level_ab_t; | ||
|  | 
 | ||
|  | /* frame format for response RESP_SET_LIGHT_LEVEL_A */ | ||
|  | typedef struct _iot_light_resp_frame_set_light_level_a_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t level; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_resp_frm_set_light_level_a_t; | ||
|  | 
 | ||
|  | /* frame format for response RESP_SET_LIGHT_LEVEL_B */ | ||
|  | typedef struct _iot_light_resp_frame_set_light_level_b_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t level; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_resp_frm_set_light_level_b_t; | ||
|  | 
 | ||
|  | /* frame format for response RESP_SET_GROUP */ | ||
|  | typedef struct _iot_light_resp_frame_set_group_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t group; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_resp_frm_set_group_t; | ||
|  | 
 | ||
|  | /* frame format for response RESP_PRIVATE_COMMAND */ | ||
|  | typedef struct _iot_light_resp_private_command_t { | ||
|  |     uint8_t opcode; | ||
|  |     uint8_t sub_opcode; | ||
|  | } iot_resp_frm_private_command_t; | ||
|  | 
 | ||
|  | /* frame format for response SUBCMD_GET_LEAKAGE_CURRENT */ | ||
|  | typedef struct _iot_light_resp_frame_private_sub_get_leakage_current_t { | ||
|  |     uint8_t current_h;  /* Current in mA, upper 8 bits. */ | ||
|  |     uint8_t current_l;  /* Current in mA, lower 8 bits. */ | ||
|  | } iot_resp_frm_prv_sub_get_leakage_current_t; | ||
|  | 
 | ||
|  | /* frame format for response SUBCMD_GET_FW_VERSION */ | ||
|  | typedef struct _iot_light_resp_frame_private_sub_get_dev_info_t { | ||
|  |     uint32_t fw_ver; /* version of firmware */ | ||
|  |     uint8_t  local_mac[IOT_MAC_ADDR_LEN]; /* mac address of this device. */ | ||
|  | } iot_resp_frm_prv_sub_get_dev_info_t; | ||
|  | 
 | ||
|  | typedef struct _iot_light_correction_header_t { | ||
|  |     uint8_t cmd_type; | ||
|  |     uint8_t subfn; | ||
|  | } iot_light_correction_header_t; | ||
|  | 
 | ||
|  | /* frame format for command SUBCMD_SET_COR_DATA_IN_POWER */ | ||
|  | typedef struct _iot_light_correction_input_power_t { | ||
|  |     iot_light_correction_header_t header; | ||
|  |     uint32_t input_power_slope; | ||
|  |     uint32_t input_power_intercept; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_correction_input_power_t; | ||
|  | 
 | ||
|  | /* frame format for correction command SUBCMD_SET_COR_DATA_IN_VOLTAGE */ | ||
|  | typedef struct _iot_light_correction_input_voltage_t { | ||
|  |     iot_light_correction_header_t header; | ||
|  |     uint32_t input_voltage_slope; | ||
|  |     uint32_t input_voltage_intercept; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_correction_input_voltage_t; | ||
|  | 
 | ||
|  | /* frame format for correction command SUBCMD_SET_COR_DATA_IN_CURRENT */ | ||
|  | typedef struct _iot_light_correction_input_current_t { | ||
|  |     iot_light_correction_header_t header; | ||
|  |     uint32_t input_current_slope; | ||
|  |     uint32_t input_current_intercept; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_correction_input_current_t; | ||
|  | 
 | ||
|  | /* frame format for correction command SUBCMD_SET_COR_DATA_OUTPUT_CURRENT */ | ||
|  | typedef struct _iot_light_correction_output_current_t { | ||
|  |     iot_light_correction_header_t header; | ||
|  |     uint8_t set_cur; | ||
|  |     uint8_t real_cur; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_correction_output_current_t; | ||
|  | 
 | ||
|  | /* frame format for command RESP_SET_CORRECTION_DATA */ | ||
|  | typedef struct _iot_light_resp_set_correction_t { | ||
|  |     iot_light_correction_header_t header; | ||
|  |     uint8_t result; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_resp_set_correction_t; | ||
|  | 
 | ||
|  | /* frame format for command CMD_GET_CORRECTION_PARAM */ | ||
|  | typedef struct _iot_light_get_correction_param_t { | ||
|  |     iot_light_correction_header_t header; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_get_correction_param_t; | ||
|  | 
 | ||
|  | /* frame format for command RESP_GET_CORRECTION_PARAM */ | ||
|  | typedef struct _iot_light_resp_get_cor_param_t { | ||
|  |     iot_light_correction_header_t header; | ||
|  |     uint8_t result; | ||
|  |     uint16_t power; | ||
|  |     uint16_t voltage; | ||
|  |     uint16_t current; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_resp_get_cor_param_t; | ||
|  | 
 | ||
|  | /* frame format for command CMD_SET_CORRECTION_PARAM */ | ||
|  | typedef struct _iot_light_set_correction_param_t { | ||
|  |     iot_light_correction_header_t header; | ||
|  |     uint8_t level; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_set_correction_param_t; | ||
|  | 
 | ||
|  | /* frame format for command RESP_SET_CORRECTION_PARAM */ | ||
|  | typedef struct _iot_light_resp_set_cor_param_t { | ||
|  |     iot_light_correction_header_t header; | ||
|  |     uint8_t result; | ||
|  |     uint8_t check_sum; | ||
|  | } iot_light_resp_set_cor_param_t; | ||
|  | 
 | ||
|  | /* the led information need save flash */ | ||
|  | typedef struct _iot_led_pib_info_t { | ||
|  |     /* cco mac */ | ||
|  |     uint8_t             cco_mac[IOT_MAC_ADDR_LEN]; | ||
|  |     /* the group be setted */ | ||
|  |     uint8_t group; | ||
|  |     /* the channel a level */ | ||
|  |     uint8_t level_a; | ||
|  |     /* the channel b level */ | ||
|  |     uint8_t level_b; | ||
|  |     /* the correction input power setted flag. */ | ||
|  |     uint8_t input_power    : 1, | ||
|  |     /* the correction input voltage setted flag. */ | ||
|  |             input_voltage  : 1, | ||
|  |     /* the correction input current setted flag. */ | ||
|  |             input_current  : 1, | ||
|  |     /* the correction output current table setted flag. */ | ||
|  |             output_current : 1, | ||
|  |     /* reserved for future */ | ||
|  |             resv           : 5; | ||
|  |     /* the correction input power slope and intercept. */ | ||
|  |     float   input_power_slope; | ||
|  |     float   input_power_intercept; | ||
|  |     /* the correction input voltage slope and intercept. */ | ||
|  |     float   input_vol_slope; | ||
|  |     float   input_vol_intercept; | ||
|  |     /* the correction input current slope and intercept. */ | ||
|  |     float   input_cur_slope; | ||
|  |     float   input_cur_intercept; | ||
|  |     /* the correction output current table. */ | ||
|  |     uint8_t cor_param[IOT_LIGHT_CORRECTION_PARAM_SIZE]; | ||
|  | } iot_led_pib_info_t; | ||
|  | 
 | ||
|  | #pragma pack(pop)
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_led_crtl_msg_post_to_led_task() - message post to led task. | ||
|  |  * @data : pkt data of this message. | ||
|  |  */ | ||
|  | uint32_t iot_led_crtl_msg_post_to_led(iot_pkt_t *p_pkt_frame); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_led_ctrl_register_get_data_fn() - register a function to led task | ||
|  |  * to send data to ge. | ||
|  |  * @fn : pointer of function. | ||
|  |  */ | ||
|  | void iot_led_ctrl_register_get_data_fn(void *fn); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_led_crtl_set_mac() - set cco mac to led task, then led task | ||
|  |  * runs data to ge. | ||
|  |  * @cco : cco mac address. | ||
|  |  */ | ||
|  | void iot_led_crtl_set_mac(uint8_t *cco); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_led_ctrl_task_init() - initialize this module. | ||
|  |  * @return ERR_FAIL - Operation failed, ERR_OK - Operation Successful. | ||
|  |  */ | ||
|  | uint32_t iot_led_ctrl_task_init(void); | ||
|  | 
 | ||
|  | #endif
 |