Skip to content

Added examples for map_range and shift_in/shift_out #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/map_range_demo.py
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions examples/shift_in_out_demo.py
Original file line number Diff line number Diff line change
@@ -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)