diff --git a/CP101_StateMachines/brute-force/code.py b/CP101_StateMachines/brute-force/code.py index 4d826bb8..2fc05ef4 100644 --- a/CP101_StateMachines/brute-force/code.py +++ b/CP101_StateMachines/brute-force/code.py @@ -17,14 +17,14 @@ All text above must be included in any redistribution. import time import random import board -from digitalio import DigitalInOut, Direction, Pull +import digitalio import busio import adafruit_ds3231 import audioio import pulseio from adafruit_motor import servo import neopixel -from debouncer import Debouncer +from adafruit_debouncer import Debouncer # Set to false to disable testing/tracing code TESTING = True @@ -58,8 +58,8 @@ RESET_STATE = 7 # Power to the speaker and neopixels must be enabled using this pin -enable = DigitalInOut(POWER_PIN) -enable.direction = Direction.OUTPUT +enable = digitalio.DigitalInOut(POWER_PIN) +enable.direction = digitalio.Direction.OUTPUT enable.value = True 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.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. pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50) diff --git a/CP101_StateMachines/brute-force/debouncer.py b/CP101_StateMachines/brute-force/debouncer.py deleted file mode 100644 index 1da07ac0..00000000 --- a/CP101_StateMachines/brute-force/debouncer.py +++ /dev/null @@ -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) diff --git a/CP101_StateMachines/classes/code.py b/CP101_StateMachines/classes/code.py index 42f759f5..824f5822 100644 --- a/CP101_StateMachines/classes/code.py +++ b/CP101_StateMachines/classes/code.py @@ -17,14 +17,14 @@ All text above must be included in any redistribution. import time import random import board -from digitalio import DigitalInOut, Direction, Pull +import digitalio import busio import adafruit_ds3231 import audioio import pulseio from adafruit_motor import servo import neopixel -from debouncer import Debouncer +from adafruit_debouncer import Debouncer # Set to false to disable testing/tracing code TESTING = True @@ -47,8 +47,8 @@ SERVO_PIN = board.A1 # Power to the speaker and neopixels must be enabled using this pin -enable = DigitalInOut(POWER_PIN) -enable.direction = Direction.OUTPUT +enable = digitalio.DigitalInOut(POWER_PIN) +enable.direction = digitalio.Direction.OUTPUT enable.value = True 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.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. pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50) diff --git a/CP101_StateMachines/classes/debouncer.py b/CP101_StateMachines/classes/debouncer.py deleted file mode 100644 index 1da07ac0..00000000 --- a/CP101_StateMachines/classes/debouncer.py +++ /dev/null @@ -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) diff --git a/CircuitPython_101/basic_data_structures/song_book/code.py b/CircuitPython_101/basic_data_structures/song_book/code.py index 7903025f..f55ee5bc 100644 --- a/CircuitPython_101/basic_data_structures/song_book/code.py +++ b/CircuitPython_101/basic_data_structures/song_book/code.py @@ -1,6 +1,6 @@ import time import board -import debouncer +from adafruit_debouncer import Debouncer import busio as io import digitalio import pulseio @@ -9,8 +9,14 @@ import adafruit_ssd1306 i2c = io.I2C(board.SCL, board.SDA) reset_pin = digitalio.DigitalInOut(board.D11) oled = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, reset=reset_pin) -button_select = debouncer.Debouncer(board.D7, mode=digitalio.Pull.UP) -button_play = debouncer.Debouncer(board.D9, mode=digitalio.Pull.UP) +select = digitalio.DigitalInOut(board.D7) +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 C_SH_4 = 277 diff --git a/CircuitPython_101/basic_data_structures/song_book/debouncer.py b/CircuitPython_101/basic_data_structures/song_book/debouncer.py deleted file mode 100644 index d7fd84e6..00000000 --- a/CircuitPython_101/basic_data_structures/song_book/debouncer.py +++ /dev/null @@ -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) diff --git a/EPROM_Emulator/debouncer.py b/EPROM_Emulator/debouncer.py deleted file mode 100644 index b3f762d7..00000000 --- a/EPROM_Emulator/debouncer.py +++ /dev/null @@ -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) diff --git a/EPROM_Emulator/main.py b/EPROM_Emulator/main.py index 16d4623d..79c12ac1 100644 --- a/EPROM_Emulator/main.py +++ b/EPROM_Emulator/main.py @@ -35,7 +35,7 @@ import board import busio import digitalio import storage -from debouncer import Debouncer +from adafruit_debouncer import Debouncer from directory_node import DirectoryNode from emulator import Emulator @@ -44,7 +44,10 @@ from emulator import Emulator # Initialize Rotary encoder # 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 rot_a = digitalio.DigitalInOut(board.D4) diff --git a/HalloWing_Tour_Guide/code.py b/HalloWing_Tour_Guide/code.py index d21f5797..4b5c51b2 100644 --- a/HalloWing_Tour_Guide/code.py +++ b/HalloWing_Tour_Guide/code.py @@ -18,7 +18,7 @@ import displayio import audioio from busio import UART import adafruit_gps -from debouncer import Debouncer +from adafruit_debouncer import Debouncer uart = UART(board.TX, board.RX, baudrate=9600, timeout=3000) 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. 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) diff --git a/HalloWing_Tour_Guide/debouncer.py b/HalloWing_Tour_Guide/debouncer.py deleted file mode 100644 index 0cb3b372..00000000 --- a/HalloWing_Tour_Guide/debouncer.py +++ /dev/null @@ -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) diff --git a/NY_Ball_Drop/code.py b/NY_Ball_Drop/code.py index fc9eae60..6c73e0eb 100644 --- a/NY_Ball_Drop/code.py +++ b/NY_Ball_Drop/code.py @@ -24,7 +24,7 @@ import audioio import pulseio from adafruit_motor import servo import neopixel -from debouncer import Debouncer +from adafruit_debouncer import Debouncer # Set to false to disable testing/tracing code 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.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. pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50) diff --git a/NY_Ball_Drop/debouncer.py b/NY_Ball_Drop/debouncer.py deleted file mode 100644 index 1da07ac0..00000000 --- a/NY_Ball_Drop/debouncer.py +++ /dev/null @@ -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) diff --git a/Pomodoro_Timer/code.py b/Pomodoro_Timer/code.py index e10fbcbf..8e7e0577 100644 --- a/Pomodoro_Timer/code.py +++ b/Pomodoro_Timer/code.py @@ -28,14 +28,17 @@ from math import ceil import board import rotaryio import neopixel -import debouncer +from adafruit_debouncer import Debouncer import digitalio import pulseio # Setup the hardware 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) last_position = 0 diff --git a/Pomodoro_Timer/debouncer.py b/Pomodoro_Timer/debouncer.py deleted file mode 100644 index 6f8fcc6d..00000000 --- a/Pomodoro_Timer/debouncer.py +++ /dev/null @@ -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) diff --git a/Servo_Tester/debouncer.py b/Servo_Tester/debouncer.py deleted file mode 100644 index 1da07ac0..00000000 --- a/Servo_Tester/debouncer.py +++ /dev/null @@ -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) diff --git a/Servo_Tester/main.py b/Servo_Tester/main.py index c8b35f81..b9ea3087 100644 --- a/Servo_Tester/main.py +++ b/Servo_Tester/main.py @@ -20,13 +20,15 @@ import rotaryio import pulseio import adafruit_ssd1306 from adafruit_motor import servo - -from debouncer import Debouncer +from adafruit_debouncer import Debouncer #-------------------------------------------------------------------------------- # 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) #-------------------------------------------------------------------------------- diff --git a/Wave_Freq_Generator/debouncer.py b/Wave_Freq_Generator/debouncer.py deleted file mode 100644 index 0cb3b372..00000000 --- a/Wave_Freq_Generator/debouncer.py +++ /dev/null @@ -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) diff --git a/Wave_Freq_Generator/main.py b/Wave_Freq_Generator/main.py index addb1752..0cdf6125 100644 --- a/Wave_Freq_Generator/main.py +++ b/Wave_Freq_Generator/main.py @@ -16,7 +16,7 @@ import rotaryio import board import digitalio from display import Display -from debouncer import Debouncer +from adafruit_debouncer import Debouncer from generator import Generator import shapes @@ -38,14 +38,19 @@ def get_encoder_change(encoder, pos): else: 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(): display = Display() generator = Generator() - button_a = Debouncer(board.D9, digitalio.Pull.UP, 0.01) - button_b = Debouncer(board.D6, digitalio.Pull.UP, 0.01) - button_c = Debouncer(board.D5, digitalio.Pull.UP, 0.01) - encoder_button = Debouncer(board.D12, digitalio.Pull.UP, 0.01) + button_a = Debouncer(make_debouncable(board.D9)) + button_b = Debouncer(make_debouncable(board.D6)) + button_c = Debouncer(make_debouncable(board.D5)) + encoder_button = Debouncer(make_debouncable(board.D12)) encoder = rotaryio.IncrementalEncoder(board.D10, board.D11) current_position = None # current encoder position