diff options
author | Joey Castillo <jose.castillo@gmail.com> | 2021-11-22 17:31:51 -0500 |
---|---|---|
committer | Joey Castillo <jose.castillo@gmail.com> | 2021-11-22 17:31:51 -0500 |
commit | 0ca729eaba7cdca543bc563912095df98f2b3786 (patch) | |
tree | 9b8f74e4fb698bcc3bf70f5b6cfb84fb8f9b604c /movement/watch_faces | |
parent | fb8f4584a59b1f1ffc804a7aa8311e91c1629f94 (diff) | |
download | Sensor-Watch-0ca729eaba7cdca543bc563912095df98f2b3786.tar.gz Sensor-Watch-0ca729eaba7cdca543bc563912095df98f2b3786.tar.bz2 Sensor-Watch-0ca729eaba7cdca543bc563912095df98f2b3786.zip |
move thermistor calculation to watch utilities
Diffstat (limited to 'movement/watch_faces')
-rw-r--r-- | movement/watch_faces/thermistor/thermistor_driver.c | 82 | ||||
-rw-r--r-- | movement/watch_faces/thermistor/thermistor_driver.h | 14 |
2 files changed, 30 insertions, 66 deletions
diff --git a/movement/watch_faces/thermistor/thermistor_driver.c b/movement/watch_faces/thermistor/thermistor_driver.c index 9e5d6fd7..37b5ba3f 100644 --- a/movement/watch_faces/thermistor/thermistor_driver.c +++ b/movement/watch_faces/thermistor/thermistor_driver.c @@ -1,76 +1,34 @@ #include "thermistor_driver.h" #include "watch.h" - -#define THERMISTOR_B_COEFFICIENT (3950.0) -#define THERMISTOR_NOMINAL_TEMPERATURE (25.0) -#define THERMISTOR_NOMINAL_RESISTANCE (10000.0) -#define THERMISTOR_SERIES_RESISTANCE (10000.0) - -// TODO: we really need a math library. -uint32_t msb(uint32_t v); -double ln(double y); +#include "watch_utility.h" void thermistor_driver_enable() { // Enable the ADC peripheral, which we'll use to read the thermistor value. watch_enable_adc(); - // Enable analog circuitry on pin A1, which is tied to the thermistor resistor divider. - watch_enable_analog_input(A1); - // Enable digital output on A0, which is the power to the thermistor circuit. - watch_enable_digital_output(A0); + // Enable analog circuitry on the sense pin, which is tied to the thermistor resistor divider. + watch_enable_analog_input(THERMISTOR_SENSE_PIN); + // Enable digital output on the enable pin, which is the power to the thermistor circuit. + watch_enable_digital_output(THERMISTOR_ENABLE_PIN); + // and make sure it's off. + watch_set_pin_level(THERMISTOR_ENABLE_PIN, !THERMISTOR_ENABLE_VALUE); } void thermistor_driver_disable() { - // Enable the ADC peripheral, which we'll use to read the thermistor value. + // Disable the ADC peripheral. watch_disable_adc(); - // Disable analog circuitry on pin A1 to save power. - watch_disable_analog_input(A1); - // Disable A0's output circuitry. - watch_disable_digital_output(A0); + // Disable analog circuitry on the sense pin to save power. + watch_disable_analog_input(THERMISTOR_SENSE_PIN); + // Disable the enable pin's output circuitry. + watch_disable_digital_output(THERMISTOR_ENABLE_PIN); } float thermistor_driver_get_temperature() { - // set A0 high to power the thermistor circuit. - watch_set_pin_level(A0, true); - // get the pin level - uint16_t val = watch_get_analog_pin_level(A1); - // and then set A0 low to power down the thermistor circuit. - watch_set_pin_level(A0, false); - - double reading = (double)val; - reading = (1023.0 * THERMISTOR_SERIES_RESISTANCE) / (reading / 64.0); - reading -= THERMISTOR_SERIES_RESISTANCE; - reading = reading / THERMISTOR_NOMINAL_RESISTANCE; - reading = ln(reading); - reading /= THERMISTOR_B_COEFFICIENT; - reading += 1.0 / (THERMISTOR_NOMINAL_TEMPERATURE + 273.15); - reading = 1.0 / reading; - reading -= 273.15; - - return reading; -} - -uint32_t msb(uint32_t v) { - static const int pos[32] = {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9}; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - v = (v >> 1) + 1; - return pos[(v * 0x077CB531UL) >> 27]; + // set the enable pin to the level that powers the thermistor circuit. + watch_set_pin_level(THERMISTOR_ENABLE_PIN, THERMISTOR_ENABLE_VALUE); + // get the sense pin level + uint16_t value = watch_get_analog_pin_level(THERMISTOR_SENSE_PIN); + // and then set the enable pin to the opposite value to power down the thermistor circuit. + watch_set_pin_level(THERMISTOR_ENABLE_PIN, !THERMISTOR_ENABLE_VALUE); + + return watch_utility_thermistor_temperature(value, THERMISTOR_HIGH_SIDE, THERMISTOR_B_COEFFICIENT, THERMISTOR_NOMINAL_TEMPERATURE, THERMISTOR_NOMINAL_RESISTANCE, THERMISTOR_SERIES_RESISTANCE); } - -double ln(double y) { - int log2; - double divisor, x, result; - - log2 = msb((int)y); // See: https://stackoverflow.com/a/4970859/6630230 - divisor = (double)(1 << log2); - x = y / divisor; // normalized value between [1.0, 2.0] - - result = -1.7417939 + (2.8212026 + (-1.4699568 + (0.44717955 - 0.056570851 * x) * x) * x) * x; - result += ((double)log2) * 0.69314718; // ln(2) = 0.69314718 - - return result; -} - diff --git a/movement/watch_faces/thermistor/thermistor_driver.h b/movement/watch_faces/thermistor/thermistor_driver.h index 837eb15b..a0b197de 100644 --- a/movement/watch_faces/thermistor/thermistor_driver.h +++ b/movement/watch_faces/thermistor/thermistor_driver.h @@ -1,10 +1,16 @@ #ifndef THERMISTOR_DRIVER_H_ #define THERMISTOR_DRIVER_H_ -// NOTE: This implementation is specific to one prototype sensor board, OSO-MISC-21-009, but both -// the sensor board design and this implementation are likely to change. Thermistor functionality -// may even end up being baked into the Sensor Watch library. This is all by way of saying this -// code is very temporary and the thermistor screens will likely get a rewrite in the future. +// TODO: Do these belong in movement_config.h? In settings we can set on the watch? In an EEPROM configuration area? +// Think on this. [joey 11/22] +#define THERMISTOR_SENSE_PIN (A2) +#define THERMISTOR_ENABLE_PIN (A0) +#define THERMISTOR_ENABLE_VALUE (false) +#define THERMISTOR_HIGH_SIDE (true) +#define THERMISTOR_B_COEFFICIENT (3380.0) +#define THERMISTOR_NOMINAL_TEMPERATURE (25.0) +#define THERMISTOR_NOMINAL_RESISTANCE (10000.0) +#define THERMISTOR_SERIES_RESISTANCE (10000.0) void thermistor_driver_enable(); void thermistor_driver_disable(); |