Skip to content

Commit ab23fe6

Browse files
authored
Merge b281593 into 169c329
2 parents 169c329 + b281593 commit ab23fe6

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# brailleDisplayDrivers/nattiqbraille.py
2+
# A part of NonVisual Desktop Access (NVDA)
3+
# This file is covered by the GNU General Public License.
4+
# See the file COPYING for more details.
5+
# Copyright (C) 2011-2020
6+
7+
8+
import serial
9+
import braille
10+
import inputCore
11+
from logHandler import log
12+
import hwIo
13+
14+
BAUD_RATE = 10000000
15+
TIMEOUT = 0.3
16+
INIT_TAG = "0"
17+
18+
19+
class BrailleDisplayDriver(braille.BrailleDisplayDriver):
20+
name = "nattiqbraille"
21+
# Translators: Names of braille displays
22+
description = _("Nattiq nBraille")
23+
isThreadSafe = True
24+
25+
@classmethod
26+
def getManualPorts(cls):
27+
return braille.getSerialPorts()
28+
29+
def __init__(self, port="auto"):
30+
super(BrailleDisplayDriver, self).__init__()
31+
self._serial = None
32+
for portType, portId, port, portInfo in self._getTryPorts(port):
33+
log.debug("Checking port %s for a Nattiq nBraille", port)
34+
try:
35+
self._serial = hwIo.Serial(port, baudrate=BAUD_RATE, timeout=0.3, writeTimeout=0.3, parity=serial.PARITY_NONE, onReceive=self._onReceive)
36+
except EnvironmentError:
37+
log.debugWarning("", exc_info=True)
38+
continue
39+
# Check for cell information
40+
if self._describe():
41+
log.debug("Nattiq nBraille found on %s with %d cells", port, self.numCells)
42+
break
43+
else:
44+
self._serial.close()
45+
else:
46+
raise RuntimeError("Can't find a Nattiq nBraille device (port = %s)" % port)
47+
48+
def terminate(self):
49+
try:
50+
super(BrailleDisplayDriver, self).terminate()
51+
finally:
52+
self._serial.write("reset".encode())
53+
self._serial.close()
54+
self._serial = None
55+
56+
def _describe(self):
57+
self.numCells = 0
58+
log.debug("Writing reset tag")
59+
self._serial.write("reset".encode())
60+
self._serial.waitForRead(3)
61+
log.debug("Writing init tag")
62+
self._serial.write(INIT_TAG)
63+
self._serial.waitForRead(3)
64+
# If a valid response was received, _onReceive will have set numCells.
65+
if self.numCells:
66+
return True
67+
log.debug("Not a Nattiq nBraille")
68+
return False
69+
70+
def _onReceive(self, command):
71+
if int(command) == 0:
72+
arg = self._serial.read(2)
73+
self.numCells = int(arg)
74+
elif int(command) == 2:
75+
inputCore.manager.executeGesture(InputGestureKeys(1))
76+
self._serial.waitForRead(1)
77+
arg = self._serial.read(2)
78+
elif int(command) == 3:
79+
inputCore.manager.executeGesture(InputGestureKeys(2))
80+
self._serial.waitForRead(1)
81+
arg = self._serial.read(2)
82+
elif int(command) == 4:
83+
inputCore.manager.executeGesture(InputGestureKeys(3))
84+
self._serial.waitForRead(1)
85+
arg = self._serial.read(2)
86+
elif int(command) == 5:
87+
inputCore.manager.executeGesture(InputGestureKeys(4))
88+
self._serial.waitForRead(1)
89+
arg = self._serial.read(2)
90+
elif int(command) == 1:
91+
arg = self._serial.read(2)
92+
try:
93+
inputCore.manager.executeGesture(RoutingInputGesture(int(arg)))
94+
except ValueError:
95+
pass
96+
97+
def _dispatch(self, command, arg):
98+
return
99+
100+
def display(self, cells):
101+
cells = "-".join(str(cell) for cell in cells)
102+
log.debug(cells)
103+
self._serial.write(cells)
104+
105+
gestureMap = inputCore.GlobalGestureMap({
106+
"globalCommands.GlobalCommands": {
107+
"braille_scrollBack": ("br(nattiqbraille):tback",),
108+
"braille_routeTo": ("br(nattiqbraille):routing",),
109+
"braille_scrollForward": ("br(nattiqbraille):tadvance",),
110+
"braille_previousLine": ("br(nattiqbraille):tprevious",),
111+
"braille_nextLine": ("br(nattiqbraille):tnext",),
112+
},
113+
})
114+
115+
116+
class InputGestureKeys(braille.BrailleDisplayGesture):
117+
source = BrailleDisplayDriver.name
118+
def __init__(self, keys):
119+
super(InputGestureKeys, self).__init__()
120+
if keys == 1:
121+
self.id = "tback"
122+
elif keys == 2:
123+
self.id = "tadvance"
124+
elif keys == 3:
125+
self.id = "tnext"
126+
elif keys == 4:
127+
self.id = "tprevious"
128+
129+
class RoutingInputGesture(braille.BrailleDisplayGesture):
130+
source = BrailleDisplayDriver.name
131+
def __init__(self, routingIndex):
132+
super(RoutingInputGesture, self).__init__()
133+
self.routingIndex = routingIndex
134+
self.id = "routing"

0 commit comments

Comments
 (0)