forked from electro/esp-irblaster
it works
This commit is contained in:
@@ -6,6 +6,7 @@ idf_component_register(SRCS
|
||||
utils.c
|
||||
wifi_conn.c
|
||||
mbiface.c
|
||||
onewires.c
|
||||
actuators.c
|
||||
fancontrol.c
|
||||
console/console_ioimpl.c
|
||||
|
||||
@@ -19,7 +19,7 @@ menu "Project configuration"
|
||||
|
||||
config PIN_DIR
|
||||
int "Motor direction pin"
|
||||
default 20
|
||||
default 23
|
||||
|
||||
config PIN_1WIRE_1
|
||||
int "1Wire 1 (indoor)"
|
||||
|
||||
+34
-6
@@ -3,8 +3,12 @@
|
||||
//
|
||||
|
||||
#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,
|
||||
@@ -37,6 +41,10 @@ static void buzzer_init()
|
||||
|
||||
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);
|
||||
@@ -65,7 +73,7 @@ static void motor_init()
|
||||
.gpio_num = CONFIG_PIN_PWM,
|
||||
.speed_mode = LEDC_HIGH_SPEED_MODE,
|
||||
.hpoint = 0,
|
||||
.timer_sel = LEDC_TIMER_0,
|
||||
.timer_sel = LEDC_TIMER_1,
|
||||
.flags.output_invert = true,
|
||||
};
|
||||
ledc_channel_config(&chan);
|
||||
@@ -76,11 +84,17 @@ static void motor_init()
|
||||
.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; }
|
||||
if (gAct.power == perc) {
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "set pwr %d", perc);
|
||||
if (perc > 0) {
|
||||
if (perc < gSettings.min_power) {
|
||||
perc = gSettings.min_power;
|
||||
@@ -92,14 +106,22 @@ void act_motor_power_set(uint16_t perc)
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -116,6 +138,10 @@ static void blind_init()
|
||||
|
||||
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
|
||||
}
|
||||
@@ -131,6 +157,10 @@ static void led_init()
|
||||
|
||||
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
|
||||
}
|
||||
@@ -145,12 +175,10 @@ void act_init()
|
||||
|
||||
int16_t act_temp_in()
|
||||
{
|
||||
// TODO
|
||||
return 200;
|
||||
return gTempSensors[0];
|
||||
}
|
||||
|
||||
int16_t act_temp_out()
|
||||
{
|
||||
// TODO
|
||||
return 40;
|
||||
return gTempSensors[1];
|
||||
}
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ extern EventGroupHandle_t g_wifi_event_group;
|
||||
esp_err_t cspemu_add_shutdown_handler(shutdown_handler_t handler);
|
||||
void cspemu_run_shutdown_handlers(void);
|
||||
|
||||
#define APP_NAME "IRBLASTER"
|
||||
#define APP_NAME "RECUP"
|
||||
#define APP_VERSION "v1"
|
||||
|
||||
#endif //_APPLICATION_H
|
||||
|
||||
+63
-21
@@ -2,16 +2,21 @@
|
||||
// Created by MightyPork on 2022/08/20.
|
||||
//
|
||||
|
||||
#include <esp_log.h>
|
||||
#include "fancontrol.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/timers.h"
|
||||
#include "settings.h"
|
||||
#include "actuators.h"
|
||||
#include "onewires.h"
|
||||
#include "tasks.h"
|
||||
|
||||
const char *TAG = "fc";
|
||||
|
||||
#define UNIDIR_T_MEAS_PERIOD 15 /* s */
|
||||
struct FanControlState gState = {};
|
||||
|
||||
static void timerCallback(TimerHandle_t xTimer);
|
||||
static void timerCallback();
|
||||
|
||||
static void invalidate_temps();
|
||||
|
||||
@@ -28,20 +33,32 @@ void settings_blind_time_set(uint16_t blind_time)
|
||||
settings_persist(SETTINGS_blind_time);
|
||||
}
|
||||
|
||||
static void fanctltask(void *dummy) {
|
||||
TickType_t last_wake_time = xTaskGetTickCount();
|
||||
while (1) {
|
||||
timerCallback();
|
||||
vTaskDelayUntil(&last_wake_time, pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
|
||||
void fancontrol_init()
|
||||
{
|
||||
gState.set_vent_mode = gSettings.initial_mode;
|
||||
gState.set_power = gSettings.initial_power;
|
||||
|
||||
xTimerCreate("fanctl",
|
||||
pdMS_TO_TICKS(1000),
|
||||
pdTRUE,
|
||||
NULL,
|
||||
timerCallback);
|
||||
// TimerHandle_t hTimer = xTimerCreate("fanctl",
|
||||
// pdMS_TO_TICKS(1000),
|
||||
// pdTRUE,
|
||||
// NULL,
|
||||
// timerCallback);
|
||||
//
|
||||
// xTimerStart(hTimer, pdMS_TO_TICKS(2000));
|
||||
|
||||
xTaskCreate(fanctltask, "fc", FCTL_TASK_STACK, NULL, FCTL_TASK_PRIO, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void timerCallback(TimerHandle_t xTimer)
|
||||
static void timerCallback()
|
||||
{
|
||||
// posun rolety
|
||||
if (gAct.blind) {
|
||||
@@ -193,8 +210,8 @@ static void timerCallback(TimerHandle_t xTimer)
|
||||
|
||||
if (end_temp_meas) {
|
||||
if (gState.t_aggr_cnt > 0) {
|
||||
int16_t t1 = (int16_t) (gState.t1_aggr / (int32_t)gState.t_aggr_cnt);
|
||||
int16_t t2 = (int16_t) (gState.t2_aggr / (int32_t)gState.t_aggr_cnt);
|
||||
int16_t t1 = (int16_t) (gState.t1_aggr / (int32_t) gState.t_aggr_cnt);
|
||||
int16_t t2 = (int16_t) (gState.t2_aggr / (int32_t) gState.t_aggr_cnt);
|
||||
switch (gState.real_direction) {
|
||||
case MOTOR_DIR_IN:
|
||||
gState.t_outdoor = t2;
|
||||
@@ -218,32 +235,55 @@ static void timerCallback(TimerHandle_t xTimer)
|
||||
gState.t_aggr_cnt = 0;
|
||||
}
|
||||
|
||||
// Measure temperatures
|
||||
int16_t t1 = act_temp_in();
|
||||
int16_t t2 = act_temp_out();
|
||||
|
||||
gState.t_actual_in = t1;
|
||||
gState.t_actual_out = t2;
|
||||
|
||||
gState.valid_t_actual_in = gState.valid_t_actual_out = tempSensorsOk;
|
||||
|
||||
if (gState.effective_vent_mode == VENT_MODE_IN
|
||||
|| gState.effective_vent_mode == VENT_MODE_OUT
|
||||
|| gState.effective_vent_mode == VENT_MODE_RECUP) {
|
||||
// Measure temperatures
|
||||
int16_t t1 = act_temp_in();
|
||||
int16_t t2 = act_temp_out();
|
||||
|
||||
gState.t_actual_in = t1;
|
||||
gState.t_actual_out = t2;
|
||||
|
||||
gState.valid_t_actual_in = gState.valid_t_actual_out = true;
|
||||
|
||||
if (gAct.dir == gState.real_direction
|
||||
&& gState.ramp >= gSettings.ramp_time)
|
||||
{
|
||||
&& gState.ramp >= gSettings.ramp_time) {
|
||||
gState.t1_aggr += t1;
|
||||
gState.t2_aggr += t2;
|
||||
gState.t_aggr_cnt++;
|
||||
}
|
||||
} else {
|
||||
gState.valid_t_actual_in = gState.valid_t_actual_out = false;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG,
|
||||
"Mode %d (ef %d, inst %d), Dir %d (real %d), PW %d (ramp %d), Blind %d (ramp %d)",
|
||||
gState.set_vent_mode, gState.effective_vent_mode, gState.instantaneous_vent_mode,
|
||||
gAct.dir, gState.real_direction,
|
||||
gAct.power, gState.ramp,
|
||||
gAct.blind, gState.blind_position
|
||||
);
|
||||
ESP_LOGI(TAG,
|
||||
"Tin %d%s, Tout %d%s, T<- %d%s, T-> %d%s, T1 %d%s, T2 %d%s",
|
||||
gState.t_indoor,
|
||||
gState.valid_t_indoor ? "" : "!",
|
||||
gState.t_outdoor,
|
||||
gState.valid_t_outdoor ? "" : "!",
|
||||
gState.t_inflow,
|
||||
gState.valid_t_inflow ? "" : "!",
|
||||
gState.t_exhaust,
|
||||
gState.valid_t_exhaust ? "" : "!",
|
||||
gState.t_actual_in,
|
||||
gState.valid_t_actual_in ? "" : "!",
|
||||
gState.t_actual_out,
|
||||
gState.valid_t_actual_out ? "" : "!"
|
||||
);
|
||||
}
|
||||
|
||||
void fan_set_vent_mode(enum ventilation_mode mode)
|
||||
{
|
||||
ESP_LOGI(TAG, "Set vent mode = %d", mode);
|
||||
|
||||
if (mode == gState.set_vent_mode) {
|
||||
return;
|
||||
}
|
||||
@@ -273,6 +313,8 @@ static void invalidate_temps()
|
||||
|
||||
void fan_set_power(uint16_t power)
|
||||
{
|
||||
ESP_LOGI(TAG, "Set power = %d%%", power);
|
||||
|
||||
gState.set_power = power;
|
||||
if (power == 0) {
|
||||
gState.effective_vent_mode = VENT_MODE_OFF;
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <math.h>
|
||||
#include <esp_log.h>
|
||||
#include "onewires.h"
|
||||
#include "tasks.h"
|
||||
#include "owb.h"
|
||||
#include "ds18b20.h"
|
||||
|
||||
static const char* TAG="1w";
|
||||
|
||||
static owb_rmt_driver_info s_rmt_driver_info[2] = {};
|
||||
static OneWireBus * sBuses[2] = {};
|
||||
static DS18B20_Info *sDevs[2] = {};
|
||||
|
||||
volatile int16_t gTempSensors[2] = {};
|
||||
volatile bool tempSensorsOk = false;
|
||||
|
||||
static void owtask(void *dummy) {
|
||||
while (1) {
|
||||
int16_t a = 0, b = 0;
|
||||
int rv = read_onewires(&a, &b);
|
||||
tempSensorsOk = (rv == 0);
|
||||
|
||||
ESP_LOGI(TAG, "t1 %d, t2 %d Cx10", a, b);
|
||||
gTempSensors[0] = a;
|
||||
gTempSensors[1] = b;
|
||||
}
|
||||
}
|
||||
|
||||
void onewires_setup()
|
||||
{
|
||||
sBuses[0] = owb_rmt_initialize(&s_rmt_driver_info[0], CONFIG_PIN_1WIRE_1, RMT_CHANNEL_1, RMT_CHANNEL_0);
|
||||
sBuses[1] = owb_rmt_initialize(&s_rmt_driver_info[1], CONFIG_PIN_1WIRE_2, RMT_CHANNEL_3, RMT_CHANNEL_2);
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
owb_use_crc(sBuses[i], true);
|
||||
sDevs[i] = ds18b20_malloc();
|
||||
ds18b20_init_solo(sDevs[i], sBuses[i]);
|
||||
ds18b20_use_crc(sDevs[i], true);
|
||||
ds18b20_set_resolution(sDevs[i], DS18B20_RESOLUTION_12_BIT);
|
||||
}
|
||||
|
||||
xTaskCreate(owtask, "1w", ONEWIRES_TASK_STACK, NULL, ONEWIRES_TASK_PRIO, NULL);
|
||||
}
|
||||
|
||||
int read_onewires(int16_t *a, int16_t *b)
|
||||
{
|
||||
for (int i = 0; i < 2; i++) {
|
||||
ds18b20_convert_all(sBuses[i]);
|
||||
}
|
||||
|
||||
float readings[2] = { 0 };
|
||||
DS18B20_ERROR errors[2] = { 0 };
|
||||
|
||||
ds18b20_wait_for_conversion(sDevs[0]);
|
||||
vTaskDelay(pdMS_TO_TICKS(10)); // to be sure its done
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
errors[i] = ds18b20_read_temp(sDevs[i], &readings[i]);
|
||||
}
|
||||
|
||||
if (errors[0] != DS18B20_OK || errors[1] != DS18B20_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*a = (int16_t) roundf(readings[0] * 10);
|
||||
*b = (int16_t) roundf(readings[1] * 10);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Reading onewire sensors
|
||||
*/
|
||||
|
||||
#ifndef ONEWIRES_H
|
||||
#define ONEWIRES_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void onewires_setup();
|
||||
|
||||
int read_onewires(int16_t *a, int16_t *b);
|
||||
|
||||
extern volatile int16_t gTempSensors[2];
|
||||
extern volatile bool tempSensorsOk;
|
||||
|
||||
#endif //ONEWIRES_H
|
||||
+8
-2
@@ -15,7 +15,13 @@
|
||||
#define TELNET_TASK_STACK 4096
|
||||
#define TELNET_TASK_PRIO PRIO_LOW
|
||||
|
||||
#define MBIFC_TASK_STACK 4096
|
||||
#define MBIFC_TASK_PRIO PRIO_LOW
|
||||
#define MBIFC_TASK_STACK 6000
|
||||
#define MBIFC_TASK_PRIO PRIO_HIGH
|
||||
|
||||
#define ONEWIRES_TASK_STACK 4096
|
||||
#define ONEWIRES_TASK_PRIO PRIO_NORMAL
|
||||
|
||||
#define FCTL_TASK_STACK 4096
|
||||
#define FCTL_TASK_PRIO PRIO_NORMAL
|
||||
|
||||
#endif //CSPEMU_PRIORITIES_H
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "mbiface.h"
|
||||
#include "actuators.h"
|
||||
#include "fancontrol.h"
|
||||
#include "onewires.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
@@ -57,6 +58,7 @@ void app_main(void) {
|
||||
console_setup_uart_stdio();
|
||||
ESP_ERROR_CHECK(console_start_stdio(NULL, NULL));
|
||||
|
||||
onewires_setup();
|
||||
act_init();
|
||||
mbiface_setup();
|
||||
fancontrol_init();
|
||||
|
||||
Reference in New Issue
Block a user