-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebUSB-I2C.ino
474 lines (429 loc) · 15.7 KB
/
WebUSB-I2C.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* WebUSB-I2C - entering commands in a web-console to control an I2C slave
** 2018 Jul 22 Maarten Pennings
*/
// Assumes
// - Arduino/Genuino Micro
// - An I2C slave connected to SCL/pin3 and SDA/pin2 (e.g. an ENS210)
// - Optionally a FTDI/CH340/CP2101/etc on RX and TX as a debug-console (115200)
// To use
// - Goto https://webusb.github.io/arduino/demos/console for a webpage with a console
// - Click the connect button to connect to the dongle
// - Type 'h' for help in command-console (or 'v' for version)
#define VERSION "v7"
#include <Wire.h> // I2C library
// ==== USB ======================================================================================
// WebUSB only works for USB 2.1 and up. Therefore, change USB_VERSION to 0x210 in file
// %AppData%\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino\USBCore.h
// See https://github.com/webusb/arduino, and also see warning on (semi) bricking.
#include <WebUSB.h>
#if USB_VERSION<0x210
#error USBCore.h file must be patched
#endif
// Creating an instance of WebUSBSerial will add an additional USB interface to
// the device that is marked as vendor-specific (rather than USB CDC-ACM) and
// is therefore accessible to the browser. The URL is a hint to the os/browser.
#define URL "maarten-pennings.github.io/webusb-ens210"
WebUSB WebUSBSerial(1 /* https:// */, URL);
// ==== Serial ===================================================================================
// Pick one for main output (command-console)
//#define MainSerial Serial // Serial over USB (use Arduino COM port)
#define MainSerial WebUSBSerial // Serial over WebUSB
//#define MainSerial Serial1 // Serial over hardware UART 1
// Pick one for debug output (debug-console)
#define DebugSerial Serial // Serial over USB (use Arduino COM port)
//#define DebugSerial WebUSBSerial // Serial over WebUSB
//#define DebugSerial Serial1 // Serial over hardware UART 1
// ==== Serial: helpers for MainSerial ===========================================================
// Shorthands
#define NL "\r\n"
#define NIBBLE(d) (char)( (d) + ((d)<10?'0':'a'-10) )
// Prints one character to MainSerial
void MainSerial_printchar(char ch) {
MainSerial.print(ch);
MainSerial.flush();
}
// Prints one data byte to MainSerial as two lower case hex digits
void MainSerial_printbyte(int d) {
char s[3] = { NIBBLE(d/16), NIBBLE(d%16), 0 };
MainSerial.print(s);
MainSerial.flush();
}
// Prints a string to MainSerial
void MainSerial_printstr(const char * s) {
MainSerial.print(s);
MainSerial.flush();
}
// ==== Serial: helpers for DebugSerial =========================================================
#define PREFIX "dbg: "
//#define PREFIX ""
static int DebugSerial_numchars=0;
static int DebugSerial_numdots=0;
static int DebugSerial_dotcalls=0;
// Prints an 'alive' dot
void DebugSerial_dot(void) {
if( DebugSerial_dotcalls++<2000 )return; else DebugSerial_dotcalls= 0;
if( DebugSerial_numchars>0 ) DebugSerial.print("\n");
if( DebugSerial_numdots==0 ) DebugSerial.print(PREFIX "waiting ..");
DebugSerial.print(".");
DebugSerial_numchars=0;
DebugSerial_numdots++;
if( DebugSerial_numdots==60 ) { DebugSerial.print("\n"); DebugSerial_numdots= 0; }
}
// Prints an (asynchronously) incoming char (typed from MainSerial)
void DebugSerial_trace(char ch) {
if( DebugSerial_numdots>0 ) DebugSerial.print("\n");
if( DebugSerial_numchars==0 ) DebugSerial.print(PREFIX "cmd=");
DebugSerial.print(ch);
DebugSerial_numchars++;
DebugSerial_numdots= 0;
DebugSerial_dotcalls= 0;
}
// Print a string (with 0 data byte arguments)
void DebugSerial_print0(const char * s1) {
if( DebugSerial_numchars>0 || DebugSerial_numdots>0 ) DebugSerial.print("\n");
DebugSerial.print(PREFIX);
DebugSerial.println(s1);
DebugSerial_numchars= 0;
DebugSerial_numdots= 0;
DebugSerial_dotcalls= 0;
}
// Print a string (with 1 data byte argument)
void DebugSerial_print1(const char * s1, int d1, const char * s2) {
if( DebugSerial_numchars>0 || DebugSerial_numdots>0 ) DebugSerial.print("\n");
DebugSerial.print(PREFIX);
DebugSerial.print(s1);
char sd1[3] = { NIBBLE(d1/16), NIBBLE(d1%16), 0 };
DebugSerial.print(sd1);
DebugSerial.println(s2);
DebugSerial_numchars= 0;
DebugSerial_numdots= 0;
DebugSerial_dotcalls= 0;
}
// Print a string (with 2 data byte arguments)
void DebugSerial_print2(const char * s1, int d1, const char * s2, int d2, const char * s3) {
if( DebugSerial_numchars>0 ) DebugSerial.print("\n");
DebugSerial.print(PREFIX);
DebugSerial.print(s1);
char sd1[3] = { NIBBLE(d1/16), NIBBLE(d1%16), 0 };
DebugSerial.print(sd1);
DebugSerial.print(s2);
char sd2[3] = { NIBBLE(d2/16), NIBBLE(d2%16), 0 };
DebugSerial.print(sd2);
DebugSerial.println(s3);
DebugSerial_numchars= 0;
}
// ==== State management =========================================================================
typedef enum state_e {
// In these comments:
// - lower case is an actual char
// - A is any hexchar 0..9,a..f (for a slave address)
// - D is any hexchar 0..9,a..f (for a data byte to be written)
// - C is any hexchar 0..9,a..f (for a count of bytes to be read)
// - X is any of p/w/r, it terminates a segment (p), maybe repeated start (wr), actual transfer of current segment
state_noconsole,// no console connected yet
state_prompt, // print prompt and transition to idle
state_idle, // wait for a new transmission (w,r)
state_wait_wa1, // entered "w", waiting for address nibble 1
state_wait_wa2, // entered "wA", waiting for address nibble 2
state_wait_wd1, // entered "wAA" (or "wAADD", "wAADDDD"), waiting for data nibble 1 - or p/w/r to transition to state_wait_wx
state_wait_wd2, // entered "wAAD", waiting for data nibble 2
state_wait_wx, // entered "wAAX", "wAADDX", "wAADDDDX" etc, transferring DDDD, transition to idle, state_wait_wa1, or state_wait_ra1
state_wait_ra1, // entered "r", waiting for address nibble 1
state_wait_ra2, // entered "rA", waiting for address nibble 2
state_wait_rc1, // entered "rAA", waiting for read count nibble 1
state_wait_rc2, // entered "rAAC", waiting for read count nibble 2
state_wait_rx, // entered "rAACC", waiting p/w/r to start transfer, transition to idle, state_wait_wa1, or state_wait_ra1
} state_t;
int ishex(int ch ) { // returns 1 iff ch is (lower case) hex
return ( '0'<=ch && ch<='9' ) || ( 'a'<=ch && ch<='f' );
}
int hex2dec(int ch) { // converts 0..9,a..f to 0..15; all other chars to -1
if( '0'<=ch && ch<='9' ) return ch-'0';
else if( 'a'<=ch && ch<='f' ) return ch-'a';
else return -1;
}
// ==== Setup/Loop ===============================================================================
// If you have a LED connected to some pin, define LEDPIN, otherwise keep it undefined.
#define LEDPIN 13
state_t state; // The state of the I2C transaction command
int ch; // Character to process
int addr; // The value of AA (once entered)
int data; // The value of DD (once entered)
int count; // The value of CC (once entered)
void setup() {
// LED
#ifdef LEDPIN
pinMode( LEDPIN, OUTPUT);
#endif
// In case DebugSerial is Serial over USB, we need a bit of time for it to be enabled
while( !DebugSerial && !MainSerial && count<200 ) {
count++;
delay(50);
digitalWrite( LEDPIN, ! digitalRead(LEDPIN));
}
// Setup DebugSerial
DebugSerial.begin(115200);
DebugSerial.println("");
DebugSerial.println("");
DebugSerial_print0("Welcome to WebUSB-I2C " VERSION);
DebugSerial_print2("USB version is ", USB_VERSION >> 8,"", USB_VERSION & 0xFF," (must be >=0210)");
// I2C
Wire.begin();
// Set state
state= state_noconsole; // No console yet (for entering commands)
ch= -1; // No char to process
DebugSerial_print0("Waiting for connect from web");
DebugSerial_print0("Visit e.g. https://" URL);
// Toggle LED to show booted
#ifdef LEDPIN
digitalWrite( LEDPIN, DebugSerial || MainSerial );
#endif
}
void loop() {
// Check connection
int wasup= state!=state_noconsole;
int isup= MainSerial;
if( wasup!=isup ) {
if( isup ) {
DebugSerial_print0("Web connected");
MainSerial_printstr("Welcome to WebUSB-I2C " VERSION NL);
state= state_prompt;
} else {
DebugSerial_print0("Web disconnected");
state= state_noconsole;
}
}
// Get command char (except when previous char has not yet been executed)
if( state!=state_noconsole && ch==-1 && MainSerial.available() ) {
ch = MainSerial.read();
if( ch=='\0' )
ch=-1; // Don't know what to do with a 0
else {
DebugSerial_trace((char)ch);
#ifdef LEDPIN
digitalWrite( LEDPIN, ! digitalRead(LEDPIN));
#endif
}
}
// Alive
DebugSerial_dot();
// State processing
switch( state ) {
case state_noconsole:
// Skip
break;
case state_prompt:
MainSerial_printstr(NL ">");
state= state_idle;
break; // transition without reading a new char (keep old 'ch')
case state_idle:
if( ch==-1 ) {
// skip
} else if( ch=='w' ) {
MainSerial_printchar('w');
state= state_wait_wa1;
} else if( ch=='r' ) {
MainSerial_printchar('r');
state= state_wait_ra1;
} else if( ch=='h' ) {
DebugSerial_print0("Help");
MainSerial_printchar('h');
MainSerial_printstr(NL "(wAA(DD)*|rAACC)+p where A, D, C are hex nibbles for address, data, count");
state= state_prompt;
} else if( ch=='v' ) {
DebugSerial_print0("Version");
MainSerial_printchar('v');
MainSerial_printstr(NL VERSION);
state= state_prompt;
} else {
DebugSerial_print1("Error in idle (ch=",ch,")");
MainSerial_printchar('X');
state= state_prompt;
}
ch= -1; // mark as processed
break;
case state_wait_wa1:
if( ch==-1 ) {
// skip
} else if( ishex(ch) ) {
MainSerial_printchar(ch);
addr= hex2dec(ch);
state= state_wait_wa2;
} else {
DebugSerial_print1("Error in wa1 (ch=",ch,")");
MainSerial_printchar('A');
state= state_prompt;
}
ch= -1; // mark as processed
break;
case state_wait_wa2:
if( ch==-1 ) {
// skip
} else if( ishex(ch) ) {
MainSerial_printchar(ch);
addr= addr*16+hex2dec(ch);
state= state_wait_wd1;
Wire.beginTransmission(addr); // Returns nothing
DebugSerial_print1("beginTransmission(",addr,")");
} else {
DebugSerial_print1("Error in wa2 (ch=",ch,")");
MainSerial_printchar('A');
state= state_prompt;
}
ch= -1; // mark as processed
break;
case state_wait_wd1:
if( ch==-1 ) {
// skip
} else if( ishex(ch) ) {
MainSerial_printchar(ch);
data= hex2dec(ch);
state= state_wait_wd2;
} else if( ch=='w' || ch=='r' || ch=='p' ) {
state= state_wait_wx;
break; // transition without reading a new char (keep old 'ch')
} else {
DebugSerial_print1("Error in wd1 (ch=",ch,")");
MainSerial_printchar('D');
state= state_prompt;
}
ch= -1; // mark as processed
break;
case state_wait_wd2:
if( ch==-1 ) {
// skip
} else if( ishex(ch) ) {
MainSerial_printchar(ch);
data= data*16+hex2dec(ch);
state= state_wait_wd1;
Wire.write(data); // returns number of bytes written, ignored
DebugSerial_print1("write(",data,")");
} else {
DebugSerial_print1("Error in wd1 (ch=",ch,")");
MainSerial_printchar('D');
state= state_prompt;
}
ch= -1; // mark as processed
break;
case state_wait_wx:
if( ch==-1 ) {
// skip
} else if( ch=='w' ) {
int result=Wire.endTransmission(false);
DebugSerial_print1("endTransmission(false) result=",result,"");
MainSerial_printchar('[');
MainSerial_printbyte(result);
MainSerial_printchar(']');
MainSerial_printchar('w');
state= state_wait_wa1;
} else if( ch=='r' ) {
int result=Wire.endTransmission(false);
DebugSerial_print1("endTransmission(false) result=",result,"");
MainSerial_printchar('[');
MainSerial_printbyte(result);
MainSerial_printchar(']');
MainSerial_printchar('r');
state= state_wait_ra1;
} else if( ch=='p' ) {
int result=Wire.endTransmission(true);
DebugSerial_print1("endTransmission(true) result=",result,"");
MainSerial_printchar('p');
MainSerial_printchar('[');
MainSerial_printbyte(result);
MainSerial_printchar(']');
state= state_prompt;
} // No other case possible, see 'case state_wait_wd1'
ch= -1; // mark as processed
break;
case state_wait_ra1:
if( ch==-1 ) {
// skip
} else if( ishex(ch) ) {
MainSerial_printchar(ch);
addr= hex2dec(ch);
state= state_wait_ra2;
} else {
DebugSerial_print1("Error in ra1 (ch=",ch,")");
MainSerial_printchar('A');
state= state_prompt;
}
ch= -1; // mark as processed
break;
case state_wait_ra2:
if( ch==-1 ) {
// skip
} else if( ishex(ch) ) {
MainSerial_printchar(ch);
addr= addr*16+hex2dec(ch);
state= state_wait_rc1;
} else {
DebugSerial_print1("Error in ra2 (ch=",ch,")");
MainSerial_printchar('A');
state= state_prompt;
}
ch= -1; // mark as processed
break;
case state_wait_rc1:
if( ch==-1 ) {
// skip
} else if( ishex(ch) ) {
MainSerial_printchar(ch);
count= hex2dec(ch);
state= state_wait_rc2;
} else {
DebugSerial_print1("Error in wrc1 (ch=",ch,")");
MainSerial_printchar('C');
state= state_prompt;
}
ch= -1; // mark as processed
break;
case state_wait_rc2:
if( ch==-1 ) {
// skip
} else if( ishex(ch) ) {
MainSerial_printchar(ch);
count= count*16+hex2dec(ch);
state= state_wait_rx;
} else {
DebugSerial_print1("Error in rc2 (ch=",ch,")");
MainSerial_printchar('C');
state= state_prompt;
}
ch= -1; // mark as processed
break;
case state_wait_rx:
if( ch==-1 ) {
// skip
} else if( ch=='w' ) {
Wire.requestFrom(addr,count,false); // Returns number of bytes read - ignored, done by read loop
DebugSerial_print2("requestFrom(",addr,",",count,",false)");
MainSerial_printchar('[');
while( Wire.available() ) MainSerial_printbyte(Wire.read());
MainSerial_printchar(']');
MainSerial_printchar('w');
state= state_wait_wa1;
} else if( ch=='r' ) {
Wire.requestFrom(addr,count,false); // Returns number of bytes read - ignored, done by read loop
DebugSerial_print2("requestFrom(",addr,",",count,",false)");
MainSerial_printchar('[');
while( Wire.available() ) MainSerial_printbyte(Wire.read());
MainSerial_printchar(']');
MainSerial_printchar('r');
state= state_wait_ra1;
} else if( ch=='p' ) {
Wire.requestFrom(addr,count); // Returns number of bytes read - ignored, done by read loop
DebugSerial_print2("requestFrom(",addr,",",count,",true)");
MainSerial_printchar('p');
MainSerial_printchar('[');
while( Wire.available() ) MainSerial_printbyte(Wire.read());
MainSerial_printchar(']');
state= state_prompt;
} else {
DebugSerial_print1("Error in rx (ch=",ch,")");
MainSerial_printchar('X');
state= state_prompt;
}
ch= -1; // mark as processed
break;
}
}