#include "peripheral_api.h"

void i2c_api_test(void)
{
	int fd = -1;
	int ret = 0;

	unsigned char reg[2] = {0x00, 0x00};

	unsigned char buf_write[5] = {0x01, 0x02, 0x03, 0x04, 0x05};
	unsigned char buf_read[5] = {0};

	fd = user_i2c_open(5);
	if (fd < 0) {
		printf("user_i2c_open fail\n");
		return;
	}

	ret = user_i2c_write(fd, 0x50, reg, 2, buf_write, 5);
	if (ret < 0) {
		printf("user_i2c_write fail\n");
		user_i2c_close(fd);
		return;
	}

	usleep(100 * 1000);
	ret = user_i2c_read(fd, 0x50, reg, 2, buf_read, 5);
	if (ret < 0) {
		printf("user_i2c_read fail\n");
		user_i2c_close(fd);
		return;
	}

	user_i2c_close(fd);
	if (memcmp(buf_write, buf_read, 5) == 0) {
		printf("buf_write buf_read same\n");
	} else {
		printf("buf_write buf_read diff %d %d %d %d %d\n", buf_read[0], buf_read[1], buf_read[2], buf_read[3], buf_read[4]);
	}

	return;
}

void ir_api_test(void)
{
	int ret = 0;
	int fd = -1;

	int code = 0, value = 0;

	fd = user_ir_open();
	if (fd < 0) {
		printf("user_ir_open fail \n");
		return;
	}

	for (;;) {
		ret = user_ir_get(fd, &code, &value);
		if (ret == 0) {
			printf("key %d %s\n", code, (value) ? "pressed" : "released");
		} else {
			usleep(10 * 1000);
		}
	}
	user_ir_close(fd);

	return;
}

void led_api_test(void)
{
	int ret = 0;

	ret = user_led_set_none_mode("work");
	if (ret < 0) {
		printf("user_led_set_none_mode fail \n");
		return;
	}	

	ret = user_led_turn_on_off("work",1);
	if (ret < 0) {
		printf("user_led_turn_on_off fail \n");
		return;
	}	
	sleep(1);
	ret = user_led_turn_on_off("work",0);
	if (ret < 0) {
		printf("user_led_turn_on_off fail \n");
		return;
	}	
	sleep(1);
	ret = user_led_turn_on_off("work",1);
	if (ret < 0) {
		printf("user_led_turn_on_off fail \n");
		return;
	}	

	ret = user_led_set_timer_mode("work");
	if (ret < 0) {
		printf("user_led_set_timer_mode fail \n");
		return;
	}		

	ret = user_led_set_timer_on_off("work", 1000, 200);
	if (ret < 0) {
		printf("user_led_set_timer_on_off fail \n");
		return;
	}	

	sleep(5);

	ret = user_led_set_none_mode("work");
	if (ret < 0) {
		printf("user_led_set_none_mode fail \n");
		return;
	}		

	return;
}

void uart_api_test(void)
{
	int ret = 0;
	int fd = -1;

	unsigned char buf_write[5] = {0};
	unsigned char buf_read[5] = {0};

	for (unsigned char i = 0; i < sizeof(buf_write); i++) {
		buf_write[i] = 0x30 + i;
	}

	fd = user_uart_open(4);
	if (fd < 0) {
		printf("user_uart_open fail \n");
		return;
	}

	ret = user_uart_set_property(fd, 115200, 8, 1, 'n');
	if (ret < 0) {
		printf("user_uart_set_property fail \n");
		return;
	}

	ret = user_uart_write(fd, buf_write, sizeof(buf_write));
	if (ret <= 0) {
		printf("user_uart_write fail \n");
		user_uart_close(fd);
		return;
	}

	fsync(fd);

	usleep(200 * 1000);

	ret = user_uart_read(fd, buf_read, sizeof(buf_read));
	if (ret <= 0) {
		printf("user_uart_read fail \n");
		user_uart_close(fd);
		return;
	}

	user_uart_close(fd);

	if (memcmp(buf_write, buf_read, sizeof(buf_write)) == 0) {
		printf("buf_write buf_read same\n");
	} else {
		printf("buf_write buf_read diff %d %d %d %d %d \n", buf_read[0], buf_read[1], buf_read[2], buf_read[3], buf_read[4]);
	}

	return;
}

void can_api_test(void)
{
	struct can_frame frame_tx = {0};

	struct can_frame frame_rx = {0};

	int nbytes = 0;

	user_can_init("can0",250000,3000000);

	//标准帧
    frame_tx.can_id = 0x123;
    frame_tx.can_dlc = 8;
    frame_tx.data[0] = 0x11;
    frame_tx.data[1] = 0x22;
    frame_tx.data[2] = 0x33;
    frame_tx.data[3] = 0x44;
    frame_tx.data[4] = 0x55;
    frame_tx.data[5] = 0x66;
    frame_tx.data[6] = 0x77;
    frame_tx.data[7] = 0x88;

	nbytes = user_can_send("can0",frame_tx);

	//扩展帧
    frame_tx.can_id = 0x12345678 | CAN_EFF_FLAG;
    frame_tx.can_dlc = 8;
    frame_tx.data[0] = 0x11;
    frame_tx.data[1] = 0x22;
    frame_tx.data[2] = 0x33;
    frame_tx.data[3] = 0x44;
    frame_tx.data[4] = 0x55;
    frame_tx.data[5] = 0x66;
    frame_tx.data[6] = 0x77;
    frame_tx.data[7] = 0x88;

	nbytes = user_can_send("can0",frame_tx);

	nbytes = user_can_recv("can0",&frame_rx);


	/* printf the received frame */
	if (nbytes > 0) {
		printf("%#x  [%d]  ", frame_rx.can_id, frame_rx.can_dlc);
		for (unsigned char i = 0; i < frame_rx.can_dlc; i++)
			printf("%#x ", frame_rx.data[i]);
		printf("\n");
	}

	user_can_deinit("can0");

	return;
}

int main()
{
	// i2c_api_test();

	// ir_api_test();

	// led_api_test();

	// uart_api_test();

	can_api_test();

	return 0;
}
