Skip to content

Commit

Permalink
Rewrite the wrapper for K1/AIN
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisib committed Dec 7, 2024
1 parent d3789be commit a30d0e7
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions software/contrib/pams.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,21 @@
]


class AnalogInReader:
"""A wrapper for `ain` that can be shared across multiple Settings
class BufferedAnalogueReader(AnalogueReader):
"""A wrapper for basic AnalogueReader instances that read the ADC hardware on-demand
This allows `ain` to be read once during the main loop, but keep its value across multiple
accesses across each output channel. It also adds gain & precision settings that can
be adjusted in application's menu
This is useful if the reader is going to be using `.choice(...)` for multiple things,
as normally every call to .percent, .choice, .voltage, etc... re-reads the ADC.
Call .update() to re-sample from the ADC
"""
def __init__(self, cv_in, label):
self.cv_in = cv_in
self.last_percent = 0.0
def __init__(self, cv_in: AnalogueReader, label: str):
"""
Create the buffered reader
@param cv_in The base reader we're buffering
@param label A label used to stringify this object
"""
self.gain = SettingMenuItem(
config_point = IntegerConfigPoint(
f"{label.lower()}_gain",
Expand All @@ -327,23 +331,42 @@ def __init__(self, cv_in, label):
}
)

def update(self):
"""Read the current voltage from the analog input using the configured precision
self._last_sample = 0

Sets self.last_voltage, which is returned by self.get_value()
super().__init__(cv_in.pin_id)
self.label = label

def percent(self, samples=None, deadzone=None):
"""
Apply our gain control to the base percentage
Note that even though the gain goes up to 200%, this returns a value in the range [0, 1].
"""
p = super().percent(samples, deadzone)
p = p * self.gain.value
p = clamp(p, 0.0, 1.0)
return p

@return The voltage read from the analog input multiplied by self.gain
def _sample_adc(self, samples=None):
"""
self.last_percent = self.cv_in.percent(self.precision.mapped_value) * self.gain.value / 100.0
return self.last_percent
Override the default _sample_adc to just return the last sample
"""
return self._last_sample

def update(self):
"""
Re-read the ADC and store the sample value
"""
self._last_sample = super()._sample_adc(samples=self.precision.mapped_value)

def __str__(self):
return self.label

def percent(self):
return self.last_percent

## Wrapped copies of all CV inputs so we can iterate through them
## Wrapped copies of all CV inputs so we can iterate through them to update them
CV_INS = {
"KNOB": AnalogInReader(k1, "Knob"),
"AIN": AnalogInReader(ain, "AIN"),
"KNOB": BufferedAnalogueReader(k1, "Knob"),
"AIN": BufferedAnalogueReader(ain, "AIN"),
}


Expand Down

0 comments on commit a30d0e7

Please sign in to comment.