#include "factory.h"
#include "bsp/bsp_driver.h"
#include "prot/can_message.h"
#include "libs/utils.h"
#include "libs/logger.h"
#include "os/os_task.h"

static u8 factory_mode = 0;

void can_process_factory_message(can_message_t *can_message){
	uint8_t response[8];
	uint8_t rsplen;
	encoder_can_key(response, can_message->key);
	response[2] = 0;
	rsplen = 3;
	switch(can_message->key) {
		case BUILD_CMD_KEY(0xE0):
			factory_mode = decode_u8(can_message->data);
			break;
		case BUILD_CMD_KEY(0xE1):
		{
			u8 item = decode_u8(can_message->data);
			u8 mode = decode_u8((u8 *)can_message->data +1);
			if (item == 1) {
				if (mode == 0) {
					pwm_3phase_sides(false, false);
				}else if (mode == 1) {
					pwm_3phase_sides(false, true);
				}else if (mode == 2) {
					pwm_3phase_sides(true, false);
				}
			}
			break;
		}
		default:
			rsplen = 0;
			break;
	}
	if (rsplen > 0) {
		can_send_response(can_message->src, response, rsplen);
	}
}

u8 factory_get_mode(void) {
	return factory_mode;
}

