#!/usr/bin/env python import gtk import ParrotZik import BluetoothPairedDevices from SysIndicator import MenuItem from SysIndicator import UniversalMenu from SysIndicator import SysIndicator REFRESH_FREQUENCY = 1000 RECONNECT_FREQUENCY = 5000 class repeat(object): def __init__(self, f): self.f = f self.id = None 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 Stop(Exception): pass class ParrotZikIndicator(SysIndicator): def __init__(self): self.menu = UniversalMenu() self.info_item = MenuItem("Parrot Zik Not connected..", None, sensitive=False) self.menu.append(self.info_item) self.version_1_interface = ParrotZikVersion1Interface(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.parrot.sock: print "Lost connection" self.active_interface.deactivate() else: self.reconnect.stop() else: mac = BluetoothPairedDevices.ParrotZikMac() if mac: parrot = ParrotZik.ParrotZik(mac) if parrot.sock: self.version_1_interface.activate(parrot) 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.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.name = self.parrot.friendly_name self.version = self.parrot.version if self.parrot.auto_connect == "true": self.auto_connection.set_active(True) else: self.auto_connection.set_active(False) 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: if self.auto_connection.get_active(): self.parrot.auto_connection = "true" else: self.parrot.auto_connection = "false" 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) if self.parrot.noise_cancel == "true": self.noise_cancelation.set_active(True) else: self.noise_cancelation.set_active(False) if self.parrot.lou_reed_mode == "true": self.lou_reed_mode.set_active(True) else: self.lou_reed_mode.set_active(False) if self.parrot.concert_hall == "true": self.concert_hall_mode.set_active(True) else: self.concert_hall_mode.set_active(False) 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: if self.noise_cancelation.get_active(): self.parrot.noise_cancel = "true" else: self.parrot.noise_cancel = "false" def toggleLouReedMode(self, widget): if self.connected: if self.lou_reed_mode.get_active(): self.parrot.lou_reed_mode = "true" self.concert_hall_mode.set_sensitive(False) else: self.parrot.lou_reed_mode = "false" self.concert_hall_mode.set_sensitive(True) def toggleParrotConcertHall(self, widget): if self.connected: if self.concert_hall_mode.get_active(): self.parrot.concert_hall = "true" else: self.parrot.concert_hall = "false" def refresh(self): if self.connected: print "Updating battery" self.batteryLevel = int(self.parrot.battery_level) if self.parrot.BatteryCharging: self.batteryLevel = "Charging" self.indicator.setIcon("zik-battery-charging") self.batteryLevel = "Unknown" self.batteryState = "Charging" elif self.batteryLevel > 80: self.indicator.setIcon("zik-battery-100") self.batteryState = "In Use" elif self.batteryLevel > 60: self.indicator.setIcon("zik-battery-080") self.batteryState = "In Use" elif self.batteryLevel > 40: self.indicator.setIcon("zik-battery-060") self.batteryState = "In Use" elif self.batteryLevel > 20: self.indicator.setIcon("zik-battery-040") self.batteryState = "In Use" else: self.indicator.setIcon("zik-battery-low") self.batteryState = "In Use" self.indicator.info_item.set_label("Connected to: " + self.name) self.firmware_version.set_label( "Firmware version: " + self.version) self.battery_state.set_label("State: " + self.batteryState) self.battery_level.set_label( "Battery Level: " + str(self.batteryLevel)) else: self.deactivate() if __name__ == "__main__": indicator = ParrotZikIndicator() indicator.main()