Skip to content

Commit 9bb6d15

Browse files
authored
Merge 2511fc7 into 701f490
2 parents 701f490 + 2511fc7 commit 9bb6d15

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)