recuperator fan control with esp32
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
esp-recuperator/main/actuators.c

133 lines
3.7 KiB

//
// Created by MightyPork on 2022/08/20.
//
#include <driver/ledc.h>
#include "actuators.h"
#include "settings.h"
struct actuators_status g_Act = {
.blind = false,
.dir = MOTOR_DIR_IN,
.power = 0,
};
static void buzzer_init() {
ledc_timer_config_t ledc_timer = {
.duty_resolution = LEDC_TIMER_8_BIT, // resolution of PWM duty
.freq_hz = 4400, // frequency of PWM signal
.speed_mode = LEDC_HIGH_SPEED_MODE, // timer mode
.timer_num = LEDC_TIMER_0, // timer index
.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
};
// Set configuration of timer0 for high speed channels
ledc_timer_config(&ledc_timer);
ledc_channel_config_t chan = {
.channel = LEDC_CHANNEL_0,
.duty = 127,
.gpio_num = CONFIG_PIN_BUZZER,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0
};
ledc_channel_config(&chan);
}
void act_buzzer_set(bool on) {
g_Act.buzzer = on;
if (on) {
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 127);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
} else {
ledc_stop(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0);
}
}
static void motor_init() {
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 = CONFIG_PIN_PWM,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0,
.flags.output_invert = true,
};
ledc_channel_config(&chan);
// Direction output
gpio_config_t ioconf = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1 << CONFIG_PIN_DIR,
};
gpio_config(&ioconf);
}
void act_motor_power_set(uint16_t perc) {
if (perc > 0) {
if (perc < g_Settings.min_power) {
perc = g_Settings.min_power;
}
g_Act.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);
} else {
ledc_stop(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
}
}
void act_motor_direction_set(enum motor_direction dir) {
g_Act.dir = dir;
gpio_set_level(CONFIG_PIN_DIR, (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) {
g_Act.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,
};
gpio_config(&ioconf);
}
void act_led_set(bool on) {
g_Act.led = on;
gpio_set_level(CONFIG_PIN_LED, (int) on); // TODO verify polarity
}
void act_init() {
buzzer_init();
motor_init();
blind_init();
led_init();
}