forked from electro/esp-irblaster
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.
78 lines
2.0 KiB
78 lines
2.0 KiB
#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"
|
|
#include "settings.h"
|
|
#include "recup_types.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 float gTempSensors[2] = {};
|
|
volatile bool tempSensorsOk = false;
|
|
volatile uint16_t gTempsSerial = 0;
|
|
|
|
static void owtask(void *dummy) {
|
|
while (1) {
|
|
cels_t a = 0, b = 0;
|
|
int rv = read_onewires(&a, &b);
|
|
tempSensorsOk = (rv == 0);
|
|
|
|
gTempSensors[0] = a;
|
|
gTempSensors[1] = b;
|
|
gTempsSerial++;
|
|
ESP_LOGI(TAG, "t1 %.2f, t2 %.2f C", a, 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(cels_t *a, cels_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(15)); // to be sure it's done
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
errors[i] = ds18b20_read_temp(sDevs[i], &readings[i]);
|
|
}
|
|
|
|
cels_t val1 = (cels_t) readings[0];
|
|
cels_t val2 = (cels_t) readings[1];
|
|
|
|
if (gSettings.swap_temps) {
|
|
*a = val2;
|
|
*b = val1;
|
|
} else {
|
|
*a = val1;
|
|
*b = val2;
|
|
}
|
|
return (errors[0] == DS18B20_OK) && (errors[1] != DS18B20_OK);
|
|
}
|
|
|