Browse Source

Tidy up.

main
David Antliff 8 years ago
parent
commit
ecfa7a61d8
  1. 67
      main/ds18b20.c

67
main/ds18b20.c

@ -70,6 +70,23 @@ typedef struct
uint8_t crc; uint8_t crc;
} Scratchpad; } Scratchpad;
static void _init(DS18B20_Info * ds18b20_info, OneWireBus * bus)
{
if (ds18b20_info != NULL)
{
ds18b20_info->bus = bus;
memset(&ds18b20_info->rom_code, 0, sizeof(ds18b20_info->rom_code));
ds18b20_info->use_crc = false;
ds18b20_info->resolution = DS18B20_RESOLUTION_INVALID;
ds18b20_info->solo = false; // assume multiple devices unless told otherwise
ds18b20_info->init = true;
}
else
{
ESP_LOGE(TAG, "ds18b20_info is NULL");
}
}
static bool _is_init(const DS18B20_Info * ds18b20_info) static bool _is_init(const DS18B20_Info * ds18b20_info)
{ {
bool ok = false; bool ok = false;
@ -127,6 +144,21 @@ static bool _check_resolution(DS18B20_RESOLUTION resolution)
return (resolution >= DS18B20_RESOLUTION_9_BIT) && (resolution <= DS18B20_RESOLUTION_12_BIT); return (resolution >= DS18B20_RESOLUTION_9_BIT) && (resolution <= DS18B20_RESOLUTION_12_BIT);
} }
static void _wait_for_conversion(DS18B20_RESOLUTION resolution)
{
if (_check_resolution(resolution))
{
int divisor = 1 << (DS18B20_RESOLUTION_12_BIT - resolution);
ESP_LOGD(TAG, "divisor %d", divisor);
float max_conversion_time = (float)T_CONV / (float)divisor;
int ticks = ceil(max_conversion_time / portTICK_PERIOD_MS);
ESP_LOGD(TAG, "wait for conversion: %.3f ms, %d ticks", max_conversion_time, ticks);
// wait at least this maximum conversion time
vTaskDelay(ticks);
}
}
static float _decode_temp(uint8_t lsb, uint8_t msb, DS18B20_RESOLUTION resolution) static float _decode_temp(uint8_t lsb, uint8_t msb, DS18B20_RESOLUTION resolution)
{ {
float result = 0.0f; float result = 0.0f;
@ -197,6 +229,9 @@ static bool _write_scratchpad(const DS18B20_Info * ds18b20_info, const Scratchpa
return result; return result;
} }
// Public API
DS18B20_Info * ds18b20_malloc(void) DS18B20_Info * ds18b20_malloc(void)
{ {
DS18B20_Info * ds18b20_info = malloc(sizeof(*ds18b20_info)); DS18B20_Info * ds18b20_info = malloc(sizeof(*ds18b20_info));
@ -223,23 +258,6 @@ void ds18b20_free(DS18B20_Info ** ds18b20_info)
} }
} }
static void _init(DS18B20_Info * ds18b20_info, OneWireBus * bus)
{
if (ds18b20_info != NULL)
{
ds18b20_info->bus = bus;
memset(&ds18b20_info->rom_code, 0, sizeof(ds18b20_info->rom_code));
ds18b20_info->use_crc = false;
ds18b20_info->resolution = DS18B20_RESOLUTION_INVALID;
ds18b20_info->solo = false; // assume multiple devices unless told otherwise
ds18b20_info->init = true;
}
else
{
ESP_LOGE(TAG, "ds18b20_info is NULL");
}
}
void ds18b20_init(DS18B20_Info * ds18b20_info, OneWireBus * bus, OneWireBus_ROMCode rom_code) void ds18b20_init(DS18B20_Info * ds18b20_info, OneWireBus * bus, OneWireBus_ROMCode rom_code)
{ {
if (ds18b20_info != NULL) if (ds18b20_info != NULL)
@ -342,21 +360,6 @@ DS18B20_RESOLUTION ds18b20_read_resolution(DS18B20_Info * ds18b20_info)
return resolution; return resolution;
} }
static void _wait_for_conversion(DS18B20_RESOLUTION resolution)
{
if (_check_resolution(resolution))
{
int divisor = 1 << (DS18B20_RESOLUTION_12_BIT - resolution);
ESP_LOGD(TAG, "divisor %d", divisor);
float max_conversion_time = (float)T_CONV / (float)divisor;
int ticks = ceil(max_conversion_time / portTICK_PERIOD_MS);
ESP_LOGD(TAG, "wait for conversion: %.3f ms, %d ticks", max_conversion_time, ticks);
// wait at least this maximum conversion time
vTaskDelay(ticks);
}
}
float ds18b20_get_temp(const DS18B20_Info * ds18b20_info) float ds18b20_get_temp(const DS18B20_Info * ds18b20_info)
{ {
float temp = 0.0f; float temp = 0.0f;

Loading…
Cancel
Save