europi_hardware
Hardware-specific definitions & implementations for Europi
Originally all of this was included as part of europi.py, but was moved here to make it easier to access e.g. button status readers independently of all of the other EuroPi initialization that occurs inside the europi module.
Normally this module should not be included directly; it’s imported automatically as part of from europi import *.
- class europi_hardware.AnalogueInput(pin, min_voltage=0, max_voltage=10.0)
A class for handling the reading of analogue control voltage.
The analogue input allows you to ‘read’ CV from anywhere between 0 and 12V.
It is protected for the entire Eurorack range, so don’t worry about plugging in a bipolar source, it will simply be clipped to 0-12V.
The functions all take an optional parameter of
samples
, which will oversample the ADC and then take an average, which will take more time per reading, but will give you a statistically more accurate result. The default is 32, provides a balance of performance vs accuracy, but if you want to process at the maximum speed you can use as little as 1, and the processor won’t bog down until you get way up into the thousands if you wan’t incredibly accurate (but quite slow) readings.The percent function takes an optional parameter
deadzone
. However this parameter is ignored and just present to be compatible with the percent function of the AnalogueReader and Knob classes- Parameters
pin – The pin ID the ADC is connected to
min_voltage – The minimum allowed input voltage
max_voltage – The maximum allowed input voltage
deadzone – An optional deadzone to restrict noise at the high and low ends
- choice(values, samples=None, deadzone=None)
Return a value from a list chosen by the current voltage value.
- percent(samples=None, deadzone=None)
Current voltage as a relative percentage of the component’s range.
- range(steps=100, samples=None, deadzone=None)
Return a value (upper bound excluded) chosen by the current voltage value.
- set_deadzone(deadzone)
Override the default deadzone with the given value.
- set_samples(samples)
Override the default number of sample reads with the given value.
- class europi_hardware.AnalogueReader(pin, samples=32, deadzone=0.0)
A base class for common analogue read methods.
This class in inherited by classes like Knob and AnalogueInput and does not need to be used by user scripts.
- Parameters
pin – The pin ID the ADC is connected to
samples – The number of ADC samples to read. More samples results in a more accurate reading, but may slow down the program
deadzone – An optional deadzone to restrict noise at the high and low ends
- choice(values, samples=None, deadzone=None)
Return a value from a list chosen by the current voltage value.
- percent(samples=None, deadzone=None)
Return the percentage of the component’s current relative range.
- range(steps=100, samples=None, deadzone=None)
Return a value (upper bound excluded) chosen by the current voltage value.
- set_deadzone(deadzone)
Override the default deadzone with the given value.
- set_samples(samples)
Override the default number of sample reads with the given value.
- class europi_hardware.Button(pin, debounce_delay=200)
A class for handling push button behavior.
Button instances have a method
last_pressed()
(similar toDigitalInput.last_triggered()
) which can be used by your script to help perform some action or behavior relative to when the button was last pressed (or input trigger received). For example, if you want to call a function to display a message that a button was pressed, you could add the following code to your main script loop:# Inside the main loop... if b1.last_pressed() > 0 and ticks_diff(ticks_ms(), b1.last_pressed()) < 2000: # Call this during the 2000 ms duration after button press. display_button_pressed()
Note, if a button has not yet been pressed, the
last_pressed()
default return value is 0, so you may want to add the check if b1.last_pressed() > 0 before you check the elapsed duration to ensure the button has been pressed. This is also useful when checking if the digital input has been triggered with theDigitalInput.last_triggered()
method.- Parameters
pin – The pin ID we read from
debounce_delay – The time in ms that we debounce the input
- handler(func)
Define the callback function to call when rising edge detected.
- handler_falling(func)
Define the callback function to call when falling edge detected.
- last_pressed()
Return the ticks_ms of the last button press
If the button has not yet been pressed, the default return value is 0.
- value()
The current binary value, HIGH (1) or LOW (0).
- class europi_hardware.DigitalInput(pin, debounce_delay=0)
A class for handling reading of the digital input.
The Digital Input jack can detect a HIGH signal when recieving voltage > 0.8v and will be LOW when below.
To use the handler method, you simply define whatever you want to happen when a button or the digital input is triggered, and then use
x.handler(new_function)
. Do not include the brackets for the function, and replace the ‘x’ in the example with the name of your input, eitherb1
,b2
, ordin
.Here is another example how you can write digital input handlers to react to a clock source and match its trigger duration.:
@din.handler def gate_on(): # Trigger outputs with a probability set by knobs. cv1.value(random() > k1.percent()) cv2.value(random() > k2.percent()) @din.handler_falling def gate_off(): # Turn off all triggers on falling clock trigger to match clock. cv1.off() cv2.off()
When writing a handler, try to keep the code as minimal as possible. Ideally handlers should be used to change state and allow your main loop to change behavior based on the altered state. See tips from the MicroPython documentation for more details.
- Parameters
pin – The pin ID we read from
debounce_delay – The time in ms that we debounce the input
- handler(func)
Define the callback function to call when rising edge detected.
- handler_falling(func)
Define the callback function to call when falling edge detected.
- last_triggered()
Return the ticks_ms of the last trigger.
If the button has not yet been pressed, the default return value is 0.
- value()
The current binary value, HIGH (1) or LOW (0).
- class europi_hardware.DigitalReader(pin, debounce_delay=500)
A base class for common digital inputs methods.
This class in inherited by classes like Button and DigitalInput and does not need to be used by user scripts.
- Parameters
pin – The pin ID we read from
debounce_delay – The time in ms that we debounce the input
- handler(func)
Define the callback function to call when rising edge detected.
- handler_falling(func)
Define the callback function to call when falling edge detected.
- value()
The current binary value, HIGH (1) or LOW (0).
- class europi_hardware.Knob(pin, deadzone=0.01)
A class for handling the reading of knob voltage and position.
Read_position has a default value of 100, meaning if you simply use
kx.read_position()
you will return a whole number percent style value from 0-100.There is also the optional parameter of
samples
(which must come after the normal parameter), the same as the analogue input uses (the knob positions are ‘read’ via an analogue to digital converter). It has a default value of 256, but you can use higher or lower depending on if you value speed or accuracy more. If you really want to avoid ‘noise’ which would present as a flickering value despite the knob being still, then I’d suggest using higher samples (and probably a smaller number to divide the position by). The defaultsamples
value can also be set using theset_samples()
method, which will then be used on all analogue read calls for that component.An optional
deadzone
parameter can be used to place deadzones at both positions (all the way left and right) of the knob to make sure the full range is available on all builds. The default value is 0.01 (resulting in 1% of the travel used as deadzone on each side). There is usually no need to change this.Additionally, the
choice()
method can be used to select a value from a list of values based on the knob’s position:def clock_division(self): return k1.choice([1, 2, 3, 4, 5, 6, 7, 8, 16, 32])
When the knob is all the way to the left, the return value will be
1
, at 12 o’clock it will return the mid point value of5
and when fully clockwise, the last list item of32
will be returned.The ADCs used to read the knob position are only 12 bit, which means that any read_position value above 4096 (2^12) will not actually be any finer resolution, but will instead just go up in steps. For example using 8192 would only return values which go up in steps of 2.
- Parameters
pin – The pin ID the ADC is connected to
deadzone – An optional deadzone to restrict noise at the high and low ends
- choice(values, samples=None, deadzone=None)
Return a value from a list chosen by the current voltage value.
- percent(samples=None, deadzone=None)
Return the knob’s position as relative percentage.
- range(steps=100, samples=None, deadzone=None)
Return a value (upper bound excluded) chosen by the current voltage value.
- read_position(steps=100, samples=None, deadzone=None)
Returns the position as a value between zero and provided integer.
- set_deadzone(deadzone)
Override the default deadzone with the given value.
- set_samples(samples)
Override the default number of sample reads with the given value.
- class europi_hardware.Output(pin, min_voltage=0, max_voltage=10.0, gate_voltage=5.0, calibration_values=[0, 6300, 12575, 19150, 25375, 31625, 38150, 44225, 50525, 56950, 63475])
A class for sending digital or analogue voltage to an output jack.
The outputs are capable of providing 0-10V, which can be achieved using the
cvx.voltage()
method.So that there is no chance of not having the full range, the chosen resistor values actually give you a range of about 0-10.5V, which is why calibration is important if you want to be able to output precise voltages.
- Parameters
pin – The GPIO pin we use as a PWM output
min_voltage – The minimum allowed output voltage
max_voltage – The maximum allowed output voltage
gate_voltage – The voltage we use for gate signals (see
.on()
,.off()
)calibration_values – Calibration data for this output
- off()
Set the voltage LOW at 0 volts.
- on()
Set the voltage HIGH according to the gate voltage
By default this is 5V, but can be overridden via the configuration file
- toggle()
Invert the Output’s current state.
- value(value)
Sets the output to 0V or 5V based on a binary input, 0 or 1.
- Parameters
value – HIGH or LOW, according to the desired state.
- voltage(voltage=None)
Set the output voltage to the provided value within the range of MIN_VOLTAGE to MAX_VOLTAGE
By default this range is 0-10, but can be overridden via the configuration file
- Parameters
voltage – The desired volts to send to the output. If None the current voltage is returned
- class europi_hardware.Thermometer
Wrapper for the temperature sensor connected to Pin 4
Reports the module’s current temperature in Celsius.
If the module’s temperature sensor is not working correctly, the temperature will always be reported as None
- read_temperature()
Read the ADC and return the current temperature
- Returns
The current temperature in Celsius, or None if the hardware did not initialze properly
- class europi_hardware.UsbConnection
Checks the USB terminal is connected or not
On the original Pico we can check Pin 24, but on the Pico 2 this does not work. In that case check the SIE_STATUS register and check bit 16
- value()
Return 0 or 1, indicating if the USB connection is disconnected or connected
- europi_hardware.clamp(value: int | float, low: int | float, high: int | float)
Returns a value that is no lower than ‘low’ and no higher than ‘high’.
- Parameters
value – The value to clamp
low – The inclusive lower bound
high – The inclusive upper bound