implement adc unsubscription (#3149)
* allow re-register of the same sensor * adc unsub
This commit is contained in:
parent
7afc52c620
commit
e6a6548b2a
|
@ -81,7 +81,7 @@ public:
|
||||||
|
|
||||||
bool Register(Sensor* sensor) {
|
bool Register(Sensor* sensor) {
|
||||||
// If there's somebody already here - a consumer tried to double-register a sensor
|
// If there's somebody already here - a consumer tried to double-register a sensor
|
||||||
if (m_sensor) {
|
if (m_sensor && m_sensor != sensor) {
|
||||||
// This sensor has already been registered. Don't re-register it.
|
// This sensor has already been registered. Don't re-register it.
|
||||||
firmwareError(CUSTOM_OBD_26, "Duplicate registration for sensor \"%s\"", sensor->getSensorName());
|
firmwareError(CUSTOM_OBD_26, "Duplicate registration for sensor \"%s\"", sensor->getSensorName());
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -6,11 +6,10 @@
|
||||||
|
|
||||||
#if EFI_UNIT_TEST
|
#if EFI_UNIT_TEST
|
||||||
|
|
||||||
void AdcSubscription::SubscribeSensor(FunctionalSensor &sensor,
|
/*static*/ void AdcSubscription::SubscribeSensor(FunctionalSensor&, adc_channel_e, float, float) {
|
||||||
adc_channel_e channel,
|
}
|
||||||
float lowpassCutoff,
|
|
||||||
float voltsPerAdcVolt /*= 0.0f*/)
|
/*static*/ void AdcSubscription::UnsubscribeSensor(FunctionalSensor&) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
@ -23,10 +22,24 @@ struct AdcSubscriptionEntry {
|
||||||
bool HasUpdated = false;
|
bool HasUpdated = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
static size_t s_nextEntry = 0;
|
|
||||||
static AdcSubscriptionEntry s_entries[16];
|
static AdcSubscriptionEntry s_entries[16];
|
||||||
|
|
||||||
void AdcSubscription::SubscribeSensor(FunctionalSensor &sensor,
|
static AdcSubscriptionEntry* findEntry(FunctionalSensor* sensor) {
|
||||||
|
for (size_t i = 0; i < efi::size(s_entries); i++) {
|
||||||
|
if (s_entries[i].Sensor == sensor) {
|
||||||
|
return &s_entries[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static AdcSubscriptionEntry* findEntry() {
|
||||||
|
// Find an entry with no sensor set
|
||||||
|
return findEntry(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*static*/ void AdcSubscription::SubscribeSensor(FunctionalSensor &sensor,
|
||||||
adc_channel_e channel,
|
adc_channel_e channel,
|
||||||
float lowpassCutoff,
|
float lowpassCutoff,
|
||||||
float voltsPerAdcVolt /*= 0.0f*/) {
|
float voltsPerAdcVolt /*= 0.0f*/) {
|
||||||
|
@ -41,8 +54,10 @@ void AdcSubscription::SubscribeSensor(FunctionalSensor &sensor,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that enough entries are available
|
auto entry = findEntry();
|
||||||
if (s_nextEntry >= efi::size(s_entries)) {
|
|
||||||
|
// Ensure that a free entry was found
|
||||||
|
if (!entry) {
|
||||||
firmwareError(CUSTOM_INVALID_ADC, "too many ADC subscriptions");
|
firmwareError(CUSTOM_INVALID_ADC, "too many ADC subscriptions");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -58,21 +73,45 @@ void AdcSubscription::SubscribeSensor(FunctionalSensor &sensor,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate the entry
|
// Populate the entry
|
||||||
auto &entry = s_entries[s_nextEntry];
|
entry->VoltsPerAdcVolt = voltsPerAdcVolt;
|
||||||
entry.Sensor = &sensor;
|
entry->Channel = channel;
|
||||||
entry.VoltsPerAdcVolt = voltsPerAdcVolt;
|
entry->Filter.configureLowpass(SLOW_ADC_RATE, lowpassCutoff);
|
||||||
entry.Channel = channel;
|
|
||||||
entry.Filter.configureLowpass(SLOW_ADC_RATE, lowpassCutoff);
|
|
||||||
|
|
||||||
s_nextEntry++;
|
// Set the sensor last - it's the field we use to determine whether this entry is in use
|
||||||
|
entry->Sensor = &sensor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*static*/ void AdcSubscription::UnsubscribeSensor(FunctionalSensor& sensor) {
|
||||||
|
auto entry = findEntry(&sensor);
|
||||||
|
|
||||||
|
if (!entry) {
|
||||||
|
// This sensor wasn't configured, skip it
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if EFI_PROD_CODE
|
||||||
|
// Release the pin
|
||||||
|
efiSetPadUnused(getAdcChannelBrainPin("adc unsubscribe", entry->Channel));
|
||||||
|
#endif // EFI_PROD_CODE
|
||||||
|
|
||||||
|
// clear the sensor first to mark this entry not in use
|
||||||
|
entry->Sensor = nullptr;
|
||||||
|
|
||||||
|
entry->VoltsPerAdcVolt = 0;
|
||||||
|
entry->Channel = EFI_ADC_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdcSubscription::UpdateSubscribers(efitick_t nowNt) {
|
void AdcSubscription::UpdateSubscribers(efitick_t nowNt) {
|
||||||
ScopePerf perf(PE::AdcSubscriptionUpdateSubscribers);
|
ScopePerf perf(PE::AdcSubscriptionUpdateSubscribers);
|
||||||
|
|
||||||
for (size_t i = 0; i < s_nextEntry; i++) {
|
for (size_t i = 0; i < efi::size(s_entries); i++) {
|
||||||
auto &entry = s_entries[i];
|
auto &entry = s_entries[i];
|
||||||
|
|
||||||
|
if (!entry.Sensor) {
|
||||||
|
// Skip unconfigured entries
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
float mcuVolts = getVoltage("sensor", entry.Channel);
|
float mcuVolts = getVoltage("sensor", entry.Channel);
|
||||||
float sensorVolts = mcuVolts * entry.VoltsPerAdcVolt;
|
float sensorVolts = mcuVolts * entry.VoltsPerAdcVolt;
|
||||||
|
|
||||||
|
|
|
@ -10,5 +10,6 @@
|
||||||
class AdcSubscription {
|
class AdcSubscription {
|
||||||
public:
|
public:
|
||||||
static void SubscribeSensor(FunctionalSensor &sensor, adc_channel_e channel, float lowpassCutoff, float voltsPerAdcVolt = 0.0f);
|
static void SubscribeSensor(FunctionalSensor &sensor, adc_channel_e channel, float lowpassCutoff, float voltsPerAdcVolt = 0.0f);
|
||||||
|
static void UnsubscribeSensor(FunctionalSensor& sensor);
|
||||||
static void UpdateSubscribers(efitick_t nowNt);
|
static void UpdateSubscribers(efitick_t nowNt);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue