59 bool found = false; |
59 bool found = false; |
60 DS18B20_ERROR errors = 0; |
60 DS18B20_ERROR errors = 0; |
61 |
61 |
62 ESP_LOGI(TAG, "Starting DS18B20 sensors"); |
62 ESP_LOGI(TAG, "Starting DS18B20 sensors"); |
63 ds18b20_state = malloc(sizeof(DS18B20_State)); |
63 ds18b20_state = malloc(sizeof(DS18B20_State)); |
64 ds18b20_state->bottle_valid = false; |
64 ds18b20_state->valid = false; |
65 ds18b20_state->bottle_temperature = 0.0; |
65 ds18b20_state->num_sensors = 0; |
66 ds18b20_state->bottle_error = DS18B20_ERR_NOSENSOR; |
66 |
|
67 for (int i = 0; i < DS18B20_MAX; i++) { |
|
68 ds18b20_state->sensor[i].temperature = 0.0; |
|
69 ds18b20_state->sensor[i].rom_code[0] = '\0'; |
|
70 ds18b20_state->sensor[i].error = DS18B20_ERR_READ; |
|
71 } |
67 |
72 |
68 /* event handler and event group for the wifi driver */ |
73 /* event handler and event group for the wifi driver */ |
69 xEventGroupDS18B20 = xEventGroupCreate(); |
74 xEventGroupDS18B20 = xEventGroupCreate(); |
70 |
75 |
71 /* |
76 /* |
72 * Initialize the MLT and HLT one-wire busses. |
77 * Initialize the MLT and HLT one-wire busses. |
73 */ |
78 */ |
74 OneWireBus *owb_bottle; |
79 OneWireBus *owb; |
75 owb_rmt_driver_info rmt_driver_info_bottle; |
80 owb_rmt_driver_info rmt_driver_info_bottle; |
76 owb_bottle = owb_rmt_initialize(&rmt_driver_info_bottle, GPIO_DS18B20_BUS, RMT_CHANNEL_1, RMT_CHANNEL_0); |
81 owb = owb_rmt_initialize(&rmt_driver_info_bottle, GPIO_DS18B20_BUS, RMT_CHANNEL_1, RMT_CHANNEL_0); |
77 owb_use_crc(owb_bottle, true); // enable CRC check for ROM code |
82 owb_use_crc(owb, true); // enable CRC check for ROM code |
78 |
83 |
79 DS18B20_Info * ds18b20_info = ds18b20_malloc(); |
84 DS18B20_Info * ds18b20_info = ds18b20_malloc(); |
80 EventBits_t uxBits; |
85 EventBits_t uxBits; |
81 |
86 |
82 /* |
87 /* |
86 |
91 |
87 uxBits = xEventGroupWaitBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS, pdFALSE, pdFALSE, portMAX_DELAY ); |
92 uxBits = xEventGroupWaitBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS, pdFALSE, pdFALSE, portMAX_DELAY ); |
88 |
93 |
89 if (uxBits & TASK_DS18B20_REQUEST_TEMPS) { |
94 if (uxBits & TASK_DS18B20_REQUEST_TEMPS) { |
90 num_devices = 0; |
95 num_devices = 0; |
91 OneWireBus_SearchState bottle_search_state = {0}; |
96 OneWireBus_SearchState search_state = {0}; |
92 found = false; |
97 found = false; |
93 owb_search_first(owb_bottle, &bottle_search_state, &found); |
98 owb_search_first(owb, &search_state, &found); |
94 while (found) { |
99 while (found) { |
|
100 char rom_code_s[17]; |
|
101 owb_string_from_rom_code(search_state.rom_code, rom_code_s, sizeof(rom_code_s)); |
|
102 rom_code_s[16] = '\0'; |
|
103 printf(" %d : %s %d\n", num_devices + 1, rom_code_s, strlen(rom_code_s)); |
|
104 sprintf(ds18b20_state->sensor[num_devices].rom_code, "%s", rom_code_s); |
95 ++num_devices; |
105 ++num_devices; |
96 owb_search_next(owb_bottle, &bottle_search_state, &found); |
106 owb_search_next(owb, &search_state, &found); |
97 } |
107 } |
98 |
108 |
99 if (num_devices == 1) { |
109 if (num_devices == 1) { |
100 ds18b20_init_solo(ds18b20_info, owb_bottle); // only one device on bus |
110 ds18b20_init_solo(ds18b20_info, owb); // only one device on bus |
101 ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings |
111 ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings |
102 ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION); // returns true if ok. |
112 ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION); // returns true if ok. |
103 |
113 |
104 // Read temperatures more efficiently by starting conversions on all devices at the same time |
114 // Read temperatures more efficiently by starting conversions on all devices at the same time |
105 ds18b20_convert_all(owb_bottle); |
115 ds18b20_convert_all(owb); |
106 ds18b20_wait_for_conversion(ds18b20_info); |
116 ds18b20_wait_for_conversion(ds18b20_info); |
107 |
117 |
108 errors = ds18b20_read_temp(ds18b20_info, &readings); |
118 errors = ds18b20_read_temp(ds18b20_info, &readings); |
109 |
119 |
110 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
120 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
|
121 ds18b20_state->num_sensors = 1; |
111 if (errors == DS18B20_OK) { |
122 if (errors == DS18B20_OK) { |
112 ds18b20_state->bottle_error = DS18B20_ERR_NONE; |
123 ds18b20_state->sensor[0].error = DS18B20_ERR_NONE; |
113 ds18b20_state->bottle_valid = true; |
124 ds18b20_state->valid = true; |
114 ds18b20_state->bottle_temperature = readings; |
125 ds18b20_state->sensor[0].temperature = readings; |
115 } else { |
126 } else { |
116 if (errors == DS18B20_ERROR_CRC) |
127 if (errors == DS18B20_ERROR_CRC) |
117 ds18b20_state->bottle_error = DS18B20_ERR_CRC; |
128 ds18b20_state->sensor[0].error = DS18B20_ERR_CRC; |
118 if (errors == DS18B20_ERROR_OWB) |
129 if (errors == DS18B20_ERROR_OWB) |
119 ds18b20_state->bottle_error = DS18B20_ERR_READ; |
130 ds18b20_state->sensor[0].error = DS18B20_ERR_READ; |
120 if (errors == DS18B20_ERROR_DEVICE) |
131 if (errors == DS18B20_ERROR_DEVICE) |
121 ds18b20_state->bottle_error = DS18B20_ERR_READ; |
132 ds18b20_state->sensor[0].error = DS18B20_ERR_READ; |
122 ds18b20_state->bottle_valid = false; |
133 ds18b20_state->valid = false; |
123 ds18b20_state->bottle_temperature = 0.0; |
134 ds18b20_state->sensor[0].temperature = 0.0; |
124 } |
135 } |
125 xSemaphoreGive(xSemaphoreDS18B20); |
136 xSemaphoreGive(xSemaphoreDS18B20); |
126 } |
137 } |
127 |
138 |
128 } else { |
139 } else { |
129 /* |
140 /* |
130 * Zero or more then one device, this is an error. |
141 * Zero or more then one device, this is an error. |
131 */ |
142 */ |
132 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
143 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
133 if (num_devices == 0) |
144 if (num_devices == 0) |
134 ds18b20_state->bottle_error = DS18B20_ERR_NOSENSOR; |
145 ds18b20_state->sensor[0].error = DS18B20_ERR_READ; |
135 else |
146 else |
136 ds18b20_state->bottle_error = DS18B20_ERR_TOOMANY; |
147 ds18b20_state->sensor[0].error = DS18B20_ERR_READ; |
137 ds18b20_state->bottle_valid = false; |
148 ds18b20_state->valid = false; |
138 ds18b20_state->bottle_temperature = 0.0; |
149 ds18b20_state->num_sensors = 0; |
139 xSemaphoreGive(xSemaphoreDS18B20); |
150 xSemaphoreGive(xSemaphoreDS18B20); |
140 } |
151 } |
141 } // if num_devices == 1 |
152 } // if num_devices == 1 |
142 |
153 |
143 xEventGroupClearBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS); |
154 xEventGroupClearBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS); |
144 xEventGroupSetBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_DONE); |
155 xEventGroupSetBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_DONE); |
145 #if 1 |
156 #if 1 |
146 ESP_LOGI(TAG, "Temperature %.3f %s", ds18b20_state->bottle_temperature, dsErrors[ds18b20_state->bottle_error]); |
157 ESP_LOGI(TAG, "Temperature %.3f %s", ds18b20_state->sensor[0].temperature, dsErrors[ds18b20_state->sensor[0].error]); |
147 #endif |
158 #endif |
148 } |
159 } |
149 vTaskDelay( (TickType_t)10); |
160 vTaskDelay( (TickType_t)10); |
150 } |
161 } |
151 } |
162 } |