Update to use debouncer library
This commit is contained in:
parent
30649574d2
commit
42ee561e30
18 changed files with 62 additions and 902 deletions
|
|
@ -17,14 +17,14 @@ All text above must be included in any redistribution.
|
||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
import board
|
import board
|
||||||
from digitalio import DigitalInOut, Direction, Pull
|
import digitalio
|
||||||
import busio
|
import busio
|
||||||
import adafruit_ds3231
|
import adafruit_ds3231
|
||||||
import audioio
|
import audioio
|
||||||
import pulseio
|
import pulseio
|
||||||
from adafruit_motor import servo
|
from adafruit_motor import servo
|
||||||
import neopixel
|
import neopixel
|
||||||
from debouncer import Debouncer
|
from adafruit_debouncer import Debouncer
|
||||||
|
|
||||||
# Set to false to disable testing/tracing code
|
# Set to false to disable testing/tracing code
|
||||||
TESTING = True
|
TESTING = True
|
||||||
|
|
@ -58,8 +58,8 @@ RESET_STATE = 7
|
||||||
|
|
||||||
# Power to the speaker and neopixels must be enabled using this pin
|
# Power to the speaker and neopixels must be enabled using this pin
|
||||||
|
|
||||||
enable = DigitalInOut(POWER_PIN)
|
enable = digitalio.DigitalInOut(POWER_PIN)
|
||||||
enable.direction = Direction.OUTPUT
|
enable.direction = digitalio.Direction.OUTPUT
|
||||||
enable.value = True
|
enable.value = True
|
||||||
|
|
||||||
i2c = busio.I2C(board.SCL, board.SDA)
|
i2c = busio.I2C(board.SCL, board.SDA)
|
||||||
|
|
@ -71,7 +71,11 @@ strip = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_PIXELS, brightness=1, auto_write=Fal
|
||||||
strip.fill(0) # NeoPixels off ASAP on startup
|
strip.fill(0) # NeoPixels off ASAP on startup
|
||||||
strip.show()
|
strip.show()
|
||||||
|
|
||||||
switch = Debouncer(SWITCH_PIN, Pull.UP, 0.01)
|
switch_io = digitalio.DigitalInOut(SWITCH_PIN)
|
||||||
|
switch_io.direction = digitalio.Direction.INPUT
|
||||||
|
switch_io.pull = digitalio.Pull.UP
|
||||||
|
|
||||||
|
switch = Debouncer(switch_io)
|
||||||
|
|
||||||
# create a PWMOut object on Pin A2.
|
# create a PWMOut object on Pin A2.
|
||||||
pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
|
pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
|
||||||
|
|
|
||||||
|
|
@ -1,92 +0,0 @@
|
||||||
"""
|
|
||||||
GPIO Pin Debouncer
|
|
||||||
|
|
||||||
Adafruit invests time and resources providing this open source code.
|
|
||||||
Please support Adafruit and open source hardware by purchasing
|
|
||||||
products from Adafruit!
|
|
||||||
|
|
||||||
Written by Dave Astels for Adafruit Industries
|
|
||||||
Copyright (c) 2018 Adafruit Industries
|
|
||||||
Licensed under the MIT license.
|
|
||||||
|
|
||||||
All text above must be included in any redistribution.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
import digitalio
|
|
||||||
|
|
||||||
class Debouncer(object):
|
|
||||||
"""Debounce an input pin"""
|
|
||||||
|
|
||||||
DEBOUNCED_STATE = 0x01
|
|
||||||
UNSTABLE_STATE = 0x02
|
|
||||||
CHANGED_STATE = 0x04
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, pin, mode=None, interval=0.010):
|
|
||||||
"""Make am instance.
|
|
||||||
:param int pin: the pin (from board) to debounce
|
|
||||||
:param int mode: digitalio.Pull.UP or .DOWN (default is no pull up/down)
|
|
||||||
:param int interval: bounce threshold in seconds (default is 0.010, i.e. 10 milliseconds)
|
|
||||||
"""
|
|
||||||
self.state = 0x00
|
|
||||||
self.pin = digitalio.DigitalInOut(pin)
|
|
||||||
self.pin.direction = digitalio.Direction.INPUT
|
|
||||||
if mode != None:
|
|
||||||
self.pin.pull = mode
|
|
||||||
if self.pin.value:
|
|
||||||
self.__set_state(Debouncer.DEBOUNCED_STATE | Debouncer.UNSTABLE_STATE)
|
|
||||||
self.previous_time = 0
|
|
||||||
if interval is None:
|
|
||||||
self.interval = 0.010
|
|
||||||
else:
|
|
||||||
self.interval = interval
|
|
||||||
|
|
||||||
|
|
||||||
def __set_state(self, bits):
|
|
||||||
self.state |= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __unset_state(self, bits):
|
|
||||||
self.state &= ~bits
|
|
||||||
|
|
||||||
|
|
||||||
def __toggle_state(self, bits):
|
|
||||||
self.state ^= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __get_state(self, bits):
|
|
||||||
return (self.state & bits) != 0
|
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update the debouncer state. Must be called before using any of the properties below"""
|
|
||||||
self.__unset_state(Debouncer.CHANGED_STATE)
|
|
||||||
current_state = self.pin.value
|
|
||||||
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.UNSTABLE_STATE)
|
|
||||||
else:
|
|
||||||
if time.monotonic() - self.previous_time >= self.interval:
|
|
||||||
if current_state != self.__get_state(Debouncer.DEBOUNCED_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
self.__set_state(Debouncer.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
"""Return the current debounced value of the input."""
|
|
||||||
return self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def rose(self):
|
|
||||||
"""Return whether the debounced input went from low to high at the most recent update."""
|
|
||||||
return self.__get_state(self.DEBOUNCED_STATE) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fell(self):
|
|
||||||
"""Return whether the debounced input went from high to low at the most recent update."""
|
|
||||||
return (not self.__get_state(self.DEBOUNCED_STATE)) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
@ -17,14 +17,14 @@ All text above must be included in any redistribution.
|
||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
import board
|
import board
|
||||||
from digitalio import DigitalInOut, Direction, Pull
|
import digitalio
|
||||||
import busio
|
import busio
|
||||||
import adafruit_ds3231
|
import adafruit_ds3231
|
||||||
import audioio
|
import audioio
|
||||||
import pulseio
|
import pulseio
|
||||||
from adafruit_motor import servo
|
from adafruit_motor import servo
|
||||||
import neopixel
|
import neopixel
|
||||||
from debouncer import Debouncer
|
from adafruit_debouncer import Debouncer
|
||||||
|
|
||||||
# Set to false to disable testing/tracing code
|
# Set to false to disable testing/tracing code
|
||||||
TESTING = True
|
TESTING = True
|
||||||
|
|
@ -47,8 +47,8 @@ SERVO_PIN = board.A1
|
||||||
|
|
||||||
# Power to the speaker and neopixels must be enabled using this pin
|
# Power to the speaker and neopixels must be enabled using this pin
|
||||||
|
|
||||||
enable = DigitalInOut(POWER_PIN)
|
enable = digitalio.DigitalInOut(POWER_PIN)
|
||||||
enable.direction = Direction.OUTPUT
|
enable.direction = digitalio.Direction.OUTPUT
|
||||||
enable.value = True
|
enable.value = True
|
||||||
|
|
||||||
i2c = busio.I2C(board.SCL, board.SDA)
|
i2c = busio.I2C(board.SCL, board.SDA)
|
||||||
|
|
@ -60,7 +60,10 @@ strip = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_PIXELS, brightness=1, auto_write=Fal
|
||||||
strip.fill(0) # NeoPixels off ASAP on startup
|
strip.fill(0) # NeoPixels off ASAP on startup
|
||||||
strip.show()
|
strip.show()
|
||||||
|
|
||||||
switch = Debouncer(SWITCH_PIN, Pull.UP, 0.01)
|
switch_io = digitalio.DigitalInOut(SWITCH_PIN)
|
||||||
|
switch_io.direction = digitalio.Direction.INPUT
|
||||||
|
switch_io.pull = digitalio.Pull.UP
|
||||||
|
switch = Debouncer(switch_io)
|
||||||
|
|
||||||
# create a PWMOut object on Pin A2.
|
# create a PWMOut object on Pin A2.
|
||||||
pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
|
pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
|
||||||
|
|
|
||||||
|
|
@ -1,92 +0,0 @@
|
||||||
"""
|
|
||||||
GPIO Pin Debouncer
|
|
||||||
|
|
||||||
Adafruit invests time and resources providing this open source code.
|
|
||||||
Please support Adafruit and open source hardware by purchasing
|
|
||||||
products from Adafruit!
|
|
||||||
|
|
||||||
Written by Dave Astels for Adafruit Industries
|
|
||||||
Copyright (c) 2018 Adafruit Industries
|
|
||||||
Licensed under the MIT license.
|
|
||||||
|
|
||||||
All text above must be included in any redistribution.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
import digitalio
|
|
||||||
|
|
||||||
class Debouncer(object):
|
|
||||||
"""Debounce an input pin"""
|
|
||||||
|
|
||||||
DEBOUNCED_STATE = 0x01
|
|
||||||
UNSTABLE_STATE = 0x02
|
|
||||||
CHANGED_STATE = 0x04
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, pin, mode=None, interval=0.010):
|
|
||||||
"""Make am instance.
|
|
||||||
:param int pin: the pin (from board) to debounce
|
|
||||||
:param int mode: digitalio.Pull.UP or .DOWN (default is no pull up/down)
|
|
||||||
:param int interval: bounce threshold in seconds (default is 0.010, i.e. 10 milliseconds)
|
|
||||||
"""
|
|
||||||
self.state = 0x00
|
|
||||||
self.pin = digitalio.DigitalInOut(pin)
|
|
||||||
self.pin.direction = digitalio.Direction.INPUT
|
|
||||||
if mode != None:
|
|
||||||
self.pin.pull = mode
|
|
||||||
if self.pin.value:
|
|
||||||
self.__set_state(Debouncer.DEBOUNCED_STATE | Debouncer.UNSTABLE_STATE)
|
|
||||||
self.previous_time = 0
|
|
||||||
if interval is None:
|
|
||||||
self.interval = 0.010
|
|
||||||
else:
|
|
||||||
self.interval = interval
|
|
||||||
|
|
||||||
|
|
||||||
def __set_state(self, bits):
|
|
||||||
self.state |= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __unset_state(self, bits):
|
|
||||||
self.state &= ~bits
|
|
||||||
|
|
||||||
|
|
||||||
def __toggle_state(self, bits):
|
|
||||||
self.state ^= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __get_state(self, bits):
|
|
||||||
return (self.state & bits) != 0
|
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update the debouncer state. Must be called before using any of the properties below"""
|
|
||||||
self.__unset_state(Debouncer.CHANGED_STATE)
|
|
||||||
current_state = self.pin.value
|
|
||||||
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.UNSTABLE_STATE)
|
|
||||||
else:
|
|
||||||
if time.monotonic() - self.previous_time >= self.interval:
|
|
||||||
if current_state != self.__get_state(Debouncer.DEBOUNCED_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
self.__set_state(Debouncer.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
"""Return the current debounced value of the input."""
|
|
||||||
return self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def rose(self):
|
|
||||||
"""Return whether the debounced input went from low to high at the most recent update."""
|
|
||||||
return self.__get_state(self.DEBOUNCED_STATE) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fell(self):
|
|
||||||
"""Return whether the debounced input went from high to low at the most recent update."""
|
|
||||||
return (not self.__get_state(self.DEBOUNCED_STATE)) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import time
|
import time
|
||||||
import board
|
import board
|
||||||
import debouncer
|
from adafruit_debouncer import Debouncer
|
||||||
import busio as io
|
import busio as io
|
||||||
import digitalio
|
import digitalio
|
||||||
import pulseio
|
import pulseio
|
||||||
|
|
@ -9,8 +9,14 @@ import adafruit_ssd1306
|
||||||
i2c = io.I2C(board.SCL, board.SDA)
|
i2c = io.I2C(board.SCL, board.SDA)
|
||||||
reset_pin = digitalio.DigitalInOut(board.D11)
|
reset_pin = digitalio.DigitalInOut(board.D11)
|
||||||
oled = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, reset=reset_pin)
|
oled = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, reset=reset_pin)
|
||||||
button_select = debouncer.Debouncer(board.D7, mode=digitalio.Pull.UP)
|
select = digitalio.DigitalInOut(board.D7)
|
||||||
button_play = debouncer.Debouncer(board.D9, mode=digitalio.Pull.UP)
|
select.direction = digitalio.Direction.INPUT
|
||||||
|
select.pull = digitalio.Pull.UP
|
||||||
|
button_select = Debouncer(select)
|
||||||
|
play = digitalio.DigitalInOut(board.D9)
|
||||||
|
play.direction = digitalio.Direction.INPUT
|
||||||
|
play.pull = digitalio.Pull.UP
|
||||||
|
button_play = Debouncer(play)
|
||||||
|
|
||||||
C4 = 261
|
C4 = 261
|
||||||
C_SH_4 = 277
|
C_SH_4 = 277
|
||||||
|
|
|
||||||
|
|
@ -1,105 +0,0 @@
|
||||||
"""
|
|
||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2018 Dave Astels
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
Debounce an input pin.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
import digitalio
|
|
||||||
|
|
||||||
class Debouncer:
|
|
||||||
"""Debounce an input pin"""
|
|
||||||
|
|
||||||
DEBOUNCED_STATE = 0x01
|
|
||||||
UNSTABLE_STATE = 0x02
|
|
||||||
CHANGED_STATE = 0x04
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, pin, mode=None, interval=0.010):
|
|
||||||
"""Make an instance.
|
|
||||||
:param int pin: the pin (from board) to debounce
|
|
||||||
:param int mode: digitalio.Pull.UP or .DOWN (default is no pull up/down)
|
|
||||||
:param int interval: bounce threshold in seconds (default is 0.010, i.e. 10 milliseconds)
|
|
||||||
"""
|
|
||||||
self.state = 0x00
|
|
||||||
self.pin = digitalio.DigitalInOut(pin)
|
|
||||||
self.pin.direction = digitalio.Direction.INPUT
|
|
||||||
if mode is not None:
|
|
||||||
self.pin.pull = mode
|
|
||||||
if self.pin.value:
|
|
||||||
self.__set_state(Debouncer.DEBOUNCED_STATE | Debouncer.UNSTABLE_STATE)
|
|
||||||
self.previous_time = 0
|
|
||||||
if interval is None:
|
|
||||||
self.interval = 0.010
|
|
||||||
else:
|
|
||||||
self.interval = interval
|
|
||||||
|
|
||||||
|
|
||||||
def __set_state(self, bits):
|
|
||||||
self.state |= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __unset_state(self, bits):
|
|
||||||
self.state &= ~bits
|
|
||||||
|
|
||||||
|
|
||||||
def __toggle_state(self, bits):
|
|
||||||
self.state ^= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __get_state(self, bits):
|
|
||||||
return (self.state & bits) != 0
|
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update the debouncer state. Must be called before using any of the properties below"""
|
|
||||||
self.__unset_state(Debouncer.CHANGED_STATE)
|
|
||||||
current_state = self.pin.value
|
|
||||||
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.UNSTABLE_STATE)
|
|
||||||
else:
|
|
||||||
if time.monotonic() - self.previous_time >= self.interval:
|
|
||||||
if current_state != self.__get_state(Debouncer.DEBOUNCED_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
self.__set_state(Debouncer.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
"""Return the current debounced value of the input."""
|
|
||||||
return self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def rose(self):
|
|
||||||
"""Return whether the debounced input went from low to high at the most recent update."""
|
|
||||||
return self.__get_state(self.DEBOUNCED_STATE) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fell(self):
|
|
||||||
"""Return whether the debounced input went from high to low at the most recent update."""
|
|
||||||
return (not self.__get_state(self.DEBOUNCED_STATE)) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
@ -1,107 +0,0 @@
|
||||||
"""
|
|
||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2018 Dave Astels
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
Debounce an input pin.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
import digitalio
|
|
||||||
|
|
||||||
|
|
||||||
class Debouncer:
|
|
||||||
"""Debounce an input pin"""
|
|
||||||
|
|
||||||
DEBOUNCED_STATE = 0x01
|
|
||||||
UNSTABLE_STATE = 0x02
|
|
||||||
CHANGED_STATE = 0x04
|
|
||||||
|
|
||||||
def __init__(self, pin, mode=None, interval=0.010):
|
|
||||||
"""Make am instance.
|
|
||||||
:param int pin: the pin (from board) to debounce
|
|
||||||
:param int mode: digitalio.Pull.UP or .DOWN (default is no
|
|
||||||
pull up/down)
|
|
||||||
:param int interval: bounce threshold in seconds (default is 0.010,
|
|
||||||
i.e. 10 milliseconds)
|
|
||||||
"""
|
|
||||||
self.state = 0x00
|
|
||||||
self.pin = digitalio.DigitalInOut(pin)
|
|
||||||
self.pin.direction = digitalio.Direction.INPUT
|
|
||||||
if mode is not None:
|
|
||||||
self.pin.pull = mode
|
|
||||||
if self.pin.value:
|
|
||||||
self.__set_state(Debouncer.DEBOUNCED_STATE |
|
|
||||||
Debouncer.UNSTABLE_STATE)
|
|
||||||
self.previous_time = 0
|
|
||||||
if interval is None:
|
|
||||||
self.interval = 0.010
|
|
||||||
else:
|
|
||||||
self.interval = interval
|
|
||||||
|
|
||||||
def __set_state(self, bits):
|
|
||||||
self.state |= bits
|
|
||||||
|
|
||||||
def __unset_state(self, bits):
|
|
||||||
self.state &= ~bits
|
|
||||||
|
|
||||||
def __toggle_state(self, bits):
|
|
||||||
self.state ^= bits
|
|
||||||
|
|
||||||
def __get_state(self, bits):
|
|
||||||
return (self.state & bits) != 0
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update the debouncer state. Must be called before using any of
|
|
||||||
the properties below"""
|
|
||||||
self.__unset_state(Debouncer.CHANGED_STATE)
|
|
||||||
current_state = self.pin.value
|
|
||||||
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.UNSTABLE_STATE)
|
|
||||||
else:
|
|
||||||
if time.monotonic() - self.previous_time >= self.interval:
|
|
||||||
debounced_state = self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
if current_state != debounced_state:
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
self.__set_state(Debouncer.CHANGED_STATE)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
"""Return the current debounced value of the input."""
|
|
||||||
return self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def rose(self):
|
|
||||||
"""Return whether the debounced input went from low to high at
|
|
||||||
the most recent update."""
|
|
||||||
return self.__get_state(self.DEBOUNCED_STATE) \
|
|
||||||
and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fell(self):
|
|
||||||
"""Return whether the debounced input went from high to low at
|
|
||||||
the most recent update."""
|
|
||||||
return (not self.__get_state(self.DEBOUNCED_STATE)) \
|
|
||||||
and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
@ -35,7 +35,7 @@ import board
|
||||||
import busio
|
import busio
|
||||||
import digitalio
|
import digitalio
|
||||||
import storage
|
import storage
|
||||||
from debouncer import Debouncer
|
from adafruit_debouncer import Debouncer
|
||||||
from directory_node import DirectoryNode
|
from directory_node import DirectoryNode
|
||||||
from emulator import Emulator
|
from emulator import Emulator
|
||||||
|
|
||||||
|
|
@ -44,7 +44,10 @@ from emulator import Emulator
|
||||||
# Initialize Rotary encoder
|
# Initialize Rotary encoder
|
||||||
|
|
||||||
# Encoder button is a digital input with pullup on D2
|
# Encoder button is a digital input with pullup on D2
|
||||||
button = Debouncer(board.D2, digitalio.Pull.UP, 0.01)
|
encoder_switch = digitalio.DigitalInOut(board.D2)
|
||||||
|
encoder_switch.direction = digitalio.Direction.INPUT
|
||||||
|
encoder_switch.pull = digitalio.Pull.UP
|
||||||
|
button = Debouncer(encoder_switch)
|
||||||
|
|
||||||
# Rotary encoder inputs with pullup on D3 & D4
|
# Rotary encoder inputs with pullup on D3 & D4
|
||||||
rot_a = digitalio.DigitalInOut(board.D4)
|
rot_a = digitalio.DigitalInOut(board.D4)
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import displayio
|
||||||
import audioio
|
import audioio
|
||||||
from busio import UART
|
from busio import UART
|
||||||
import adafruit_gps
|
import adafruit_gps
|
||||||
from debouncer import Debouncer
|
from adafruit_debouncer import Debouncer
|
||||||
|
|
||||||
uart = UART(board.TX, board.RX, baudrate=9600, timeout=3000)
|
uart = UART(board.TX, board.RX, baudrate=9600, timeout=3000)
|
||||||
gps = adafruit_gps.GPS(uart)
|
gps = adafruit_gps.GPS(uart)
|
||||||
|
|
@ -31,7 +31,11 @@ gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
|
||||||
# Set update rate to once a second (1hz) which is what you typically want.
|
# Set update rate to once a second (1hz) which is what you typically want.
|
||||||
gps.send_command(b'PMTK220,1000')
|
gps.send_command(b'PMTK220,1000')
|
||||||
|
|
||||||
switch = Debouncer(board.SENSE, Pull.UP, 0.01)
|
switch_io = DigitalInOut(board.SENSE)
|
||||||
|
switch_io.direction = Direction.INPUT
|
||||||
|
switch_io.pull = Pull.UP
|
||||||
|
|
||||||
|
switch = Debouncer(switch_io)
|
||||||
|
|
||||||
audio = audioio.AudioOut(board.A0)
|
audio = audioio.AudioOut(board.A0)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,94 +0,0 @@
|
||||||
"""
|
|
||||||
Input pin debouncer.
|
|
||||||
|
|
||||||
Adafruit invests time and resources providing this open source code.
|
|
||||||
Please support Adafruit and open source hardware by purchasing
|
|
||||||
products from Adafruit!
|
|
||||||
|
|
||||||
Written by Dave Astels for Adafruit Industries
|
|
||||||
Copyright (c) 2018 Adafruit Industries
|
|
||||||
Licensed under the MIT license.
|
|
||||||
|
|
||||||
All text above must be included in any redistribution.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
import digitalio
|
|
||||||
|
|
||||||
|
|
||||||
class Debouncer:
|
|
||||||
"""Debounce an input pin"""
|
|
||||||
|
|
||||||
DEBOUNCED_STATE = 0x01
|
|
||||||
UNSTABLE_STATE = 0x02
|
|
||||||
CHANGED_STATE = 0x04
|
|
||||||
|
|
||||||
def __init__(self, pin, mode=None, interval=0.010):
|
|
||||||
"""Make am instance.
|
|
||||||
:param int pin: the pin (from board) to debounce
|
|
||||||
:param int mode: digitalio.Pull.UP or .DOWN (default is no
|
|
||||||
pull up/down)
|
|
||||||
:param int interval: bounce threshold in seconds (default is 0.010,
|
|
||||||
i.e. 10 milliseconds)
|
|
||||||
"""
|
|
||||||
self.state = 0x00
|
|
||||||
self.pin = digitalio.DigitalInOut(pin)
|
|
||||||
self.pin.direction = digitalio.Direction.INPUT
|
|
||||||
if mode is not None:
|
|
||||||
self.pin.pull = mode
|
|
||||||
if self.pin.value:
|
|
||||||
self.__set_state(Debouncer.DEBOUNCED_STATE |
|
|
||||||
Debouncer.UNSTABLE_STATE)
|
|
||||||
self.previous_time = 0
|
|
||||||
if interval is None:
|
|
||||||
self.interval = 0.010
|
|
||||||
else:
|
|
||||||
self.interval = interval
|
|
||||||
|
|
||||||
def __set_state(self, bits):
|
|
||||||
self.state |= bits
|
|
||||||
|
|
||||||
def __unset_state(self, bits):
|
|
||||||
self.state &= ~bits
|
|
||||||
|
|
||||||
def __toggle_state(self, bits):
|
|
||||||
self.state ^= bits
|
|
||||||
|
|
||||||
def __get_state(self, bits):
|
|
||||||
return (self.state & bits) != 0
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update the debouncer state. Must be called before using any of
|
|
||||||
the properties below"""
|
|
||||||
self.__unset_state(Debouncer.CHANGED_STATE)
|
|
||||||
current_state = self.pin.value
|
|
||||||
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.UNSTABLE_STATE)
|
|
||||||
else:
|
|
||||||
if time.monotonic() - self.previous_time >= self.interval:
|
|
||||||
debounced_state = self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
if current_state != debounced_state:
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
self.__set_state(Debouncer.CHANGED_STATE)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
"""Return the current debounced value of the input."""
|
|
||||||
return self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def rose(self):
|
|
||||||
"""Return whether the debounced input went from low to high at
|
|
||||||
the most recent update."""
|
|
||||||
return self.__get_state(self.DEBOUNCED_STATE) \
|
|
||||||
and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fell(self):
|
|
||||||
"""Return whether the debounced input went from high to low at
|
|
||||||
the most recent update."""
|
|
||||||
return (not self.__get_state(self.DEBOUNCED_STATE)) \
|
|
||||||
and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
@ -24,7 +24,7 @@ import audioio
|
||||||
import pulseio
|
import pulseio
|
||||||
from adafruit_motor import servo
|
from adafruit_motor import servo
|
||||||
import neopixel
|
import neopixel
|
||||||
from debouncer import Debouncer
|
from adafruit_debouncer import Debouncer
|
||||||
|
|
||||||
# Set to false to disable testing/tracing code
|
# Set to false to disable testing/tracing code
|
||||||
TESTING = True
|
TESTING = True
|
||||||
|
|
@ -71,7 +71,10 @@ strip = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_PIXELS, brightness=1, auto_write=Fal
|
||||||
strip.fill(0) # NeoPixels off ASAP on startup
|
strip.fill(0) # NeoPixels off ASAP on startup
|
||||||
strip.show()
|
strip.show()
|
||||||
|
|
||||||
switch = Debouncer(SWITCH_PIN, Pull.UP, 0.01)
|
switch_io = DigitalInOut(SWITCH_PIN)
|
||||||
|
switch_io.direction = Direction.INPUT
|
||||||
|
switch_io.pull = Pull.UP
|
||||||
|
switch = Debouncer(switch_io)
|
||||||
|
|
||||||
# create a PWMOut object on Pin A2.
|
# create a PWMOut object on Pin A2.
|
||||||
pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
|
pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
|
||||||
|
|
|
||||||
|
|
@ -1,92 +0,0 @@
|
||||||
"""
|
|
||||||
GPIO Pin Debouncer
|
|
||||||
|
|
||||||
Adafruit invests time and resources providing this open source code.
|
|
||||||
Please support Adafruit and open source hardware by purchasing
|
|
||||||
products from Adafruit!
|
|
||||||
|
|
||||||
Written by Dave Astels for Adafruit Industries
|
|
||||||
Copyright (c) 2018 Adafruit Industries
|
|
||||||
Licensed under the MIT license.
|
|
||||||
|
|
||||||
All text above must be included in any redistribution.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
import digitalio
|
|
||||||
|
|
||||||
class Debouncer(object):
|
|
||||||
"""Debounce an input pin"""
|
|
||||||
|
|
||||||
DEBOUNCED_STATE = 0x01
|
|
||||||
UNSTABLE_STATE = 0x02
|
|
||||||
CHANGED_STATE = 0x04
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, pin, mode=None, interval=0.010):
|
|
||||||
"""Make am instance.
|
|
||||||
:param int pin: the pin (from board) to debounce
|
|
||||||
:param int mode: digitalio.Pull.UP or .DOWN (default is no pull up/down)
|
|
||||||
:param int interval: bounce threshold in seconds (default is 0.010, i.e. 10 milliseconds)
|
|
||||||
"""
|
|
||||||
self.state = 0x00
|
|
||||||
self.pin = digitalio.DigitalInOut(pin)
|
|
||||||
self.pin.direction = digitalio.Direction.INPUT
|
|
||||||
if mode != None:
|
|
||||||
self.pin.pull = mode
|
|
||||||
if self.pin.value:
|
|
||||||
self.__set_state(Debouncer.DEBOUNCED_STATE | Debouncer.UNSTABLE_STATE)
|
|
||||||
self.previous_time = 0
|
|
||||||
if interval is None:
|
|
||||||
self.interval = 0.010
|
|
||||||
else:
|
|
||||||
self.interval = interval
|
|
||||||
|
|
||||||
|
|
||||||
def __set_state(self, bits):
|
|
||||||
self.state |= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __unset_state(self, bits):
|
|
||||||
self.state &= ~bits
|
|
||||||
|
|
||||||
|
|
||||||
def __toggle_state(self, bits):
|
|
||||||
self.state ^= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __get_state(self, bits):
|
|
||||||
return (self.state & bits) != 0
|
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update the debouncer state. Must be called before using any of the properties below"""
|
|
||||||
self.__unset_state(Debouncer.CHANGED_STATE)
|
|
||||||
current_state = self.pin.value
|
|
||||||
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.UNSTABLE_STATE)
|
|
||||||
else:
|
|
||||||
if time.monotonic() - self.previous_time >= self.interval:
|
|
||||||
if current_state != self.__get_state(Debouncer.DEBOUNCED_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
self.__set_state(Debouncer.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
"""Return the current debounced value of the input."""
|
|
||||||
return self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def rose(self):
|
|
||||||
"""Return whether the debounced input went from low to high at the most recent update."""
|
|
||||||
return self.__get_state(self.DEBOUNCED_STATE) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fell(self):
|
|
||||||
"""Return whether the debounced input went from high to low at the most recent update."""
|
|
||||||
return (not self.__get_state(self.DEBOUNCED_STATE)) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
@ -28,14 +28,17 @@ from math import ceil
|
||||||
import board
|
import board
|
||||||
import rotaryio
|
import rotaryio
|
||||||
import neopixel
|
import neopixel
|
||||||
import debouncer
|
from adafruit_debouncer import Debouncer
|
||||||
import digitalio
|
import digitalio
|
||||||
import pulseio
|
import pulseio
|
||||||
|
|
||||||
# Setup the hardware
|
# Setup the hardware
|
||||||
|
|
||||||
encoder = rotaryio.IncrementalEncoder(board.D9, board.D7)
|
encoder = rotaryio.IncrementalEncoder(board.D9, board.D7)
|
||||||
button = debouncer.Debouncer(board.D10, digitalio.Pull.UP, 0.01)
|
button_io = digitalio.DigitalInOut(board.D10)
|
||||||
|
button_io.direction = digitalio.Direction.INPUT
|
||||||
|
button_io.pull = digitalio.Pull.UP
|
||||||
|
button = Debouncer(button_io)
|
||||||
strip = neopixel.NeoPixel(board.D11, 16, brightness=1, auto_write=False)
|
strip = neopixel.NeoPixel(board.D11, 16, brightness=1, auto_write=False)
|
||||||
|
|
||||||
last_position = 0
|
last_position = 0
|
||||||
|
|
|
||||||
|
|
@ -1,105 +0,0 @@
|
||||||
"""
|
|
||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2018 Dave Astels
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
Debounce an input pin.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
import digitalio
|
|
||||||
|
|
||||||
class Debouncer:
|
|
||||||
"""Debounce an input pin"""
|
|
||||||
|
|
||||||
DEBOUNCED_STATE = 0x01
|
|
||||||
UNSTABLE_STATE = 0x02
|
|
||||||
CHANGED_STATE = 0x04
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, pin, mode=None, interval=0.010):
|
|
||||||
"""Make am instance.
|
|
||||||
:param int pin: the pin (from board) to debounce
|
|
||||||
:param int mode: digitalio.Pull.UP or .DOWN (default is no pull up/down)
|
|
||||||
:param int interval: bounce threshold in seconds (default is 0.010, i.e. 10 milliseconds)
|
|
||||||
"""
|
|
||||||
self.state = 0x00
|
|
||||||
self.pin = digitalio.DigitalInOut(pin)
|
|
||||||
self.pin.direction = digitalio.Direction.INPUT
|
|
||||||
if mode is not None:
|
|
||||||
self.pin.pull = mode
|
|
||||||
if self.pin.value:
|
|
||||||
self.__set_state(Debouncer.DEBOUNCED_STATE | Debouncer.UNSTABLE_STATE)
|
|
||||||
self.previous_time = 0
|
|
||||||
if interval is None:
|
|
||||||
self.interval = 0.010
|
|
||||||
else:
|
|
||||||
self.interval = interval
|
|
||||||
|
|
||||||
|
|
||||||
def __set_state(self, bits):
|
|
||||||
self.state |= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __unset_state(self, bits):
|
|
||||||
self.state &= ~bits
|
|
||||||
|
|
||||||
|
|
||||||
def __toggle_state(self, bits):
|
|
||||||
self.state ^= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __get_state(self, bits):
|
|
||||||
return (self.state & bits) != 0
|
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update the debouncer state. Must be called before using any of the properties below"""
|
|
||||||
self.__unset_state(Debouncer.CHANGED_STATE)
|
|
||||||
current_state = self.pin.value
|
|
||||||
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.UNSTABLE_STATE)
|
|
||||||
else:
|
|
||||||
if time.monotonic() - self.previous_time >= self.interval:
|
|
||||||
if current_state != self.__get_state(Debouncer.DEBOUNCED_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
self.__set_state(Debouncer.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
"""Return the current debounced value of the input."""
|
|
||||||
return self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def rose(self):
|
|
||||||
"""Return whether the debounced input went from low to high at the most recent update."""
|
|
||||||
return self.__get_state(self.DEBOUNCED_STATE) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fell(self):
|
|
||||||
"""Return whether the debounced input went from high to low at the most recent update."""
|
|
||||||
return (not self.__get_state(self.DEBOUNCED_STATE)) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
@ -1,92 +0,0 @@
|
||||||
"""
|
|
||||||
GPIO Pin Debouncer
|
|
||||||
|
|
||||||
Adafruit invests time and resources providing this open source code.
|
|
||||||
Please support Adafruit and open source hardware by purchasing
|
|
||||||
products from Adafruit!
|
|
||||||
|
|
||||||
Written by Dave Astels for Adafruit Industries
|
|
||||||
Copyright (c) 2018 Adafruit Industries
|
|
||||||
Licensed under the MIT license.
|
|
||||||
|
|
||||||
All text above must be included in any redistribution.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
import digitalio
|
|
||||||
|
|
||||||
class Debouncer(object):
|
|
||||||
"""Debounce an input pin"""
|
|
||||||
|
|
||||||
DEBOUNCED_STATE = 0x01
|
|
||||||
UNSTABLE_STATE = 0x02
|
|
||||||
CHANGED_STATE = 0x04
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, pin, mode=None, interval=0.010):
|
|
||||||
"""Make am instance.
|
|
||||||
:param int pin: the pin (from board) to debounce
|
|
||||||
:param int mode: digitalio.Pull.UP or .DOWN (default is no pull up/down)
|
|
||||||
:param int interval: bounce threshold in seconds (default is 0.010, i.e. 10 milliseconds)
|
|
||||||
"""
|
|
||||||
self.state = 0x00
|
|
||||||
self.pin = digitalio.DigitalInOut(pin)
|
|
||||||
self.pin.direction = digitalio.Direction.INPUT
|
|
||||||
if mode != None:
|
|
||||||
self.pin.pull = mode
|
|
||||||
if self.pin.value:
|
|
||||||
self.__set_state(Debouncer.DEBOUNCED_STATE | Debouncer.UNSTABLE_STATE)
|
|
||||||
self.previous_time = 0
|
|
||||||
if interval is None:
|
|
||||||
self.interval = 0.010
|
|
||||||
else:
|
|
||||||
self.interval = interval
|
|
||||||
|
|
||||||
|
|
||||||
def __set_state(self, bits):
|
|
||||||
self.state |= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __unset_state(self, bits):
|
|
||||||
self.state &= ~bits
|
|
||||||
|
|
||||||
|
|
||||||
def __toggle_state(self, bits):
|
|
||||||
self.state ^= bits
|
|
||||||
|
|
||||||
|
|
||||||
def __get_state(self, bits):
|
|
||||||
return (self.state & bits) != 0
|
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update the debouncer state. Must be called before using any of the properties below"""
|
|
||||||
self.__unset_state(Debouncer.CHANGED_STATE)
|
|
||||||
current_state = self.pin.value
|
|
||||||
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.UNSTABLE_STATE)
|
|
||||||
else:
|
|
||||||
if time.monotonic() - self.previous_time >= self.interval:
|
|
||||||
if current_state != self.__get_state(Debouncer.DEBOUNCED_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
self.__set_state(Debouncer.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
"""Return the current debounced value of the input."""
|
|
||||||
return self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def rose(self):
|
|
||||||
"""Return whether the debounced input went from low to high at the most recent update."""
|
|
||||||
return self.__get_state(self.DEBOUNCED_STATE) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fell(self):
|
|
||||||
"""Return whether the debounced input went from high to low at the most recent update."""
|
|
||||||
return (not self.__get_state(self.DEBOUNCED_STATE)) and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
@ -20,13 +20,15 @@ import rotaryio
|
||||||
import pulseio
|
import pulseio
|
||||||
import adafruit_ssd1306
|
import adafruit_ssd1306
|
||||||
from adafruit_motor import servo
|
from adafruit_motor import servo
|
||||||
|
from adafruit_debouncer import Debouncer
|
||||||
from debouncer import Debouncer
|
|
||||||
|
|
||||||
#--------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------
|
||||||
# Initialize Rotary encoder
|
# Initialize Rotary encoder
|
||||||
|
|
||||||
button = Debouncer(board.D12, digitalio.Pull.UP, 0.01)
|
button_io = digitalio.DigitalInOut(board.D12)
|
||||||
|
button_io.direction = digitalio.Direction.INPUT
|
||||||
|
button_io.pull = digitalio.Pull.UP
|
||||||
|
button = Debouncer(button_io)
|
||||||
rotary_encoder = rotaryio.IncrementalEncoder(board.D10, board.D11)
|
rotary_encoder = rotaryio.IncrementalEncoder(board.D10, board.D11)
|
||||||
|
|
||||||
#--------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -1,94 +0,0 @@
|
||||||
"""
|
|
||||||
Input pin debouncer.
|
|
||||||
|
|
||||||
Adafruit invests time and resources providing this open source code.
|
|
||||||
Please support Adafruit and open source hardware by purchasing
|
|
||||||
products from Adafruit!
|
|
||||||
|
|
||||||
Written by Dave Astels for Adafruit Industries
|
|
||||||
Copyright (c) 2018 Adafruit Industries
|
|
||||||
Licensed under the MIT license.
|
|
||||||
|
|
||||||
All text above must be included in any redistribution.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
import digitalio
|
|
||||||
|
|
||||||
|
|
||||||
class Debouncer:
|
|
||||||
"""Debounce an input pin"""
|
|
||||||
|
|
||||||
DEBOUNCED_STATE = 0x01
|
|
||||||
UNSTABLE_STATE = 0x02
|
|
||||||
CHANGED_STATE = 0x04
|
|
||||||
|
|
||||||
def __init__(self, pin, mode=None, interval=0.010):
|
|
||||||
"""Make am instance.
|
|
||||||
:param int pin: the pin (from board) to debounce
|
|
||||||
:param int mode: digitalio.Pull.UP or .DOWN (default is no
|
|
||||||
pull up/down)
|
|
||||||
:param int interval: bounce threshold in seconds (default is 0.010,
|
|
||||||
i.e. 10 milliseconds)
|
|
||||||
"""
|
|
||||||
self.state = 0x00
|
|
||||||
self.pin = digitalio.DigitalInOut(pin)
|
|
||||||
self.pin.direction = digitalio.Direction.INPUT
|
|
||||||
if mode is not None:
|
|
||||||
self.pin.pull = mode
|
|
||||||
if self.pin.value:
|
|
||||||
self.__set_state(Debouncer.DEBOUNCED_STATE |
|
|
||||||
Debouncer.UNSTABLE_STATE)
|
|
||||||
self.previous_time = 0
|
|
||||||
if interval is None:
|
|
||||||
self.interval = 0.010
|
|
||||||
else:
|
|
||||||
self.interval = interval
|
|
||||||
|
|
||||||
def __set_state(self, bits):
|
|
||||||
self.state |= bits
|
|
||||||
|
|
||||||
def __unset_state(self, bits):
|
|
||||||
self.state &= ~bits
|
|
||||||
|
|
||||||
def __toggle_state(self, bits):
|
|
||||||
self.state ^= bits
|
|
||||||
|
|
||||||
def __get_state(self, bits):
|
|
||||||
return (self.state & bits) != 0
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update the debouncer state. Must be called before using any of
|
|
||||||
the properties below"""
|
|
||||||
self.__unset_state(Debouncer.CHANGED_STATE)
|
|
||||||
current_state = self.pin.value
|
|
||||||
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.UNSTABLE_STATE)
|
|
||||||
else:
|
|
||||||
if time.monotonic() - self.previous_time >= self.interval:
|
|
||||||
debounced_state = self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
if current_state != debounced_state:
|
|
||||||
self.previous_time = time.monotonic()
|
|
||||||
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
self.__set_state(Debouncer.CHANGED_STATE)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
"""Return the current debounced value of the input."""
|
|
||||||
return self.__get_state(Debouncer.DEBOUNCED_STATE)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def rose(self):
|
|
||||||
"""Return whether the debounced input went from low to high at
|
|
||||||
the most recent update."""
|
|
||||||
return self.__get_state(self.DEBOUNCED_STATE) \
|
|
||||||
and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fell(self):
|
|
||||||
"""Return whether the debounced input went from high to low at
|
|
||||||
the most recent update."""
|
|
||||||
return (not self.__get_state(self.DEBOUNCED_STATE)) \
|
|
||||||
and self.__get_state(self.CHANGED_STATE)
|
|
||||||
|
|
@ -16,7 +16,7 @@ import rotaryio
|
||||||
import board
|
import board
|
||||||
import digitalio
|
import digitalio
|
||||||
from display import Display
|
from display import Display
|
||||||
from debouncer import Debouncer
|
from adafruit_debouncer import Debouncer
|
||||||
from generator import Generator
|
from generator import Generator
|
||||||
import shapes
|
import shapes
|
||||||
|
|
||||||
|
|
@ -38,14 +38,19 @@ def get_encoder_change(encoder, pos):
|
||||||
else:
|
else:
|
||||||
return (new_position, new_position - pos)
|
return (new_position, new_position - pos)
|
||||||
|
|
||||||
|
def make_debouncable(pin):
|
||||||
|
switch_io = digitalio.DigitalInOut(pin)
|
||||||
|
switch_io.direction = digitalio.Direction.INPUT
|
||||||
|
switch_io.pull = digitalio.Pull.UP
|
||||||
|
return switch_io
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
display = Display()
|
display = Display()
|
||||||
generator = Generator()
|
generator = Generator()
|
||||||
button_a = Debouncer(board.D9, digitalio.Pull.UP, 0.01)
|
button_a = Debouncer(make_debouncable(board.D9))
|
||||||
button_b = Debouncer(board.D6, digitalio.Pull.UP, 0.01)
|
button_b = Debouncer(make_debouncable(board.D6))
|
||||||
button_c = Debouncer(board.D5, digitalio.Pull.UP, 0.01)
|
button_c = Debouncer(make_debouncable(board.D5))
|
||||||
encoder_button = Debouncer(board.D12, digitalio.Pull.UP, 0.01)
|
encoder_button = Debouncer(make_debouncable(board.D12))
|
||||||
encoder = rotaryio.IncrementalEncoder(board.D10, board.D11)
|
encoder = rotaryio.IncrementalEncoder(board.D10, board.D11)
|
||||||
|
|
||||||
current_position = None # current encoder position
|
current_position = None # current encoder position
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue