|
|
|
//
|
|
|
|
// Created by MightyPork on 2022/08/20.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <driver/ledc.h>
|
|
|
|
#include <esp_log.h>
|
|
|
|
#include "actuators.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "onewires.h"
|
|
|
|
|
|
|
|
static const char *TAG="act";
|
|
|
|
|
|
|
|
struct actuators_status gAct = {
|
|
|
|
.blind = false,
|
|
|
|
.dir = MOTOR_DIR_IN,
|
|
|
|
.power = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
int sPinPwm = 0;
|
|
|
|
int sPinDir = 0;
|
|
|
|
|
|
|
|
static void motor_init()
|
|
|
|
{
|
|
|
|
sPinPwm = (gSettings.swap_pwm_dir ? CONFIG_PIN_DIR : CONFIG_PIN_PWM);
|
|
|
|
sPinDir = (gSettings.swap_pwm_dir ? CONFIG_PIN_PWM : CONFIG_PIN_DIR);
|
|
|
|
|
|
|
|
ledc_timer_config_t ledc_timer = {
|
|
|
|
.duty_resolution = LEDC_TIMER_10_BIT, // resolution of PWM duty
|
|
|
|
.freq_hz = 1000, // frequency of PWM signal
|
|
|
|
.speed_mode = LEDC_HIGH_SPEED_MODE, // timer mode
|
|
|
|
.timer_num = LEDC_TIMER_1, // timer index
|
|
|
|
.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
|
|
|
|
};
|
|
|
|
// Set configuration of timer1 for high speed channels
|
|
|
|
ledc_timer_config(&ledc_timer);
|
|
|
|
|
|
|
|
// PWM output
|
|
|
|
ledc_channel_config_t chan = {
|
|
|
|
.channel = LEDC_CHANNEL_1,
|
|
|
|
.duty = 512,
|
|
|
|
.gpio_num = sPinPwm,
|
|
|
|
.speed_mode = LEDC_HIGH_SPEED_MODE,
|
|
|
|
.hpoint = 0,
|
|
|
|
.timer_sel = LEDC_TIMER_1,
|
|
|
|
.flags.output_invert = true,
|
|
|
|
};
|
|
|
|
ledc_channel_config(&chan);
|
|
|
|
|
|
|
|
// Direction output
|
|
|
|
gpio_config_t ioconf = {
|
|
|
|
.mode = GPIO_MODE_OUTPUT,
|
|
|
|
.pin_bit_mask = 1 << sPinDir,
|
|
|
|
};
|
|
|
|
gpio_config(&ioconf);
|
|
|
|
|
|
|
|
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
|
|
|
|
ledc_stop(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void act_motor_power_set(uint16_t perc)
|
|
|
|
{
|
|
|
|
if (gAct.power == perc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "set pwr %d", perc);
|
|
|
|
if (perc > 0) {
|
|
|
|
if (perc < gSettings.min_power) {
|
|
|
|
perc = gSettings.min_power;
|
|
|
|
}
|
|
|
|
gAct.power = perc;
|
|
|
|
uint16_t duty = (uint16_t) (10.23f * (float) perc);
|
|
|
|
if (duty > 1023) {
|
|
|
|
duty = 1023;
|
|
|
|
}
|
|
|
|
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, duty);
|
|
|
|
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1);
|
|
|
|
|
|
|
|
act_led_set(true);
|
|
|
|
} else {
|
|
|
|
gAct.power = 0;
|
|
|
|
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
|
|
|
|
ledc_stop(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
|
|
|
|
act_led_set(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void act_motor_direction_set(enum motor_direction dir)
|
|
|
|
{
|
|
|
|
if (dir == gAct.dir) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "set dir %d", dir);
|
|
|
|
gAct.dir = dir;
|
|
|
|
gpio_set_level(sPinDir, (int) dir); // TODO verify polarity
|
|
|
|
}
|
|
|
|
|
|
|
|
static void blind_init()
|
|
|
|
{
|
|
|
|
// Blind output
|
|
|
|
gpio_config_t ioconf = {
|
|
|
|
.mode = GPIO_MODE_OUTPUT,
|
|
|
|
.pin_bit_mask = 1 << CONFIG_PIN_BLIND,
|
|
|
|
};
|
|
|
|
gpio_config(&ioconf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void act_blind_set(bool open)
|
|
|
|
{
|
|
|
|
if (open == gAct.blind) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "set blind %d", open);
|
|
|
|
gAct.blind = open;
|
|
|
|
gpio_set_level(CONFIG_PIN_BLIND, (int) open); // TODO verify polarity
|
|
|
|
}
|
|
|
|
|
|
|
|
static void led_init()
|
|
|
|
{
|
|
|
|
gpio_config_t ioconf = {
|
|
|
|
.mode = GPIO_MODE_OUTPUT,
|
|
|
|
.pin_bit_mask = (1 << CONFIG_PIN_LED) | (1 << CONFIG_PIN_STATUSLED),
|
|
|
|
};
|
|
|
|
gpio_config(&ioconf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void act_led_set(bool on)
|
|
|
|
{
|
|
|
|
if (on == gAct.led) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "set LED %d", on);
|
|
|
|
gAct.led = on;
|
|
|
|
gpio_set_level(CONFIG_PIN_LED, (int) on); // TODO verify polarity
|
|
|
|
}
|
|
|
|
|
|
|
|
void act_statusled_set(bool on)
|
|
|
|
{
|
|
|
|
gpio_set_level(CONFIG_PIN_STATUSLED, (int) on);
|
|
|
|
}
|
|
|
|
|
|
|
|
void act_init()
|
|
|
|
{
|
|
|
|
motor_init();
|
|
|
|
blind_init();
|
|
|
|
led_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t act_temp1()
|
|
|
|
{
|
|
|
|
return gTempSensors[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t act_temp2()
|
|
|
|
{
|
|
|
|
return gTempSensors[1];
|
|
|
|
}
|