-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsony_slink_arduino.ino
More file actions
243 lines (200 loc) · 5.08 KB
/
sony_slink_arduino.ino
File metadata and controls
243 lines (200 loc) · 5.08 KB
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
#define DEBUG_PULSES
const byte OUTPUT_PIN = 2;
const byte INPUT_PIN = 3;
const byte PULSE_BUFFER_SIZE = 200;
volatile unsigned long timeLowTransition = 0;
volatile byte bufferReadPosition = 0;
volatile byte bufferWritePosition = 0;
volatile byte pulseBuffer[PULSE_BUFFER_SIZE];
#ifdef DEBUG_PULSES
String pulseLengths;
#endif
void setup()
{
pinMode(OUTPUT_PIN, OUTPUT);
digitalWrite(OUTPUT_PIN, LOW);
pinMode(INPUT_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(INPUT_PIN), busChange, CHANGE);
Serial.begin(115200);
}
// This interrupt handler receives data from a remote slink device
void busChange()
{
static unsigned long timeOfPreviousInterrupt = 0;
unsigned long timeNow = micros();
if (timeNow - timeOfPreviousInterrupt < 100) {
return;
}
timeOfPreviousInterrupt = timeNow;
int busState = digitalRead(INPUT_PIN);
if (busState == LOW) {
timeLowTransition = timeNow;
return;
}
// Bus is high. The time that the bus has been low determines what
// has happened. Let's store this information for analysis outside
// of the interrupt handler.
int timeLow = timeNow - timeLowTransition;
if ((bufferWritePosition + 1) % PULSE_BUFFER_SIZE == bufferReadPosition) {
Serial.println(F("Pulse buffer overflow when receiving data"));
return;
}
// Divide by 10 to make the pulse length fit in 8 bits
pulseBuffer[bufferWritePosition] = min(255, timeLow / 10);
bufferWritePosition = (bufferWritePosition + 1) % PULSE_BUFFER_SIZE;
}
void processSlinkInput()
{
static byte currentByte = 0;
static byte currentBit = 0;
static bool partialOutput = false;
while (bufferReadPosition != bufferWritePosition) {
int timeLow = pulseBuffer[bufferReadPosition] * 10;
bufferReadPosition = (bufferReadPosition + 1) % PULSE_BUFFER_SIZE;
#ifdef DEBUG_PULSES
if (timeLow > 2000) {
pulseLengths = String();
}
else {
pulseLengths += " ";
}
pulseLengths += String(timeLow, DEC);
#endif
if (timeLow > 2000) {
// 2400 us -> new data sequence
if (partialOutput) {
if (currentBit != 0) {
Serial.print(F("!Discarding "));
Serial.print(currentBit);
Serial.print(F(" stray bits"));
}
Serial.print('\n');
partialOutput = false;
}
currentBit = 0;
continue;
}
partialOutput = true;
currentBit += 1;
if (timeLow > 900) {
// 1200 us -> bit == 1
bitSet(currentByte, 8 - currentBit);
}
else {
// 600 us -> bit == 0
bitClear(currentByte, 8 - currentBit);
}
if (currentBit == 8) {
if (currentByte <= 0xF) {
Serial.print(0, HEX);
}
Serial.print(currentByte, HEX);
currentBit = 0;
}
}
if (partialOutput && isBusIdle()) {
Serial.print('\n');
partialOutput = false;
}
}
bool isBusIdle()
{
noInterrupts();
bool isBusIdle = micros() - timeLowTransition > 1200 + 600 + 20000;
interrupts();
return isBusIdle;
}
void sendPulseDelimiter()
{
digitalWrite(OUTPUT_PIN, LOW);
delayMicroseconds(600);
}
void sendSyncPulse()
{
digitalWrite(OUTPUT_PIN, HIGH);
delayMicroseconds(2400);
sendPulseDelimiter();
}
void sendBit(int bit)
{
digitalWrite(OUTPUT_PIN, HIGH);
if (bit) {
delayMicroseconds(1200);
}
else {
delayMicroseconds(600);
}
sendPulseDelimiter();
}
void sendByte(int value)
{
for (int i = 7; i >= 0; --i) {
sendBit(bitRead(value, i));
}
}
void idleAfterCommand()
{
delayMicroseconds(20000);
}
bool sendCommand(byte command[], int commandLength)
{
if (!isBusIdle()) {
return false;
}
noInterrupts();
sendSyncPulse();
for (int i = 0; i < commandLength; ++i) {
sendByte(command[i]);
}
// Clear interrupt flags because interrupts triggered when we sent
// the command and the interrupts are queued for processing once
// interrupts are re-enabled.
EIFR = bit(INTF0) | bit(INTF1);
interrupts();
idleAfterCommand();
return true;
}
void processSerialInput()
{
static String bytesReceived;
while (Serial.available()) {
bytesReceived += char(Serial.read());
}
const int eolPos = bytesReceived.indexOf("\n");
if (eolPos == -1) {
return;
}
const String command = bytesReceived.substring(0, eolPos);
bytesReceived.remove(0, command.length() + 1);
#ifdef DEBUG_PULSES
if (command == "pulsedump") {
Serial.println(pulseLengths);
return;
}
#endif
// A hexadecimal command is expected
if (command.length() % 2 != 0) {
Serial.println(F("Uneven length of serial input"));
return;
}
for (int i = 0; i < command.length(); ++i) {
if (!isHexadecimalDigit(command[i])) {
Serial.println(F("Non-hexadecimal serial input"));
return;
}
}
byte commandBytes[command.length() / 2];
for (int i = 0; i < sizeof(commandBytes); ++i) {
String hexByte = command.substring(2 * i, 2 * i + 2);
commandBytes[i] = strtol(hexByte.c_str(), NULL, 16);
}
if (!sendCommand(commandBytes, sizeof(commandBytes))) {
// If send fails, re-queue command
bytesReceived = command + "\n" + bytesReceived;
}
}
void loop()
{
processSlinkInput();
processSerialInput();
}