| 1 | # -*- coding: iso-8859-1 -*- |
|---|
| 2 | |
|---|
| 3 | """ Panohead remote control. |
|---|
| 4 | |
|---|
| 5 | License |
|---|
| 6 | ======= |
|---|
| 7 | |
|---|
| 8 | - B{papywizard} (U{http://trac.gbiloba.org/papywizard}) is Copyright: |
|---|
| 9 | - (C) 2007-2008 Frédéric Mantegazza |
|---|
| 10 | |
|---|
| 11 | This software is governed by the B{CeCILL} license under French law and |
|---|
| 12 | abiding by the rules of distribution of free software. You can use, |
|---|
| 13 | modify and/or redistribute the software under the terms of the CeCILL |
|---|
| 14 | license as circulated by CEA, CNRS and INRIA at the following URL |
|---|
| 15 | U{http://www.cecill.info}. |
|---|
| 16 | |
|---|
| 17 | As a counterpart to the access to the source code and rights to copy, |
|---|
| 18 | modify and redistribute granted by the license, users are provided only |
|---|
| 19 | with a limited warranty and the software's author, the holder of the |
|---|
| 20 | economic rights, and the successive licensors have only limited |
|---|
| 21 | liability. |
|---|
| 22 | |
|---|
| 23 | In this respect, the user's attention is drawn to the risks associated |
|---|
| 24 | with loading, using, modifying and/or developing or reproducing the |
|---|
| 25 | software by the user in light of its specific status of free software, |
|---|
| 26 | that may mean that it is complicated to manipulate, and that also |
|---|
| 27 | therefore means that it is reserved for developers and experienced |
|---|
| 28 | professionals having in-depth computer knowledge. Users are therefore |
|---|
| 29 | encouraged to load and test the software's suitability as regards their |
|---|
| 30 | requirements in conditions enabling the security of their systems and/or |
|---|
| 31 | data to be ensured and, more generally, to use and operate it in the |
|---|
| 32 | same conditions as regards security. |
|---|
| 33 | |
|---|
| 34 | The fact that you are presently reading this means that you have had |
|---|
| 35 | knowledge of the CeCILL license and that you accept its terms. |
|---|
| 36 | |
|---|
| 37 | Module purpose |
|---|
| 38 | ============== |
|---|
| 39 | |
|---|
| 40 | Hardware driver |
|---|
| 41 | |
|---|
| 42 | Implements |
|---|
| 43 | ========== |
|---|
| 44 | |
|---|
| 45 | - BluetoothDriver |
|---|
| 46 | |
|---|
| 47 | @author: Frédéric Mantegazza |
|---|
| 48 | @copyright: (C) 2007-2008 Frédéric Mantegazza |
|---|
| 49 | @license: CeCILL |
|---|
| 50 | """ |
|---|
| 51 | |
|---|
| 52 | __revision__ = "$Id$" |
|---|
| 53 | |
|---|
| 54 | import time |
|---|
| 55 | |
|---|
| 56 | import bluetooth |
|---|
| 57 | |
|---|
| 58 | from papywizard.common.configManager import ConfigManager |
|---|
| 59 | from papywizard.common.loggingServices import Logger |
|---|
| 60 | from papywizard.common.exception import HardwareError |
|---|
| 61 | from papywizard.hardware.busDriver import BusDriver |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | class BluetoothDriver(BusDriver): |
|---|
| 65 | """ Driver for bluetooth connection. |
|---|
| 66 | |
|---|
| 67 | This driver only uses bluetooth socket. |
|---|
| 68 | """ |
|---|
| 69 | def init(self): |
|---|
| 70 | if not self._init: |
|---|
| 71 | try: |
|---|
| 72 | self.setDeviceAddress(ConfigManager().get('Hardware', 'BLUETOOTH_DEVICE_ADDRESS')) |
|---|
| 73 | self._sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) |
|---|
| 74 | self._sock.connect((self.__deviceAddress, 1)) |
|---|
| 75 | self._init = True |
|---|
| 76 | |
|---|
| 77 | except: |
|---|
| 78 | Logger().exception("BluetoothDriver.init()") |
|---|
| 79 | raise HardwareError("Can't init BluetoothDriver object") |
|---|
| 80 | |
|---|
| 81 | def shutdown(self): |
|---|
| 82 | if self._init: |
|---|
| 83 | self._sock.close() |
|---|
| 84 | self._init = False |
|---|
| 85 | |
|---|
| 86 | def setDeviceAddress(self, address): |
|---|
| 87 | """ Set the address of the device to connect to. |
|---|
| 88 | |
|---|
| 89 | @param address: address of the device |
|---|
| 90 | @type address: str |
|---|
| 91 | """ |
|---|
| 92 | self.__deviceAddress = address |
|---|
| 93 | |
|---|
| 94 | def discoverDevices(self): |
|---|
| 95 | """ Discover bluetooth devices. |
|---|
| 96 | |
|---|
| 97 | @return: devices addresses and names |
|---|
| 98 | @rtype: list of tuple |
|---|
| 99 | """ |
|---|
| 100 | return bluetooth.discover_devices(lookup_names=True) |
|---|
| 101 | |
|---|
| 102 | def sendCmd(self, cmd): |
|---|
| 103 | """ |
|---|
| 104 | @todo: see how to empty buffer. |
|---|
| 105 | """ |
|---|
| 106 | if not self._init: |
|---|
| 107 | raise HardwareError("BluetoothDriver not initialized") |
|---|
| 108 | |
|---|
| 109 | self.acquireBus() |
|---|
| 110 | try: |
|---|
| 111 | # Empty buffer |
|---|
| 112 | #self._sock.read(self._sock.inWaiting()) |
|---|
| 113 | |
|---|
| 114 | self._sock.send(":%s\r" % cmd) |
|---|
| 115 | c = '' |
|---|
| 116 | while c != '=': |
|---|
| 117 | c = self._sock.recv(1) |
|---|
| 118 | #Logger().debug("BluetoothDriver.sendCmd(): c=%s" % repr(c)) |
|---|
| 119 | if not c: |
|---|
| 120 | raise IOError("Timeout while reading on bluetooth bus") |
|---|
| 121 | data = "" |
|---|
| 122 | while True: |
|---|
| 123 | c = self._sock.recv(1) |
|---|
| 124 | #Logger().debug("BluetoothDriver.sendCmd(): c=%s, data=%s" % (repr(c), repr(data))) |
|---|
| 125 | if not c: |
|---|
| 126 | raise IOError("Timeout while reading on bluetooth bus") |
|---|
| 127 | elif c == '\r': |
|---|
| 128 | break |
|---|
| 129 | data += c |
|---|
| 130 | |
|---|
| 131 | finally: |
|---|
| 132 | self.releaseBus() |
|---|
| 133 | |
|---|
| 134 | return data |
|---|