-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for rotary encoder #12
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3f0a4a3
Initial works to add support for rotary encoders.
martinfelis 97592cb
Added rotenc tests in frontend.
martinfelis a65b65a
Added configuration examples for rotation encoder support
martinfelis b3778d8
Applied formatting using black
martinfelis 5c97976
Fixes of additional flake8 errors
martinfelis 651da1c
Fixed more flake8 and black tests
martinfelis 289edf8
Fixed import statement.
martinfelis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RotEncoder: | ||
def __init__(self, rot_id): | ||
self.id = rot_id | ||
self.pins = [] | ||
self.events = [] | ||
self.state = (None, None) | ||
self.state_map = { | ||
((False, False), (False, True)): 0, | ||
((False, False), (True, False)): 1, | ||
((False, True), (True, True)): 0, | ||
((False, True), (False, False)): 1, | ||
((True, False), (False, False)): 0, | ||
((True, False), (True, True)): 1, | ||
((True, True), (True, False)): 0, | ||
((True, True), (False, True)): 1, | ||
} | ||
|
||
def add_pin(self, pin, event): | ||
if len(self.pins) == 2: | ||
raise RuntimeError(f"Too many pins for rotary encoder {self.id}!") | ||
self.pins.append(pin) | ||
self.events.append(event) | ||
|
||
def get_state(self): | ||
import RPi.GPIO as GPIO | ||
|
||
level0 = GPIO.input(self.pins[0]) | ||
level1 = GPIO.input(self.pins[1]) | ||
|
||
return (level0, level1) | ||
|
||
def get_direction(self, current, new): | ||
return self.state_map[(current, new)] | ||
|
||
def get_event(self): | ||
next_state = self.get_state() | ||
|
||
event = None | ||
try: | ||
direction = self.get_direction(self.state, next_state) | ||
event = self.events[direction] | ||
except KeyError: | ||
pass | ||
|
||
self.state = next_state | ||
return event |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import unittest | ||
|
||
from unittest.mock import patch | ||
from mopidy_raspberry_gpio import RotEncoder | ||
|
||
|
||
class RotEncoderTests(unittest.TestCase): | ||
def test_rotenc_init(self): | ||
rot_enc = RotEncoder("vol") | ||
self.assertTrue(rot_enc.id == "vol") | ||
self.assertTrue(((False, False), (False, True)) in rot_enc.state_map) | ||
|
||
def test_get_direction(self): | ||
rot_enc = RotEncoder("vol") | ||
rot_enc.add_pin(123, "vol_up") | ||
rot_enc.add_pin(124, "vol_down") | ||
|
||
dir_down = rot_enc.get_direction((False, False), (False, True)) | ||
dir_up = rot_enc.get_direction((False, False), (True, False)) | ||
|
||
self.assertEqual(dir_up, 1) | ||
self.assertEqual(dir_down, 0) | ||
|
||
def test_add_pin_invalid(self): | ||
rot_enc = RotEncoder("vol") | ||
rot_enc.add_pin(123, "vol_up") | ||
rot_enc.add_pin(124, "vol_down") | ||
|
||
with self.assertRaises(RuntimeError): | ||
rot_enc.add_pin(124, "vol_down") | ||
|
||
@patch("RPi.GPIO.input") | ||
def test_get_event(self, patched_input): | ||
# Always return False for GPIO.input | ||
patched_input.return_value = False | ||
|
||
rot_enc = RotEncoder("vol") | ||
rot_enc.add_pin(123, "vol_down") # dir 0 => vol_down | ||
rot_enc.add_pin(124, "vol_up") # dir 1 => vol_up | ||
|
||
# from False,True to False,False => dir 1 | ||
rot_enc.state = (False, True) | ||
event = rot_enc.get_event() | ||
self.assertEqual(event, "vol_up") | ||
|
||
# from True,False to False,False => dir 0 | ||
rot_enc.state = (True, False) | ||
event = rot_enc.get_event() | ||
self.assertEqual(event, "vol_down") | ||
|
||
# from True,True to False,False => None | ||
rot_enc.state = (True, True) | ||
event = rot_enc.get_event() | ||
self.assertEqual(event, None) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be:
Tests pass locally for me with this change.