Skip to content

Commit 5d7479a

Browse files
authored
Merge 229ebde into 701f490
2 parents 701f490 + 229ebde commit 5d7479a

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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=TIMEOUT, writeTimeout=TIMEOUT,
36+
parity=serial.PARITY_NONE, onReceive=self._onReceive)
37+
except EnvironmentError:
38+
log.debugWarning("", exc_info=True)
39+
continue
40+
# Check for cell information
41+
if self._describe():
42+
log.debug("Nattiq nBraille found on %s with %d cells", port, self.numCells)
43+
break
44+
else:
45+
self._serial.close()
46+
else:
47+
raise RuntimeError("Can't find a Nattiq nBraille device (port = %s)" % port)
48+
49+
def terminate(self):
50+
try:
51+
super(BrailleDisplayDriver, self).terminate()
52+
finally:
53+
self._serial.write("reset".encode())
54+
self._serial.close()
55+
self._serial = None
56+
57+
def _describe(self):
58+
self.numCells = 0
59+
log.debug("Writing reset tag")
60+
self._serial.write("reset".encode())
61+
self._serial.waitForRead(3)
62+
log.debug("Writing init tag")
63+
self._serial.write(INIT_TAG)
64+
self._serial.waitForRead(3)
65+
# If a valid response was received, _onReceive will have set numCells.
66+
if self.numCells:
67+
return True
68+
log.debug("Not a Nattiq nBraille")
69+
return False
70+
71+
def _onReceive(self, command):
72+
if int(command) == 0:
73+
arg = self._serial.read(2)
74+
self.numCells = int(arg)
75+
elif int(command) == 2:
76+
inputCore.manager.executeGesture(InputGestureKeys(1))
77+
self._serial.waitForRead(1)
78+
arg = self._serial.read(2)
79+
elif int(command) == 3:
80+
inputCore.manager.executeGesture(InputGestureKeys(2))
81+
self._serial.waitForRead(1)
82+
arg = self._serial.read(2)
83+
elif int(command) == 4:
84+
inputCore.manager.executeGesture(InputGestureKeys(3))
85+
self._serial.waitForRead(1)
86+
arg = self._serial.read(2)
87+
elif int(command) == 5:
88+
inputCore.manager.executeGesture(InputGestureKeys(4))
89+
self._serial.waitForRead(1)
90+
arg = self._serial.read(2)
91+
elif int(command) == 1:
92+
arg = self._serial.read(2)
93+
try:
94+
inputCore.manager.executeGesture(RoutingInputGesture(int(arg)))
95+
except ValueError:
96+
pass
97+
98+
def _dispatch(self, command, arg):
99+
return
100+
101+
def display(self, cells):
102+
cells = "-".join(str(cell) for cell in cells)
103+
log.debug(cells)
104+
self._serial.write(cells)
105+
106+
gestureMap = inputCore.GlobalGestureMap({
107+
"globalCommands.GlobalCommands": {
108+
"braille_scrollBack": ("br(nattiqbraille):tback",),
109+
"braille_routeTo": ("br(nattiqbraille):routing",),
110+
"braille_scrollForward": ("br(nattiqbraille):tadvance",),
111+
"braille_previousLine": ("br(nattiqbraille):tprevious",),
112+
"braille_nextLine": ("br(nattiqbraille):tnext",),
113+
},
114+
})
115+
116+
117+
class InputGestureKeys(braille.BrailleDisplayGesture):
118+
source = BrailleDisplayDriver.name
119+
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+
132+
class RoutingInputGesture(braille.BrailleDisplayGesture):
133+
source = BrailleDisplayDriver.name
134+
135+
def __init__(self, routingIndex):
136+
super(RoutingInputGesture, self).__init__()
137+
self.routingIndex = routingIndex
138+
self.id = "routing"

0 commit comments

Comments
 (0)