Skip to content

Instantly share code, notes, and snippets.

@mattvenn
Created May 22, 2017 14:30
Show Gist options
  • Save mattvenn/1ca4dfad085c3331542450fbdee46525 to your computer and use it in GitHub Desktop.
Save mattvenn/1ca4dfad085c3331542450fbdee46525 to your computer and use it in GitHub Desktop.
samp = [1 ,1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
state = 1
db0 = 0
db1 = 0
print("samp = button state 0 is pressed")
print("delta = samp ^ state difference between sample and state")
print("db0 = ~(db0) & delta first bit of 2 bit counter")
print("db1 = (db1 ^ db0) & delta second bit of 2 bit counter")
print("changes = ~(~delta | db0 | db1) if a change has happened")
print("state = state ^ changes state of the debouncer")
print("-----------")
print("{:5} {:5} {:5} {:5} {:5} {:5}".format("samp", "delta", "db0", "db1", "chngs", "state"))
for s in samp:
delta = s ^ state
db1 = (db1 ^ db0 ) & delta
db0 = ~(db0) & delta
changes = ~(~delta | db0 | db1)
state ^= changes
print("{:5} {:5} {:5} {:5} {:5} {:5}".format(s, delta, db0, db1, changes, state))
@mattvenn
Copy link
Author

samp     = button state               0 is pressed
delta    = samp ^ state               difference between sample and state
db0      = ~(db0) & delta             first bit of 2 bit counter
db1      = (db1 ^ db0) & delta        second bit of 2 bit counter
changes  = ~(~delta | db0 | db1)      if a change has happened
state    = state ^ changes            state of the debouncer
-----------
samp  delta db0   db1   chngs state
    1     0     0     0     0     1
    1     0     0     0     0     1
    1     0     0     0     0     1
    1     0     0     0     0     1
    1     0     0     0     0     1
    0     1     1     0     0     1
    0     1     0     1     0     1
    0     1     1     1     0     1
    0     1     0     0     1     0
    0     0     0     0     0     0
    0     0     0     0     0     0
    1     1     1     0     0     0
    1     1     0     1     0     0
    1     1     1     1     0     0
    0     0     0     0     0     0
    0     0     0     0     0     0
    1     1     1     0     0     0
    1     1     0     1     0     0
    0     0     0     0     0     0
    0     0     0     0     0     0
    0     0     0     0     0     0
    1     1     1     0     0     0
    1     1     0     1     0     0
    1     1     1     1     0     0
    1     1     0     0     1     1
    1     0     0     0     0     1
    1     0     0     0     0     1
    1     0     0     0     0     1
    1     0     0     0     0     1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment