Skip to content

Commit 2f104d9

Browse files
authored
Merge db2de89 into 169c329
2 parents 169c329 + db2de89 commit 2f104d9

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)