Skip to content

Instantly share code, notes, and snippets.

@ryos36 ryos36/fix_bounce.py
Created Mar 11, 2019

Embed
What would you like to do?
Polyphony によるチャタリング除去プログラム
from polyphony import testbench, module, is_worker_running
from polyphony.timing import clksleep, wait_rising, wait_value
from polyphony.io import Port, Queue
from polyphony.typing import bit, bit32
@module
class fix_bounce_contact:
def __init__(self, wait_n, long_press_count_n):
self.wait_n = wait_n
self.long_press_count_n = long_press_count_n
self.button_in = Port(bit, 'in')
self.button_out = Port(bit, 'out', 0)
self.long_press_button_out = Port(bit, 'out', 0)
self.append_worker(self.main)
#----------------------------------------------------------------
#----------------------------------------------------------------
def loop_wait(self):
for i in range(self.wait_n):
pass
#----------------------------------------------------------------
def main(self):
while is_worker_running():
count:bit32 = 0
v0:bit = 0
v1:bit = 0
while True:
max_count:bit32 = self.long_press_count_n
count = 0
while True:
self.loop_wait()
v0 = v1
v1 = self.button_in.rd()
count += 1
if v0 != v1:
break
if (v1 == 1) and ( count > max_count ):
self.long_press_button_out.wr(1)
self.long_press_button_out.wr(0)
self.button_out.wr(v1)
#----------------------------------------------------------------
@testbench
def test(m):
for i in range(10):
m.button_in.wr(1)
wait_value(1, m.button_out)
print("0button", m.button_out.rd(), m.long_press_button_out.rd())
m.button_in.wr(0)
wait_value(0, m.button_out)
print("1button", m.button_out.rd(), m.long_press_button_out.rd())
for i in range(10):
m.button_in.wr(1)
wait_value(1, m.button_out)
print("2button", m.button_out.rd(), m.long_press_button_out.rd())
wait_value(1, m.long_press_button_out)
print("3button", m.button_out.rd(), m.long_press_button_out.rd())
m.button_in.wr(0)
wait_value(0, m.button_out)
print("4button", m.button_out.rd(), m.long_press_button_out.rd())
m = fix_bounce_contact(100, 10)
test(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.