|
|
|
@ -28,12 +28,19 @@ static int mb_write_and_read(uint8_t *buffer, int num, size_t resplen) { |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// sometimes there's trash in the buffer, get rid of it
|
|
|
|
|
int discarded = 0; |
|
|
|
|
uint8_t junk; |
|
|
|
|
do { |
|
|
|
|
discarded = uart_read_bytes(CO2_UART_NUM, &junk, 1, pdMS_TO_TICKS(2)); |
|
|
|
|
} while(discarded); |
|
|
|
|
|
|
|
|
|
ESP_LOGD(TAG, "Sending"); |
|
|
|
|
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, num, ESP_LOG_DEBUG); |
|
|
|
|
uart_write_bytes(CO2_UART_NUM, buffer, num); |
|
|
|
|
|
|
|
|
|
ESP_LOGD(TAG, "Expect resp of %d bytes", resplen); |
|
|
|
|
num = uart_read_bytes(CO2_UART_NUM, buffer, resplen, pdMS_TO_TICKS(500)); |
|
|
|
|
num = uart_read_bytes(CO2_UART_NUM, buffer, resplen, pdMS_TO_TICKS(TIMEOUT_MS)); |
|
|
|
|
ESP_LOGD(TAG, "Received %d bytes", num); |
|
|
|
|
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, num, ESP_LOG_DEBUG); |
|
|
|
|
|
|
|
|
@ -54,7 +61,7 @@ static void write_saved_calib_to_sensor() { |
|
|
|
|
|
|
|
|
|
/* Write calib */ |
|
|
|
|
resplen = 0; |
|
|
|
|
num = mb_build_fc16(buffer, 128, &resplen, 104, 4, g_Settings.co2_calib, 5); |
|
|
|
|
num = mb_build_fc16(buffer, 128, &resplen, CO2_ADDR, 4, g_Settings.co2_calib, 5); |
|
|
|
|
num = mb_write_and_read(buffer, num, resplen); |
|
|
|
|
resp = mb_parse_fc16(buffer, num); |
|
|
|
|
if (resp < 0) { |
|
|
|
@ -85,6 +92,8 @@ static bool ppm_looks_valid(uint16_t ppm) { |
|
|
|
|
void co2_read_task(void *param) { |
|
|
|
|
(void) param; |
|
|
|
|
|
|
|
|
|
// 68 04 0a 00 00 00 00 00 00 03 b5 03 f2 dd c3
|
|
|
|
|
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(500)); |
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
@ -97,7 +106,7 @@ void co2_read_task(void *param) { |
|
|
|
|
|
|
|
|
|
write_saved_calib_to_sensor(); |
|
|
|
|
|
|
|
|
|
const uint32_t read_cycle_time_ticks = 1000; //10 * 1000;
|
|
|
|
|
const uint32_t read_cycle_time_ticks = 10 * 1000; |
|
|
|
|
const uint32_t calib_persist_time_ticks = 12 * 3600 * 1000; |
|
|
|
|
_Static_assert(configTICK_RATE_HZ == 1000, "1kHz tick"); |
|
|
|
|
|
|
|
|
@ -118,7 +127,7 @@ void co2_read_task(void *param) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
resplen = 0; |
|
|
|
|
num = mb_build_fc4(buffer, 128, &resplen, 104, 0, 5); |
|
|
|
|
num = mb_build_fc4(buffer, 128, &resplen, CO2_ADDR, 0, 5); |
|
|
|
|
num = mb_write_and_read(buffer, num, resplen); |
|
|
|
|
qty = mb_parse_fc4(buffer, num, values, 32); |
|
|
|
|
if (qty < 0) { |
|
|
|
@ -179,7 +188,10 @@ void co2_read_task(void *param) { |
|
|
|
|
|
|
|
|
|
if (!ppm_looks_valid(ppm)) { |
|
|
|
|
ESP_LOGW(TAG, "CO2 measures nonsense!"); |
|
|
|
|
co2_restart(true); |
|
|
|
|
if (ppm != 0) { |
|
|
|
|
// ppm=0 is OK, it means the ppm wasn't measured yet. just ignore it.
|
|
|
|
|
co2_restart(true); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// CO2 measurement looks OK
|
|
|
|
|
|
|
|
|
@ -191,7 +203,7 @@ void co2_read_task(void *param) { |
|
|
|
|
last_calib_persist = tickNow; |
|
|
|
|
|
|
|
|
|
resplen = 0; |
|
|
|
|
num = mb_build_fc3(buffer, 128, &resplen, 104, 4, 5); |
|
|
|
|
num = mb_build_fc3(buffer, 128, &resplen, CO2_ADDR, 4, 5); |
|
|
|
|
num = mb_write_and_read(buffer, num, resplen); |
|
|
|
|
qty = mb_parse_fc3(buffer, num, values, 32); |
|
|
|
|
if (qty < 0) { |
|
|
|
|