aboutsummaryrefslogtreecommitdiffstats
path: root/remote/libsimplepwm/gui/mainwindow.py
blob: 2428d66a03d152ea897a3db82381af5561c8412d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#
# Simple PWM controller
# Remote control tool
#
# Copyright (c) 2018-2021 Michael Buesch <m@bues.ch>
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

__all__ = [
    "MainWindow",
]

from libsimplepwm.connect import *
from libsimplepwm.gui.blocker import *
from libsimplepwm.gui.coloradjwidget import *
from libsimplepwm.gui.gtk_import import *
from libsimplepwm.util import *
import sys

class MainWindow(Gtk.Window):
    def __init__(self, connectTo=None):
        super().__init__(title="Simple PWM controller - Remote")

        self.__changeBlocked = Blocker()
        self.__comm = None

        self.set_border_width(10)

        grid = Grid()
        grid.set_column_homogeneous(True)
        self.add(grid)

        ttys = Gtk.ListStore(str)
        if connectTo:
            ttys.append([connectTo])
        for i in range(4):
            tty = f"/dev/ttyUSB{i}"
            if tty != connectTo:
                ttys.append([tty])
        for i in range(2):
            tty = f"/dev/ttyS{i}"
            if tty != connectTo:
                ttys.append([tty])
        self.__ttyCombo = Gtk.ComboBox.new_with_model_and_entry(ttys)
        self.__ttyCombo.set_entry_text_column(0)
        self.__ttyCombo.set_active(0)
        grid.attach(self.__ttyCombo, 0, 0, 1, 1)

        self.__connectButton = Gtk.Button.new_with_mnemonic("_Connect")
        grid.attach(self.__connectButton, 1, 0, 1, 1)

        self.__adjWidget = ColorAdjWidget()
        self.__adjWidget.set_sensitive(False)
        grid.attach(self.__adjWidget, 0, 1, 2, 1)

        self.__connectButton.connect("clicked", self.__on_connect)
        self.__adjWidget.connect("colorChanged", self.__on_colorChanged)
        self.connect("destroy", Gtk.main_quit)

        if connectTo:
            self.__on_connect(None)

    def __getTTY(self):
        return self.__ttyCombo.get_model().get_value(self.__ttyCombo.get_active_iter(), 0)

    def __connect(self):
        if self.__comm:
            self.__comm.disconnect()
            self.__comm = None
        try:
            self.__comm = SimplePWM(port=self.__getTTY(),
                                    dumpDataStream=False,
                                    timeout=0.1)
            r, g, b = self.__comm.getRGB()
        except SimplePWMError as e:
            self.__comm = None
            raise e
        return r, g, b

    def __on_connect(self, _):
        try:
            r, g, b = self.__connect()
            self.__adjWidget.set_sensitive(True)
        except SimplePWMError as e:
            printError(e)
            msg = Gtk.MessageDialog(parent=self,
                                    flags=Gtk.DialogFlags.MODAL,
                                    type=Gtk.MessageType.ERROR,
                                    buttons=Gtk.ButtonsType.OK,
                                    text=f"Failed to connect to {self.__getTTY()}:\n{e}")
            msg.connect("response", lambda w, r: msg.destroy())
            msg.show()
            return
        with self.__changeBlocked:
            self.__adjWidget.setRGB(round(r / 0xFFFF * 100),
                                    round(g / 0xFFFF * 100),
                                    round(b / 0xFFFF * 100))
        printInfo(f"Connected to {self.__getTTY()}.")

    def __on_colorChanged(self, _, mode, r, g, b, h, l, s):
        if not self.__comm:
            return
        if self.__changeBlocked:
            return
        setValuesTries = 3
        while setValuesTries > 0:
            try:
                if mode == "RGB":
                    self.__comm.setRGB((round(r / 100 * 0xFFFF),
                                        round(g / 100 * 0xFFFF),
                                        round(b / 100 * 0xFFFF)))
                elif mode == "HLS":
                    self.__comm.setHSL((round(h / 360 * 0xFFFF),
                                        round(s / 100 * 0xFFFF),
                                        round(l / 100 * 0xFFFF)))
                else:
                    assert False
                setValuesTries = 0 # done
            except SimplePWMError as e:
                printError(e)
                for i in range(3):
                    try:
                        self.__connect()
                        printInfo("Reconnected.")
                        setValuesTries -= 1
                        break
                    except SimplePWMError:
                        continue
                else:
                    setValuesTries = 0 # give up
                    self.__adjWidget.set_sensitive(False)

# vim: ts=4 sw=4 expandtab
bues.ch cgit interface