simpleio - Simple, beginner friendly IO.

The simpleio module contains classes to provide simple access to IO.

  • Author(s): Scott Shawcroft

class simpleio.DigitalIn(pin: Pin, **kwargs: Any)[source]

Simple digital input that is valid until reload.

param pin microcontroller.Pin:

input pin

param pull digitalio.Pull:

pull configuration for the input

property value: bool

The digital logic level of the input pin.

class simpleio.DigitalOut(pin: Pin, **kwargs: Any)[source]

Simple digital output that is valid until reload.

param pin microcontroller.Pin:

output pin

param value bool:

default value

param drive_mode digitalio.DriveMode:

drive mode for the output

property value: bool

The digital logic level of the output pin.

simpleio.bitWrite(x: int, n: int, b: int) int[source]

Based on the Arduino bitWrite function, changes a specific bit of a value to 0 or 1. The return value is the original value with the changed bit. This function is written for use with 8-bit shift registers

Parameters:
  • x – numeric value

  • n – position to change starting with least-significant (right-most) bit as 0

  • b – value to write (0 or 1)

simpleio.map_range(x: float, in_min: float, in_max: float, out_min: float, out_max: float) float[source]

Maps a number from one range to another. Note: This implementation handles values < in_min differently than arduino’s map function does.

Returns:

Returns value mapped to new range

Return type:

float

simpleio.shift_in(data_pin: DigitalInOut, clock: DigitalInOut, msb_first: bool = True) int[source]

Shifts in a byte of data one bit at a time. Starts from either the LSB or MSB.

Warning

Data and clock are swapped compared to other CircuitPython libraries in order to match Arduino.

Parameters:
  • data_pin (DigitalInOut) – pin on which to input each bit

  • clock (DigitalInOut) – toggles to signal data_pin reads

  • msb_first (bool) – True when the first bit is most significant

Returns:

returns the value read

Return type:

int

simpleio.shift_out(data_pin: DigitalInOut, clock: DigitalInOut, value: int, msb_first: bool = True, bitcount: int = 8) None[source]

Shifts out a byte of data one bit at a time. Data gets written to a data pin. Then, the clock pulses hi then low

Warning

Data and clock are swapped compared to other CircuitPython libraries in order to match Arduino.

Parameters:
  • data_pin (DigitalInOut) – value bits get output on this pin

  • clock (DigitalInOut) – toggled once the data pin is set

  • msb_first (bool) – True when the first bit is most significant

  • value (int) – byte to be shifted

  • bitcount (unsigned) – number of bits to shift

Example for Metro M0 Express:

import digitalio
import simpleio
from board import *
clock = digitalio.DigitalInOut(D12)
data_pin = digitalio.DigitalInOut(D11)
latchPin = digitalio.DigitalInOut(D10)
clock.direction = digitalio.Direction.OUTPUT
data_pin.direction = digitalio.Direction.OUTPUT
latchPin.direction = digitalio.Direction.OUTPUT

while True:
    valueSend = 500
    # shifting out least significant bits
    # must toggle latchPin.value before and after shift_out to push to IC chip
    # this sample code was tested using
    latchPin.value = False
    simpleio.shift_out(data_pin, clock, (valueSend>>8), msb_first = False)
    latchPin.value = True
    time.sleep(1.0)
    latchPin.value = False
    simpleio.shift_out(data_pin, clock, valueSend, msb_first = False)
    latchPin.value = True
    time.sleep(1.0)

    # shifting out most significant bits
    latchPin.value = False
    simpleio.shift_out(data_pin, clock, (valueSend>>8))
    latchPin.value = True
    time.sleep(1.0)
    latchpin.value = False
    simpleio.shift_out(data_pin, clock, valueSend)
    latchpin.value = True
    time.sleep(1.0)
simpleio.tone(pin: Pin, frequency: float, duration: int = 1, length: float = 100) None[source]

Generates a square wave of the specified frequency on a pin

Parameters:
  • pin (Pin) – Pin on which to output the tone

  • frequency (float) – Frequency of tone in Hz

  • length (float) – Variable size buffer (optional)

  • duration (int) – Duration of tone in seconds (optional)