codeair – CodeAIR hardware interface

Core low-level module for CodeAIR

The codeair module gives you direct access to all the hardware on the CodeAIR.

class codeair.codeair.Buttons[source]

Access to pushbutton switches BTN-0 and BTN-1.

is_pressed(num=None)[source]

Return True if specified button is pressed, or tuple of both buttons if num=None.

Parameters

num (int) – 0 for BTN_0, 1 for BTN_1

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 both buttons status since last call.

Parameters

num (int) – 0 for BTN_0, 1 for BTN_1

class codeair.codeair.LEDs(i2c)[source]

Access to the eight blue LEDs on the CodeAIR, plus the STAtus LED.

get(num=None)[source]

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

Returns

brightness (0-255) of given LED num, OR tuple : eight numbers – one for each LED

Return type

number

get_status()[source]

Get the current brightness of the status LED.

Returns

brightness (0-255) of the status LED

Return type

number

set(num, brightness)[source]

Set the brightness of one or more LEDs

Parameters
  • num (int) – LED number (0-7) OR (tuple) : multiple LED numbers

  • brightness (int) – (0-255) OR (tuple of ints) : multiple brightness values

set_mask(bitmask, brightness)[source]

Set the brightness of all LEDs using a binary mask

Parameters
  • bitmask (int) – 0-255, but represents a bitmask of which LEDs should be ON (1 bits) or OFF (0 bits)

  • b7

  • brightness (int) – 0-255 is allowed, but 0 is somewhat redundant

set_status(brightness)[source]

Set the brightness of the status LED

Parameters

brightness (int) – (0-255)

class codeair.codeair.NEOPixels[source]

Access to the eight CodeAIR neopixels.

(TOP_FRONT_RIGHT, TOP_FRONT_LEFT, TOP_REAR_RIGHT, TOP_REAR_LEFT,
BOTTOM_FRONT_RIGHT, BOTTOM_FRONT_LEFT, BOTTOM_REAR_RIGHT, BOTTOM_REAR_LEFT)
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 - 7 will return a single value, None will return tuple of all 8 values.

Returns

value (tuple/tuple of 8 tuples)

off()[source]

Turn off all Neopixels.

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

Set the RGB state of the CodeAIR Neopixels.

Parameters
  • num (int/list/tuple) – Pixel number (0,1,2,3,4,5,6,7 or a list of 8 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 codeair.codeair.Power[source]

Access to the CodeAIR power monitoring.

battery_voltage(sample_count=2000, battery_minimum=3.0)[source]

Measure the battery voltage.

A sample_count of 1 seems sufficient when a battery is connected, but when a battery is not connected, the computer sees a voltage that is output periodically by the battery CHARGER. 2000 or more samples are necessary to filter out these readings.

battery_minimum parameter allows observing effects of the battery charger with no battery connected.

Typical use case:

volts = power.battery_voltage() # slow but accurate

If you know for sure a battery is connected:

volts = power.battery_voltage(1) # faster but can be "tricked" by the voltage put out by the battery charger

If you want to see the real-world effects of the battery charger “probing” to see if a battery has been connected:

volts = power.battery_voltage(1, 0.0)
Parameters
  • sample_count (int) – 1 or higher.

  • battery_minimum (float) – defaults to 3.0

Returns

Battery voltage in volts

Return type

float

charger_current(sample_count=1)[source]

Measure the battery charger current. Note that it will be 0 when the charger is not actually charging.

Parameters

sample_count (int) – 1 or higher. The default of 1 seems sufficient, but we chose to match the capabilities of the battery_voltage() function

Returns

Battery charger current in amps

Return type

float

is_usb()[source]

Are we powered by USB or Battery?

Returns

True (USB) or False (Battery)

Return type

bool

class codeair.codeair.SDCard(baudrate=8000000)[source]

Access the SD Card - Flight Data Recorder. This provides a filesystem on ‘/sd’ for Python (no USB direct MSC access). When the SD card is removed/reinserted you’ll need to call ‘mount()’ again.

is_ready()[source]

Return True if SD card is present and readable

mount()[source]

Attempt to mount the SD card. Returns True if successful.

umount()[source]

Disconnect SD interface from filesystem

class codeair.codeair.Speaker[source]

Access to the CodeAIR speaker.

beep(frequency=440, duration=250, duty_cycle=32767)[source]

Output a beep (possibly continuous)

Parameters
  • frequency (int) – xxx or higher. The default value of 440 corresponds to a musical note “A4”

  • duration (int) – Must be >= 0, with 0 being a special case used for starting a beep and leaving it running When not equal to 0, duration indicates the number of milliseconds to play the beep for.

  • duty_cycle (16-bit int) – can be used to vary the width of the pulses sent to the speaker, which will have some effect on the sound characteristics and volume (you can hear the sound get “thinner” as you move off the center point, which is 32767)

Returns

None

off()[source]

Stop outputting sound.

Download Source