-
Notifications
You must be signed in to change notification settings - Fork 12
/
CBL2.cpp
executable file
·318 lines (275 loc) · 8.73 KB
/
CBL2.cpp
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
/*************************************************
* CBL2.h - Library for emulating CBL2 devices *
* or CBL-connected calculators with *
* Arduinos. *
* Created by Christopher Mitchell, *
* 2011-2019, all rights reserved. *
*************************************************/
#include "Arduino.h"
#include "CBL2.h"
#include "TIVar.h"
// Constructor with default communication lines
CBL2::CBL2() :
TICL()
{
return;
}
// Constructor with custom communication lines.
CBL2::CBL2(int tip, int ring) :
TICL(tip, ring)
{
return;
}
int CBL2::getFromCBL2(uint8_t type, uint8_t* header, uint8_t* data, int* datalength, int maxlength) {
uint8_t msg_header[4];
uint8_t endpoint = (type == 0x01)?CALC85b:CALC82; // CALC82 for strings and other types, CALC85b for lists
int length;
int rval;
// Step 1: Send REQ, wait for ACK and VAR
// We will assume that the CBL2 can use 11-byte (TI-82/TI-83/TI-85-style)
// variable headers when we send messages with a CALC82 endpoint
msg_header[0] = endpoint;
msg_header[1] = REQ;
TIVar::intToSizeWord(11, &msg_header[2]);
rval = send(msg_header, header, 11);
if (rval || get(msg_header, NULL, &length, 0) || msg_header[1] != ACK) {
// Either the message was not an ACK, or we didn't even get a message
return -1;
}
if (get(msg_header, header, &length, 11) || msg_header[1] != VAR) {
// Either the message was not a VAR, or we didn't even get a message
return -1;
}
// Step 2: ACK VAR, send CTS
msg_header[0] = endpoint;
msg_header[1] = ACK;
msg_header[2] = msg_header[3] = 0;
rval = send(msg_header, NULL, 0);
if (rval) {
// The send did not complete successfully
return -1;
}
msg_header[1] = CTS;
rval = send(msg_header, NULL, 0);
// Step 3: Receive CTS ACK and DATA
if (rval || get(msg_header, NULL, &length, 0) || msg_header[1] != ACK) {
// Either the message was not an ACK, or we didn't even get a message
return -1;
}
if (get(msg_header, NULL, datalength, maxlength) || msg_header[1] != DATA) {
// Either the message was not a DATA, or we didn't even get a message
return -1;
}
// Step 4: ACK DATA (do NOT perform EOT)
msg_header[0] = endpoint;
msg_header[1] = ACK;
msg_header[2] = msg_header[3] = 0;
return send(msg_header, NULL, 0);
}
int CBL2::sendToCBL2(uint8_t type, uint8_t* header, uint8_t* data, int datalength) {
uint8_t msg_header[4];
uint8_t endpoint = (type == 0x01) ? CALC85b : CALC82; // CALC82 for strings and other types, CALC85b for lists
int length;
int rval;
// Step 1: Send RTS, wait for RTS ACK
// We will assume that the CBL2 can use 11-byte (TI-82/TI-83/TI-85-style)
// variable headers when we send messages with a CALC82 endpoint
msg_header[0] = endpoint;
msg_header[1] = RTS;
msg_header[2] = 11;
msg_header[3] = 0;
rval = send(msg_header, header, 11);
if (rval || (rval = get(msg_header, NULL, &length, 0)) || msg_header[1] != ACK) {
// Either the message was not an ACK, or we didn't even get a message
return -1;
}
// Step 2: Wait for CTS, send CTS ACK
if (get(msg_header, NULL, &length, 0) || msg_header[1] != CTS) {
// Either the message was not a CTS, or we didn't even get a message
return -1;
}
msg_header[0] = endpoint;
msg_header[1] = ACK;
msg_header[2] = msg_header[3] = 0;
rval = send(msg_header, NULL, 0);
if (rval) {
// The send did not complete successfully
return -1;
}
// Step 3: Send DATA, wait for DATA ACK
msg_header[0] = endpoint;
msg_header[1] = DATA;
TIVar::intToSizeWord(11, &msg_header[2]);
rval = send(msg_header, data, datalength);
if (rval || get(msg_header, NULL, &length, 0) || msg_header[1] != ACK) {
// Either the message was not an ACK, or we didn't even get a message
return -1;
}
// Step 4: Send EOT and wait for EOT ACK
msg_header[0] = endpoint;
msg_header[1] = EOT;
msg_header[2] = msg_header[3] = 0;
rval = send(msg_header, NULL, 0);
if (rval || get(msg_header, NULL, &length, 0) || msg_header[1] != ACK) {
// Either the message was not an ACK, or we didn't even get a message
return -1;
}
return 0;
}
int CBL2::setupCallbacks(uint8_t* header, uint8_t* data, int maxlength,
int (*get_callback)(uint8_t, enum Endpoint, int),
int (*send_callback)(uint8_t, enum Endpoint, int*, int*, data_callback*))
{
header_ = header;
data_ = data;
maxlength_ = maxlength;
get_callback_ = get_callback;
send_callback_ = send_callback;
callback_init = true;
return 0;
}
int CBL2::eventLoopTick(bool quick_fail) {
uint8_t msg_header[4];
int length;
int rval;
int endpoint = CBL82;
if (!callback_init) {
return -1;
}
// See if there's a message coming
rval = get(msg_header, data_, &length, maxlength_, quick_fail ? TIMEOUT : GET_ENTER_TIMEOUT);
if (rval) {
if (serial_) {
serial_->print("No msg: code ");
serial_->println(rval);
}
return 0; // No message coming
}
// Deduce what kind of operation is happening
// CBL2 responds to TI-82 as 0x12, "0x95" endpoint as 0x15
enum Endpoint model = (enum Endpoint)msg_header[0];
switch(model) {
case CALC82:
endpoint = CBL82;
break;
case CALC85a:
case CALC85b:
endpoint = CBL85;
break;
case CALC89:
endpoint = CBL89;
break;
case COMP83:
endpoint = CALC83;
break;
case COMP83P:
endpoint = CALC83P;
break;
default:
return -1; // Unknown endpoint
};
// Now deal with the message
switch(msg_header[1]) {
case ACK:
break; // Drop ACKs on the floor
case RTS:
memcpy(header_, data_, length); // Save the variable header
// Send an ACK
msg_header[0] = endpoint;
msg_header[1] = ACK;
msg_header[2] = msg_header[3] = 0x00;
rval = send(msg_header, NULL, 0);
if (rval) {
// The send did not complete successfully
break;
}
// Send a CTS
msg_header[0] = endpoint;
msg_header[1] = CTS;
msg_header[2] = msg_header[3] = 0x00;
rval = 10*send(msg_header, NULL, 0);
break;
case DATA:
// Send an ACK
msg_header[0] = endpoint;
msg_header[1] = ACK;
msg_header[2] = msg_header[3] = 0x00;
rval = send(msg_header, NULL, 0);
if (rval) {
// The send did not complete successfully
break;
}
// Deliver the data to the callback
normalizeVariableHeader(model); // Deal with all the wacky way headers can be constructed
rval = get_callback_(header_[2], model, length); // Ignore rval for now
break;
case EOT:
// Send an ACK
msg_header[0] = endpoint;
msg_header[1] = ACK;
msg_header[2] = msg_header[3] = 0x00;
rval = send(msg_header, NULL, 0);
break;
case REQ: {
memcpy(header_, data_, length); // Save the variable header
// Send an ACK
msg_header[0] = endpoint;
msg_header[1] = ACK;
msg_header[2] = msg_header[3] = 0x00;
rval = send(msg_header, NULL, 0);
if (rval) {
// The send did not complete successfully
break;
}
// Get the header and data from the callback
data_callback_ = NULL;
int headerlength = length;
uint8_t tmp_header[16];
normalizeVariableHeader(model); // Deal with all the wacky way headers can be constructed
memcpy(tmp_header, header_, 16); // Save it...
send_callback_(header_[2], model,
&headerlength, &datalength_, &data_callback_);
// Copy in the size.
tmp_header[0] = header_[0];
tmp_header[1] = header_[1];
memcpy(header_, tmp_header, 16); // ...and restore it
// Send the VAR message
msg_header[0] = endpoint;
msg_header[1] = VAR;
msg_header[2] = headerlength;
msg_header[3] = 0x00;
rval = send(msg_header, header_, headerlength);
}
break;
case CTS:
// Send an ACK
msg_header[0] = endpoint;
msg_header[1] = ACK;
msg_header[2] = msg_header[3] = 0x00;
rval = send(msg_header, NULL, 0);
if (rval) {
// The send did not complete successfully
break;
}
// Send the DATA
msg_header[0] = endpoint;
msg_header[1] = DATA;
msg_header[2] = (datalength_ & 0x00ff);
msg_header[3] = (datalength_ >> 8);
rval = send(msg_header, data_, datalength_, data_callback_);
break;
}
return rval;
}
void CBL2::normalizeVariableHeader(const int model) {
if ((model == CALC82 || model == CALC85b) && header_[2] == VarTypes82::VarString && header_[3] == VarTypes82::VarRList) {
// Real list from "TI-82" (could be TI-84+SE or TI-84+CSE , variable name encoded with some odd format
header_[2] = VarTypes82::VarRList;
} else if (model == CALC82 && header_[2] == VarTypes82::VarReal && header_[3] == 0xAA /* tVarStr */) {
header_[2] = VarTypes82::VarString;
} else if (model == CALC82 && header_[2] == VarTypes82::VarReal && header_[3] == 0x5E /* tVarYVar */) {
header_[2] = VarTypes82::VarYVar;
} else if (model == CALC82 && header_[2] == VarTypes82::VarReal && header_[3] == 0x60 /* tVarPic */) {
header_[2] = VarTypes82::VarPic;
}
}