-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrail_al.goc
319 lines (277 loc) · 10.4 KB
/
brail_al.goc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
@include <stdapp.goh>
@include <foam.goh>
#include <streamC.h>
#include <ansi/stdio.h>
#include <ansi/string.h>
@include "common.goh"
#include "macroeng.h"
#include "brail_al.h"
@extern object SpeakApp;
static byte brALReplyBuf[80], brALReplyFun;
static int brALReplyPos, brALReplyLen;
static byte brALKeyGroup;
// Specific states for AL Braille implementation (brState)
#define BR_AWAITING_REPLY 1
#define BR_AWAITING_KEY 2
#define BR_AWAITING_NEW_REPLY 3
#define BR_AWAITING_REPLY_KEY 4
void LoadALBraille(void)
{
Boolean loaded;
loaded = BrailleLoad(); // Attempt to initialize common braille
if(loaded)
{
/* Set format used by Speechbox */
SerialSetFormat(brSerialDriver, BRAILLE_PORT, AL_FORMAT, SM_RAW, AL_BAUD);
/* Enable notification on incoming data */
StreamSetMessageNotify(brSerialDriver, BRAILLE_PORT,
((1 << STREAM_TYPE_READER_OFFSET) |
(STREAM_EVENT_DATA << STREAM_TYPE_EVENT_OFFSET) |
(STREAM_MODE_MESSAGE << STREAM_TYPE_HOW_OFFSET)),
MSG_MAPP_SYNTH_ALBRAILLE_SERIAL, @SpeakApp);
/* Initialize protocol state */
brLinePending = FALSE;
brPressedKeys = brChordKeys = brPrevChord = 0;
brTypewriter = FALSE;
brALReplyPos = brALReplyLen = brALReplyFun = 0;
/* Initialize protocol by clearing display */
SpeakSentenceALBraille((byte *)"");
}
}
void UnloadALBraille(void)
{
if(brState!=BR_ERROR)
{
StreamSetNoNotify(brSerialDriver, BRAILLE_PORT,
((1 << STREAM_TYPE_READER_OFFSET) |
(STREAM_EVENT_DATA << STREAM_TYPE_EVENT_OFFSET) |
(STREAM_MODE_MESSAGE << STREAM_TYPE_HOW_OFFSET)));
}
BrailleUnload(); // deinitialize generic braille
}
static void ALBrailleHandleChord(dword chord)
{
byte cs, key = 0, shift = 0;
switch(chord)
{
case BRK_MASK(BRK_LEFT):
@call SpeakApp::MSG_BRAILLE_KEY_LEFT();
break;
case BRK_MASK(BRK_RIGHT):
@call SpeakApp::MSG_BRAILLE_KEY_RIGHT();
break;
case BRK_MASK(BRK_UPPAD) | BRK_MASK(BRK_DOWNPAD):
cs = CS_CONTROL; key = VC_F4; shift = 0; break;
case BRK_MASK(BRK_BTAB) | BRK_MASK(BRK_FTAB):
cs = CS_CONTROL; key = VC_F1; shift = 0; break;
case BRK_MASK(BRK_CUR1): @call SpeakApp::MSG_BRAILLE_KEY_FOCUS(); break;
case BRK_MASK(BRK_HOME1): @call SpeakApp::MSG_BRAILLE_KEY_PSEUDOCUR(); break;
case BRK_MASK(BRK_EASTPAD): @call SpeakApp::MSG_BRAILLE_KEY_KEYS(); break;
case BRK_MASK(BRK_NORTHPAD): @call SpeakApp::MSG_BRAILLE_KEY_TITLE(); break;
case BRK_MASK(BRK_WESTPAD): @call SpeakApp::MSG_BRAILLE_KEY_STATUS(); break;
case BRK_MASK(BRK_SOUTHPAD): @call SpeakApp::MSG_BRAILLE_KEY_APPKEYS(); break;
case BRK_MASK(BRK_RIGHT) | BRK_MASK(BRK_CUR1):
BrailleToggleCursor(chord==brPrevChord);
brLinePending = TRUE; // Update when we get the chance
@send,forceQueue SpeakApp::MSG_MAPP_SYNTH_ALBRAILLE_SERIAL();
break;
case BRK_MASK(BRK_BEAR) | BRK_MASK(BRK_FEAR):
@call SpeakApp::MSG_MAPP_BRING_TO_FRONT(0);
break;
case BRK_MASK(BRK_UPPAD):
cs = CS_CONTROL; key = VC_UP; shift = 0; break;
case BRK_MASK(BRK_DOWNPAD):
cs = CS_CONTROL; key = VC_DOWN; shift = 0; break;
case BRK_MASK(BRK_LEFTPAD):
cs = CS_CONTROL; key = VC_LEFT; shift = 0; break;
case BRK_MASK(BRK_RIGHTPAD):
cs = CS_CONTROL; key = VC_RIGHT; shift = 0; break;
case BRK_MASK(BRK_BTAB):
cs = CS_CONTROL; key = VC_TAB; shift = SS_LSHIFT; break;
case BRK_MASK(BRK_FTAB):
cs = CS_CONTROL; key = VC_TAB; shift = 0; break;
case BRK_MASK(BRK_UP):
cs = CS_CONTROL; key = VC_UP; shift = SS_LCTRL; break;
case BRK_MASK(BRK_DOWN):
cs = CS_CONTROL; key = VC_DOWN; shift = SS_LCTRL; break;
case BRK_MASK(BRK_BEAR):
cs = CS_CONTROL; key = VC_HOME; shift = 0; break;
case BRK_MASK(BRK_FEAR):
cs = CS_CONTROL; key = VC_END; shift = 0; break;
}
if(key) // Convert to simulated keystroke?
SynthesizeKey(cs, key, shift);
brPrevChord = chord;
}
static void ALBrailleHandleKey(byte group, byte key)
{
int i;
/* Special handling for routing keys (TCx) */
if(group==AL_GROUP_TC && !(key & AL_RELEASE_FLAG))
{
@call SpeakApp::MSG_BRAILLE_KEY_ROUTING(key);
return;
}
/* Find key on braille keyboard that this code is for */
for(i=0; i<sizeof(brALKeyTable)/sizeof(brALKeyTable[0]); i++)
if(group==brALKeyTable[i].group &&
(key & ~AL_RELEASE_FLAG)==brALKeyTable[i].code)
{
/* Update lists of currently pressed keys */
if(key & AL_RELEASE_FLAG)
brPressedKeys &= ~BRK_MASK(brALKeyTable[i].flag);
else
{
brPressedKeys |= BRK_MASK(brALKeyTable[i].flag);
brChordKeys |= BRK_MASK(brALKeyTable[i].flag);
}
break;
}
if(!brPressedKeys && brChordKeys)
{
ALBrailleHandleChord(brChordKeys); // handle completed chord
brChordKeys = 0; // chord has been handled
}
#ifdef LOG
if(brPressedKeys)
LogPrintf("pressed: %08lx\r\n", brPressedKeys);
#endif
}
static void ALBrailleHandleOldReply(void)
{
const byte reply_id[] = AL_REPLY_ID;
const byte get_config[] = AL_CODE_GET_CONFIG;
brState = BR_IDLE; // Reply was handled
/* Received ID byte reply? */
if(brALReplyPos >= sizeof(reply_id)+2 &&
memcmp(brALReplyBuf, reply_id, sizeof(reply_id))==0)
{
switch(brALReplyBuf[sizeof(reply_id)])
{
case 0x00: brStatus = 3; G_brailleWidth = 20; break; // ABT320
case 0x01: brStatus = 3; G_brailleWidth = 40; break; // ABT340
case 0x02: brStatus = 5; G_brailleWidth = 40; break; // ABT34D
case 0x03: brStatus = 5; G_brailleWidth = 80; break; // ABT380
case 0x04: brStatus = 5; G_brailleWidth = 80; break; // ABT382
case 0x0A: brStatus = 3; G_brailleWidth = 20; break; // AD420
case 0x0B: brStatus = 5; G_brailleWidth = 40; break; // AD440
case 0x0C: brStatus = 5; G_brailleWidth = 40; break; // AD44D
case 0x0D: brStatus = 5; G_brailleWidth = 80; break; // AD480
case 0x0E: // Sat 544
case 0x0F: // Sat 570
/* Satellite: use enhanced return code for getting config */
BrailleWriteString(get_config, sizeof(get_config));
G_brailleWidth = 0;
brState = BR_AWAITING_NEW_REPLY;
break;
default:
G_brailleWidth = 0;
}
}
}
static void ALBrailleHandleNewReply(void)
{
if(brALReplyFun==0x07) // terminal config reply
{
if(brALReplyPos==4) // we only understand this format
{
brStatus = brALReplyBuf[2]; // get display dimensions from reply
G_brailleWidth = brALReplyBuf[3];
}
}
}
@extern method SpeakApplicationClass, MSG_MAPP_SYNTH_ALBRAILLE_SERIAL
{
byte c;
if(!brSerialDriver) return; // Ignore if message is stale...
/* Process the characters we can grab */
while(BrailleReadByte(&c))
{
/* Collect reply once we have found the starting character */
if(brState==BR_AWAITING_REPLY && (brALReplyPos>0 || c==AL_REPLY_START))
{
/* Put reply bytes into buffer (while there is room...) */
if(brALReplyPos<sizeof(brALReplyBuf))
brALReplyBuf[brALReplyPos++] = c;
/* Finished reply? */
if(c==AL_REPLY_END)
{
ALBrailleHandleOldReply(); // Handly old-style ASCII reply
}
}
else
{
// Everything else is a key code, which means that it is to
// be interpreted in character pairs: (group,key)
// This includes all enhanced Satellite return codes
if((brState==BR_IDLE || brState==BR_AWAITING_NEW_REPLY) && AL_IS_GROUP(c))
{
brALKeyGroup = c;
brState = (brState==BR_IDLE)? BR_AWAITING_KEY : BR_AWAITING_REPLY_KEY;
}
else
{
brState = (brState==BR_AWAITING_KEY)? BR_IDLE : BR_AWAITING_NEW_REPLY;
if(brALKeyGroup==AL_GROUP_REP_START)
{
brALReplyLen = -1; // expect length byte for packet
brALReplyFun = c; // start code specifies function number
}
else if(brALKeyGroup==AL_GROUP_REP_DATA)
{
if(brALReplyLen==-1) // length of packet unknown?
{
/* First pseudo-keycode specifies length of following reply */
brALReplyLen = c;
brALReplyPos = 0;
}
else if(brALReplyLen) // are we collecting a reply packet?
{
if(brALReplyPos<sizeof(brALReplyBuf))
brALReplyBuf[brALReplyPos++] = c;
if(--brALReplyLen==0) // received last byte: process reply
{
ALBrailleHandleNewReply();
brState = BR_IDLE; // nothing more expected
}
}
}
else
{
/* Second character completes the group */
ALBrailleHandleKey(brALKeyGroup, c);
}
}
}
}
/* Do we have a pending update? Execute it when we get ready */
if(brState==BR_IDLE && brLinePending)
{
if(G_brailleWidth) // do we know the display?
{
byte pre[] = {AL_CODE_WRITE1, AL_CODE_WRITE2, 0, 0};
const byte post = AL_CODE_WRITE_POST;
/* Set width - start position is always 0 (send entire line) */
pre[3] = brStatus + G_brailleWidth;
/* Send line to display */
BrailleSendLine(pre, sizeof(pre), &post, 1);
brLinePending = FALSE;
}
else
{
const byte get_id[] = AL_CODE_GET_ID;
/* First identify display */
BrailleWriteString(get_id, sizeof(get_id));
brState = BR_AWAITING_REPLY;
brALReplyPos = 0;
}
}
}
void SpeakSentenceALBraille(byte *buf)
{
if(BrailleUpdate(buf)) // See if display needs an update
{
brLinePending = TRUE; // Update when we get the chance
@send,forceQueue SpeakApp::MSG_MAPP_SYNTH_ALBRAILLE_SERIAL();
}
}