Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. diff --git a/things/input/mechanical/rotary_encoder.py b/things/input/mechanical/rotary_encoder.py
  2. index e1f4244..5c6ecda 100644
  3. --- a/things/input/mechanical/rotary_encoder.py
  4. +++ b/things/input/mechanical/rotary_encoder.py
  5. @@ -47,7 +47,7 @@
  6. #
  7. ###
  8.  
  9. -from IoTPy.pyuper.interrupt import Interrupt
  10. +from weioLib.weio import *
  11.  
  12. class RotaryEncoder:
  13.  
  14. @@ -74,6 +74,8 @@ class RotaryEncoder:
  15. #obj = {'previous_states': list(self._null_state), 'position': 0}
  16.  
  17. self._previous_states = list(self._null_state)
  18. + self.pin1 = 0
  19. + self.pin2 = 0
  20. self.position = 0
  21.  
  22. self._chan0 = chan0
  23. @@ -81,8 +83,8 @@ class RotaryEncoder:
  24.  
  25. self._user_callback = callback
  26.  
  27. - self._id0 = chan0.attach(Interrupt.EDGE_CHANGE, self.call_back, None, 3)
  28. - self._id1 = chan1.attach(Interrupt.EDGE_CHANGE, self.call_back, None, 3)
  29. + attachInterrupt(self._chan0, CHANGE, self.cb1, self._chan0)
  30. + attachInterrupt(self._chan1, CHANGE, self.cb2, self._chan1)
  31.  
  32. def __enter__(self):
  33. return self
  34. @@ -90,14 +92,18 @@ class RotaryEncoder:
  35. def __exit__(self, exc_type, exc_value, traceback):
  36. pass
  37.  
  38. + def cb1(self, event, obj):
  39. + self.pin1 = (event['values'] >> event['id']) & 0x01
  40. + self.call_back(event, obj)
  41. +
  42. + def cb2(self, event, obj):
  43. + self.pin2 = (event['values'] >> event['id']) & 0x01
  44. + self.call_back(event, obj)
  45. +
  46. def call_back(self, event, obj):
  47. - pins = event['values']
  48. - pin1 = (pins >> self._id0) & 0x01
  49. - pin2 = (pins >> self._id1) & 0x01
  50. -
  51. - if [pin1, pin2] != self._previous_states[2]:
  52. + if [self.pin1, self.pin2] != self._previous_states[2]:
  53. self._previous_states[0:2] = self._previous_states[1:3]
  54. - self._previous_states[2] = [pin1, pin2]
  55. + self._previous_states[2] = [self.pin1, self.pin2]
  56.  
  57. if self._previous_states in self._forward_states:
  58. self.position += 1
  59. @@ -108,4 +114,4 @@ class RotaryEncoder:
  60. self.position -= 1
  61. self._previous_states = list(self._null_state)
  62. if self._user_callback:
  63. - self._user_callback(RotaryEncoder.BACKWARD, self.position)
  64. \ No newline at end of file
  65. + self._user_callback(RotaryEncoder.BACKWARD, self.position)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement