This repository has been archived by the owner on Nov 20, 2022. It is now read-only.
/
digital_input_debounce.py
105 lines (84 loc) · 3.41 KB
/
digital_input_debounce.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Copyright (c) 2020 Alan Yorinks All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
Version 3 as published by the Free Software Foundation; either
or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import asyncio
import sys
import time
from pymata_express import pymata_express
"""
Setup a pin for digital input and monitor its changes
Both polling and callback are being used in this example.
This demonstrates one possible way to debounce a switch.
"""
# A global to hold the time of the last change detected.
# Used to debounce a switch
previous_change_time = 0
# differential in time to suppress switch bounces
# adjust this to your device "bounciness"
debounce_time = 600
# Setup a pin for analog input and monitor its changes
DIGITAL_PIN = 12 # arduino pin number
POLL_TIME = 5 # number of seconds between polls
# Callback data indices
# Callback data indices
CB_PIN_MODE = 0
CB_PIN = 1
CB_VALUE = 2
CB_TIME = 3
async def the_callback(data):
"""
A callback function to report data changes.
This will print the pin number, its reported value and
the date and time when the change occurred
:param data: [pin, current reported value, pin_mode, timestamp]
"""
global debounce_time, previous_change_time
# see if we waited long enough for debounce
# get the reported change time
ts_milliseconds = int(round(data[CB_TIME] * 1000))
if ts_milliseconds - previous_change_time > debounce_time:
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[CB_TIME]))
print(f'Pin: {data[CB_PIN]} Value: {data[CB_VALUE]} Time Stamp: {date}')
previous_change_time = ts_milliseconds
async def digital_in(my_board, pin):
"""
This function establishes the pin as a
digital input. Any changes on this pin will
be reported through the call back function.
:param my_board: a pymata_express instance
:param pin: Arduino pin number
"""
# set the pin mode
await my_board.set_pin_mode_digital_input(pin, callback=the_callback)
while True:
# Do a read of the last value reported every 5 seconds and print it
# digital_read returns A tuple of last value change and the time that it occurred
value, time_stamp = await my_board.digital_read(pin)
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_stamp))
print(f'Polling - last change: {value} change received on {date} ')
try:
await asyncio.sleep(POLL_TIME)
except (KeyboardInterrupt, RuntimeError) as e:
loop.run_until_complete(board.shutdown())
sys.exit(0)
# get the event loop
loop = asyncio.get_event_loop()
# instantiate pymata_express
board = pymata_express.PymataExpress()
try:
# start the main function
loop.run_until_complete(digital_in(board, 12))
except (KeyboardInterrupt, RuntimeError) as e:
loop.run_until_complete(board.shutdown())
sys.exit(0)