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

161 lines
3.7 KiB

//
// Created by MightyPork on 2022/08/20.
//
#include <driver/ledc.h>
2 years ago
#include <esp_log.h>
#include "actuators.h"
#include "settings.h"
2 years ago
#include "onewires.h"
static const char *TAG="act";
struct actuators_status gAct = {
.blind = false,
.dir = MOTOR_DIR_IN,
.power = 0,
};
2 years ago
int sPinPwm = 0;
int sPinDir = 0;
2 years ago
static void motor_init()
{
2 years ago
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,
2 years ago
.gpio_num = sPinPwm,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.hpoint = 0,
2 years ago
.timer_sel = LEDC_TIMER_1,
.flags.output_invert = true,
};
ledc_channel_config(&chan);
// Direction output
gpio_config_t ioconf = {
.mode = GPIO_MODE_OUTPUT,
2 years ago
.pin_bit_mask = 1 << sPinDir,
};
gpio_config(&ioconf);
2 years ago
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
ledc_stop(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
}
2 years ago
void act_motor_power_set(uint16_t perc)
{
2 years ago
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;
2 years ago
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);
2 years ago
act_led_set(true);
} else {
gAct.power = 0;
2 years ago
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
ledc_stop(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
2 years ago
act_led_set(false);
}
}
2 years ago
void act_motor_direction_set(enum motor_direction dir)
{
2 years ago
if (dir == gAct.dir) {
return;
}
ESP_LOGI(TAG, "set dir %d", dir);
gAct.dir = dir;
2 years ago
gpio_set_level(sPinDir, (int) dir); // TODO verify polarity
}
2 years ago
static void blind_init()
{
// Blind output
gpio_config_t ioconf = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1 << CONFIG_PIN_BLIND,
};
gpio_config(&ioconf);
}
2 years ago
void act_blind_set(bool open)
{
2 years ago
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
}
2 years ago
static void led_init()
{
gpio_config_t ioconf = {
.mode = GPIO_MODE_OUTPUT,
2 years ago
.pin_bit_mask = (1 << CONFIG_PIN_LED) | (1 << CONFIG_PIN_STATUSLED),
};
gpio_config(&ioconf);
}
2 years ago
void act_led_set(bool on)
{
2 years ago
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
}
2 years ago
void act_statusled_set(bool on)
{
gpio_set_level(CONFIG_PIN_STATUSLED, (int) on);
}
2 years ago
void act_init()
{
motor_init();
blind_init();
led_init();
}
2 years ago
cels_t act_temp1()
2 years ago
{
2 years ago
return gTempSensors[0];
2 years ago
}
cels_t act_temp2()
2 years ago
{
2 years ago
return gTempSensors[1];
2 years ago
}
uint16_t act_temps_serial() {
return gTempsSerial;
}