Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions headers/addons/he_trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,49 @@
#define HETRIGGER_S3_PIN -1
#endif

#ifndef HETRIGGER_SEPARATE_SELECT_PINS
#define HETRIGGER_SEPARATE_SELECT_PINS 0
#endif

#ifndef HETRIGGER_MUX1_S0_PIN
#define HETRIGGER_MUX1_S0_PIN -1
#endif
#ifndef HETRIGGER_MUX1_S1_PIN
#define HETRIGGER_MUX1_S1_PIN -1
#endif
#ifndef HETRIGGER_MUX1_S2_PIN
#define HETRIGGER_MUX1_S2_PIN -1
#endif
#ifndef HETRIGGER_MUX1_S3_PIN
#define HETRIGGER_MUX1_S3_PIN -1
#endif

#ifndef HETRIGGER_MUX2_S0_PIN
#define HETRIGGER_MUX2_S0_PIN -1
#endif
#ifndef HETRIGGER_MUX2_S1_PIN
#define HETRIGGER_MUX2_S1_PIN -1
#endif
#ifndef HETRIGGER_MUX2_S2_PIN
#define HETRIGGER_MUX2_S2_PIN -1
#endif
#ifndef HETRIGGER_MUX2_S3_PIN
#define HETRIGGER_MUX2_S3_PIN -1
#endif

#ifndef HETRIGGER_MUX3_S0_PIN
#define HETRIGGER_MUX3_S0_PIN -1
#endif
#ifndef HETRIGGER_MUX3_S1_PIN
#define HETRIGGER_MUX3_S1_PIN -1
#endif
#ifndef HETRIGGER_MUX3_S2_PIN
#define HETRIGGER_MUX3_S2_PIN -1
#endif
#ifndef HETRIGGER_MUX3_S3_PIN
#define HETRIGGER_MUX3_S3_PIN -1
#endif

#ifndef HETRIGGER_ADC0
#define HETRIGGER_ADC0 -1
#endif
Expand Down Expand Up @@ -858,12 +901,12 @@ class HETriggerAddon : public GPAddon {
virtual void reinit() {}
virtual std::string name() { return HETriggerAddonName; }
private:
void selectChannel(uint8_t channel);
void selectChannel(uint8_t mux, uint8_t channel);
uint16_t emaSmoothing(uint16_t value, uint16_t previous);
int muxTotal;
int selectPins;
Pin_t muxPinArray[4];
Pin_t selectPinArray[4];
Pin_t selectPinArray[4][4]; // Select pins indexed by [mux][selectBit]
Pin_t lastADCSelected;

uint16_t emaSmoothingReads[32];
Expand Down
19 changes: 15 additions & 4 deletions proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -929,13 +929,21 @@ message HETriggerInfo
optional bool is_polarized = 9;
}

message HETriggerMux
{
optional int32 selectPin0 = 1;
optional int32 selectPin1 = 2;
optional int32 selectPin2 = 3;
optional int32 selectPin3 = 4;
}

message HETriggerOptions
{
optional bool enabled = 1;
optional int32 selectPin0 = 2;
optional int32 selectPin1 = 3;
optional int32 selectPin2 = 4;
optional int32 selectPin3 = 5;
optional int32 selectPin0 = 2 [deprecated = true]; // Deprecated in favor of muxes[]
optional int32 selectPin1 = 3 [deprecated = true];
optional int32 selectPin2 = 4 [deprecated = true];
optional int32 selectPin3 = 5 [deprecated = true];
optional int32 muxADCPin0 = 6;
optional int32 muxADCPin1 = 7;
optional int32 muxADCPin2 = 8;
Expand All @@ -944,6 +952,8 @@ message HETriggerOptions
repeated HETriggerInfo triggers = 11 [(nanopb).max_count = 32];
optional bool emaSmoothing = 12;
optional int32 smoothingFactor = 13;
optional bool separateSelectPins = 14;
repeated HETriggerMux muxes = 15 [(nanopb).max_count = 4];
}

message AddonOptions
Expand Down Expand Up @@ -988,6 +998,7 @@ message MigrationHistory
optional bool gpioMappingsMigrated = 2 [default = false];
optional bool buttonProfilesMigrated = 3 [default = false];
optional bool profileEnabledFlagsMigrated = 4 [default = false];
optional bool heTriggerSelectPinsMigrated = 5 [default = false];
}

message Config
Expand Down
36 changes: 21 additions & 15 deletions src/addons/he_trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ void HETriggerAddon::setup() {
break;
}

selectPinArray[0] = options.selectPin0;
selectPinArray[1] = options.selectPin1;
selectPinArray[2] = options.selectPin2;
selectPinArray[3] = options.selectPin3;
for(int i = 0; i < selectPins; i++) {
if ( selectPinArray[i] != -1 ) {
gpio_init(selectPinArray[i]);
gpio_set_dir(selectPinArray[i], GPIO_OUT);
gpio_put(selectPinArray[i], 0);
for(int mux = 0; mux < 4; mux++) {
int src = options.separateSelectPins ? mux : 0;
selectPinArray[mux][0] = options.muxes[src].selectPin0;
selectPinArray[mux][1] = options.muxes[src].selectPin1;
selectPinArray[mux][2] = options.muxes[src].selectPin2;
selectPinArray[mux][3] = options.muxes[src].selectPin3;
}

for(int mux = 0; mux < muxTotal; mux++) {
for(int i = 0; i < selectPins; i++) {
if ( selectPinArray[mux][i] != -1 ) {
gpio_init(selectPinArray[mux][i]);
gpio_set_dir(selectPinArray[mux][i], GPIO_OUT);
gpio_put(selectPinArray[mux][i], 0);
}
}
}

Expand All @@ -65,7 +71,7 @@ void HETriggerAddon::setup() {
continue;
mux = (i / options.muxChannels);
channel = (i % options.muxChannels);
selectChannel(channel);
selectChannel(mux, channel);
// Only Switch ADC if we are not currently on the mux ADC
if ( lastADCSelected != muxPinArray[mux]) {
adc_select_input(muxPinArray[mux]-26);
Expand All @@ -79,11 +85,11 @@ void HETriggerAddon::setup() {
}
}

void HETriggerAddon::selectChannel(uint8_t channel) {
void HETriggerAddon::selectChannel(uint8_t mux, uint8_t channel) {
for(int i = 0; i < selectPins; i++) {
if ( selectPinArray[i] != -1 ) {
gpio_put(selectPinArray[i], (channel >> i) & 0x01);
}
if ( selectPinArray[mux][i] != -1 ) {
gpio_put(selectPinArray[mux][i], (channel >> i) & 0x01);
}
}
}

Expand All @@ -102,7 +108,7 @@ void HETriggerAddon::preprocess() {
continue;
mux = (he / options.muxChannels);
channel = (he % options.muxChannels);
selectChannel(channel);
selectChannel(mux, channel);
// Only Switch ADC if we are not currently on the mux ADC
if ( lastADCSelected != muxPinArray[mux]) {
adc_select_input(muxPinArray[mux]-26);
Expand Down
71 changes: 59 additions & 12 deletions src/config_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,17 +939,31 @@ void ConfigUtils::initUnsetPropertiesWithDefaults(Config& config)

// addonOptions.heTriggerOptions
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, enabled, !!HETRIGGER_ENABLED);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, selectPin0, HETRIGGER_S0_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, selectPin1, HETRIGGER_S1_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, selectPin2, HETRIGGER_S2_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, selectPin3, HETRIGGER_S3_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, muxADCPin0, HETRIGGER_ADC0);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, muxADCPin1, HETRIGGER_ADC1);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, muxADCPin2, HETRIGGER_ADC2);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, muxADCPin3, HETRIGGER_ADC3);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, muxChannels, HETRIGGER_MUX_CHANNELS);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, emaSmoothing, HETRIGGER_SMOOTHING_ENABLED);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, smoothingFactor, HETRIGGER_SMOOTHING_FACTOR);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions, separateSelectPins, !!HETRIGGER_SEPARATE_SELECT_PINS);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[0], selectPin0, HETRIGGER_S0_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[0], selectPin1, HETRIGGER_S1_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[0], selectPin2, HETRIGGER_S2_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[0], selectPin3, HETRIGGER_S3_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[1], selectPin0, HETRIGGER_MUX1_S0_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[1], selectPin1, HETRIGGER_MUX1_S1_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[1], selectPin2, HETRIGGER_MUX1_S2_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[1], selectPin3, HETRIGGER_MUX1_S3_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[2], selectPin0, HETRIGGER_MUX2_S0_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[2], selectPin1, HETRIGGER_MUX2_S1_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[2], selectPin2, HETRIGGER_MUX2_S2_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[2], selectPin3, HETRIGGER_MUX2_S3_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[3], selectPin0, HETRIGGER_MUX3_S0_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[3], selectPin1, HETRIGGER_MUX3_S1_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[3], selectPin2, HETRIGGER_MUX3_S2_PIN);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.muxes[3], selectPin3, HETRIGGER_MUX3_S3_PIN);
config.addonOptions.heTriggerOptions.muxes_count = 4;
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.triggers[0], action, HETRIGGER_HE0_ACTION);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.triggers[0], active, HETRIGGER_HE0_ACTIVE);
INIT_UNSET_PROPERTY(config.addonOptions.heTriggerOptions.triggers[0], idle, HETRIGGER_HE0_IDLE);
Expand Down Expand Up @@ -1750,14 +1764,18 @@ void gpioMappingsMigrationCore(Config& config)

// Set our HE trigger options
if (config.addonOptions.heTriggerOptions.enabled) {
markAddonPinIfUsed(config.addonOptions.heTriggerOptions.muxADCPin0);
markAddonPinIfUsed(config.addonOptions.heTriggerOptions.muxADCPin1);
markAddonPinIfUsed(config.addonOptions.heTriggerOptions.muxADCPin2);
markAddonPinIfUsed(config.addonOptions.heTriggerOptions.muxADCPin3);
markAddonPinIfUsed(config.addonOptions.heTriggerOptions.selectPin0);
markAddonPinIfUsed(config.addonOptions.heTriggerOptions.selectPin1);
markAddonPinIfUsed(config.addonOptions.heTriggerOptions.selectPin2);
markAddonPinIfUsed(config.addonOptions.heTriggerOptions.selectPin3);
HETriggerOptions& heOptions = config.addonOptions.heTriggerOptions;
markAddonPinIfUsed(heOptions.muxADCPin0);
markAddonPinIfUsed(heOptions.muxADCPin1);
markAddonPinIfUsed(heOptions.muxADCPin2);
markAddonPinIfUsed(heOptions.muxADCPin3);
int muxSelectRows = heOptions.separateSelectPins ? 4 : 1;
for (int m = 0; m < muxSelectRows; m++) {
markAddonPinIfUsed(heOptions.muxes[m].selectPin0);
markAddonPinIfUsed(heOptions.muxes[m].selectPin1);
markAddonPinIfUsed(heOptions.muxes[m].selectPin2);
markAddonPinIfUsed(heOptions.muxes[m].selectPin3);
}
}


Expand Down Expand Up @@ -1909,6 +1927,28 @@ void profileEnabledFlagsMigration(Config& config) {
config.migrations.profileEnabledFlagsMigrated = true;
}

void heTriggerSelectPinsMigration(Config& config) {
// Migrate the legacy shared select pins (selectPin0-3) into muxes[0]
HETriggerOptions& heOptions = config.addonOptions.heTriggerOptions;
if (heOptions.has_selectPin0) {
heOptions.muxes[0].selectPin0 = heOptions.selectPin0;
heOptions.muxes[0].has_selectPin0 = true;
}
if (heOptions.has_selectPin1) {
heOptions.muxes[0].selectPin1 = heOptions.selectPin1;
heOptions.muxes[0].has_selectPin1 = true;
}
if (heOptions.has_selectPin2) {
heOptions.muxes[0].selectPin2 = heOptions.selectPin2;
heOptions.muxes[0].has_selectPin2 = true;
}
if (heOptions.has_selectPin3) {
heOptions.muxes[0].selectPin3 = heOptions.selectPin3;
heOptions.muxes[0].has_selectPin3 = true;
}
config.migrations.heTriggerSelectPinsMigrated = true;
}

void migrateMacroPinsToGpio(Config& config) {
// Convert Macro pin mapping to GPIO mapping configs
MacroOptions & macroOptions = config.addonOptions.macroOptions;
Expand Down Expand Up @@ -2082,6 +2122,10 @@ void ConfigUtils::load(Config& config)
// They were probably added with a newer version of the firmware.
initUnsetPropertiesWithDefaults(config);

// Migrate legacy HE Trigger shared select pins into muxes[0]
if (!config.migrations.heTriggerSelectPinsMigrated)
heTriggerSelectPinsMigration(config);

// Run migrations that need to happen after initUnset...
// ProtoBuf && Board Config settings are loaded here
if (!config.migrations.gpioMappingsMigrated)
Expand Down Expand Up @@ -2730,6 +2774,9 @@ bool ConfigUtils::fromJSON(Config& config, const char* data, size_t dataLen)
initUnsetPropertiesWithDefaults(config);

// we need to run migrations here too, in case the json document changed pins or things derived from pins
if (!config.migrations.heTriggerSelectPinsMigrated)
heTriggerSelectPinsMigration(config);

gpioMappingsMigrationCore(config);
migrateTurboPinToGpio(config);
migrateAuthenticationMethods(config);
Expand Down
Loading