diff --git a/fw/lib_i2c.h b/fw/lib_i2c.h index c3bf8fd..e6780f4 100644 --- a/fw/lib_i2c.h +++ b/fw/lib_i2c.h @@ -10,6 +10,16 @@ #define I2C_DEFAULT_TIMEOUT 50000 + +#if defined(FUNCONF_USE_DEBUGPRINTF) || defined(FUNCONF_USE_USBPRINTF) || defined(FUNCONF_USE_UARTPRINTF) +#include +static inline void print_i2c_device(uint8_t addr) +{ + printf("Device found at 0x%02X\n", addr); +} +#endif + + //! #################################### //! I2C INIT FUNCTIONS //! #################################### diff --git a/fw/main.c b/fw/main.c index e46f25f..63053e4 100644 --- a/fw/main.c +++ b/fw/main.c @@ -9,42 +9,18 @@ #include "filter.h" #include "sc7a20.h" #include "pd.h" +#include "ntc.h" #include "fpmath.h" #include "coroutine.h" // constants -// LUT for converting NTC readings to degrees celsius -// Nominal: 10kOhm, Beta: 3380, Step: 64 -// TODO: Add a fixed offset to account for toollerances -const uint8_t ntc_step_size = 64; -const int16_t ntc_lut[] = { - 1316, 197, 155, 133, 119, 108, 100, 93, 87, 82, 77, 73, 69, 66, 63, 60, - 57, 54, 52, 50, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 28, 27, - 25, 23, 22, 20, 19, 17, 15, 14, 12, 11, 9, 7, 6, 4, 2, 0, - -1, -3, -5, -7, -9, -11, -14, -16, -19, -22, -25, -28, -32, -38, -44, -55, - -55 // extra value to not have an extra if statement -}; - - u8g2_t *u8g2; int16_t encoder = 0; // rotary encoder counter uint32_t last_interrupt = 0; // last time the encoder interrupt was triggered struct pd_profile_t pd_profile; uint16_t vcc_mv = 3300; -// Convert the raw adc reading to a temperature in celsius with the ntc lut, -// linearly interpolating between positions -static inline int16_t get_temp_c(uint16_t adc_reading) -{ - if (adc_reading > 4095) return 0; - uint8_t index = adc_reading / ntc_step_size; - uint8_t remainder = adc_reading % ntc_step_size; - int16_t temp_base = index < 64 ? ntc_lut[index] : 0; - int16_t temp_next = ntc_lut[index + 1]; - return temp_base + ((temp_next - temp_base) * remainder)/ntc_step_size; -} - // convert the raw TPA191 adc reading to a current in milliamps static inline int16_t get_current_ma(uint16_t adc_reading) @@ -56,12 +32,6 @@ static inline int16_t get_current_ma(uint16_t adc_reading) } -void print_i2c_device(uint8_t addr) -{ - printf("Device found at 0x%02X\n", addr); -} - - // this callback is mandatory when FUNCONF_USE_USBPRINTF is defined, // can be empty though void handle_usbfs_input(int numbytes, uint8_t *data) diff --git a/fw/ntc.h b/fw/ntc.h new file mode 100644 index 0000000..8b18acd --- /dev/null +++ b/fw/ntc.h @@ -0,0 +1,31 @@ +#ifndef _NTC_H +#define _NTC_H + +#include + +// LUT for converting NTC readings to degrees celsius +// Nominal: 10kOhm, Beta: 3380, Step: 64 +// TODO: Add a fixed offset to account for toollerances +const uint8_t ntc_step_size = 64; +const int16_t ntc_lut[] = { + 1316, 197, 155, 133, 119, 108, 100, 93, 87, 82, 77, 73, 69, 66, 63, 60, + 57, 54, 52, 50, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 28, 27, + 25, 23, 22, 20, 19, 17, 15, 14, 12, 11, 9, 7, 6, 4, 2, 0, + -1, -3, -5, -7, -9, -11, -14, -16, -19, -22, -25, -28, -32, -38, -44, -55, + -55 // extra value to not have an extra if statement +}; + +// Convert the raw adc reading to a temperature in celsius with the ntc lut, +// linearly interpolating between positions +static inline int16_t get_temp_c(uint16_t adc_reading) +{ + if (adc_reading > 4095) return 0; + uint8_t index = adc_reading / ntc_step_size; + uint8_t remainder = adc_reading % ntc_step_size; + int16_t temp_base = index < ntc_step_size ? ntc_lut[index] : 0; + int16_t temp_next = ntc_lut[index + 1]; + return temp_base + ((temp_next - temp_base) * remainder)/ntc_step_size; +} + + +#endif