Add explicit SolarXiao Advanced carrier selection and capability reporting
This commit is contained in:
1 parent
204fc5265b
commit
e6b08cafab
11 files changed
+700
-12
No files matched your search
@@ -0,0 +1,93 @@
|
||||
#include "SolarXiaoCarrier.h"
|
||||
#if defined(MESH_SOLARXIAO_POWER) && MESH_SOLARXIAO_POWER
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace mesh { namespace solar {
|
||||
namespace {
|
||||
// Complete canonical records detect bad version, length, value or complement.
|
||||
// The filesystem adapter also verifies atomic publication and public readback.
|
||||
const uint8_t images[2][CarrierSelection::IMAGE_SIZE] = {
|
||||
{'S','X','B',1,0,0xff,0xa5,0x5a},
|
||||
{'S','X','B',1,1,0xfe,0xa5,0x5a}
|
||||
};
|
||||
}
|
||||
|
||||
void CarrierSelection::begin() {
|
||||
type=Mppt; saved=false; healthy=store.available();
|
||||
if (!healthy || !store.exists()) return;
|
||||
uint8_t image[IMAGE_SIZE];
|
||||
if (!store.load(image,sizeof(image)) || image[4]>1
|
||||
|| memcmp(image,images[image[4]],sizeof(image))) {
|
||||
healthy=false;
|
||||
return;
|
||||
}
|
||||
type=static_cast<Type>(image[4]); saved=true;
|
||||
}
|
||||
|
||||
bool CarrierSelection::select(Type requested) {
|
||||
if (!store.available() || !store.save(images[requested],IMAGE_SIZE)) {
|
||||
// A failed publication/readback may leave either record live on disk.
|
||||
// Block native I2C/replay until an explicit selection succeeds or reloads.
|
||||
healthy=false;
|
||||
return false;
|
||||
}
|
||||
type=requested; saved=true; healthy=true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CarrierSelection::handle(const char* command,char* reply,size_t capacity,
|
||||
float battery_v,float mcu_c,bool history_available) {
|
||||
if (!command || !reply || !capacity) return false;
|
||||
if (!strcmp(command,"get solar.board")) {
|
||||
if (!healthy) snprintf(reply,capacity,"Err - carrier selection unconfirmed; set solar.board advanced|mppt");
|
||||
else snprintf(reply,capacity,"> %s source=%s (configured, not autodetected)",
|
||||
type==Advanced?"advanced":"mppt",saved?"saved":"default");
|
||||
return true;
|
||||
}
|
||||
if (!strncmp(command,"set solar.board",15) && (!command[15] || command[15]==' ')) {
|
||||
const char* value=command+15;
|
||||
while (*value==' ') ++value;
|
||||
if (strcmp(value,"advanced") && strcmp(value,"mppt")) {
|
||||
snprintf(reply,capacity,"Err - use set solar.board advanced|mppt");
|
||||
} else if (!select(!strcmp(value,"advanced")?Advanced:Mppt)) {
|
||||
snprintf(reply,capacity,"Err - carrier save unconfirmed; native access blocked; repeat selection");
|
||||
} else if (type==Advanced) {
|
||||
snprintf(reply,capacity,"OK - Advanced saved; native LTC/INA access disabled; self ADC/MCU telemetry retained");
|
||||
} else {
|
||||
snprintf(reply,capacity,"OK - MPPT saved; native access and retained charger overrides re-enabled");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (strncmp(command,"get solar",9) || (command[9] && command[9]!=' ')) return false;
|
||||
const char* key=command+9; if (*key==' ') ++key;
|
||||
if (!strcmp(key,"readings") || !strcmp(key,"battery")) {
|
||||
char battery[24],temperature[24];
|
||||
if (isfinite(battery_v) && battery_v>0) snprintf(battery,sizeof(battery),"%.3fV",double(battery_v));
|
||||
else snprintf(battery,sizeof(battery),"unavailable");
|
||||
if (isfinite(mcu_c)) snprintf(temperature,sizeof(temperature),"%.2fC",double(mcu_c));
|
||||
else snprintf(temperature,sizeof(temperature),"unavailable");
|
||||
snprintf(reply,capacity,"> XIAO battery=%s MCU=%s (processor); self channel=1",battery,temperature);
|
||||
} else if (!strcmp(key,"history")) {
|
||||
snprintf(reply,capacity,history_available
|
||||
? "> get telemetry.volt [1-7]; get telemetry.temp [1-7]; 30min samples, 7d RAM history; voltage trend does not measure charge current"
|
||||
: "Err - telemetry history is not included in this build");
|
||||
} else if (!healthy) {
|
||||
snprintf(reply,capacity,"Err - carrier selection unconfirmed; self readings available; set solar.board advanced|mppt");
|
||||
} else if (!*key || !strcmp(key,"info")) {
|
||||
snprintf(reply,capacity,"> carrier=%s source=%s charger=%s; %s",
|
||||
type==Advanced?"advanced":"mppt",saved?"configured":"default",
|
||||
type==Advanced?"BQ24650":"LTC4162",
|
||||
type==Advanced?"ADC/MCU only; charger settings hardware-only":"use get mppt for live chip identity/readings");
|
||||
} else if (!strcmp(key,"capabilities")) {
|
||||
snprintf(reply,capacity,type==Advanced
|
||||
? "> Advanced: battery ADC + MCU temp; solar V/I, battery I/W, NTC and charger flags unwired; settings hardware-only"
|
||||
: "> MPPT: self ADC/MCU + INA260 V/A/W + LTC4162 readings/settings when reachable; use get mppt fields 0");
|
||||
} else {
|
||||
snprintf(reply,capacity,"Err - use get solar info|readings|capabilities|history; get/set solar.board");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}} // namespace mesh::solar
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "SolarXiaoPowerCore.h"
|
||||
|
||||
namespace mesh { namespace solar {
|
||||
|
||||
// Carrier identity is selected by the operator. A missing/unpowered I2C
|
||||
// peripheral cannot distinguish the Advanced board from the MPPT board.
|
||||
class CarrierSelection {
|
||||
public:
|
||||
enum Type : uint8_t { Mppt = 0, Advanced = 1 };
|
||||
static constexpr size_t IMAGE_SIZE = 8;
|
||||
|
||||
private:
|
||||
Store& store;
|
||||
Type type = Mppt;
|
||||
bool healthy = false;
|
||||
bool saved = false;
|
||||
bool select(Type requested);
|
||||
|
||||
public:
|
||||
explicit CarrierSelection(Store& persistence) : store(persistence) {}
|
||||
void begin();
|
||||
bool nativeAllowed() const { return healthy && type == Mppt; }
|
||||
bool isAdvanced() const { return healthy && type == Advanced; }
|
||||
bool settingsHealthy() const { return healthy; }
|
||||
bool handle(const char* command, char* reply, size_t capacity,
|
||||
float xiao_battery_v, float mcu_c, bool history_available);
|
||||
};
|
||||
|
||||
}} // namespace mesh::solar
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "SolarXiaoPower.h"
|
||||
#if defined(MESH_SOLARXIAO_POWER) && MESH_SOLARXIAO_POWER
|
||||
#include "SolarXiaoPowerCore.h"
|
||||
#include "SolarXiaoCarrier.h"
|
||||
#include <helpers/CheckedSettingsFile.h>
|
||||
#include <helpers/FilePresence.h>
|
||||
#include <Wire.h>
|
||||
#include <math.h>
|
||||
|
||||
@@ -55,21 +57,68 @@ public:
|
||||
}
|
||||
};
|
||||
constexpr const char* CarrierStore::path;
|
||||
class SelectionStore : public Store {
|
||||
public:
|
||||
FILESYSTEM* fs=nullptr;
|
||||
static constexpr const char* path="/solar_board";
|
||||
bool available() const override { return fs!=nullptr; }
|
||||
bool exists() const override {
|
||||
bool present=false;
|
||||
// Unknown metadata must force a complete checked read, never a default.
|
||||
return fs && (!mesh::filePresence(fs,path,present) || present);
|
||||
}
|
||||
bool load(uint8_t* data,size_t size) override {
|
||||
return storage::readFixedSettingsFile(fs,path,data,size);
|
||||
}
|
||||
bool save(const uint8_t* data,size_t size) override {
|
||||
return storage::writeCheckedSettingsFile(fs,path,data,size)
|
||||
== AtomicFileWriter::Outcome::Committed;
|
||||
}
|
||||
};
|
||||
constexpr const char* SelectionStore::path;
|
||||
MainBoard* main_board=nullptr;
|
||||
bool history_available=false;
|
||||
CarrierBus transport;
|
||||
CarrierStore persistence;
|
||||
SelectionStore selection_store;
|
||||
CarrierSelection selection(selection_store);
|
||||
Controller controller(transport,persistence);
|
||||
}
|
||||
|
||||
void beginSettings(FILESYSTEM* fs) {
|
||||
void beginSettings(MainBoard& board,FILESYSTEM* fs,bool telemetry_history_available) {
|
||||
const volatile char* marker=meshcore_capability_solarxiao_power;
|
||||
(void)*marker;
|
||||
main_board=&board;
|
||||
history_available=telemetry_history_available;
|
||||
selection_store.fs=fs;
|
||||
selection.begin();
|
||||
persistence.fs=fs; controller.begin();
|
||||
}
|
||||
void service() { controller.service(millis()); }
|
||||
void service() { if (selection.nativeAllowed()) controller.service(millis()); }
|
||||
bool handleCommand(const char* command,char* reply,size_t capacity,bool local) {
|
||||
float battery=NAN,temperature=NAN;
|
||||
if (main_board && command && (!strcmp(command,"get solar readings") || !strcmp(command,"get solar battery"))) {
|
||||
battery=main_board->getBattMilliVolts()/1000.0f;
|
||||
temperature=main_board->getMCUTemperature();
|
||||
}
|
||||
const bool native_before=selection.nativeAllowed();
|
||||
if (selection.handle(command,reply,capacity,battery,temperature,history_available)) {
|
||||
if (!native_before && selection.nativeAllowed()) controller.reload();
|
||||
return true;
|
||||
}
|
||||
if (!command || !reply || !capacity) return false;
|
||||
const bool mppt=(!strncmp(command,"get mppt",8) && (!command[8] || command[8]==' '))
|
||||
|| !strncmp(command,"set mppt ",9) || !strncmp(command,"mppt ",5);
|
||||
if (mppt && !selection.nativeAllowed()) {
|
||||
snprintf(reply,capacity,selection.isAdvanced()
|
||||
? "Err - BQ24650 Advanced has no digital charger/INA interface; use get solar readings"
|
||||
: "Err - carrier selection unconfirmed; set solar.board advanced|mppt");
|
||||
return true;
|
||||
}
|
||||
return controller.handle(command,reply,capacity,local);
|
||||
}
|
||||
void addTelemetry(CayenneLPP& lpp) {
|
||||
if (!selection.nativeAllowed()) return;
|
||||
Snapshot s; controller.snapshot(s);
|
||||
if (s.monitor) {
|
||||
lpp.addVoltage(2,s.battery_v); lpp.addCurrent(2,s.battery_a); lpp.addPower(2,s.battery_w);
|
||||
@@ -89,12 +138,13 @@ void addTelemetry(CayenneLPP& lpp) {
|
||||
for (unsigned i=0;i<3;++i) lpp.addGenericSensor(11+i,s.alerts[i]);
|
||||
}
|
||||
uint8_t voltageChannels(uint8_t* channels,uint8_t capacity) {
|
||||
if (!selection.nativeAllowed()) return 0;
|
||||
const uint8_t count=capacity<4 ? capacity : 4;
|
||||
for (uint8_t i=0;channels && i<count;++i) channels[i]=2+i;
|
||||
return count;
|
||||
}
|
||||
uint8_t queryVoltages(SensorManager::VoltageSensorReading* readings,uint8_t capacity) {
|
||||
if (!readings || !capacity) return 0;
|
||||
if (!selection.nativeAllowed() || !readings || !capacity) return 0;
|
||||
Snapshot s; controller.snapshot(s);
|
||||
const float volts[]={s.battery_v,s.vin_v,s.vout_v,s.charger_vbat_v};
|
||||
const uint8_t count=capacity<4 ? capacity : 4;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
namespace mesh { namespace solar {
|
||||
// Stable carrier channels 2..13 precede optional environmental sensors.
|
||||
constexpr uint8_t FIRST_OPTIONAL_CHANNEL = 14;
|
||||
void beginSettings(FILESYSTEM* fs);
|
||||
void beginSettings(MainBoard& board, FILESYSTEM* fs, bool telemetry_history_available);
|
||||
void service();
|
||||
bool handleCommand(const char* command, char* reply, size_t capacity, bool local);
|
||||
void addTelemetry(CayenneLPP& telemetry);
|
||||
|
||||
@@ -303,6 +303,11 @@ bool Controller::identify(uint16_t& identity) {
|
||||
if (cells) last_cells=cells;
|
||||
return true;
|
||||
}
|
||||
void Controller::reload() {
|
||||
started=false;
|
||||
begin();
|
||||
next_service=0;
|
||||
}
|
||||
bool Controller::verifyWritableIdentity(char* reply,size_t capacity) {
|
||||
uint16_t identity;
|
||||
if (!identify(identity)) {snprintf(reply,capacity,"Err - charger unavailable; power SOLAR/VIN"); return false;}
|
||||
@@ -523,7 +528,7 @@ bool Controller::action(const char* args,bool local,char* reply,size_t capacity)
|
||||
return true;
|
||||
}
|
||||
if (!strcmp(args,"reload")) {
|
||||
started=false; begin(); next_service=0;
|
||||
reload();
|
||||
snprintf(reply,capacity,storage_healthy?"OK - saved settings loaded; apply on service":"Err - saved settings invalid/unavailable"); return true;
|
||||
}
|
||||
if (!strcmp(args,"forget")) {
|
||||
|
||||
@@ -70,6 +70,7 @@ public:
|
||||
settings.defaults();
|
||||
}
|
||||
void begin();
|
||||
void reload();
|
||||
void service(uint32_t now);
|
||||
bool snapshot(Snapshot& result);
|
||||
bool readMonitor(float& volts, float& amps, float& watts);
|
||||
|
||||
Reference in new issue
Block a user