From b52a1217a9f91e3019526e0793da153a30f8ad0a Mon Sep 17 00:00:00 2001 From: Wurstnase Date: Thu, 28 Apr 2016 22:23:39 +0200 Subject: [PATCH] temp.c: refactor MCP3008 to similar structure. --- temp.c | 60 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/temp.c b/temp.c index e527185..c5f5940 100644 --- a/temp.c +++ b/temp.c @@ -140,36 +140,6 @@ void temp_init() { } } -/** - Read a measurement from the MCP3008 analog-digital-converter (ADC). - - \param channel The ADC channel to read. - - \return The raw ADC reading. - - Documentation for this ADC see - - https://www.adafruit.com/datasheets/MCP3008.pdf. -*/ -#ifdef TEMP_MCP3008 -static uint16_t mcp3008_read(uint8_t channel) { - uint8_t temp_h, temp_l; - - spi_select_mcp3008(); - - // Start bit. - spi_rw(0x01); - - // Send read address and get MSB, then LSB byte. - temp_h = spi_rw((0b1000 | channel) << 4) & 0b11; - temp_l = spi_rw(0); - - spi_deselect_mcp3008(); - - return temp_h << 8 | temp_l; -} -#endif /* TEMP_MCP3008 */ - /** Look up a degree Celsius value from a raw ADC reading. @@ -315,10 +285,38 @@ static uint16_t temp_read_thermistor(temp_sensor_t i) { #endif /* TEMP_THERMISTOR */ #ifdef TEMP_MCP3008 +/** + Read a measurement from the MCP3008 analog-digital-converter (ADC). + + \param channel The ADC channel to read. + + \return The raw ADC reading. + + Documentation for this ADC see + + https://www.adafruit.com/datasheets/MCP3008.pdf. +*/ +static uint16_t temp_mcp3008_read(uint8_t channel) { + uint8_t temp_h, temp_l; + + spi_select_mcp3008(); + + // Start bit. + spi_rw(0x01); + + // Send read address and get MSB, then LSB byte. + temp_h = spi_rw((0b1000 | channel) << 4) & 0b11; + temp_l = spi_rw(0); + + spi_deselect_mcp3008(); + + return temp_h << 8 | temp_l; +} + static uint16_t temp_read_mcp3008(temp_sensor_t i) { switch (temp_sensors_runtime[i].active++) { case 1: - return temp_table_lookup(mcp3008_read(temp_sensors[i].temp_pin), i); + return temp_table_lookup(temp_mcp3008_read(temp_sensors[i].temp_pin), i); case 10: // Idle for 100ms. temp_sensors_runtime[i].active = 0; }