codex – CodeX hardware interface

Core low-level module for CodeX

The codex module gives you direct access to all the hardware on the CodeX.
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

int

read_channels()[source]

Read both light sensor ADC values. Value depends on current gain setting.

Returns

The values from both sensors as (ir, ir+visible)

Return type

tuple(int)

read_ir()[source]

Read the IR light level ADC value. Value depends on current gain setting.

Returns

ir light

Return type

int

read_lux(max_lux=500)[source]

Read the infrared plus visible light level in LUX.

Parameters

max_lux (float) – Maximum range of LUX to measure.

Returns

lux value

Return type

float

set_gain(max_lux)[source]

Set the gain (sensitivity) of the sensor.

Parameters

max_lux (float) – Maximum range of LUX to measure.

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 int value 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, and busio directly 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 value attribute 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

analogio.AnalogIn

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 value attribute 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

digitalio.DigitalInOut

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 value attribute of the returned object sets the port’s output level, where True is high and False is low.

Parameters
Returns

A peripheral object representing the digital output.

Return type

digitalio.DigitalInOut

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_cycle attribute of the returned object changes the fraction of each pulse that is high, from no time at 0 to fully on at 65535. Setting the frequency attribute 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

pwmio.PWMOut

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

pwmio.PWMOut

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.

get(num=None)[source]

Get the current states of an LED or all LEDs if None is specified.

Returns

value of given LED num, OR tuple : six bools – one for each LED

Return type

bool

set(num, value=None)[source]

Set the state of a single LED

Parameters
  • num (int) – led number (0-5)

  • value (bool) – True (on) or False (off)

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)

off()[source]

Turn off all Neopixels.

set(num, value=(255, 255, 255), brightness=10)[source]

Set the RGB state of the Codex Neopixels.

Parameters
  • num (int/list/tuple) – Pixel number (0,1,2,3, or a list of 4 values to set all pixels)

  • value (tuple) – A tuple of three values for RGB like (10,88,102)

  • brightness (int) – Value between 0 - 100 to scale the brightness of the RGBs

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

float

get_battery_voltage()[source]

Measure the power supply voltage (battery or USB).

Returns

Power supply voltage

Return type

float

is_batt_meas_enable()[source]

Is the battery measurement ability enabled?

Returns

True if the measurement ability is enabled

Return type

bool

is_periph_vcc_enable()[source]

Is the power enabled for peripheral devices?

Returns

True if power is enabled for peripheral devices

Return type

bool

is_usb()[source]

Are we powered by USB or Battery?

Returns

True (USB) or False (Battery)

Return type

bool

Download Source