/*
 * fml_pwm.c
 *
 *  Created on: 2025年4月10日
 *      Author: 22332
 */
#include <stdio.h>
#include <stdlib.h>
#include "cola_init.h"
#include "cola_device.h"
#include "cola_os.h"
#include "fml_pwm.h"
#include "shell.h"

extern void logWrite(const char *fmt, ...);

static cola_device_t *g_pDevpwm = NULL;
static uint8_t g_iHostPWMValue = 240;					//主机发过来的PWM值

void fml_pwm_init(void)
{
	g_pDevpwm = cola_device_find("DEVICE_PWM");
    if(g_pDevpwm)
        cola_device_open(g_pDevpwm, 0);
}

void fml_pwm_change_duty(uint8_t duty)
{
	g_iHostPWMValue = duty;
	cola_device_cfg(g_pDevpwm,duty,0);
}

void fml_pwm_adjust(bool bTempHigh)
{
	if(g_iHostPWMValue > 0)
	{
		uint8_t value = g_iHostPWMValue;
		if(bTempHigh)
			value /= 2;
		cola_device_cfg(g_pDevpwm,value,0);
	}
	//logWrite("fml_pwm_adjust,g_iHostPWMValue=%d\r\n",g_iHostPWMValue);
}

void fml_pwm_deinit(void)
{
    if(g_pDevpwm)
    {
    	g_pDevpwm = NULL;
        cola_device_close(g_pDevpwm);
    }
}


int fml_pwm(int argc, char *argv[])
{
	if (argc < 3)
	{
		logWrite("para length must greater than 3\r\n");
		return 0;
	}
	int mode 		= atoi(argv[1]);
	int value 		= atoi(argv[2]);
	if(mode == 0)
	{
		logWrite("pwm_value=%d\r\n",value);
		fml_pwm_change_duty(value);
	}
	else if(mode == 1)
	{
		logWrite("g_iHostPWMValue=%d\r\n",g_iHostPWMValue);
	}
	return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),pwm, fml_pwm, pwm control);



