forked from ImSwitch/ImSwitch
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Joystick for ESP32 STage Manager, Updating ESPSerialCamera
- Loading branch information
1 parent
c30a5c7
commit cac306a
Showing
9 changed files
with
344 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import sys | ||
import math | ||
|
||
from PyQt5.QtWidgets import QApplication, QWidget | ||
from PyQt5.QtWidgets import QLabel | ||
from PyQt5.QtGui import QPainter, QBrush, QPen | ||
from PyQt5.QtCore import Qt | ||
from qtpy import QtCore | ||
|
||
class Joystick(QWidget): | ||
floatValueChanged = QtCore.Signal(float) | ||
''' based on https://github.com/bsiyoung/PyQt5-Joystick/ ''' | ||
|
||
def __init__(self, window_min_size = [200, 200], callbackFct=None): | ||
super().__init__() | ||
|
||
self.window_title = 'Joystick' | ||
self.window_min_size = window_min_size | ||
self.wnd_fit_size = 400 | ||
self.window_size = [self.wnd_fit_size, self.wnd_fit_size] | ||
|
||
self.circle_margin_ratio = 0.1 | ||
self.circle_diameter = int(self.window_size[0] * (1 - self.circle_margin_ratio * 2)) | ||
|
||
self.stick_diameter_ratio = 0.1 | ||
self.stick_diameter = int(self.circle_diameter * self.stick_diameter_ratio) | ||
self.is_mouse_down = False | ||
self.stick_pos = [0, 0] | ||
self.strength = 0 | ||
|
||
self.stat_label_margin = 10 | ||
self.stat_label = QLabel(self) | ||
|
||
self.callbackFct = callbackFct | ||
|
||
self.init_ui() | ||
|
||
def init_ui(self): | ||
self.setWindowTitle(self.window_title) | ||
|
||
self.setMinimumSize(self.window_min_size[0], self.window_min_size[1]) | ||
self.resize(self.window_size[0], self.window_size[1]) | ||
|
||
self.stat_label.setAlignment(Qt.AlignLeft) | ||
self.stat_label.setGeometry(self.stat_label_margin, self.stat_label_margin, | ||
self.window_min_size[0] - self.stat_label_margin * 2, | ||
self.window_min_size[0] - self.stat_label_margin * 2) | ||
font = self.stat_label.font() | ||
font.setPointSize(10) | ||
|
||
self.setMouseTracking(True) | ||
|
||
self.show() | ||
|
||
def resizeEvent(self, event): | ||
self.wnd_fit_size = min(self.width(), self.height()) | ||
|
||
self.circle_diameter = int(self.wnd_fit_size * (1 - self.circle_margin_ratio * 2)) | ||
self.stick_diameter = int(self.circle_diameter * self.stick_diameter_ratio) | ||
|
||
def _draw_outer_circle(self, painter): | ||
painter.setPen(QPen(Qt.black, 2, Qt.SolidLine)) | ||
|
||
circle_margin = int(self.wnd_fit_size * self.circle_margin_ratio) | ||
painter.drawEllipse(circle_margin, circle_margin, | ||
self.circle_diameter, self.circle_diameter) | ||
|
||
def _draw_sub_lines(self, painter): | ||
painter.setRenderHint(QPainter.Antialiasing) | ||
painter.setPen(QPen(Qt.lightGray, 1, Qt.DashLine)) | ||
|
||
num_sub_line = 6 | ||
for i in range(num_sub_line): | ||
theta = math.pi / 2 - math.pi * i / num_sub_line | ||
x0 = int(self.wnd_fit_size / 2 - self.circle_diameter / 2 * math.cos(theta)) | ||
y0 = int(self.wnd_fit_size / 2 - self.circle_diameter / 2 * math.sin(theta)) | ||
x1 = int(self.wnd_fit_size / 2 - self.circle_diameter / 2 * math.cos(theta + math.pi)) | ||
y1 = int(self.wnd_fit_size / 2 - self.circle_diameter / 2 * math.sin(theta + math.pi)) | ||
painter.drawLine(x0, y0, x1, y1) | ||
|
||
def _draw_sub_circles(self, painter): | ||
painter.setPen(QPen(Qt.lightGray, 1, Qt.DashLine)) | ||
|
||
num_sub_circle = 4 | ||
for i in range(num_sub_circle): | ||
sub_radius = int(self.circle_diameter / 2 * (i + 1) / (num_sub_circle + 1)) | ||
sub_margin = int(self.wnd_fit_size / 2 - sub_radius) | ||
painter.drawEllipse(sub_margin, sub_margin, sub_radius * 2, sub_radius * 2) | ||
|
||
# Draw Inner(Joystick) Circle | ||
painter.setBrush(QBrush(Qt.black, Qt.SolidPattern)) | ||
stick_margin = [int(self.wnd_fit_size / 2 + self.stick_pos[0] - self.stick_diameter / 2), | ||
int(self.wnd_fit_size / 2 - self.stick_pos[1] - self.stick_diameter / 2)] | ||
painter.drawEllipse(stick_margin[0], stick_margin[1], self.stick_diameter, self.stick_diameter) | ||
|
||
def paintEvent(self, event): | ||
painter = QPainter(self) | ||
|
||
# Draw Outer(Main) Circle | ||
self._draw_outer_circle(painter) | ||
|
||
# Draw Sub Lines | ||
self._draw_sub_lines(painter) | ||
|
||
# Draw Sub Circles | ||
self._draw_sub_circles(painter) | ||
|
||
# Change Status Label Text (Angle In Degree) | ||
strength = self.get_strength() | ||
angle = self.get_angle(in_deg=True) | ||
if angle < 0: | ||
angle += 360 | ||
#self.stat_label.setText('Strength : {:.2f} \nDirection : {:.2f}°'.format(strength, angle)) | ||
|
||
def mouseMoveEvent(self, event): | ||
# Move Stick Only When Mouse Left Button Pressed | ||
if self.is_mouse_down is False: | ||
return | ||
|
||
# Window Coordinate To Cartesian Coordinate | ||
pos = event.pos() | ||
stick_pos_buf = [pos.x() - self.wnd_fit_size / 2, self.wnd_fit_size / 2 - pos.y()] | ||
|
||
# If Cursor Is Not In Available Range, Correct It | ||
if self._get_strength(stick_pos_buf) > 1.0: | ||
theta = math.atan2(stick_pos_buf[1], stick_pos_buf[0]) | ||
radius = (self.circle_diameter - self.stick_diameter) / 2 | ||
stick_pos_buf[0] = radius * math.cos(theta) | ||
stick_pos_buf[1] = radius * math.sin(theta) | ||
|
||
# Emit signal #TODO: Not sure if this is the right way to do it | ||
if self.callbackFct is not None: | ||
self.callbackFct(stick_pos_buf[0], stick_pos_buf[1]) | ||
|
||
self.stick_pos = stick_pos_buf | ||
self.repaint() | ||
|
||
def mousePressEvent(self, event): | ||
if event.button() == Qt.LeftButton: | ||
self.is_mouse_down = True | ||
|
||
def mouseReleaseEvent(self, event): | ||
if event.button() == Qt.LeftButton: | ||
self.is_mouse_down = False | ||
self.stick_pos = [0, 0] | ||
self.repaint() | ||
|
||
# Get Strength With Argument | ||
def _get_strength(self, stick_pos): | ||
max_distance = (self.circle_diameter - self.stick_diameter) / 2 | ||
distance = math.sqrt(stick_pos[0] * stick_pos[0] + stick_pos[1] * stick_pos[1]) | ||
|
||
return distance / max_distance | ||
|
||
# Get Strength With Current Stick Position | ||
def get_strength(self): | ||
max_distance = (self.circle_diameter - self.stick_diameter) / 2 | ||
distance = math.sqrt(self.stick_pos[0] * self.stick_pos[0] + self.stick_pos[1] * self.stick_pos[1]) | ||
|
||
return distance / max_distance | ||
|
||
def get_angle(self, in_deg=False): | ||
angle = math.atan2(self.stick_pos[1], self.stick_pos[0]) | ||
if in_deg is True: | ||
angle = angle * 180 / math.pi | ||
|
||
return angle |
60 changes: 60 additions & 0 deletions
60
imswitch/imcontrol/controller/controllers/JoystickController.py
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,60 @@ | ||
|
||
from imswitch.imcommon.framework import Signal, Thread, Worker, Mutex | ||
from imswitch.imcontrol.view import guitools | ||
from imswitch.imcommon.model import initLogger | ||
from ..basecontrollers import LiveUpdatedController | ||
|
||
|
||
class JoystickController(LiveUpdatedController): | ||
""" Linked to JoystickWidget.""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
# scaler | ||
self.scaler = 1 | ||
|
||
# initialize the positioner | ||
self.positioner_name = self._master.positionersManager.getAllDeviceNames()[0] | ||
self.positioner = self._master.positionersManager[self.positioner_name] | ||
|
||
self._widget.sigJoystickXY.connect(self.moveXY) | ||
self._widget.sigJoystickZA.connect(self.moveZA) | ||
|
||
def moveXY(self, x, y): | ||
if abs(x)>0 or abs(y) >0: | ||
self.positioner.moveForever(speed=(0, x, y, 0), is_stop=False) | ||
else: | ||
self.stop_x() | ||
self.stop_y() | ||
return x, y | ||
|
||
def moveZA(self, a, z): | ||
if abs(a)>0 or abs(z) >0: | ||
self.positioner.moveForever(speed=(a, 0, 0, z), is_stop=False) | ||
else: | ||
self.stop_a() | ||
self.stop_z() | ||
return a, z | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
# Copyright (C) 2020-2021 ImSwitch developers | ||
# This file is part of ImSwitch. | ||
# | ||
# ImSwitch is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# ImSwitch is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
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
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
Oops, something went wrong.