Skip to content

Commit 21fed8e

Browse files
author
brentru
committed
added examples for map_range and shift_in/shift_out
1 parent 31ae5bd commit 21fed8e

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

examples/map_range_demo.py

Lines changed: 22 additions & 0 deletions
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

Lines changed: 35 additions & 0 deletions
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+
CLK.direction = digitalio.Direction.OUTPUT
16+
DATA = digitalio.DigitalInOut(board.D11)
17+
LATCH = digitalio.DigitalInOut(board.D10)
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)