Skip to content

Commit e0c1084

Browse files
authored
Merge 708a27a into fb2d1cd
2 parents fb2d1cd + 708a27a commit e0c1084

1 file changed

Lines changed: 150 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)