diff --git a/examples/map_range_demo.py b/examples/map_range_demo.py new file mode 100644 index 0000000..0fcdef5 --- /dev/null +++ b/examples/map_range_demo.py @@ -0,0 +1,22 @@ +""" +'map_range_demo.py'. + +================================================= +maps a number from one range to another +""" +import time +import simpleio + +while True: + sensor_value = 150 + + # Map the sensor's range from 0<=sensor_value<=255 to 0<=sensor_value<=1023 + print('original sensor value: ', sensor_value) + mapped_value = simpleio.map_range(sensor_value, 0, 255, 0, 1023) + print('mapped sensor value: ', mapped_value) + time.sleep(2) + + # Map the new sensor value back to the old range + sensor_value = simpleio.map_range(mapped_value, 0, 1023, 0, 255) + print('original value returned: ', sensor_value) + time.sleep(2) diff --git a/examples/shift_in_out_demo.py b/examples/shift_in_out_demo.py new file mode 100644 index 0000000..5b6d978 --- /dev/null +++ b/examples/shift_in_out_demo.py @@ -0,0 +1,35 @@ +""" +'shift_in_out_demo.py'. + +================================================= +shifts data into and out of a data pin +""" + +import time +import board +import digitalio +import simpleio + +# set up clock, data, and latch pins +clk = digitalio.DigitalInOut(board.D12) +data = digitalio.DigitalInOut(board.D11) +latch = digitalio.DigitalInOut(board.D10) +clk.direction = digitalio.Direction.OUTPUT +latch.direction = digitalio.Direction.OUTPUT + +while True: + data_to_send = 256 + # shifting 256 bits out of data pin + latch.value = False + data.direction = digitalio.Direction.OUTPUT + print('shifting out...') + simpleio.shift_out(data, clk, data_to_send, msb_first=False) + latch.value = True + time.sleep(3) + + # shifting 256 bits into the data pin + latch.value = False + data.direction = digitalio.Direction.INPUT + print('shifting in...') + simpleio.shift_in(data, clk) + time.sleep(3)