#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <linux/input.h>
#include <pthread.h>

void pwmInit(void)
{
	printf("c-pwm:init\n");
	system("echo 3 > /sys/class/pwm/pwmchip0/export");				 /*启动pwm1功能位*/
	system("echo 300000 > /sys/class/pwm/pwmchip0/pwm3/period");	 /*设置pwm1的周期，单位是ns*/
	system("echo 120000 > /sys/class/pwm/pwmchip0/pwm3/duty_cycle"); /*设置pwm1的占空比,单位是ns*/
	system("echo normal > /sys/class/pwm/pwmchip0/pwm3/polarity");	 /*设置pwm1的极性：normal 正极性，inverse 负极性*/
	system("echo 0 > /sys/class/pwm/pwmchip0/pwm3/enable");			 /*使能pwm1,1 使能，0 禁用*/
	return;
}

// void pwmLv0(void)
// {
// 	printf("c-pwm:lv0 on\n");
// 	system("echo 434782 > /sys/class/pwm/pwmchip0/pwm1/period");	 /*设置pwm1的周期，单位是ns*/
// 	system("echo 217391> /sys/class/pwm/pwmchip0/pwm1/duty_cycle"); /*设置pwm1的占空比,单位是ns*/
// 	return;
// }
void pwmLv1(void)
{
	printf("c-pwm:lv1 on\n");
	system("echo 434782 > /sys/class/pwm/pwmchip0/pwm1/period");	 /*设置pwm1的周期，单位是ns*/
	system("echo 217391 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle"); /*设置pwm1的占空比,单位是ns*/
	return;
}
void pwmLv2(void)
{
	printf("c-pwm:lv2 on\n");
	system("echo 300000 > /sys/class/pwm/pwmchip0/pwm1/period");	/*设置pwm1的周期，单位是ns*/
	system("echo 90000 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle"); /*设置pwm1的占空比,单位是ns*/
	return;
}
void pwmLv3(void)
{
	printf("c-pwm:lv3 on\n");
	system("echo 1 > /sys/class/pwm/pwmchip0/export");				 /*启动pwm1功能位*/
	system("echo 400000 > /sys/class/pwm/pwmchip0/pwm1/period");	 /*设置pwm1的周期，单位是ns*/
	system("echo 160000 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle"); /*设置pwm1的占空比,单位是ns*/
	return;
}
void pwmLv4(void)
{
	printf("c-pwm:lv4 on\n");
	system("echo 1 > /sys/class/pwm/pwmchip0/export");				 /*启动pwm1功能位*/
	system("echo 400000 > /sys/class/pwm/pwmchip0/pwm1/period");	 /*设置pwm1的周期，单位是ns*/
	system("echo 120000 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle"); /*设置pwm1的占空比,单位是ns*/
	return;
}
void pwmLv5(void)
{
	printf("c-pwm:lv5 on\n");
	system("echo 1 > /sys/class/pwm/pwmchip0/export");				 /*启动pwm1功能位*/
	system("echo 500000 > /sys/class/pwm/pwmchip0/pwm1/period");	 /*设置pwm1的周期，单位是ns*/
	system("echo 200000 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle"); /*设置pwm1的占空比,单位是ns*/
	return;
}
void pwmLv6(void)
{
	printf("c-pwm:lv6 on\n");
	system("echo 1 > /sys/class/pwm/pwmchip0/export");				 /*启动pwm1功能位*/
	system("echo 500000 > /sys/class/pwm/pwmchip0/pwm1/period");	 /*设置pwm1的周期，单位是ns*/
	system("echo 150000 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle"); /*设置pwm1的占空比,单位是ns*/
	return;
}
void pwmOff(void)
{
	printf("c-pwm:off\n");
	system("echo 0 > /sys/class/pwm/pwmchip0/pwm1/enable"); /*使能pwm1,1 使能，0 禁用*/
	return;
}

void pwmOn(void)
{
	printf("c-pwm:off\n");
	system("echo 1 > /sys/class/pwm/pwmchip0/pwm1/enable"); /*使能pwm1,1 使能，0 禁用*/
	return;
}

// system("echo 163 > /sys/class/gpio/unexport"); /*释放对应编号的io口*/
void gpio1On(char val)
{
	system("echo 1 > /sys/class/gpio/gpio132/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio1Off(char val)
{
	system("echo 0 > /sys/class/gpio/gpio132/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio2On(char val)
{
	system("echo 1 > /sys/class/gpio/gpio133/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio2Off(char val)
{
	system("echo 0 > /sys/class/gpio/gpio133/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}

void gpio3On(char val)
{
	system("echo 1 > /sys/class/gpio/gpio135/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio3Off(char val)
{
	system("echo 0 > /sys/class/gpio/gpio135/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio4On(char val)
{
	system("echo 1 > /sys/class/gpio/gpio136/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio4Off(char val)
{
	system("echo 0 > /sys/class/gpio/gpio136/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}

void gpio5On(char val)
{
	system("echo 1 > /sys/class/gpio/gpio137/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio5Off(char val)
{
	system("echo 0 > /sys/class/gpio/gpio137/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio6On(char val)
{
	system("echo 1 > /sys/class/gpio/gpio138/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio6Off(char val)
{
	system("echo 0 > /sys/class/gpio/gpio138/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio7On(char val)
{
	system("echo 1 > /sys/class/gpio/gpio139/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}
void gpio7Off(char val)
{
	system("echo 0 > /sys/class/gpio/gpio139/value"); /*使能io口的高低电平：1 高电平，0 低电平*/
	return;
}

void insModEncoder(void)
{
	system("insmod /etc/rotary_encoder_device.ko ");
	system("insmod /etc/rotary_encoder.ko");
	system("insmod /etc/gpio_keys.ko");
}
void rmModEncoder(void)
{
	system("rmmod /etc/gpio_keys.ko");
	system("rmmod /etc/rotary_encoder.ko");
	system("rmmod /etc/rotary_encoder_device.ko ");
}
void gpioUnexport(void)
{
	system("echo 132 > /sys/class/gpio/unexport");
	system("echo 133 > /sys/class/gpio/unexport");
	system("echo 135 > /sys/class/gpio/unexport");
	system("echo 136 > /sys/class/gpio/unexport");
	system("echo 137 > /sys/class/gpio/unexport");
	system("echo 138 > /sys/class/gpio/unexport");
	system("echo 139 > /sys/class/gpio/unexport");
}
void gpioInit(void)
{
	system("echo 132 > /sys/class/gpio/export");
	system("echo out > /sys/class/gpio/gpio132/direction");
	system("echo 0 > /sys/class/gpio/gpio132/value");

	system("echo 133 > /sys/class/gpio/export");
	system("echo out > /sys/class/gpio/gpio133/direction");
	system("echo 0 > /sys/class/gpio/gpio133/value");

	system("echo 135 > /sys/class/gpio/export");
	system("echo out > /sys/class/gpio/gpio135/direction");
	system("echo 0 > /sys/class/gpio/gpio135/value");

	system("echo 136 > /sys/class/gpio/export");
	system("echo out > /sys/class/gpio/gpio136/direction");
	system("echo 0 > /sys/class/gpio/gpio136/value");

	system("echo 137 > /sys/class/gpio/export");
	system("echo out > /sys/class/gpio/gpio137/direction");
	system("echo 0 > /sys/class/gpio/gpio137/value");

	system("echo 138 > /sys/class/gpio/export");
	system("echo out > /sys/class/gpio/gpio138/direction");
	system("echo 0 > /sys/class/gpio/gpio138/value");

	system("echo 139 > /sys/class/gpio/export");
	system("echo out > /sys/class/gpio/gpio139/direction");
	system("echo 0 > /sys/class/gpio/gpio139/value");
}
