Skip to content

Commit f5ad0ad

Browse files
authored
USB Debugging Tutorial (#1162)
* Checkpoint - Work in progress * Checkpoint - Serial tutorial * Checkpoint - Device lookup * Checkpoint - Device ID and ICCID lookup functions * Checkpoint - Work in progress before refactoring listening mode support * Checkpoint - Code cleanup [ci skip] * USB Serial Debug Tutorial
1 parent ce58c52 commit f5ad0ad

20 files changed

+2090
-5
lines changed

src/assets/files/UsbSerialSimple.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "Particle.h"
2+
3+
SerialLogHandler logHandler;
4+
5+
SYSTEM_THREAD(ENABLED);
6+
7+
const std::chrono::milliseconds logPeriod = 5s;
8+
unsigned long lastLog;
9+
int counter;
10+
11+
void setup()
12+
{
13+
}
14+
15+
void loop()
16+
{
17+
if (Network.listening())
18+
{
19+
// If we are in listening mode (blinking dark blue), don't
20+
// output by USB serial, because it can conflict with
21+
// serial commands.
22+
return;
23+
}
24+
25+
if (millis() - lastLog >= logPeriod.count())
26+
{
27+
lastLog = millis();
28+
29+
Log.info("counter=%d", ++counter);
30+
}
31+
}

src/assets/files/UsbSerialTwoWay.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "Particle.h"
2+
3+
SerialLogHandler logHandler;
4+
5+
SYSTEM_THREAD(ENABLED);
6+
7+
const std::chrono::milliseconds logPeriod = 5s;
8+
unsigned long lastLog;
9+
int counter;
10+
11+
const size_t BUF_SIZE = 128;
12+
char buf[BUF_SIZE];
13+
size_t bufOffset = 0;
14+
15+
void setup()
16+
{
17+
}
18+
19+
void loop()
20+
{
21+
if (Network.listening())
22+
{
23+
// If we are in listening mode (blinking dark blue), don't
24+
// output by USB serial, because it can conflict with
25+
// serial commands.
26+
return;
27+
}
28+
29+
if (millis() - lastLog >= logPeriod.count())
30+
{
31+
lastLog = millis();
32+
33+
Log.info("counter=%d", ++counter);
34+
}
35+
36+
while (Serial.available())
37+
{
38+
char c = (char)Serial.read();
39+
if (c == '\r' || c == '\n')
40+
{
41+
if (bufOffset > 0)
42+
{
43+
buf[bufOffset] = 0;
44+
Log.info("received %s", buf);
45+
}
46+
bufOffset = 0;
47+
}
48+
else
49+
{
50+
if ((bufOffset + 1) < (BUF_SIZE - 1))
51+
{
52+
buf[bufOffset++] = c;
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)