aboutsummaryrefslogtreecommitdiff
path: root/ParrotZikTray
blob: 6de9e1c44af5ece99cf84522251eb753e23111cb (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python
import gtk

import BluetoothPairedDevices
from ParrotZik import BatteryStates
from ParrotZik import connect
from SysIndicator import MenuItem
from SysIndicator import Menu
from SysIndicator import SysIndicator

REFRESH_FREQUENCY = 30000
RECONNECT_FREQUENCY = 5000


class repeat(object):
    def __init__(self, f):
        self.f = f
        self.id = None

    def __call__(self, cls):
        self.f(cls)

    def start(self, cls, frequency):
        if not self.id:
            def run():
                self.f(cls)
                return True

            self.id = gtk.timeout_add(frequency, run)

    def stop(self):
        if self.id:
            gtk.timeout_remove(self.id)
            self.id = None


class ParrotZikIndicator(SysIndicator):
    def __init__(self):
 
        self.menu = Menu()

        self.info_item = MenuItem("Parrot Zik Not connected..",
                                  None, sensitive=False)
        self.menu.append(self.info_item)

        self.version_1_interface = ParrotZikVersion1Interface(self)
        self.version_2_interface = ParrotZikVersion2Interface(self)
        self.quit = MenuItem("Quit", gtk.main_quit, checkitem=True)
        self.menu.append(self.quit)

        SysIndicator.__init__(self, icon="zik-audio-headset", menu=self.menu)

        self.active_interface = None

    @repeat
    def reconnect(self):
        if self.active_interface:
            if not self.active_interface.connected:
                print "Lost connection"
                self.active_interface.deactivate()
            else:
                self.reconnect.stop()
        else:
            mac = BluetoothPairedDevices.ParrotZikMac()
            if mac:
                parrot = connect(mac)
                if parrot.api.sock:
                    if parrot.version.startswith('1'):
                        self.version_1_interface.activate(parrot)
                    else:
                        self.version_2_interface.activate(parrot)
                    self.autorefresh(self)
                    self.autorefresh.start(self, REFRESH_FREQUENCY)
                    self.reconnect.stop()
                    print "Connected"
                else:
                    print "Failed to connect to Parrot Zik %s" % mac

    @repeat
    def autorefresh(self):
        if self.active_interface:
            self.active_interface.refresh()
        else:
            self.reconnect.start(self, RECONNECT_FREQUENCY)
            self.autorefresh.stop()

    def main(self):
        self.reconnect.start(self, RECONNECT_FREQUENCY)
        SysIndicator.main(self)

class ParrotZikBaseInterface(object):
    def __init__(self, indicator):
        self.indicator = indicator
        self.parrot = None
        self.battery_level = MenuItem("Battery Level:", None, sensitive=False,
                                      visible=False)
        self.battery_state = MenuItem("Battery State:", None, sensitive=False,
                                      visible=False)
        self.firmware_version = MenuItem("Firmware Version:", None,
                                         sensitive=False, visible=False)
        self.auto_connection = MenuItem("Auto Connection", self.toggleAuto,
                                        checkitem=True, visible=False)
        self.indicator.menu.append(self.battery_level)
        self.indicator.menu.append(self.battery_state)
        self.indicator.menu.append(self.firmware_version)
        self.indicator.menu.append(self.auto_connection)

    @property
    def connected(self):
        if self.parrot:
            return self.parrot.api.sock
        else:
            return False

    def activate(self, parrot):
        self.parrot = parrot
        self.battery_level.show()
        self.battery_state.show()
        self.firmware_version.show()
        self.auto_connection.show()

        self.indicator.info_item.set_label("Connected to: "
                                           + self.parrot.friendly_name)
        self.firmware_version.set_label(
            "Firmware version: " + self.parrot.version)

        self.auto_connection.set_active(self.parrot.auto_connect)

        self.indicator.active_interface = self
        self.indicator.menu.reposition()

    def deactivate(self):
        self.parrot = None
        self.battery_level.hide()
        self.battery_state.hide()
        self.firmware_version.hide()
        self.auto_connection.hide()
        self.indicator.setIcon("zik-audio-headset")
        self.indicator.info_item.set_label("Parrot Zik Not connected..")
        self.indicator.menu.reposition()
        self.indicator.active_interface = None

    def toggleAuto(self, widget):
        if self.connected:
            self.parrot.auto_connection = self.auto_connection.get_active()
            self.auto_connection.set_active(self.parrot.auto_connection)

    def refresh(self):
        if self.connected:
            print "Updating battery"
            battery_level = self.parrot.battery_level
            battery_state = self.parrot.battery_state

            if battery_state == BatteryStates.CHARGING:
                self.indicator.setIcon("zik-battery-charging")
            elif battery_level > 80:
                self.indicator.setIcon("zik-battery-100")
            elif battery_level > 60:
                self.indicator.setIcon("zik-battery-080")
            elif battery_level > 40:
                self.indicator.setIcon("zik-battery-060")
            elif battery_level > 20:
                self.indicator.setIcon("zik-battery-040")
            else:
                self.indicator.setIcon("zik-battery-low")

            self.battery_state.set_label(
                "State: " + BatteryStates.representation[battery_state])
            self.battery_level.set_label(
                "Battery Level: " + str(battery_level))
        else:
            self.deactivate()


class ParrotZikVersion1Interface(ParrotZikBaseInterface):
    def __init__(self, indicator):
        super(ParrotZikVersion1Interface, self).__init__(indicator)
        self.noise_cancelation = MenuItem("Noise Cancellation", self.toggleANC,
                                          checkitem=True, visible=False)
        self.lou_reed_mode = MenuItem("Lou Reed Mode", self.toggleLouReedMode,
                                      checkitem=True, visible=False)
        self.concert_hall_mode = MenuItem(
            "Concert Hall Mode", self.toggleParrotConcertHall,
            checkitem=True, visible=False)
        self.indicator.menu.append(self.noise_cancelation)
        self.indicator.menu.append(self.lou_reed_mode)
        self.indicator.menu.append(self.concert_hall_mode)

    def activate(self, parrot):
        self.noise_cancelation.show()
        self.lou_reed_mode.show()
        self.concert_hall_mode.show()
        super(ParrotZikVersion1Interface, self).activate(parrot)

        self.noise_cancelation.set_active(self.parrot.noise_cancel)
        self.lou_reed_mode.set_active(self.parrot.lou_reed_mode)
        self.concert_hall_mode.set_active(self.parrot.concert_hall)

    def deactivate(self):
        self.noise_cancelation.hide()
        self.lou_reed_mode.hide()
        self.concert_hall_mode.hide()
        super(ParrotZikVersion1Interface, self).deactivate()

    def toggleANC(self, widget):
        if self.connected:
            self.parrot.noise_cancel = self.noise_cancelation.get_active()
            self.noise_cancelation.set_active(self.parrot.noise_cancel)

    def toggleLouReedMode(self, widget):
        if self.connected:
            self.parrot.lou_reed_mode = self.lou_reed_mode.get_active()
            self.lou_reed_mode.set_active(self.parrot.lou_reed_mode)
            self.concert_hall_mode.set_active(self.parrot.concert_hall)
            self.concert_hall_mode.set_sensitive(
                not self.lou_reed_mode.get_active())

    def toggleParrotConcertHall(self, widget):
        if self.connected:
            self.parrot.concert_hall = self.concert_hall_mode.get_active()
            self.concert_hall_mode.set_active(self.parrot.concert_hall)


class ParrotZikVersion2Interface(ParrotZikBaseInterface):
    def __init__(self, indicator):
        super(ParrotZikVersion2Interface, self).__init__(indicator)
        self.noise_cancelation = MenuItem("Noise Cancellation", None,
                                          sensitive=True, visible=False)
        self.noise_cancelation_submenu = Menu()
        self.noise_cancelation.set_submenu(self.noise_cancelation_submenu)

        self.noise_cancelation_enabled = MenuItem("Enabled", self.toggleANC,
                                                  sensitive=False, checkitem=True)
        self.noise_cancelation_mode0 = MenuItem("Mode0", self.toggledummy,
                                                sensitive=False, checkitem=True)
        self.noise_cancelation_mode1 = MenuItem("Mode1", self.toggledummy,
                                                sensitive=False, checkitem=True)
        self.noise_cancelation_mode2 = MenuItem("Mode2", self.toggledummy,
                                                sensitive=False, checkitem=True)
        self.noise_cancelation_mode3 = MenuItem("Mode3", self.toggledummy,
                                                sensitive=False, checkitem=True)
        self.noise_cancelation_submenu.append(self.noise_cancelation_enabled)
        self.noise_cancelation_submenu.append(self.noise_cancelation_mode0)
        self.noise_cancelation_submenu.append(self.noise_cancelation_mode1)
        self.noise_cancelation_submenu.append(self.noise_cancelation_mode2)
        self.noise_cancelation_submenu.append(self.noise_cancelation_mode3)
        self.indicator.menu.append(self.noise_cancelation)

        self.concert_hall_mode = MenuItem(
            "Sound Mode", None, sensitive=True, checkitem=False, visible=False)
        self.concert_hall_mode_submenu = Menu()
        self.concert_hall_mode.set_submenu(self.concert_hall_mode_submenu)

        self.concert_hall_mode_enabled = MenuItem("Enabled", None,
                                                  sensitive=True, checkitem=True)
        self.concert_hall = MenuItem("Concert Hall", None,
                                     sensitive=True, checkitem=True)
        self.jazz_mode = MenuItem("Jazz Club", None,
                                  sensitive=True, checkitem=True)
        self.living_mode = MenuItem("Libing Room", None,
                                    sensitive=True, checkitem=True)
        self.silent_mode = MenuItem("Silent Room", None,
                                    sensitive=True, checkitem=True)
        self.concert_hall_mode_submenu.append(self.concert_hall_mode_enabled)
        self.concert_hall_mode_submenu.append(self.concert_hall)
        self.concert_hall_mode_submenu.append(self.jazz_mode)
        self.concert_hall_mode_submenu.append(self.living_mode)
        self.concert_hall_mode_submenu.append(self.silent_mode)
        self.indicator.menu.append(self.concert_hall_mode)

        self.flight_mode = MenuItem("Flight Mode", None,
                                    sensitive=True, checkitem=True, visible=False)
        self.indicator.menu.append(self.flight_mode)

    def activate(self, parrot):
        self.noise_cancelation.show()
        self.concert_hall_mode.show()
        self.flight_mode.show()
        super(ParrotZikVersion2Interface, self).activate(parrot)
        self.noise_cancelation_enabled.set_active(self.parrot.noise_cancel)
        # self.flight_mode.set_active(self.parrot.flight_mode)

    def toggleANC(self, widget):
        if self.connected:
            self.parrot.noise_cancel = self.noise_cancelation.get_active()
            self.noise_cancelation.set_active(self.parrot.noise_cancel)

    def toggledummy(self, widget):
        print(widget.get_name())


if __name__ == "__main__":
    indicator = ParrotZikIndicator()
    indicator.main()