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.
71 lines
1.8 KiB
71 lines
1.8 KiB
2 years ago
|
#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;
|
||
|
}
|