-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsony_slink_esp8266.ino
More file actions
executable file
·241 lines (203 loc) · 5.85 KB
/
sony_slink_esp8266.ino
File metadata and controls
executable file
·241 lines (203 loc) · 5.85 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
#include <ArduinoOTA.h>
#include <ESP8266mDNS.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
ESP8266WiFiMulti wifiMulti;
WebSocketsServer webSocket = WebSocketsServer(80);
const byte OUTPUT_PIN = 4;
const byte INPUT_PIN = 5;
const byte PULSE_BUFFER_SIZE = 100;
volatile unsigned long timeLowTransition = 0;
volatile byte bufferReadPosition = 0;
volatile byte bufferWritePosition = 0;
volatile byte pulseBuffer[PULSE_BUFFER_SIZE];
// debug data
uint8_t debugClient = WEBSOCKETS_SERVER_CLIENT_MAX;
String pulseLengths;
bool bufferOverflowDetected = false;
void setup()
{
ArduinoOTA.begin();
WiFi.hostname("sony_slink");
WiFi.mode(WIFI_STA);
wifiMulti.addAP("Network_name", "Network_password");
webSocket.begin();
webSocket.onEvent(webSocketEvent);
pinMode(OUTPUT_PIN, OUTPUT);
digitalWrite(OUTPUT_PIN, LOW);
pinMode(INPUT_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(INPUT_PIN), busChange, CHANGE);
}
void webSocketEvent(uint8_t clientNum, WStype_t type, uint8_t* payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
if (clientNum == debugClient) {
debugClient = WEBSOCKETS_SERVER_CLIENT_MAX;
}
break;
case WStype_CONNECTED:
break;
case WStype_TEXT:
if (strcmp((const char*)payload, "debug") == 0) {
debugClient = clientNum;
DEBUG("Debug output enabled");
break;
}
// A hexadecimal command is expected
if (length % 2 != 0) {
DEBUG("Uneven length of S-link command");
return;
}
byte commandBytes[length / 2];
for (int i = 0; i < length; i += 2) {
if (!isHexadecimalDigit(char(payload[i])) || !isHexadecimalDigit(char(payload[i + 1]))) {
DEBUG("Non-hexadecimal S-link command");
return;
}
String hexByte = String(char(payload[i])) + String(char(payload[i + 1]));
commandBytes[i / 2] = strtol(hexByte.c_str(), NULL, 16);
}
sendCommand(commandBytes, sizeof(commandBytes));
break;
}
}
void DEBUG(String msg)
{
if (webSocket.clientIsConnected(debugClient)) {
webSocket.sendTXT(debugClient, msg);
}
}
// This interrupt handler receives data from a remote slink device
IRAM_ATTR void busChange()
{
unsigned long timeNow = micros();
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) {
bufferOverflowDetected = true;
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 String bytesReceivedHex;
bool completeMessageReceived = false;
while (bufferReadPosition != bufferWritePosition) {
int timeLow = pulseBuffer[bufferReadPosition] * 10;
bufferReadPosition = (bufferReadPosition + 1) % PULSE_BUFFER_SIZE;
if (timeLow > 2000) {
// 2400 us -> new data sequence
completeMessageReceived = true;
break;
}
if (!pulseLengths.isEmpty()) {
pulseLengths += " ";
}
pulseLengths += String(timeLow, DEC);
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) {
bytesReceivedHex += "0" + String(currentByte, HEX);
}
else {
bytesReceivedHex += String(currentByte, HEX);
}
currentBit = 0;
}
}
completeMessageReceived |= isBusIdle();
if (completeMessageReceived && (!bytesReceivedHex.isEmpty() || currentBit != 0)) {
if (!bytesReceivedHex.isEmpty()) {
webSocket.broadcastTXT(bytesReceivedHex);
}
DEBUG("Pulses (us): " + pulseLengths);
if (currentBit != 0) {
DEBUG("Received " + String(currentBit) + " stray bits");
}
DEBUG("Buffer overflow detected: " + String(bufferOverflowDetected? "yes":"no"));
bytesReceivedHex = String();
currentByte = 0;
currentBit = 0;
pulseLengths = String();
}
}
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()
{
delay(20); // will yield
}
void sendCommand(byte command[], int commandLength)
{
do {
yield(); // Can't yield after this point since precise timing is needed when sending data
} while (!isBusIdle());
noInterrupts();
sendSyncPulse();
for (int i = 0; i < commandLength; ++i) {
sendByte(command[i]);
}
interrupts();
idleAfterCommand();
}
void loop()
{
if (wifiMulti.run() == WL_CONNECTED) {
processSlinkInput();
webSocket.loop();
ArduinoOTA.handle();
}
}