Skip to content

Commit 94409e4

Browse files
authored
Merge pull request #25 from brentru/example-codes
Added examples for map_range and shift_in/shift_out
2 parents 31ae5bd + fbfe6e3 commit 94409e4

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

examples/map_range_demo.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
'map_range_demo.py'.
3+
4+
=================================================
5+
maps a number from one range to another
6+
"""
7+
import time
8+
import simpleio
9+
10+
while True:
11+
sensor_value = 150
12+
13+
# Map the sensor's range from 0<=sensor_value<=255 to 0<=sensor_value<=1023
14+
print('original sensor value: ', sensor_value)
15+
mapped_value = simpleio.map_range(sensor_value, 0, 255, 0, 1023)
16+
print('mapped sensor value: ', mapped_value)
17+
time.sleep(2)
18+
19+
# Map the new sensor value back to the old range
20+
sensor_value = simpleio.map_range(mapped_value, 0, 1023, 0, 255)
21+
print('original value returned: ', sensor_value)
22+
time.sleep(2)

examples/shift_in_out_demo.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
'shift_in_out_demo.py'.
3+
4+
=================================================
5+
shifts data into and out of a data pin
6+
"""
7+
8+
import time
9+
import board
10+
import digitalio
11+
import simpleio
12+
13+
# set up clock, data, and latch pins
14+
clk = digitalio.DigitalInOut(board.D12)
15+
data = digitalio.DigitalInOut(board.D11)
16+
latch = digitalio.DigitalInOut(board.D10)
17+
clk.direction = digitalio.Direction.OUTPUT
18+
latch.direction = digitalio.Direction.OUTPUT
19+
20+
while True:
21+
data_to_send = 256
22+
# shifting 256 bits out of data pin
23+
latch.value = False
24+
data.direction = digitalio.Direction.OUTPUT
25+
print('shifting out...')
26+
simpleio.shift_out(data, clk, data_to_send, msb_first=False)
27+
latch.value = True
28+
time.sleep(3)
29+
30+
# shifting 256 bits into the data pin
31+
latch.value = False
32+
data.direction = digitalio.Direction.INPUT
33+
print('shifting in...')
34+
simpleio.shift_in(data, clk)
35+
time.sleep(3)

0 commit comments

Comments
 (0)