refact: ADC, switches and keys

Add "hardware_defs" target to dump hardware defines from hal.h per target

Fixed hal.h definitions for switches & ADC inputs

Tools to generate hardware definitions from current hal.h

generator: add support for EXTx and analog switches

WiP

Add generated header to hardware_defs target

Add support for ADC input direction and index per GPIO port

Incredible: it compiles

Removed StdPeriph ADC driver

Works on QX7

Works on TX16S (VBat missing)

Switch driver using generated switches for stm32 targets

Switch driver with generated switches for simu

Use switch names from switch driver

Fixed single conversion handling

Fixed horus VBat reading

Added switch name

Fix warnings about bitfield offsets and GCC 4.4

This is caused by bitfields that cross type boundaries. In this particular case, it happens as the bitfield is not defined the same in `rtc_backup.cpp`.

Switches has already been moved to generic driver

Jitter measurement for each sample, not just for oversampled values

Moved periph clock init to the ADC driver

ADC & GPIOs.

Add switchInit() to init pins

Fix NV14 CMake

Fix taranis target

Fix NV14 (again)

Model audio files: use new interface for switch index

Fix X9-lite

Fix simu headers

Fix getSwitchName()

Add support for ADC switches

Fix switch warning YAML

Test only switches which are defined

Conversions tests for QX7 only on QX7

Fix X12S

Fix function switch names

Enable ADC periph clock before initialising channels

NV14: offset inputs for the sticks & fix switches

WiP: get rid of VSRCRAW and some more

Simu shall use normal ADC driver

fix x7

fix nv14

Fix 9x nav

fix 212x64

fix xlite

fix x9e

fix simu lib

fix unit tests

fix x7 tests

PWM stick values should be written directly into adcValues

... just like FlySky sticks.

fix x12s

fix nv14

Add some support for SPI ADC (only unit tests)

Remove `jq` usage

fix generate_yaml.py for newer libclang

fix pot config

fix stick names

Some YAML generator improvements

Max 16 Pots on COLORLCD

Fix some source names

Use 10 bits for switches to prevent overflow

Removed horus constants for switches & ADC inputs

Fix unit tests

Fix curve edit

Fix libsimulator

Fix function switches (tpro)

Refine ADC & analog API

Moved hardware definitions scripts and templates into own dir

cleanup API refine

Fix unit tests

Mixer: apply calibration only on required inputs

Place ADC switches after all other ADC inputs

Fix X12S

Remove useless TR_POTS_VSRCRAW, TR_SW_VSRCRAW, and TR_VSRCRAW

Cleanup redundant generated YAML parsers

Fixes Boxer rebase

Add boxer to generate-hw-defs.sh

Obsolete LUA exports

Add boxer ADC_DIRECTION

Split generate_hw_defs.py into multiple files

Some cleanup (+ LUA switches)

Fix AXIS definitions

Fixed translation strings declarations

Some rebase fixes

Remove some useless StdPeriph drivers

fix: set pin mode to reset state on usart deinit

fix: use canonical names for calibration data

fix: ADC driver does not care about unconfigured inputs

fix X12S ADC

Add support for ADC conversion chaining

Better X12S ADC implem

Moved X12S ADC driver to HAL/LL

Improve ADC driver separation

Fixed taranis & nv14

Moved PWM gimbals to HAL/LL

Removed useless TR_EXTRA_VSRCRAW

Fixed include guards

Small fixes

Renamed control inputs

Moved internal and external module pulse driver def to boards

Move more things to the generic_stm32 board

Remove STR_VMIXTRIMS

Added analog labels

Generate main control names

Misuse labels / short labels for main controls

Backward compatibility: sliders and legacy names

New keys driver

Let's get rid of StdPeriph headers in simu

Add jinja2 to CI workflows

Use `grep` instead of `sed`

chore: add python jinja2 oto msys setup script

Removed more static definitions and use dynamic ones instead

Fixes X9E

Fix x7 nav too

212 switch display

Fix default sliders

Removed NUM_STICKS

212 switch display working nicely

Surface radio 'stick' replacements

fix: switch, trim & rotary pin init

Remove old key drivers

Removed VKEYS

Removed ETX_FOURCC

Fixed SURFACE_NAME

Removed TR_VSWITCHES & TR_TRIMS_SWITCHES

Fix xpot switches

Store calibrated analogs in physical order

Use getSourceString in drawSource

Implement missing YAML read/write

Fix isSourceAvailableInInputs()

Fix isSourceAvailable()

Some surface fixes

Fix CLI debug

Ported colour UI of analog diagnostics to new ADC API

Small fixes

Better input mapping

Minor tpro fixes

Fix trims available

Remove TX mode choice on surface radio

Rename surface controls

Channel order alignement

Fix throttle warning

Fix checkTrims()

Fix pot config casting issue

Fix IS_SWITCH_FS()

Fixed input mapping

Fix fswitch auto-switch

Fixed storing / reading function switches in YAML

Fix enableVBatBridge() / disableVBatBridge()

Fix interesting mode conversion

Fix sources issues

Fix special funnction list

Proper function switch handling

128 GUI improvements

Fix compilation

Boxer cosmetics

128 hardware screen cosmetic

Fix TX12 switches

More switches fixes

Simplify CPU type handling

Fix flash size

Fixed page-up / page-down in libopenui
This commit is contained in:
Raphael Coeffic
2023-06-25 14:32:39 +02:00
committed by Malte Langermann
parent b6dd3a0ef8
commit 2b100e0eb5
323 changed files with 12812 additions and 22301 deletions
+294 -256
View File
@@ -24,8 +24,13 @@
#include "yaml_tree_walker.h"
#include "pulses/multi.h"
#include "switches.h"
#include "analogs.h"
#include "stamp.h"
#include "hal/switch_driver.h"
#include "hal/adc_driver.h"
//
// WARNING:
// ========
@@ -41,25 +46,6 @@ static inline void check_yaml_funcs()
static_assert(offsetof(ModuleData, ppm) == 4,"");
check_size<ModuleData, 29>();
static_assert(MAX_GVARS == 9,"");
#if defined(PCBHORUS)
static_assert(offsetof(FlightModeData, gvars) == 26,"");
check_size<FlightModeData, 44>();
check_size<CustomFunctionData, 9>();
#elif defined(PCBNV14)
static_assert(offsetof(FlightModeData, gvars) == 22,"");
check_size<FlightModeData, 40>();
check_size<CustomFunctionData, 9>();
#elif defined(PCBX7) || defined(PCBXLITE) || defined(PCBX9LITE)
static_assert(offsetof(FlightModeData, gvars) == 18,"");
check_size<FlightModeData, 36>();
check_size<CustomFunctionData, 11>();
check_size<TelemetryScreenData, 24>();
#else
static_assert(offsetof(FlightModeData, gvars) == 22,"");
check_size<FlightModeData, 40>();
check_size<CustomFunctionData, 11>();
check_size<TelemetryScreenData, 24>();
#endif
}
static bool w_semver(void* user, uint8_t* data, uint32_t bitoffs,
@@ -120,6 +106,25 @@ bool in_write_weight(const YamlNode* node, uint32_t val, yaml_writer_func wf,
return wf(opaque, s, strlen(s));
}
static int _legacy_input_idx(const char* val, uint8_t val_len)
{
for (uint8_t i = 0; i < DIM(_legacy_inputs); i++){
if (!strncmp(_legacy_inputs[i].legacy, val, val_len))
return i;
}
return -1;
}
static int _legacy_mix_src(const char* val, uint8_t val_len)
{
auto idx = _legacy_input_idx(val, val_len);
if (idx >= 0)
return _legacy_inputs[idx].src_raw;
return -1;
}
extern const struct YamlIdStr enum_MixSources[];
// sources: parse/output
@@ -132,6 +137,7 @@ extern const struct YamlIdStr enum_MixSources[];
//
static uint32_t r_mixSrcRaw(const YamlNode* node, const char* val, uint8_t val_len)
{
// TODO: parse switch name as well
if (val_len > 0 && val[0] == 'I') {
return yaml_str2uint(val+1, val_len-1) + MIXSRC_FIRST_INPUT;
} else if (val_len > 4 &&
@@ -208,8 +214,40 @@ static uint32_t r_mixSrcRaw(const YamlNode* node, const char* val, uint8_t val_l
// parse int and ignore closing ')'
return yaml_str2uint(val, val_len) * 3 + sign + MIXSRC_FIRST_TELEM;
} else if (val_len > 3 &&
val[0] == 'C' &&
val[1] == 'Y' &&
val[2] == 'C' &&
val[3] >= '1' &&
val[3] <= '3') {
return MIXSRC_FIRST_HELI + (val[3] - '1');
} else if (val_len > 1 &&
val[0] == 'T' &&
val[1] >= '1' &&
val[1] <= '9') {
return yaml_str2uint(val + 1, val_len - 1) + MIXSRC_FIRST_TRIM - 1;
}
auto idx = analogLookupCanonicalIdx(ADC_INPUT_MAIN, val, val_len);
if (idx >= 0) return idx + MIXSRC_FIRST_STICK;
idx = analogLookupCanonicalIdx(ADC_INPUT_POT, val, val_len);
if (idx >= 0) return idx + MIXSRC_FIRST_POT;
#if MAX_AXIS > 0
idx = analogLookupCanonicalIdx(ADC_INPUT_AXIS, val, val_len);
if (idx >= 0) return idx + MIXSRC_FIRST_POT;
#endif
idx = switchLookupIdx(val, val_len);
if (idx >= 0) return idx + MIXSRC_FIRST_SWITCH;
idx = _legacy_mix_src(val, val_len);
if (idx >= 0) return idx;
return yaml_parse_enum(enum_MixSources, val, val_len);
}
@@ -228,8 +266,11 @@ static bool w_mixSrcRaw(const YamlNode* node, uint32_t val, yaml_writer_func wf,
{
const char* str = nullptr;
if (val >= MIXSRC_FIRST_INPUT
&& val <= MIXSRC_LAST_INPUT) {
if (val == MIXSRC_NONE) {
return wf(opaque, "NONE", 4);
} else if (val <= MIXSRC_LAST_INPUT) {
if (!wf(opaque, "I", 1))
return false;
@@ -237,8 +278,7 @@ static bool w_mixSrcRaw(const YamlNode* node, uint32_t val, yaml_writer_func wf,
str = yaml_unsigned2str(val - MIXSRC_FIRST_INPUT);
}
#if defined(LUA_INPUTS)
else if (val >= MIXSRC_FIRST_LUA
&& val <= MIXSRC_LAST_LUA) {
else if (val <= MIXSRC_LAST_LUA) {
val -= MIXSRC_FIRST_LUA;
uint32_t script = val / MAX_SCRIPT_OUTPUTS;
@@ -254,6 +294,31 @@ static bool w_mixSrcRaw(const YamlNode* node, uint32_t val, yaml_writer_func wf,
str = closing_parenthesis;
}
#endif
else if (val <= MIXSRC_LAST_STICK) {
str = analogGetCanonicalName(ADC_INPUT_MAIN, val - MIXSRC_FIRST_STICK);
}
else if (val <= MIXSRC_LAST_POT) {
str = analogGetCanonicalName(ADC_INPUT_POT, val - MIXSRC_FIRST_POT);
}
#if MAX_AXIS > 0
else if (val <= MIXSRC_LAST_AXIS) {
str = analogGetCanonicalName(ADC_INPUT_AXIS, val - MIXSRC_FIRST_AXIS);
}
#endif
else if (val >= MIXSRC_FIRST_HELI
&& val <= MIXSRC_LAST_HELI) {
if (!wf(opaque, "CYC", 3)) return false;
str = yaml_unsigned2str(val - MIXSRC_FIRST_HELI + 1);
}
else if (val >= MIXSRC_FIRST_TRIM
&& val <= MIXSRC_LAST_TRIM) {
if (!wf(opaque, "T", 1)) return false;
str = yaml_unsigned2str(val - MIXSRC_FIRST_TRIM + 1);
}
else if (val >= MIXSRC_FIRST_SWITCH
&& val <= MIXSRC_LAST_SWITCH) {
str = switchGetCanonicalName(val - MIXSRC_FIRST_SWITCH);
}
else if (val >= MIXSRC_FIRST_LOGICAL_SWITCH
&& val <= MIXSRC_LAST_LOGICAL_SWITCH) {
@@ -517,8 +582,11 @@ static uint32_t r_calib(void* user, const char* val, uint8_t val_len)
{
(void)user;
uint32_t sw = yaml_parse_enum(enum_MixSources, val, val_len);
if (sw >= MIXSRC_Rud) return sw - MIXSRC_Rud;
int idx = adcGetInputIdx(val, val_len);
if (idx >= 0) return idx;
idx = _legacy_input_idx(val, val_len);
if (idx >= 0) return idx;
// detect invalid values
if (val_len == 0 || (val[0] < '0') || (val[0] > '9')) {
@@ -533,18 +601,102 @@ static bool w_calib(void* user, yaml_writer_func wf, void* opaque)
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts();
const char* str =
yaml_output_enum(idx + MIXSRC_Rud, enum_MixSources);
const char* str = adcGetInputName(idx);
return str ? wf(opaque, str, strlen(str)) : true;
}
static void _read_analog_name(uint8_t type, void* user, uint8_t* data,
uint32_t bitoffs, const char* val,
uint8_t val_len)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
analogSetCustomLabel(type, idx, val, val_len);
}
static bool _write_analog_name(uint8_t type, void* user, uint8_t* data,
uint32_t bitoffs, yaml_writer_func wf, void* opaque)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
const char* name = analogGetCustomLabel(ADC_INPUT_MAIN, idx);
if (!wf(opaque, "\"", 1)) return false;
if (!wf(opaque, name, strlen(name))) return false;
return wf(opaque, "\"", 1);
}
static void r_stick_name(void* user, uint8_t* data, uint32_t bitoffs,
const char* val, uint8_t val_len)
{
_read_analog_name(ADC_INPUT_MAIN, user, data, bitoffs, val, val_len);
}
static bool w_stick_name(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque)
{
return _write_analog_name(ADC_INPUT_MAIN, user, data, bitoffs, wf, opaque);
}
static bool stick_name_valid(void* user, uint8_t* data, uint32_t bitoffs)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts();
return analogHasCustomLabel(ADC_INPUT_MAIN, idx);
}
static const struct YamlNode struct_stickConfig[] = {
YAML_IDX,
YAML_CUSTOM( "name", r_stick_name, w_stick_name),
YAML_END
};
static uint32_t slider_read(void* user, const char* val, uint8_t val_len)
{
(void)user;
auto idx = _legacy_mix_src(val, val_len);
if (idx >= 0) return idx - MIXSRC_FIRST_POT;
return -1;
}
static const struct YamlIdStr enum_SliderConfig[] = {
{ POT_NONE, "none" },
{ POT_SLIDER_WITH_DETENT, "with_detent" },
{ 0, NULL }
};
static void sl_type_read(void* user, uint8_t* data, uint32_t bitoffs,
const char* val, uint8_t val_len)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
bitoffs += POT_CFG_BITS * idx;
data += bitoffs >> 3UL;
bitoffs &= 7;
auto cfg = yaml_parse_enum(enum_SliderConfig, val, val_len);
yaml_put_bits(data, cfg, bitoffs, POT_CFG_BITS);
}
static void sl_name_read(void* user, uint8_t* data, uint32_t bitoffs,
const char* val, uint8_t val_len)
{
_read_analog_name(ADC_INPUT_POT, user, data, bitoffs, val, val_len);
}
static const struct YamlNode struct_sliderConfig[] = {
YAML_IDX_CUST( "sl", slider_read, nullptr ),
YAML_CUSTOM( "type", sl_type_read, nullptr ),
YAML_CUSTOM( "name", sl_name_read, nullptr ),
YAML_END
};
static uint32_t sw_read(void* user, const char* val, uint8_t val_len)
{
(void)user;
uint32_t sw = yaml_parse_enum(enum_MixSources, val, val_len);
if (sw >= MIXSRC_FIRST_SWITCH) return sw - MIXSRC_FIRST_SWITCH;
return -1;
return switchLookupIdx(val, val_len);
}
bool sw_write(void* user, yaml_writer_func wf, void* opaque)
@@ -552,65 +704,18 @@ bool sw_write(void* user, yaml_writer_func wf, void* opaque)
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts();
const char* str =
yaml_output_enum(idx + MIXSRC_FIRST_SWITCH, enum_MixSources);
const char* str = switchGetCanonicalName(idx);
return str ? wf(opaque, str, strlen(str)) : true;
}
static void r_stick_name(void* user, uint8_t* data, uint32_t bitoffs,
const char* val, uint8_t val_len)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
if (idx >= NUM_STICKS) return;
data -= offsetof(RadioData, switchConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
strncpy(rd->anaNames[idx], val, std::min<uint8_t>(val_len, LEN_ANA_NAME));
}
static bool w_stick_name(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
data -= offsetof(RadioData, switchConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
if (!wf(opaque, "\"", 1)) return false;
if (!wf(opaque, rd->anaNames[idx],
strnlen(rd->anaNames[idx], LEN_ANA_NAME)))
return false;
return wf(opaque, "\"", 1);
}
static bool stick_name_valid(void* user, uint8_t* data, uint32_t bitoffs)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts();
RadioData* rd = reinterpret_cast<RadioData*>(data);
return rd->anaNames[idx][0] != '\0';
}
static const struct YamlNode struct_sticksConfig[] = {
YAML_IDX,
YAML_CUSTOM( "name", r_stick_name, w_stick_name),
YAML_END
};
static void sw_name_read(void* user, uint8_t* data, uint32_t bitoffs,
const char* val, uint8_t val_len)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
// data / bitoffs already incremented
data -= ((idx + 1) * 2) / 8;
data -= offsetof(RadioData, switchConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
strncpy(rd->switchNames[idx], val,
std::min<uint8_t>(val_len, LEN_SWITCH_NAME));
switchSetCustomName(idx, val, val_len);
}
static bool sw_name_write(void* user, uint8_t* data, uint32_t bitoffs,
@@ -619,12 +724,8 @@ static bool sw_name_write(void* user, uint8_t* data, uint32_t bitoffs,
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
// data / bitoffs already incremented
data -= ((idx + 1) * 2) / 8;
data -= offsetof(RadioData, switchConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
const char* str = rd->switchNames[idx];
const char* str = switchGetCustomName(idx);
if (!wf(opaque, "\"", 1)) return false;
if (!wf(opaque, str, strnlen(str, LEN_SWITCH_NAME)))
return false;
@@ -649,8 +750,12 @@ static const struct YamlNode struct_switchConfig[] = {
static uint32_t pot_read(void* user, const char* val, uint8_t val_len)
{
(void)user;
uint32_t pot = yaml_parse_enum(enum_MixSources, val, val_len);
if (pot >= MIXSRC_FIRST_POT) return pot - MIXSRC_FIRST_POT;
auto idx = analogLookupPhysicalIdx(ADC_INPUT_POT, val, val_len);
if (idx >= 0) return idx;
idx = _legacy_mix_src(val, val_len);
if (idx >= MIXSRC_FIRST_POT && idx <= MIXSRC_LAST_POT)
return idx - MIXSRC_FIRST_POT;
return -1;
}
@@ -660,42 +765,20 @@ static bool pot_write(void* user, yaml_writer_func wf, void* opaque)
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts();
const char* str = yaml_output_enum(idx + MIXSRC_FIRST_POT, enum_MixSources);
const char* str = analogGetPhysicalName(ADC_INPUT_POT, idx);
return str ? wf(opaque, str, strlen(str)) : true;
}
static void pot_name_read(void* user, uint8_t* data, uint32_t bitoffs,
const char* val, uint8_t val_len)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
// data / bitoffs already incremented
data -= ((idx + 1) * 2) / 8;
data -= offsetof(RadioData, potsConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
idx += NUM_STICKS;
strncpy(rd->anaNames[idx], val, std::min<uint8_t>(val_len, LEN_ANA_NAME));
_read_analog_name(ADC_INPUT_POT, user, data, bitoffs, val, val_len);
}
static bool pot_name_write(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
// data / bitoffs already incremented
data -= ((idx + 1) * 2) / 8;
data -= offsetof(RadioData, potsConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
idx += NUM_STICKS;
const char* str = rd->anaNames[idx];
if (!wf(opaque, "\"", 1)) return false;
if (!wf(opaque, str, strnlen(str, LEN_ANA_NAME)))
return false;
return wf(opaque, "\"", 1);
return _write_analog_name(ADC_INPUT_POT, user, data, bitoffs, wf, opaque);
}
static const struct YamlIdStr enum_PotConfig[] = {
@@ -703,100 +786,17 @@ static const struct YamlIdStr enum_PotConfig[] = {
{ POT_WITH_DETENT, "with_detent" },
{ POT_MULTIPOS_SWITCH, "multipos_switch" },
{ POT_WITHOUT_DETENT, "without_detent" },
{ POT_SLIDER_WITH_DETENT, "slider" },
{ 0, NULL }
};
static const struct YamlNode struct_potConfig[] = {
YAML_IDX_CUST( "pot", pot_read, pot_write ),
YAML_ENUM( "type", 2, enum_PotConfig),
YAML_ENUM( "type", POT_CFG_BITS, enum_PotConfig),
YAML_CUSTOM( "name", pot_name_read, pot_name_write),
YAML_END
};
static uint32_t slider_read(void* user, const char* val, uint8_t val_len)
{
(void)user;
uint32_t sl = yaml_parse_enum(enum_MixSources, val, val_len);
if (sl >= MIXSRC_FIRST_SLIDER) return sl - MIXSRC_FIRST_SLIDER;
return -1;
}
static bool slider_write(void* user, yaml_writer_func wf, void* opaque)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts();
const char* str =
yaml_output_enum(idx + MIXSRC_FIRST_SLIDER, enum_MixSources);
return str ? wf(opaque, str, strlen(str)) : true;
}
static void sl_name_read(void* user, uint8_t* data, uint32_t bitoffs,
const char* val, uint8_t val_len)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
// data / bitoffs already incremented
#if defined(PCBTARANIS)
// Please note:
// slidersConfig is defined as a bit-field member,
// so let's take the next field and subtract 1
//
data -= (idx + 4 /* bitsize previous field (auxSerialMode) */ + 1) / 8;
data -= offsetof(RadioData, potsConfig) - 1;
#else
data -= (idx + 1) / 8;
data -= offsetof(RadioData, slidersConfig);
#endif
RadioData* rd = reinterpret_cast<RadioData*>(data);
idx += NUM_STICKS + STORAGE_NUM_POTS;
strncpy(rd->anaNames[idx], val, std::min<uint8_t>(val_len, LEN_ANA_NAME));
}
static bool sl_name_write(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque)
{
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
uint16_t idx = tw->getElmts(1);
// data / bitoffs already incremented
#if defined(PCBTARANIS)
// Please note:
// slidersConfig is defined as a bit-field member,
// so let's take the next field and subtract 1
//
data -= (idx + 4 /* bitsize previous field (auxSerialMode) */ + 1) / 8;
data -= offsetof(RadioData, potsConfig) - 1;
#else
data -= (idx + 1) / 8;
data -= offsetof(RadioData, slidersConfig);
#endif
RadioData* rd = reinterpret_cast<RadioData*>(data);
idx += NUM_STICKS + STORAGE_NUM_POTS;
const char* str = rd->anaNames[idx];
if (!wf(opaque, "\"", 1)) return false;
if (!wf(opaque, str, strnlen(str, LEN_ANA_NAME)))
return false;
return wf(opaque, "\"", 1);
}
static const struct YamlIdStr enum_SliderConfig[] = {
{ SLIDER_NONE, "none" },
{ SLIDER_WITH_DETENT, "with_detent" },
{ 0, NULL }
};
static const struct YamlNode struct_sliderConfig[] = {
YAML_IDX_CUST( "sl", slider_read, slider_write ),
YAML_ENUM( "type", 1, enum_SliderConfig),
YAML_CUSTOM( "name", sl_name_read, sl_name_write),
YAML_END
};
extern const struct YamlIdStr enum_SwitchSources[];
static uint32_t r_swtchSrc(const YamlNode* node, const char* val, uint8_t val_len)
@@ -809,23 +809,46 @@ static uint32_t r_swtchSrc(const YamlNode* node, const char* val, uint8_t val_le
val_len--;
}
if (val_len >= 2
&& val[0] == 'L'
&& (val[1] >= '0' && val[1] <= '9')) {
if (val_len > 3 && val[0] == 'S' && val[1] >= 'W'
&& val[2] >= '0' && val[2] <= '9'
&& val[3] >= '0' && val[2] <= '2') {
ival = SWSRC_FIRST_LOGICAL_SWITCH + yaml_str2int(val+1, val_len-1) - 1;
ival = switchLookupIdx(val, val_len - 1) * 3;
ival += yaml_str2int(val + 3, val_len - 2);
ival += SWSRC_FIRST_SWITCH;
} else if (val_len > 2 && val[0] == 'S'
&& val[1] >= 'A' && val[1] <= 'Z'
&& val[2] >= '0' && val[2] <= '2') {
ival = switchLookupIdx(val, val_len - 1) * 3;
ival += yaml_str2int(val + 2, val_len - 2);
ival += SWSRC_FIRST_SWITCH;
}
#if NUM_XPOTS > 0
else if (val_len > 3
&& val[0] == '6'
&& val[1] == 'P'
&& (val[2] >= '0' && val[2] <= '9')
&& (val[3] >= '0' && val[3] < (XPOTS_MULTIPOS_COUNT + '0'))) {
ival = (val[2] - '0') * XPOTS_MULTIPOS_COUNT + (val[3] - '0')
+ SWSRC_FIRST_MULTIPOS_SWITCH;
ival = (val[2] - '0') * XPOTS_MULTIPOS_COUNT + (val[3] - '0')
+ SWSRC_FIRST_MULTIPOS_SWITCH;
}
else if (val_len > 3
&& val[0] == 'T' && val[1] == 'R'
&& val[2] >= '1' && val[2] <= '9') {
ival = SWSRC_FIRST_TRIM + yaml_str2int(val + 2, val_len - 3) * 2;
if (val[val_len - 1] == '+') ival++;
}
else if (val_len >= 2
&& val[0] == 'L'
&& (val[1] >= '0' && val[1] <= '9')) {
ival = SWSRC_FIRST_LOGICAL_SWITCH + yaml_str2int(val+1, val_len-1) - 1;
}
#endif
else if (val_len == 3
&& val[0] == 'F'
&& val[1] == 'M'
@@ -856,46 +879,60 @@ static bool w_swtchSrc_unquoted(const YamlNode* node, uint32_t val,
}
const char* str = NULL;
if (sval >= SWSRC_FIRST_LOGICAL_SWITCH
&& sval <= SWSRC_LAST_LOGICAL_SWITCH) {
wf(opaque, "L", 1);
str = yaml_unsigned2str(sval - SWSRC_FIRST_LOGICAL_SWITCH + 1);
return wf(opaque,str, strlen(str));
}
#if NUM_XPOTS > 0
else if (sval >= SWSRC_FIRST_MULTIPOS_SWITCH
&& sval <= SWSRC_LAST_MULTIPOS_SWITCH) {
wf(opaque, "6P", 2);
// pot #: start with 6P1
sval -= SWSRC_FIRST_MULTIPOS_SWITCH;
str = yaml_unsigned2str(sval / XPOTS_MULTIPOS_COUNT);
wf(opaque,str, strlen(str));
// position
str = yaml_unsigned2str(sval % XPOTS_MULTIPOS_COUNT);
return wf(opaque,str, strlen(str));
}
#endif
else if (sval >= SWSRC_FIRST_FLIGHT_MODE
&& sval <= SWSRC_LAST_FLIGHT_MODE) {
wf(opaque, "FM", 2);
str = yaml_unsigned2str(sval - SWSRC_FIRST_FLIGHT_MODE);
return wf(opaque,str, strlen(str));
}
else if (sval >= SWSRC_FIRST_SENSOR
&& sval <= SWSRC_LAST_SENSOR) {
wf(opaque, "T", 1);
str = yaml_unsigned2str(sval - SWSRC_FIRST_SENSOR + 1);
return wf(opaque,str, strlen(str));
}
str = yaml_output_enum(sval, enum_SwitchSources);
return wf(opaque, str, strlen(str));
if (str) return wf(opaque, str, strlen(str));
if (sval <= SWSRC_LAST_SWITCH) {
auto sw_info = switchInfo(sval);
str = switchGetCanonicalName(sw_info.quot);
if (!str) return true;
wf(opaque, str, strlen(str));
str = yaml_unsigned2str(sw_info.rem);
return wf(opaque, str, strlen(str));
} else if (sval <= SWSRC_LAST_MULTIPOS_SWITCH) {
wf(opaque, "6P", 2);
// pot #: start with 6P1
sval -= SWSRC_FIRST_MULTIPOS_SWITCH;
str = yaml_unsigned2str(sval / XPOTS_MULTIPOS_COUNT);
wf(opaque,str, strlen(str));
// position
str = yaml_unsigned2str(sval % XPOTS_MULTIPOS_COUNT);
return wf(opaque,str, strlen(str));
} else if (sval <= SWSRC_LAST_TRIM) {
wf(opaque, "TR", 2);
auto trim = (sval - SWSRC_FIRST_TRIM) / 2;
str = yaml_unsigned2str(trim + 1);
wf(opaque, str, strlen(str));
return wf(opaque, sval & 1 ? "+" : "-", 1);
} else if (sval <= SWSRC_LAST_LOGICAL_SWITCH) {
wf(opaque, "L", 1);
str = yaml_unsigned2str(sval - SWSRC_FIRST_LOGICAL_SWITCH + 1);
return wf(opaque,str, strlen(str));
}
else if (sval <= SWSRC_LAST_FLIGHT_MODE) {
wf(opaque, "FM", 2);
str = yaml_unsigned2str(sval - SWSRC_FIRST_FLIGHT_MODE);
return wf(opaque,str, strlen(str));
}
else if (sval <= SWSRC_LAST_SENSOR) {
wf(opaque, "T", 1);
str = yaml_unsigned2str(sval - SWSRC_FIRST_SENSOR + 1);
return wf(opaque,str, strlen(str));
}
return true; // ignore error
}
bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque)
@@ -904,6 +941,7 @@ bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* o
|| !w_swtchSrc_unquoted(node, val, wf, opaque)
|| !wf(opaque,"\"",1))
return false;
return true;
}
@@ -935,8 +973,8 @@ static bool fmd_is_active(void* user, uint8_t* data, uint32_t bitoffs)
data += bitoffs >> 3UL;
FlightModeData* fmd = (FlightModeData*)(data);
for (uint8_t i=0; i<MAX_GVARS; i++) {
is_active |= fmd->gvars[i] != GVAR_MAX+1; // FM0 -> default
for (uint8_t i = 0; i < MAX_GVARS; i++) {
is_active |= fmd->gvars[i] != GVAR_MAX + 1; // FM0 -> default
}
return is_active;
@@ -965,7 +1003,7 @@ static void r_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs,
swarnstate_t swtchWarn = 0;
while (val_len--) {
signed swtch = getRawSwitchIdx(*(val++));
signed swtch = switchLookupIdx(*(val++));
if (swtch < 0) break;
swarnstate_t state = 0;
@@ -999,7 +1037,7 @@ static bool w_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs,
swarnstate_t states;
memcpy(&states, data, sizeof(states));
for (int i = 0; i < NUM_SWITCHES; i++) {
for (uint8_t i = 0; i < switchGetMaxSwitches(); i++) {
// TODO: SWITCH_EXISTS() uses the g_eeGeneral stucture, which might not be
// avail
if (SWITCH_EXISTS(i)) {
@@ -1011,7 +1049,7 @@ static bool w_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs,
// state == 1 -> UP
// state == 2 -> MIDDLE
// state == 3 -> DOWN
char swtchWarn[2] = {getRawSwitchFromIdx(i), 0};
char swtchWarn[2] = {switchGetLetter(i), 0};
switch (state) {
case 0:
@@ -1031,7 +1069,7 @@ static bool w_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs,
break;
}
if (swtchWarn[1] != 0) {
if (swtchWarn[0] >= 'A' && swtchWarn[1] != 0) {
if (!wf(opaque, swtchWarn, 2)) {
return false;
}
@@ -1303,11 +1341,11 @@ static void r_customFn(void* user, uint8_t* data, uint32_t bitoffs,
&& val[2] == 'a'
&& val[3] == 'n'
&& val[4] == 's') {
CFN_CH_INDEX(cfn) = NUM_STICKS + 1;
CFN_CH_INDEX(cfn) = MAX_STICKS + 1;
} else {
uint32_t stick = yaml_parse_enum(enum_MixSources, val, l_sep);
if (stick >= MIXSRC_FIRST_STICK && stick <= MIXSRC_LAST_STICK) {
CFN_CH_INDEX(cfn) = stick - MIXSRC_FIRST_STICK + 1;
auto stick = analogLookupCanonicalIdx(ADC_INPUT_MAIN, val, val_len);
if (stick >= 0) {
CFN_CH_INDEX(cfn) = stick + 1;
}
}
break;
@@ -1526,12 +1564,12 @@ static bool w_customFn(void* user, uint8_t* data, uint32_t bitoffs,
case 0:
if (!wf(opaque, "sticks", 6)) return false;
break;
case NUM_STICKS + 1:
case MAX_STICKS + 1:
if (!wf(opaque, "chans", 5)) return false;
break;
default:
if (value > 0 && value < NUM_STICKS + 1) {
str = yaml_output_enum(value - 1 + MIXSRC_FIRST_STICK, enum_MixSources);
if (value > 0 && value <= MAX_STICKS) {
str = analogGetCanonicalName(ADC_INPUT_MAIN, value - 1);
if (str && !wf(opaque, str, strlen(str))) return false;
}
}