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

184 lines
4.6 KiB

//
// 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,
};
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)
{
if (gAct.buzzer == on) {
return;
}
ESP_LOGI(TAG, "set buzzer %d", on);
gAct.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_1,
.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);
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(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)
{
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,
};
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_init()
{
buzzer_init();
motor_init();
blind_init();
led_init();
}
int16_t act_temp_in()
{
return gTempSensors[0];
}
int16_t act_temp_out()
{
return gTempSensors[1];
}