This repository has been archived by the owner on Dec 23, 2021. It is now read-only.
forked from DSchndr/serial2ti83
-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial2ti83.ino
261 lines (230 loc) · 6.5 KB
/
serial2ti83.ino
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
/* ArduGrayLink, a modern yet simple TI Graph-Link cable
* https://github.com/MTres19/serial2ti83
* https://github.com/DSchndr/serial2ti83
* https://github.com/jw0k/serial2ti83
*
* Copyright (C) 2017 jw0k
* Copyright (C) 2018 DSchndr
* Copyright (C) 2018 Matthew Trescott
*
* MIT-licensed.
*/
const int TIP = A0; //arduino port where the tip of the stereo jack plug is connected
const int RING = A1; //arduino port where the middle part of the stereo jack plug is connected
const unsigned long TXTIMEOUT = 50; //ms
const unsigned long RXTIMEOUT = 20; //ms
#define TI_TIP_IS_HIGH (PINC & (uint8_t)0b00000001) // No need for bitshift because it's the first pin.
#define TI_RING_IS_HIGH ((PINC & (uint8_t)0b00000010) >> 1)
#define TI_TIP_IS_LOW ((~PINC) & (uint8_t)0b00000001) // No need for bitshift because it's the first pin.
#define TI_RING_IS_LOW (((~PINC) & (uint8_t)0b00000010) >> 1)
inline void TiTipLow()
{
PORTC &= (uint8_t)0b11111110; // Save an instruction https://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_intpromote
}
inline void TiRingLow()
{
PORTC &= (uint8_t)0b11111101;
}
inline void TiTipHigh()
{
PORTC |= (uint8_t)0b00000001;
}
inline void TiRingHigh()
{
PORTC |= (uint8_t)0b00000010;
}
inline void TiTipOutput()
{
DDRC |= (uint8_t)0b00000001;
}
inline void TiRingOutput()
{
DDRC |= (uint8_t)0b00000010;
}
inline void TiTipInput()
{
DDRC &= (uint8_t)0b11111110;
}
inline void TiRingInput()
{
DDRC &= (uint8_t)0b11111101;
}
void sendByte(uint8_t byte)
{
unsigned long currentTime;
for (int i = 0; i < 8; ++i)
{
bool bit = byte & 0x01;
byte >>= 1;
//poll both lines until they are both high, which means we're ready to send a bit
currentTime = millis();
while (TI_TIP_IS_LOW || TI_RING_IS_LOW)
{
if (millis() - currentTime > TXTIMEOUT)
{
return;
}
}
//int ourLine;
//int oppositeLine;
if (bit)
{
//ourLine = RING;
//oppositeLine = TIP;
// send a bit by pulling appropriate line low
TiRingOutput();
TiRingLow();
// wait for opposite line to become low
currentTime = millis();
while (TI_TIP_IS_HIGH)
{
if (millis() - currentTime > TXTIMEOUT)
{
TiRingInput();
TiRingHigh();
return;
}
}
// release our line
TiRingInput();
TiRingHigh();
// wait for opposite line to become high
currentTime = millis();
while (TI_TIP_IS_LOW)
{
if (millis() - currentTime > TXTIMEOUT)
{
return;
}
}
}
else
{
//ourLine = TIP;
//oppositeLine = RING;
// send a bit by pulling appropriate line low
TiTipOutput();
TiTipLow();
// wait for opposite line to become low
currentTime = millis();
while (TI_RING_IS_HIGH)
{
if (millis() - currentTime > TXTIMEOUT)
{
TiTipInput();
TiTipHigh();
return;
}
}
// release our line
TiTipInput();
TiTipHigh();
// wait for opposite line to become high
currentTime = millis();
while (TI_RING_IS_LOW)
{
if (millis() - currentTime > TXTIMEOUT)
{
return;
}
}
}
}
}
bool getByte(uint8_t& byte)
{
unsigned long currentTime;
uint8_t result = 0;
for (int i = 0; i < 8; ++i)
{
//poll both lines until one of them becomes low
delayMicroseconds(6); // Why is this needed after I replaced digitalRead() in the while loop below?
currentTime = millis();
while (TI_RING_IS_HIGH && TI_TIP_IS_HIGH)
{
if (millis() - currentTime > RXTIMEOUT)
{
return false;
}
}
bool bit = TI_RING_IS_LOW;
result >>= 1;
if (bit)
{
//ourLine = TIP;
//oppositeLine = RING;
result |= 0x80; //bits are always transmitted LSb first (least significant bit)
// acknowledge a bit by pulling appropriate line low
TiTipOutput();
TiTipLow();
//wait for opposite line to become high
currentTime = millis();
while (TI_RING_IS_LOW)
{
if (millis() - currentTime > RXTIMEOUT)
{
TiTipInput();
TiTipHigh();
return false;
}
}
// release our line
TiTipInput();
TiTipHigh();
}
else
{
//ourLine = RING;
//oppositeLine = TIP;
// acknowledge a bit by pulling appropriate line low
TiRingOutput();
TiRingLow();
//wait for opposite line to become high
while (TI_TIP_IS_LOW)
{
if (millis() - currentTime > RXTIMEOUT)
{
TiRingInput();
TiRingHigh();
}
}
// release our line
TiRingInput();
TiRingHigh();
}
}
byte = result;
return true;
}
void setup()
{
//turn off the built-in LED
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(9600);
//configure both lines to be in a high-impedance state and enable pull-up resistors
//pinMode(TIP, INPUT_PULLUP);
//pinMode(RING, INPUT_PULLUP);
TiTipInput();
TiTipHigh();
TiRingInput();
TiRingHigh();
}
void loop()
{
//forward incoming data from PC to the calculator
while (Serial.available() > 0)
{
// Warn if the calculator isn't keeping up with the serial port
if (Serial.available() > 50) {
digitalWrite(LED_BUILTIN, HIGH);
}
sendByte(Serial.read());
}
//forward incoming data from calculator to the PC
uint8_t byteFromCalc;
if (getByte(byteFromCalc))
{
Serial.write(byteFromCalc);
}
}