mirror of
https://github.com/Febbweiss/mopidy-touchscreen.git
synced 2026-03-04 22:25:39 +00:00
Code cleanup
Create packages
This commit is contained in:
2
mopidy_touchscreen/input/__init__.py
Normal file
2
mopidy_touchscreen/input/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
# flake8: noqa
|
||||
from input_manager import InputManager
|
||||
95
mopidy_touchscreen/input/gpio_inpput_manager.py
Normal file
95
mopidy_touchscreen/input/gpio_inpput_manager.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import logging
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
import pygame
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class GPIOManager():
|
||||
def __init__(self, pins):
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
||||
# Left Button
|
||||
GPIO.setup(pins['left'], GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
GPIO.add_event_detect(pins['left'], GPIO.BOTH, callback=left,
|
||||
bouncetime=30)
|
||||
|
||||
# Right Button
|
||||
GPIO.setup(pins['right'], GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
GPIO.add_event_detect(pins['right'], GPIO.BOTH,
|
||||
callback=right,
|
||||
bouncetime=30)
|
||||
|
||||
# Up Button
|
||||
GPIO.setup(pins['up'], GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
GPIO.add_event_detect(pins['up'], GPIO.BOTH, callback=up,
|
||||
bouncetime=30)
|
||||
|
||||
# Down Button
|
||||
GPIO.setup(pins['down'], GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
GPIO.add_event_detect(pins['down'], GPIO.BOTH, callback=right,
|
||||
bouncetime=30)
|
||||
|
||||
# Enter Button
|
||||
GPIO.setup(pins['enter'], GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
GPIO.add_event_detect(pins['enter'], GPIO.BOTH,
|
||||
callback=right,
|
||||
bouncetime=30)
|
||||
|
||||
|
||||
def right(channel):
|
||||
dict = {}
|
||||
if GPIO.input(channel) == 1:
|
||||
type = pygame.KEYUP
|
||||
else:
|
||||
type = pygame.KEYDOWN
|
||||
dict['key'] = pygame.K_RIGHT
|
||||
event = pygame.event.Event(type, dict)
|
||||
pygame.event.post(event)
|
||||
|
||||
|
||||
def left(channel):
|
||||
dict = {}
|
||||
if GPIO.input(channel) == 1:
|
||||
type = pygame.KEYUP
|
||||
else:
|
||||
type = pygame.KEYDOWN
|
||||
dict['key'] = pygame.K_RIGHT
|
||||
event = pygame.event.Event(type, dict)
|
||||
pygame.event.post(event)
|
||||
|
||||
|
||||
def down(channel):
|
||||
dict = {}
|
||||
if GPIO.input(channel) == 1:
|
||||
type = pygame.KEYUP
|
||||
else:
|
||||
type = pygame.KEYDOWN
|
||||
dict['key'] = pygame.K_DOWN
|
||||
event = pygame.event.Event(type, dict)
|
||||
pygame.event.post(event)
|
||||
|
||||
|
||||
def up(channel):
|
||||
dict = {}
|
||||
if GPIO.input(channel) == 1:
|
||||
type = pygame.KEYUP
|
||||
else:
|
||||
type = pygame.KEYDOWN
|
||||
dict['key'] = pygame.K_UP
|
||||
event = pygame.event.Event(type, dict)
|
||||
pygame.event.post(event)
|
||||
|
||||
|
||||
def enter(channel):
|
||||
dict = {}
|
||||
if GPIO.input(channel) == 1:
|
||||
type = pygame.KEYUP
|
||||
else:
|
||||
type = pygame.KEYDOWN
|
||||
dict['key'] = pygame.K_RETURN
|
||||
event = pygame.event.Event(type, dict)
|
||||
pygame.event.post(event)
|
||||
138
mopidy_touchscreen/input/input_manager.py
Normal file
138
mopidy_touchscreen/input/input_manager.py
Normal file
@@ -0,0 +1,138 @@
|
||||
import logging
|
||||
import time
|
||||
|
||||
import pygame
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class InputManager():
|
||||
click = 1
|
||||
swipe = 2
|
||||
long_click = 3
|
||||
key = 4
|
||||
|
||||
long_click_min_time = 0.3
|
||||
|
||||
up = 0
|
||||
down = 1
|
||||
left = 2
|
||||
right = 3
|
||||
enter = 4
|
||||
|
||||
def __init__(self, size):
|
||||
self.down_pos = (0, 0)
|
||||
self.up_pos = (0, 0)
|
||||
self.screen_size = size
|
||||
self.max_move_margin = self.screen_size[1] / 6
|
||||
self.min_swipe_move = self.screen_size[1] / 3
|
||||
self.down_button = -1
|
||||
self.down_time = -1
|
||||
self.last_key = -1
|
||||
|
||||
def event(self, event):
|
||||
|
||||
if event.type == pygame.MOUSEBUTTONUP:
|
||||
if event.button == 4:
|
||||
touch_event = InputEvent(InputManager.swipe,
|
||||
self.down_pos,
|
||||
self.up_pos, True,
|
||||
InputManager.up)
|
||||
return touch_event
|
||||
elif event.button == 5:
|
||||
touch_event = InputEvent(InputManager.swipe,
|
||||
self.down_pos,
|
||||
self.up_pos, True,
|
||||
InputManager.down)
|
||||
return touch_event
|
||||
elif event.button == 1 and self.down_button == 1:
|
||||
touch_event = self.mouse_up(event)
|
||||
return touch_event
|
||||
elif event.button == 3 and self.down_button == 3:
|
||||
touch_event = InputEvent(InputManager.long_click,
|
||||
self.down_pos, self.up_pos,
|
||||
None, None)
|
||||
return touch_event
|
||||
else:
|
||||
return None
|
||||
elif event.type == pygame.MOUSEBUTTONDOWN:
|
||||
self.mouse_down(event)
|
||||
return None
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
self.key_down(event)
|
||||
return None
|
||||
elif event.type == pygame.KEYUP:
|
||||
return self.key_up(event)
|
||||
|
||||
def key_down(self, event):
|
||||
self.last_key = event.key
|
||||
self.down_time = time.time()
|
||||
|
||||
def key_up(self, event):
|
||||
if self.last_key == event.key:
|
||||
if self.last_key == pygame.K_DOWN:
|
||||
direction = InputManager.down
|
||||
elif self.last_key == pygame.K_UP:
|
||||
direction = InputManager.up
|
||||
elif self.last_key == pygame.K_LEFT:
|
||||
direction = InputManager.left
|
||||
elif self.last_key == pygame.K_RIGHT:
|
||||
direction = InputManager.right
|
||||
elif self.last_key == pygame.K_RETURN:
|
||||
direction = InputManager.enter
|
||||
else:
|
||||
return None
|
||||
if direction is not None:
|
||||
return InputEvent(InputManager.key, None, None, None,
|
||||
direction)
|
||||
|
||||
def mouse_down(self, event):
|
||||
self.down_pos = event.pos
|
||||
self.down_button = event.button
|
||||
self.down_time = time.time()
|
||||
|
||||
def mouse_up(self, event):
|
||||
self.up_pos = event.pos
|
||||
if abs(self.down_pos[0] -
|
||||
self.up_pos[0]) < self.max_move_margin:
|
||||
if abs(self.down_pos[1] -
|
||||
self.up_pos[1]) < self.max_move_margin:
|
||||
if time.time() - InputManager.long_click_min_time > \
|
||||
self.down_time:
|
||||
return InputEvent(InputManager.long_click,
|
||||
self.down_pos,
|
||||
self.up_pos, None, None)
|
||||
else:
|
||||
return InputEvent(InputManager.click,
|
||||
self.down_pos,
|
||||
self.up_pos, None, None)
|
||||
elif abs(self.down_pos[1] - self.up_pos[1])\
|
||||
> self.min_swipe_move:
|
||||
return InputEvent(InputManager.swipe, self.down_pos,
|
||||
self.up_pos, True, None)
|
||||
elif self.down_pos[1] - self.up_pos[1] < self.max_move_margin:
|
||||
if abs(self.down_pos[0] - self.up_pos[0]) > \
|
||||
self.min_swipe_move:
|
||||
return InputEvent(InputManager.swipe, self.down_pos,
|
||||
self.up_pos, False, None)
|
||||
|
||||
|
||||
class InputEvent():
|
||||
def __init__(self, event_type, down_pos, current_pos, vertical,
|
||||
direction):
|
||||
self.type = event_type
|
||||
self.down_pos = down_pos
|
||||
self.current_pos = current_pos
|
||||
if event_type is InputManager.swipe and direction is None:
|
||||
if vertical:
|
||||
if self.down_pos[1] < self.current_pos[1]:
|
||||
self.direction = InputManager.down
|
||||
else:
|
||||
self.direction = InputManager.up
|
||||
else:
|
||||
if self.down_pos[0] < self.current_pos[0]:
|
||||
self.direction = InputManager.right
|
||||
else:
|
||||
self.direction = InputManager.left
|
||||
else:
|
||||
self.direction = direction
|
||||
Reference in New Issue
Block a user