OSDN Git Service

baum driver: Update to perhaps recognise more keys on displays other than the SuperVario.
authorJames Teh <jamie@jantrid.net>
Sun, 23 Jan 2011 14:33:55 +0000 (00:33 +1000)
committerJames Teh <jamie@jantrid.net>
Sun, 23 Jan 2011 14:33:55 +0000 (00:33 +1000)
This also removes duplication in the key handling code.
In addition, try to detect Refreshabraille and VarioConnect via bluetooth.

source/brailleDisplayDrivers/baum.py

index 93593d8..61b5e22 100644 (file)
@@ -24,7 +24,9 @@ BAUM_PROTOCOL_ONOFF = "\x15"
 BAUM_COMMUNICATION_CHANNEL = "\x16"\r
 BAUM_POWERDOWN = "\x17"\r
 BAUM_ROUTING_KEYS = "\x22"\r
-BAUM_BUTTONS = "\x24"\r
+BAUM_DISPLAY_KEYS = "\x24"\r
+BAUM_BRAILLE_KEYS = "\x33"\r
+BAUM_JOYSTICK_KEYS = "\x34"\r
 BAUM_DEVICE_ID = "\x84"\r
 BAUM_SERIAL_NUMBER = "\x8A"\r
 \r
@@ -32,12 +34,19 @@ BAUM_RSP_LENGTHS = {
        BAUM_CELL_COUNT: 1,\r
        BAUM_POWERDOWN: 1,\r
        BAUM_COMMUNICATION_CHANNEL: 1,\r
-       BAUM_BUTTONS: 1,\r
+       BAUM_DISPLAY_KEYS: 1,\r
+       BAUM_BRAILLE_KEYS: 2,\r
+       BAUM_JOYSTICK_KEYS: 1,\r
        BAUM_DEVICE_ID: 16,\r
        BAUM_SERIAL_NUMBER: 8,\r
 }\r
 \r
-BUTTON_NAMES = ("d1", "d2", "d3", "d4", "d5", "d6")\r
+KEY_NAMES = {\r
+       BAUM_ROUTING_KEYS: None,\r
+       BAUM_DISPLAY_KEYS: ("d1", "d2", "d3", "d4", "d5", "d6"),\r
+       BAUM_BRAILLE_KEYS: ("b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b10", None, None, "c1", "c2", "c3", "c4"),\r
+       BAUM_JOYSTICK_KEYS: ("up", "left", "down", "right", "select"),\r
+}\r
 \r
 USB_IDS = frozenset((\r
        "VID_0403&PID_FE70", # Vario 40\r
@@ -67,6 +76,8 @@ BLUETOOTH_NAMES = (
        "Baum PocketVario",\r
        "Baum SVario",\r
        "HWG Brailliant",\r
+       "Refreshabraille",\r
+       "VarioConnect",\r
 )\r
 \r
 class BrailleDisplayDriver(braille.BrailleDisplayDriver):\r
@@ -133,8 +144,7 @@ class BrailleDisplayDriver(braille.BrailleDisplayDriver):
 \r
                self._readTimer = wx.PyTimer(self._handleResponses)\r
                self._readTimer.Start(READ_INTERVAL)\r
-               self._lastButtons = 0\r
-               self._lastRoutingKeys = 0\r
+               self._keysDown = {}\r
                self._ignoreKeyReleases = False\r
 \r
        def terminate(self):\r
@@ -190,8 +200,8 @@ class BrailleDisplayDriver(braille.BrailleDisplayDriver):
                return command, arg\r
 \r
        def _handleResponse(self, command, arg):\r
-               routingKeys = None\r
-               buttons = None\r
+               updateKeys = False\r
+               executeKeys = False\r
 \r
                if command == BAUM_CELL_COUNT:\r
                        oldNumCells = self.numCells\r
@@ -201,27 +211,18 @@ class BrailleDisplayDriver(braille.BrailleDisplayDriver):
                elif command == BAUM_DEVICE_ID:\r
                        self._deviceID = arg\r
 \r
-               elif command == BAUM_ROUTING_KEYS:\r
+               elif command in KEY_NAMES:\r
+                       updateKeys = True\r
                        arg = sum(ord(byte) << offset * 8 for offset, byte in enumerate(arg))\r
-                       if arg < self._lastRoutingKeys:\r
-                               # Release.\r
-                               if not self._ignoreKeyReleases:\r
-                                       routingKeys = self._lastRoutingKeys\r
-                       else:\r
-                               # Press.\r
-                               self._ignoreKeyReleases = False\r
-                       self._lastRoutingKeys = arg\r
-\r
-               elif command == BAUM_BUTTONS:\r
-                       arg = ord(arg)\r
-                       if arg < self._lastButtons:\r
+                       if arg < self._keysDown.get(command, 0):\r
                                # Release.\r
                                if not self._ignoreKeyReleases:\r
-                                       buttons = self._lastButtons\r
+                                       # The first key released executes the key combination.\r
+                                       executeKeys = True\r
                        else:\r
                                # Press.\r
+                               # This begins a new key combination.\r
                                self._ignoreKeyReleases = False\r
-                       self._lastButtons = arg\r
 \r
                elif command == BAUM_POWERDOWN:\r
                        log.debug("Power down")\r
@@ -231,15 +232,18 @@ class BrailleDisplayDriver(braille.BrailleDisplayDriver):
                else:\r
                        log.debugWarning("Unknown command {command!r}, arg {arg!r}".format(command=command, arg=arg))\r
 \r
-               if buttons or routingKeys:\r
+               if executeKeys:\r
                        try:\r
-                               inputCore.manager.executeGesture(InputGesture(buttons or self._lastButtons, routingKeys or self._lastRoutingKeys))\r
+                               inputCore.manager.executeGesture(InputGesture(self._keysDown))\r
                        except inputCore.NoInputGestureAction:\r
                                pass\r
-                       # The first key released executes the key combination.\r
                        # Any further releases are just the rest of the keys in the combination being released,\r
                        # so they should be ignored.\r
                        self._ignoreKeyReleases = True\r
+               if updateKeys:\r
+                       # We must update key state after execution instead of before because\r
+                       # execution needs to include the key that was just released.\r
+                       self._keysDown[command] = arg\r
 \r
        def display(self, cells):\r
                # cells will already be padded up to numCells.\r
@@ -259,20 +263,21 @@ class InputGesture(braille.BrailleDisplayGesture):
 \r
        source = BrailleDisplayDriver.name\r
 \r
-       def __init__(self, buttons, routingKeys):\r
+       def __init__(self, keysDown):\r
                super(InputGesture, self).__init__()\r
-               self.buttons = buttons\r
-               self.routingKeys = routingKeys\r
+               self.keysDown = dict(keysDown)\r
 \r
                self.keyNames = names = set()\r
-               if buttons:\r
-                       for index, name in enumerate(BUTTON_NAMES):\r
-                               if buttons & (1 << index):\r
-                                       names.add(name)\r
-               if routingKeys:\r
-                       for index in xrange(braille.handler.display.numCells):\r
-                               if routingKeys & (1 << index):\r
-                                       self.routingIndex = index\r
-                                       names.add("routing")\r
+               for group, groupKeysDown in keysDown.iteritems():\r
+                       if group == BAUM_ROUTING_KEYS:\r
+                               for index in xrange(braille.handler.display.numCells):\r
+                                       if groupKeysDown & (1 << index):\r
+                                               self.routingIndex = index\r
+                                               names.add("routing")\r
+                                               break\r
+                       else:\r
+                               for index, name in enumerate(KEY_NAMES[group]):\r
+                                       if groupKeysDown & (1 << index):\r
+                                               names.add(name)\r
 \r
                self.id = "+".join(names)\r