simpleio
- Simple, beginner friendly IO.¶
The simpleio
module contains classes to provide simple access to IO.
- Author(s): Scott Shawcroft
-
class
simpleio.
DigitalIn
(pin, **kwargs)[source]¶ Simple digital input that is valid until reload.
param pin microcontroller.Pin: input pin param pull digitalio.Pull: pull configuration for the input -
value
¶ The digital logic level of the input pin.
-
-
class
simpleio.
DigitalOut
(pin, **kwargs)[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 -
value
¶ The digital logic level of the output pin.
-
-
simpleio.
bitWrite
(x, n, b)[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, in_min, in_max, out_min, out_max)[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, clock, msb_first=True)[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:
-
simpleio.
shift_out
(data_pin, clock, value, msb_first=True, bitcount=8)[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)