9
9
10
10
#include < utility/ExampleStandardFirmataCommon.h>
11
11
12
- #define DEBUG_ENABLED 0 // 0 or 1
12
+ #define LOG_ENABLED 1 // 0 or 1
13
+ #define DEBUG_ENABLED 1 // 0 or 1
13
14
14
- #if DEBUG_ENABLED
15
- #define DEBUG (x... ) do { x; } while (0 )
15
+ #if LOG_ENABLED
16
+ #define LOG (x... ) do { x; } while (0 )
16
17
#define IS_IGNORE_PIN (i ) ((i) == 1 ) // 1 == Serial TX
17
18
#else
19
+ #define LOG (x... ) do { (void )0 ; } while (0 )
20
+ #endif
21
+
22
+ #if LOG_ENABLED && DEBUG_ENABLED
23
+ #define DEBUG (x... ) do { x; } while (0 )
24
+ #else
18
25
#define DEBUG (x... ) do { (void )0 ; } while (0 )
19
26
#endif
20
27
21
28
PipedStreamPair pipe; // streamify data coming from websocket
22
- PipedStream& StreamFirmataInternal = pipe.first;
23
- PipedStream& StreamAsFirmata = pipe.second;
29
+ PipedStream& firmataInternalStream = pipe.first;
30
+ PipedStream& firmataStream = pipe.second;
24
31
WebSocketsServer webSocket (3031 );
25
32
26
33
/*
@@ -42,22 +49,23 @@ void ignorePins()
42
49
43
50
void initTransport ()
44
51
{
52
+ WiFi.persistent (false );
45
53
WiFi.mode (WIFI_STA);
46
54
WiFi.begin (STASSID, STAPSK);
47
- DEBUG (Serial.printf (" Connecting to SSID '%s'...\n " , STASSID));
55
+ LOG (Serial.printf (" Connecting to SSID '%s'...\n " , STASSID));
48
56
while (WiFi.status () != WL_CONNECTED)
49
57
{
50
58
delay (1000 );
51
- DEBUG (Serial.print (' .' ));
59
+ LOG (Serial.print (' .' ));
52
60
}
53
- DEBUG (Serial.printf (" Connected, IP address: %s\n " , WiFi.localIP ().toString ().c_str ()));
61
+ LOG (Serial.printf (" Connected, IP address: %s\n " , WiFi.localIP ().toString ().c_str ()));
54
62
}
55
63
56
64
int clients = 0 ;
57
65
58
66
void webSocketEvent (uint8_t num, WStype_t type, uint8_t * payload, size_t length)
59
67
{
60
- DEBUG (Serial.printf (" [ws:%u,t:%d,l:%z ]: " , num, (int )type, length));
68
+ DEBUG (Serial.printf (" [ws:%u,t:%d,l:%zd ]: " , num, (int )type, length));
61
69
62
70
switch (type) {
63
71
@@ -81,22 +89,16 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length
81
89
case WStype_BIN:
82
90
DEBUG (Serial.printf (" binary from WebSocket\n " ));
83
91
#if 1
84
- // !! there is a Print::write(ptr,size_t) !?
85
- // DEBUG(Serial.print("before: ");
86
- // DEBUG(Serial.println(StreamFirmataInternal.available());
92
+ // !! there _is_StreamFirmataInternal a Print::write(ptr,size_t) !?
87
93
for (size_t i = 0 ; i < length; i++)
88
94
{
89
- // Serial.printf("0x%02x ", (int)payload[i]);
90
- if (StreamAsFirmata.write (payload[i]) != 1 ) {
91
- DEBUG (Serial.println (" stream error writing to StreamAsFirmata" ));
95
+ if (firmataStream.write (payload[i]) != 1 ) {
96
+ LOG (Serial.println (" stream error writing to firmataStream" ));
92
97
}
93
98
}
94
- // DEBUG(Serial.println();
95
- // DEBUG(Serial.print("after: ");
96
- // DEBUG(Serial.println(StreamFirmataInternal.available());
97
99
#else
98
- if (StreamAsFirmata .write(payload, length) != length) {
99
- DEBUG (Serial.println("stream error writing to StreamAsFirmata "));
100
+ if (firmataStream .write(payload, length) != length) {
101
+ LOG (Serial.println("stream error writing to firmataStream "));
100
102
}
101
103
#endif
102
104
break ;
@@ -110,25 +112,22 @@ void initWebSocket()
110
112
{
111
113
webSocket.begin ();
112
114
webSocket.onEvent (webSocketEvent);
113
- DEBUG (Serial.println (" WS: initialized" ));
115
+ LOG (Serial.println (" WS: initialized" ));
114
116
}
115
117
116
118
void initFirmata ()
117
119
{
118
120
initFirmataCommonBegin ();
119
121
ignorePins ();
120
122
121
- // Using the pipes stream
122
- // it is written to by the websocket event function
123
- // it is read from by the main loop
124
- Firmata.begin (StreamFirmataInternal);
123
+ Firmata.begin (firmataInternalStream);
125
124
126
125
initFirmataCommonEnd ();
127
126
}
128
127
129
128
void setup ()
130
129
{
131
- DEBUG (Serial.begin (115200 ));
130
+ LOG (Serial.begin (115200 ));
132
131
133
132
initTransport ();
134
133
initWebSocket ();
@@ -145,34 +144,34 @@ void loop()
145
144
webSocket.loop ();
146
145
MDNS.update ();
147
146
148
- size_t StreamAsFirmataLen = StreamAsFirmata .available ();
147
+ size_t firmataStreamLen = firmataStream .available ();
149
148
150
- #if DEBUG_ENABLED
149
+ #if 0 && DEBUG_ENABLED
151
150
static unsigned long last = 0;
152
- if (millis () - last > 1000 )
151
+ if (millis() - last > 5000 )
153
152
{
154
153
last += 1000;
155
- DEBUG ( Serial.printf (" Firmata to WS: available=%zd --- WS to Firmata: available=%zd\n " ,
156
- StreamAsFirmataLen ,
157
- StreamFirmataInternal .available () ));
154
+ Serial.printf("Firmata to WS: available=%zd --- WS to Firmata: available=%zd\n",
155
+ firmataStreamLen ,
156
+ firmataInternalStream .available());
158
157
}
159
158
#endif
160
159
161
- if (clients && StreamAsFirmataLen )
160
+ if (clients && firmataStreamLen )
162
161
{
163
- DEBUG (Serial.printf (" Firmata -> WS: %zd bytes" , StreamAsFirmataLen ));
162
+ DEBUG (Serial.printf (" Firmata -> WS: %zd bytes\n " , firmataStreamLen ));
164
163
165
164
static byte tempBuffer [MAX_DATA_BYTES];
166
- if (StreamAsFirmataLen > sizeof (tempBuffer))
167
- StreamAsFirmataLen = sizeof (tempBuffer);
168
- if ( StreamAsFirmata .readBytes (tempBuffer, StreamAsFirmataLen ) == StreamAsFirmataLen
169
- && webSocket.broadcastBIN (tempBuffer, StreamAsFirmataLen ))
165
+ if (firmataStreamLen > sizeof (tempBuffer))
166
+ firmataStreamLen = sizeof (tempBuffer);
167
+ if ( firmataStream .readBytes (tempBuffer, firmataStreamLen ) == firmataStreamLen
168
+ && webSocket.broadcastBIN (tempBuffer, firmataStreamLen ))
170
169
{
171
- DEBUG (Serial.printf (" Successfully broadcasted-to-websocket a binary message of %zd bytes\n " , StreamAsFirmataLen ));
170
+ DEBUG (Serial.printf (" Successfully broadcasted-to-websocket a binary message of %zd bytes\n " , firmataStreamLen ));
172
171
}
173
172
else
174
173
{
175
- DEBUG (Serial.printf (" Error broadcasting-to-websocket a binary message of %zd bytes" , StreamAsFirmataLen ));
174
+ LOG (Serial.printf (" Error broadcasting-to-websocket a binary message of %zd bytes\n " , firmataStreamLen ));
176
175
}
177
176
}
178
177
}
0 commit comments