def test_setting_interval(): # Check that setting the interval does change the time the debouncer waits db = adafruit_debouncer.Debouncer(_false, interval=0.01) db.update() # set the interval to a longer time, sleep for a time between # the two interval settings, and assert that the value hasn't changed. db.function = _true db.interval = 0.2 db.update() assert db.interval - 0.2 < 0.00001, "interval is not consistent" time.sleep(0.11) db.update() assertEqual(db.value, False) assertEqual(db.rose, False) assertEqual(db.fell, False) # and then once the whole time has passed make sure it did change time.sleep(0.11) db.update() assertEqual(db.value, True) assertEqual(db.rose, True) assertEqual(db.fell, False)
def test_interval_is_the_same(): db = adafruit_debouncer.Debouncer(_false, interval=0.25) assertEqual(db.value, False) db.update() db.function = _true db.update() time.sleep(0.1) # longer than default interval db.update() assertEqual(db.value, False) time.sleep(0.2) # 0.1 + 0.2 > 0.25 db.update() assertEqual(db.value, True) assertEqual(db.rose, True) assertEqual(db.interval, 0.25)
def test_back_and_forth(): # Start false db = adafruit_debouncer.Debouncer(_false) assertEqual(db.value, False) # Set the raw state to true, update, and make sure the debounced # state has not changed yet: db.function = _true db.update() assertEqual(db.value, False) assert not db.last_duration, "There was no previous interval??" # Sleep longer than the debounce interval, so state can change: time.sleep(0.02) db.update() assert db.last_duration # is actually duration between powerup and now assertEqual(db.value, True) assertEqual(db.rose, True) assertEqual(db.fell, False) # Duration since last change has only been long enough to run these # asserts, which should be well under 1/10 second assert db.current_duration < 0.1, "Unit error? %d" % db.current_duration # Set raw state back to false, make sure it's not instantly reflected, # then wait and make sure it IS reflected after the interval has passed. db.function = _false db.update() assertEqual(db.value, True) assertEqual(db.fell, False) assertEqual(db.rose, False) time.sleep(0.02) assert 0.019 < db.current_duration <= 1, ( "Unit error? sleep .02 -> duration %d" % db.current_duration ) db.update() assertEqual(db.value, False) assertEqual(db.rose, False) assertEqual(db.fell, True) assert 0 < db.current_duration <= 0.1, ( "Unit error? time to run asserts %d" % db.current_duration ) assert 0 < db.last_duration < 0.1, ( "Unit error? Last dur should be ~.02, is %d" % db.last_duration )
######### def makeButton(_pin, _col, _name): return [digitalio.DigitalInOut(_pin), _name, _col] btns = [ makeButton(board.D3, makeColor(255, 0, 0), "lx"), makeButton(board.D1, makeColor(0, 255, 0), "rx") ] for i in range(len(btns)): btns[i][0].direction = digitalio.Direction.INPUT btns[i][0].pull = digitalio.Pull.UP btns[i].append(debouncer.Debouncer(btns[i][0])) ######### # config ######### pages = [] curPage = -1 def makePageFromCsv(_csv): global pages s = _csv.split(",") nTokens = len(s) if nTokens > 3: page = []