Skip to content

Commit 2fcbea9

Browse files
authored
Merge 28883c2 into 169c329
2 parents 169c329 + 28883c2 commit 2fcbea9

1 file changed

Lines changed: 167 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)