codex – CodeX hardware interface
Core low-level module for CodeX
- The codex module gives you direct access to all the hardware on the CodeX.
power =
Powerbuttons =
Buttonsleds =
LEDspixels =
NEOPixelsdisplay =
canvas.Canvasaudio =
codec.Codeclight =
AmbientLightexp =
Expansiontft =
tft.TFT
- class codex.codex.AmbientLight(i2c, power)[source]
Access to the CodeX ambient light sensor.
This device has two light sensors. One reads only infrared light. The other reads infrared light and visibile light at the same time.
- read()[source]
Read the infrared plus visible light level ADC value. Value depends on current gain setting.
- Returns:
visible + infrared
- Return type:
- read_ir()[source]
Read the IR light level ADC value. Value depends on current gain setting.
- Returns:
ir light
- Return type:
- codex.codex.BTN_A = 4
A
- codex.codex.BTN_B = 5
B
- codex.codex.BTN_D = 2
DOWN
- codex.codex.BTN_L = 3
LEFT
- codex.codex.BTN_R = 1
RIGHT
- codex.codex.BTN_U = 0
UP
- class codex.codex.Buttons(io_expander)[source]
Access to pushbutton switches BTN-0 through BTN-5. Buttons can be referenced by
intvalue 0-5, or by symbolic names:BTN_U,BTN_R,BTN_D,BTN_L,BTN_A,BTN_B- is_pressed(num=None)[source]
Return True if specified button is pressed, or tuple of status for all buttons if num=None.
- Parameters:
num (int/None) – 0-5 or None
- Returns:
bool if num was an int else a tuple of all values
- Return type:
value (bool/tuple)
- was_pressed(num=None)[source]
Return True if specified button was pressed since last call of this function. Default with num=None, returns tuple of status for all buttons since last call.
- Parameters:
num (int/None) – 0-5 or None
- Returns:
bool if num was an int else a tuple of all values
- Return type:
value (bool/tuple)
- class codex.codex.Expansion[source]
Access to Expansion connectors and Peripheral ports.
This class provides functions for basic usage of the available expansion pins.
For advanced users, feel free to use CircuitPython hardware interface libraries like
digitalio,analogio,pwmio, andbusiodirectly with these GPIO pins as well.- GPIO0
- GPIO1
- GPIO2
- GPIO3
- GPIO4
Expansion Connectors
- PORT0
- PORT1
- PORT2
- PORT3
Peripheral Connectors
- analog_in(port)[source]
Connect a analog input peripheral to the specified port.
You can use this method to set up input devices like potentiometers, analog temperature sensors, and light sensors.
The peripheral will use a 16-bit Analog-to-Digital Converter (ADC). Accessing the
valueattribute of the returned object returns the latest reading as an integer. Analog readings are in the range of 0-65535 (inclusive), where 0 is a reading of 0 mV and 65535 is a reading of approximately 3300 mV.Note
Analog devices saturate the CodeX’s Analog to Digital Converter (ADC) at 2.5V, half of the 5V output on the Peripheral connector ports. If full analog range is desired, remember to connect your CodeX Peripheral Divider!
- Parameters:
port (microcontroller.Pin) – Expansion port to configure.
- Returns:
A peripheral object representing the analog input.
- Return type:
- deinit(port)[source]
Releases peripheral port for other use.
- Parameters:
port (microcontroller.Pin) – Port to release
- digital_in(port, pull=None)[source]
Connect a digital input peripheral to the specified port.
You can use this method to set up digital input devices like buttons and switches.
Accessing the
valueattribute of the returned object returns the latest reading as a bool, where True is high and False is low. Note that for most switches and buttons, the pressed state will return False.- Parameters:
port (microcontroller.Pin) – Expansion port to configure.
pull (bool) – Default output value when not being used.
- Returns:
A peripheral object representing the digital input.
- Return type:
- digital_out(port, value=False)[source]
Connect a digital output peripheral to the specified port.
You can use this method to set up digital output devices like LEDs without brightness control.
Setting the
valueattribute of the returned object sets the port’s output level, where True is high and False is low.- Parameters:
port (microcontroller.Pin) – Expansion port to configure.
value (bool) – Initial output value.
- Returns:
A peripheral object representing the digital output.
- Return type:
- pwm_out(port, duty_cycle=0, frequency=500)[source]
Connect a PWM (pulse width modulation) peripheral to the specified pin.
You can use this method to set up devices that want a (simulated) analog output, like an LED you control the brightness of.
The peripheral will use a 16-bit PWM signal. Setting the
duty_cycleattribute of the returned object changes the fraction of each pulse that is high, from no time at 0 to fully on at 65535. Setting thefrequencyattribute of the returned object changes the length of each pulse in Hertz.- Parameters:
port (microcontroller.Pin) – Expansion port to configure.
duty_cycle (int) – Initial PWM duty cycle.
frequency (int) – Initial frequency.
- Returns:
A peripheral object representing the PWM output.
- Return type:
- release_pwm(pwm)[source]
Disconnect the internal PWM peripheral previously bound to a pin.
- Parameters:
pwm (pwmio.PWMOut) – A PWM peripheral created with make_pwm()
- servo_pwm(pin, duty_cycle=0, frequency=50)[source]
Connect a PWM (pulse width modulation) peripheral to the specified pin. This can be used to control servos, or as a general-purpose oscillating output.
- Parameters:
pin (microcontroller.Pin) – The IO pin to use, for example exp.PORT1
duty_cycle (int) – duration pin is high during each period, with 65535 being 100%.
frequency (int) – periodic oscillation frequency in Hz (cycles per second)
- Returns:
The PWMOut object bound to specified pin.
- Return type:
- set_servo(pwm, position_pct)[source]
Set the position of a servo controlled with a PWM output. The PWM frequency must be 50Hz (default value of make_pwm()). Note: Many servos require position_pct value to exceed the ±100 range to reach their mechanical limits. Also an offset may be needed to center the servo.
- Parameters:
pwm (pwmio.PWMOut) – A PWM output created with make_pwm().
position_pct (float) – Desired servo position with “nominal” range -100 to +100. A value of 0 will “center” the servo.
- codex.codex.LED_A = 5
LED A
- codex.codex.LED_B = 4
LED B
- class codex.codex.LEDs(io_expander)[source]
Access to the six LEDs on the CodeX.
- class codex.codex.NEOPixels[source]
Access to the four CodeX neopixels.
- fill(value, brightness=10)[source]
Set all NeoPixels to a single color.
- Parameters:
value (tuple) – A tuple of three values for RGB like (10,88,102)
- get(num=None)[source]
Get current RGB value of Codex NeoPixels. This will be the value after brightness was applied.
- Parameters:
num (int/None) – A number from 0 - 3 will return a single value, None will return tuple of all 4 values.
- Returns:
value (tuple/tuple of 4 tuples)
- class codex.codex.Power(io_expander)[source]
Access to the CodeX power control and power monitoring.
- enable_batt_meas(do_enable)[source]
Enable the battery measurement ability.
- Parameters:
do_enable – True to enable measurement or False to disable
- enable_periph_vcc(do_enable, guarded=True)[source]
Enable power (+5V) for peripheral devices.
- Parameters:
do_enable – True to enable power for peripheral devices
guarded – Protect CPU power from dipping due to peripheral surges.
- get_battery_current()[source]
Measure the power supply current consumption (battery or USB).
- Returns:
Current consumption in milliamps
- Return type:
- get_battery_voltage()[source]
Measure the power supply voltage (battery or USB).
- Returns:
Power supply voltage
- Return type:
- is_batt_meas_enable()[source]
Is the battery measurement ability enabled?
- Returns:
True if the measurement ability is enabled
- Return type: