Skip to content

Commit

Permalink
Merge pull request #1 from matthias-bs/refactor-adc
Browse files Browse the repository at this point in the history
Refactored ADC handling
  • Loading branch information
matthias-bs authored Apr 13, 2024
2 parents 8124ac6 + 05e6326 commit 8f0a5ba
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 110 deletions.
45 changes: 43 additions & 2 deletions config.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
///////////////////////////////////////////////////////////////////////////////
// config.h
//
// RadioLib / LoRaWAN specific configuration including radio module wiring
//
// based on https://github.com/radiolib-org/radiolib-persistence
//
// created: 04/2024
//
//
// MIT License
//
// Copyright (c) 2024 Matthias Prinke
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//
// History:
//
// 20240412 Created
//
// ToDo:
// -
//
///////////////////////////////////////////////////////////////////////////////

#ifndef _CONFIG_H
#define _CONFIG_H

Expand Down Expand Up @@ -43,7 +85,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0
// pinmap could not be determined - see the notes for more info

// See https://github.com/espressif/arduino-esp32/pull/9250
#define FIREBEETLE_ESP32_COVER_LORA
//#define FIREBEETLE_ESP32_COVER_LORA

// Adafruit
#if defined(ARDUINO_FEATHER_ESP32)
Expand Down Expand Up @@ -219,7 +261,6 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0
#pragma message("Required wiring: D2 to RESET, D3 to DIO0, D4 to CS, D5 to DIO1")
#define LORA_CHIP SX1276


#else
#pragma message ("Unknown board - no automagic pinmap available")

Expand Down
32 changes: 16 additions & 16 deletions src/BresserWeatherSensorLWCfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
//
// 20240407 Created from BresserWeatherSensorTTNCfg.h
// 20240410 Removed obsolete defines
// 20240413 Refactored ADC handling
//
// Note:
// Depending on board package file date, either
Expand Down Expand Up @@ -251,11 +252,8 @@
// Enable rain data statistics
#define RAINDATA_EN

#if !defined(ARDUINO_M5STACK_Core2) && !defined(ARDUINO_M5STACK_CORE2)
// Enable battery / supply voltage measurement
// Note: For M5Stack Core2 use 'float batVoltage = M5.Axp.GetBatVoltage();'
// Enable battery / supply voltage uplink
#define ADC_EN
#endif

// Enable OneWire temperature measurement
// #define ONEWIRE_EN
Expand Down Expand Up @@ -291,32 +289,34 @@
// Adafruit Feather ESP32: on-board connection to VBAT
// Adafruit Feather ESP32-S2: no VBAT input circuit
// Adafruit Feather RP2040: no VBAT input circuit (connect external divider to A0)
#ifdef ADC_EN
#if defined(ARDUINO_TTGO_LoRa32_V1) || defined(ARDUINO_TTGO_LoRa32_V2) || defined(ARDUINO_TTGO_LoRa32_v21new)
#define PIN_ADC_IN 35
#elif defined(ARDUINO_FEATHER_ESP32)
#define PIN_ADC_IN A13
#elif defined(LORAWAN_NODE) || defined(FIREBEETLE_ESP32_COVER_LORA)
#elif defined(LORAWAN_NODE)
// External Li-Ion Battery connected to solar charger
#define PIN_ADC_IN A3
#elif defined(FIREBEETLE_ESP32_COVER_LORA)
// On-board VB
#define PIN_ADC_IN A0
#elif defined(ARDUINO_M5STACK_Core2) || defined(ARDUINO_M5STACK_CORE2)
// Unused
#define PIN_ADC_IN -1
#elif defined(ARDUINO_ADAFRUIT_FEATHER_RP2040)
#define PIN_ADC_IN A0
#else
#define PIN_ADC_IN 34
#endif
#endif

// Additional ADC pins (default: FireBeetle ESP32)
// #define PIN_ADC0_IN A0
// #define PIN_ADC1_IN A1
// #define PIN_ADC2_IN A2
#if defined(LORAWAN_NODE) || defined(FIREBEETLE_ESP32_COVER_LORA)
#define PIN_ADC3_IN A3
// Additional ADC pins
#if defined(LORAWAN_NODE)
#define PIN_SUPPLY_IN A0
#endif

#ifdef PIN_ADC0_IN
#ifdef PIN_SUPPLY_IN
// Voltage divider R1 / (R1 + R2) -> V_meas = V(R1 + R2); V_adc = V(R1)
const float ADC0_DIV = 0.5;
const uint8_t ADC0_SAMPLES = 10;
const float SUPPLY_DIV = 0.5;
const uint8_t SUPPLY_SAMPLES = 10;
#endif

#ifdef PIN_ADC1_IN
Expand Down
60 changes: 5 additions & 55 deletions src/adc/adc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// History:
// 20240405 Created
// 20240410 Added RP2040 specific implementation
// 20240413 Refactored ADC handling
//
// ToDo:
// -
Expand All @@ -44,36 +45,18 @@
#include <M5Unified.h>
#endif

#if defined(ESP32) && defined(ADC_EN)
// ESP32 ADC with calibration
ESP32AnalogRead adc; //!< ADC object for supply voltage measurement

// ESP32 ADC with calibration
#if defined(PIN_ADC0_IN)
ESP32AnalogRead adc0; //!< ADC object
#endif
#if defined(PIN_ADC1_IN)
ESP32AnalogRead adc1; //!< ADC object
#endif
#if defined(PIN_ADC2_IN)
ESP32AnalogRead adc2; //!< ADC object
#endif
#if defined(PIN_ADC3_IN)
ESP32AnalogRead adc3; //!< ADC object
#endif
#endif
//
// Get supply / battery voltage
// Get voltage
//
#if defined(ADC_EN)
uint16_t
getVoltage(void)
getVoltage(uint8_t pin, uint8_t samples, float div)
{
float voltage_raw = 0;
for (uint8_t i = 0; i < UBATT_SAMPLES; i++)
{
#if defined(ESP32)
voltage_raw += float(adc.readMiliVolts());
voltage_raw += float(analogReadMilliVolts(PIN_ADC_IN));
#else
voltage_raw += float(analogRead(PIN_ADC_IN)) / 4095.0 * 3300;
#endif
Expand All @@ -84,7 +67,7 @@ getVoltage(void)

return voltage;
}
#endif


uint16_t getBatteryVoltage(void)
{
Expand All @@ -101,37 +84,4 @@ uint16_t getBatteryVoltage(void)
return 0;
#endif
}
//
// Get an additional voltage
//
#if defined(ESP32) && defined(ADC_EN)
uint16_t
getVoltage(ESP32AnalogRead &adc, uint8_t samples, float divider)
{
float voltage_raw = 0;
for (uint8_t i = 0; i < samples; i++)
{
voltage_raw += float(adc.readMiliVolts());
}
uint16_t voltage = int(voltage_raw / samples / divider);

log_d("Voltage = %dmV", voltage);

return voltage;
}
#elif defined(ARDUINO_ARCH_RP2040)
uint16_t
getVoltage(pin_size_t pin, uint8_t samples, float divider)
{
float voltage_raw = 0;
for (uint8_t i = 0; i < samples; i++)
{
voltage_raw += float(analogRead(pin)) / 4095.0 * 3.3;
}
uint16_t voltage = int(voltage_raw / samples / divider);

log_d("Voltage = %dmV", voltage);

return voltage;
}
#endif
27 changes: 5 additions & 22 deletions src/adc/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// History:
// 20240405 Created
// 20240410 Added RP2040 specific implementation
// 20240413 Refactored ADC handling
//
// ToDo:
// -
Expand All @@ -43,21 +44,17 @@
#include <Arduino.h>
#include "../BresserWeatherSensorLWCfg.h"

#if defined(ESP32) && defined(ADC_EN)
// ESP32 calibrated Analog Input Reading
#include <ESP32AnalogRead.h>
#endif

#if defined(ADC_EN)
/*!
* \brief Get supply / battery voltage
*
* Returns the default voltage measurement provided by the specified board.
* Returns a voltage measurement with oversampling and divider
*
* \returns Voltage in mV
*/
uint16_t getVoltage(void);
#endif

uint16_t getVoltage(uint8_t pin = PIN_ADC_IN, uint8_t samples = UBATT_SAMPLES, float divider = UBATT_DIV);


/*!
* \brief Get battery voltage
Expand All @@ -68,18 +65,4 @@ uint16_t getVoltage(void);
*/
uint16_t getBatteryVoltage(void);

/*!
* \brief Get an additional voltage
*
* Returns the voltage of the specified ADC channel
* (architecture, board and configuration specific)
*
* \returns Voltage in mV
*/
#if defined(ESP32) && defined(ADC_EN)
uint16_t getVoltage(ESP32AnalogRead &adc, uint8_t samples, float divider);
#elif defined(ARDUINO_ARCH_RP2040)
uint16_t getVoltage(pin_size_t pin, uint8_t samples, float divider);
#endif

#endif // _ADC_H
23 changes: 8 additions & 15 deletions src/payload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
// History:
//
// 20240402 Created
// 20240413 Refactored ADC handling
//
// ToDo:
// -
Expand All @@ -47,9 +48,6 @@
#include "BresserWeatherSensorLWCfg.h"
#include "adc/adc.h"

#if defined(ESP32) && defined(ADC_EN) && defined(PIN_ADC3_IN)
extern ESP32AnalogRead adc3;
#endif

#if defined(MITHERMOMETER_EN)
// BLE Temperature/Humidity Sensor
Expand Down Expand Up @@ -137,15 +135,10 @@ void genPayload(uint8_t port, LoraEncoder &encoder)

void getPayloadStage1(uint8_t port, LoraEncoder &encoder)
{
// uint8_t result;
#ifdef ADC_EN
uint16_t supply_voltage = getVoltage();
#endif
#if defined(ADC_EN) && defined(PIN_ADC3_IN)
// FIXME linker error
uint16_t battery_voltage = getVoltage(adc3, ADC3_SAMPLES, ADC3_DIV);
// uint16_t battery_voltage = 0;
#ifdef PIN_SUPPLY_IN
uint16_t supply_voltage = getVoltage(PIN_SUPPLY_IN, SUPPLY_SAMPLES, SUPPLY_DIV);
#endif
uint16_t battery_voltage = getBatteryVoltage();
bool mithermometer_valid = false;
#if defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)
float indoor_temp_c;
Expand Down Expand Up @@ -346,10 +339,10 @@ void getPayloadStage1(uint8_t port, LoraEncoder &encoder)
log_i("Distance: ---- mm");
}
#endif
#ifdef ADC_EN
#ifdef PIN_SUPPLY_IN
log_i("Supply Voltage: %4d mV", supply_voltage);
#endif
#if defined(ADC_EN) && defined(PIN_ADC3_IN)
#if defined(ADC_EN) && defined(PIN_ADC_IN)
log_i("Battery Voltage: %4d mV", battery_voltage);
#endif

Expand Down Expand Up @@ -464,10 +457,10 @@ void getPayloadStage1(uint8_t port, LoraEncoder &encoder)
}

// Voltages / auxiliary sensor data
#ifdef ADC_EN
#ifdef PIN_SUPPLY_IN
encoder.writeUint16(supply_voltage);
#endif
#if defined(ADC_EN) && defined(PIN_ADC3_IN)
#if defined(PIN_ADC_IN)
encoder.writeUint16(battery_voltage);
#endif
#ifdef ONEWIRE_EN
Expand Down

0 comments on commit 8f0a5ba

Please sign in to comment.