Files
MeshCore/variants/meshtracker_x1/MeshTrackerX1Board.h
T
mikecarper ab6412e73c Add persistent battery ADC calibration across boards
Add meter-reference calibration with saved ADC corrections, checked persistence and rollback. Preserve existing board coefficients and nRF52 voltage policy, and merge the Mini Repeater feature gates.

Validation: 69 focused tests, hosted documentation checks, and four firmware builds passed. Mini Repeater retains calibration and its existing filesystem with 376 flash bytes free; earlier Mini Sensor size failures remain documented.
2026-10-22 18:56:32 -07:00

103 lines
2.4 KiB
C++

#pragma once
#include <MeshCore.h>
#include <Arduino.h>
#include <helpers/NRF52Board.h>
class MeshTrackerX1Board : public NRF52BoardDCDC {
protected:
uint8_t btn_prev_state;
public:
MeshTrackerX1Board() : NRF52Board("X1_OTA") {}
void begin();
bool supportsBatteryCalibration() const override {
#ifdef BATTERY_PIN
return true;
#else
return false;
#endif
}
uint16_t getBattMilliVolts() override {
#ifdef BATTERY_PIN
#ifdef PIN_BAT_ADC_EN
digitalWrite(PIN_BAT_ADC_EN, HIGH);
#endif
analogReference(AR_INTERNAL_3_0);
analogReadResolution(12);
delay(10);
const int raw = analogRead(BATTERY_PIN);
#ifdef PIN_BAT_ADC_EN
// The divider is only needed while sampling; do not leave it powered
// between battery checks, including after an invalid ADC result.
digitalWrite(PIN_BAT_ADC_EN, LOW);
#endif
analogReference(AR_DEFAULT); // put back to default
analogReadResolution(10);
if (raw < 0 || raw > 4095) return 0;
float volts = (raw * ADC_MULTIPLIER * AREF_VOLTAGE) / 4096;
return calibrateBatteryMillivolts(volts * 1000);
#else
return 0;
#endif
}
const char* getManufacturerName() const override {
return "Seeed SenseCAP MeshTracker X1";
}
int buttonStateChanged() {
#ifdef BUTTON_PIN
uint8_t v = digitalRead(BUTTON_PIN);
if (v != btn_prev_state) {
btn_prev_state = v;
return (v == USER_BTN_PRESSED) ? 1 : -1;
}
#endif
return 0;
}
void shutdownPeripherals() override {
NRF52Board::shutdownPeripherals();
#ifdef HAS_GPS
digitalWrite(GPS_VRTC_EN, LOW);
digitalWrite(GPS_RESET, LOW);
digitalWrite(GPS_SLEEP_INT, LOW);
digitalWrite(GPS_RTC_INT, LOW);
digitalWrite(GPS_EN, LOW);
#endif
#ifdef PIN_DRV_EN
digitalWrite(PIN_DRV_EN, LOW);
#endif
#ifdef PIN_BAT_ADC_EN
digitalWrite(PIN_BAT_ADC_EN, LOW);
#endif
#ifdef PIN_3V3_EN
digitalWrite(PIN_3V3_EN, LOW);
#endif
// set led on and wait for button release before poweroff
#ifdef LED_PIN
if (mesh::leds::enabled()) digitalWrite(LED_PIN, HIGH);
#endif
#ifdef BUTTON_PIN
while(digitalRead(BUTTON_PIN));
#endif
#ifdef LED_PIN
digitalWrite(LED_PIN, LOW);
#endif
#ifdef BUTTON_PIN
nrf_gpio_cfg_sense_input(BUTTON_PIN, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
#endif
}
};