-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathioe_out.py
executable file
·66 lines (58 loc) · 2.19 KB
/
ioe_out.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2020-2021 by Murray Altheim. All rights reserved. This file is part
# of the Robot Operating System project, released under the MIT License. Please
# see the LICENSE file included as part of this package.
#
# author: Murray Altheim
# created: 2020-12-14
# modified: 2020-12-15
#
# A test file for use with the Pimoroni IO Expander Breakout board.
#
import time, itertools
import ioexpander as io
from colorama import init, Fore, Style
init()
print(Fore.CYAN + '''
Demonstrates handling a single analog or digital input using IO Expander.
Your sensor should be wired between ground and pin 6 for digital, pin 8
for analog on the IO Expander. There is a flag in the file to select which
mode.
For the digital mode a pull-up resistor will be enabled, causing your
sensor to read 1 normally, and 0 when pressed.
''' + Style.RESET_ALL)
analog = True
ioe = io.IOE(i2c_addr=0x18)
if analog:
pin = 8
ioe.set_mode(pin, io.ADC)
print(Fore.CYAN + Style.BRIGHT + 'Configured for analog mode using pin 8.' + Style.RESET_ALL)
else:
pin = 6
ioe.set_mode(pin, io.PIN_MODE_PU)
print(Fore.CYAN + Style.BRIGHT + 'Configured for digital mode using pin 6.' + Style.RESET_ALL)
print(Fore.RED + '\nPress Ctrl+C to exit.\n' + Style.RESET_ALL)
last_value = 0
counter = itertools.count()
while True:
count = next(counter)
value = round(ioe.input(pin),2)
if value < last_value:
if analog:
print(Fore.RED + "{:d}\tbutton value: {:5.2f}v".format(count, value) + Style.RESET_ALL)
else:
print(Fore.RED + "{:d}\tbutton value: {}".format(count, value) + Style.RESET_ALL)
elif value > last_value:
if analog:
print(Fore.GREEN + "{:d}\tbutton value: {:5.2f}v".format(count, value) + Style.RESET_ALL)
else:
print(Fore.GREEN + "{:d}\tbutton value: {}".format(count, value) + Style.RESET_ALL)
else:
if analog:
print(Fore.YELLOW + "{:d}\tbutton value: {:5.2f}v".format(count, value) + Style.RESET_ALL)
else:
print(Fore.YELLOW + "{:d}\tbutton value: {}".format(count, value) + Style.RESET_ALL)
last_value = value
time.sleep(0.5)