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.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.BTN_A = 4

A

codex.BTN_B = 5

B

codex.BTN_D = 2

DOWN

codex.BTN_L = 3

LEFT

codex.BTN_R = 1

RIGHT

codex.BTN_U = 0

UP

class 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.Expansion[source]

Access to Expansion connectors and Peripheral ports. This class provides some functions to simplify use of the available expansion pins, but feel free to use CircuitPython hardware interface functions such as digitalio, analogio, and busio directly with these GPIO pins as well.

GPIO0
GPIO1
GPIO2
GPIO3
GPIO4

Expansion Connectors

PORT0
PORT1
PORT2
PORT3

Peripheral Connectors

make_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 – The IO pin to use, for example exp.PORT1

  • duty_cycle – duration pin is high during each period, with 65535 being 100%.

  • frequency – periodic oscillation frequency in Hz (cycles per second)

Returns

The PWMOut object bound to specified pin.

Return type

pwmio.PWMOut

release_pwm(pwm)[source]

Disconnect the internal PWM peripheral previously bound to a pin.

Parameters

pwm – A PWM peripheral created with make_pwm()

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 – A PWM output created with make_pwm().

  • position_pct – Desired servo position with “nominal” range -100 to +100. A value of 0 will “center” the servo.

codex.LED_A = 5

LED A

codex.LED_B = 4

LED B

class 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.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.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