Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. from polyphony import testbench, module, is_worker_running
  2. from polyphony.timing import clksleep, wait_rising, wait_value
  3. from polyphony.io import Port, Queue
  4. from polyphony.typing import bit, bit32
  5.  
  6. @module
  7. class fix_bounce_contact:
  8. def __init__(self, wait_n, long_press_count_n):
  9. self.wait_n = wait_n
  10. self.long_press_count_n = long_press_count_n
  11.  
  12. self.button_in = Port(bit, 'in')
  13.  
  14. self.button_out = Port(bit, 'out', 0)
  15. self.long_press_button_out = Port(bit, 'out', 0)
  16. self.append_worker(self.main)
  17.  
  18. #----------------------------------------------------------------
  19. #----------------------------------------------------------------
  20.  
  21. def loop_wait(self):
  22. for i in range(self.wait_n):
  23. pass
  24.  
  25. #----------------------------------------------------------------
  26. def main(self):
  27. while is_worker_running():
  28. count:bit32 = 0
  29. v0:bit = 0
  30. v1:bit = 0
  31. while True:
  32. max_count:bit32 = self.long_press_count_n
  33. count = 0
  34. while True:
  35. self.loop_wait()
  36. v0 = v1
  37. v1 = self.button_in.rd()
  38. count += 1
  39. if v0 != v1:
  40. break
  41.  
  42. if (v1 == 1) and ( count > max_count ):
  43. self.long_press_button_out.wr(1)
  44.  
  45. self.long_press_button_out.wr(0)
  46. self.button_out.wr(v1)
  47.  
  48. #----------------------------------------------------------------
  49. @testbench
  50. def test(m):
  51. for i in range(10):
  52. m.button_in.wr(1)
  53. wait_value(1, m.button_out)
  54. print("0button", m.button_out.rd(), m.long_press_button_out.rd())
  55.  
  56. m.button_in.wr(0)
  57. wait_value(0, m.button_out)
  58. print("1button", m.button_out.rd(), m.long_press_button_out.rd())
  59.  
  60. for i in range(10):
  61. m.button_in.wr(1)
  62. wait_value(1, m.button_out)
  63. print("2button", m.button_out.rd(), m.long_press_button_out.rd())
  64.  
  65. wait_value(1, m.long_press_button_out)
  66. print("3button", m.button_out.rd(), m.long_press_button_out.rd())
  67.  
  68. m.button_in.wr(0)
  69. wait_value(0, m.button_out)
  70. print("4button", m.button_out.rd(), m.long_press_button_out.rd())
  71.  
  72. m = fix_bounce_contact(100, 10)
  73. test(m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement