Skip to content

Commit 0a91fad

Browse files
committed
generalize PRINT/PRINTLN via Serial/USBSerial
1 parent d43012f commit 0a91fad

File tree

9 files changed

+144
-135
lines changed

9 files changed

+144
-135
lines changed

examples/StepperDemo/StepperDemo.ino

Lines changed: 79 additions & 86 deletions
Large diffs are not rendered by default.

examples/StepperDemo/generic.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,25 @@
1414
#endif
1515

1616
#if defined(CONFIG_IDF_TARGET_ESP32C3) && (ARDUINO_USB_MODE == 1)
17-
#define SerialInterface USBSerial
17+
#define PRINT_INIT() \
18+
USBSerial.begin(115200); \
19+
while (!Serial) { \
20+
/* wait for USB serial port to connect */ \
21+
}
22+
#define PRINTLN USBSerial.println
23+
#define PRINT USBSerial.println
24+
#define POLL_CHAR_IF_ANY(ch) \
25+
if (USBSerial.available()) { \
26+
ch = USBSerial.read(); \
27+
}
1828
#else
19-
#define SerialInterface Serial
29+
#define PRINT_INIT() Serial.begin(115200);
30+
#define PRINTLN Serial.println
31+
#define PRINT Serial.println
32+
#define POLL_CHAR_IF_ANY(ch) \
33+
if (Serial.available()) { \
34+
ch = Serial.read(); \
35+
}
2036
#endif
2137

2238
#endif

examples/StepperDemo/test_seq_07.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bool test_seq_07(FastAccelStepper *stepper, struct test_seq_s *seq,
1414
case 1:
1515
if ((stepper->rampState() & RAMP_STATE_MASK) == RAMP_STATE_COAST) {
1616
int32_t dt = time_ms - seq->u32_1;
17-
SerialInterface.println(dt);
17+
PRINTLN(dt);
1818
// 779 esp, 805 avr (neu 810 avr), 820: esp32 with rmt, 812: esp32 with
1919
// rmt second channel
2020
if (abs(dt - 792) > 30) {
@@ -43,11 +43,11 @@ bool test_seq_07(FastAccelStepper *stepper, struct test_seq_s *seq,
4343
case 4:
4444
if (!stepper->isRunning()) {
4545
int32_t dt = time_ms - seq->u32_1;
46-
SerialInterface.println(dt);
46+
PRINTLN(dt);
4747
if (abs(dt - 1495) > 30) {
4848
seq->state = TEST_STATE_ERROR;
4949
}
50-
SerialInterface.println(stepper->getPositionAfterCommandsCompleted());
50+
PRINTLN(stepper->getPositionAfterCommandsCompleted());
5151
if (stepper->getPositionAfterCommandsCompleted() != 0) {
5252
seq->state = TEST_STATE_ERROR;
5353
}

examples/StepperDemo/test_seq_08.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ bool test_seq_08(FastAccelStepper *stepper, struct test_seq_s *seq,
1515
case 0: // INIT
1616
srand(135);
1717
if (!stepper->attachToPulseCounter(7)) {
18-
SerialInterface.println("Error attaching to pulse counter");
18+
PRINTLN("Error attaching to pulse counter");
1919
seq->state = TEST_STATE_ERROR;
2020
return true;
2121
}
@@ -27,10 +27,10 @@ bool test_seq_08(FastAccelStepper *stepper, struct test_seq_s *seq,
2727
int16_t pcnt = stepper->readPulseCounter();
2828
int32_t spos = stepper->getPositionAfterCommandsCompleted();
2929
if ((pcnt & 0x3fff) != (spos & 0x3fff)) {
30-
SerialInterface.print("stepper pos=");
31-
SerialInterface.print(spos);
32-
SerialInterface.print(" real pos=");
33-
SerialInterface.println(pcnt);
30+
PRINT("stepper pos=");
31+
PRINT(spos);
32+
PRINT(" real pos=");
33+
PRINTLN(pcnt);
3434

3535
seq->state = TEST_STATE_ERROR;
3636
return true;
@@ -69,12 +69,12 @@ bool test_seq_08(FastAccelStepper *stepper, struct test_seq_s *seq,
6969
move = -move;
7070
}
7171

72-
SerialInterface.print("speed=");
73-
SerialInterface.print(speed);
74-
SerialInterface.print(" accel=");
75-
SerialInterface.print(accel);
76-
SerialInterface.print(" move=");
77-
SerialInterface.println(move);
72+
PRINT("speed=");
73+
PRINT(speed);
74+
PRINT(" accel=");
75+
PRINT(accel);
76+
PRINT(" move=");
77+
PRINTLN(move);
7878
stepper->setSpeedInUs(speed);
7979
stepper->setAcceleration(accel);
8080
stepper->move(move);

examples/StepperDemo/test_seq_09.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ bool test_seq_09(FastAccelStepper *stepper, struct test_seq_s *seq,
77
srand(135);
88
#if defined(SUPPORT_ESP32_PULSE_COUNTER)
99
if (!stepper->attachToPulseCounter(7)) {
10-
SerialInterface.println("Error attaching to pulse counter");
10+
PRINTLN("Error attaching to pulse counter");
1111
seq->state = TEST_STATE_ERROR;
1212
return true;
1313
}
@@ -28,10 +28,10 @@ bool test_seq_09(FastAccelStepper *stepper, struct test_seq_s *seq,
2828
uint32_t accel = rand() % (AMAX * 4);
2929
accel = accel >> ((accel % 4) + 2);
3030
accel = accel + AMIN;
31-
SerialInterface.print("speed=");
32-
SerialInterface.print(speed);
33-
SerialInterface.print(" accel=");
34-
SerialInterface.println(accel);
31+
PRINT("speed=");
32+
PRINT(speed);
33+
PRINT(" accel=");
34+
PRINTLN(accel);
3535
stepper->setSpeedInUs(speed);
3636
stepper->setAcceleration(accel);
3737
if (rand() & 1) {
@@ -58,8 +58,8 @@ bool test_seq_09(FastAccelStepper *stepper, struct test_seq_s *seq,
5858
#if defined(SUPPORT_ESP32_PULSE_COUNTER)
5959
int16_t old = seq->s16_1;
6060
seq->s16_1 = stepper->readPulseCounter();
61-
SerialInterface.print("Steps needed for stop=");
62-
SerialInterface.println(old - seq->s16_1);
61+
PRINT("Steps needed for stop=");
62+
PRINTLN(old - seq->s16_1);
6363
#endif
6464
seq->state++;
6565
}
@@ -68,7 +68,7 @@ bool test_seq_09(FastAccelStepper *stepper, struct test_seq_s *seq,
6868
if (time_ms - seq->u32_1 >= 100) {
6969
#if defined(SUPPORT_ESP32_PULSE_COUNTER)
7070
if (seq->s16_1 != stepper->readPulseCounter()) {
71-
SerialInterface.println("Step AFTER stop");
71+
PRINTLN("Step AFTER stop");
7272
}
7373
#endif
7474
seq->u32_1 = time_ms;

examples/StepperDemo/test_seq_10.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ bool test_seq_10(FastAccelStepper *stepper, struct test_seq_s *seq,
5050
stepper->applySpeedAcceleration();
5151
stepper->stopMove();
5252
} else {
53-
SerialInterface.print("Speed changes 64us <=> ");
54-
SerialInterface.print(64 + seq->s16_1);
55-
SerialInterface.println("us");
53+
PRINT("Speed changes 64us <=> ");
54+
PRINT(64 + seq->s16_1);
55+
PRINTLN("us");
5656
seq->state = 2;
5757
}
5858
}

examples/StepperDemo/test_seq_11.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ bool test_seq_11(FastAccelStepper *stepper, struct test_seq_s *seq,
2828
for (uint16_t i = 0; i <= 1000; i++) {
2929
int32_t pos = stepper->getCurrentPosition();
3030
if (pos < seq->s32_1) {
31-
SerialInterface.print("i=");
32-
SerialInterface.print(i);
31+
PRINT("i=");
32+
PRINT(i);
3333
if (i != 0) {
34-
SerialInterface.print(" prev pos=");
35-
SerialInterface.print(prev_pos);
34+
PRINT(" prev pos=");
35+
PRINT(prev_pos);
3636
}
37-
SerialInterface.print(" old pos=");
38-
SerialInterface.print(seq->s32_1);
39-
SerialInterface.print(" curr pos=");
40-
SerialInterface.println(pos);
37+
PRINT(" old pos=");
38+
PRINT(seq->s32_1);
39+
PRINT(" curr pos=");
40+
PRINTLN(pos);
4141
stepper->stopMove();
4242
seq->state = TEST_STATE_ERROR;
4343
return true;
@@ -54,16 +54,16 @@ bool test_seq_11(FastAccelStepper *stepper, struct test_seq_s *seq,
5454
for (uint16_t i = 0; i <= 1000; i++) {
5555
int32_t pos = stepper->getCurrentPosition();
5656
if (pos > seq->s32_1) {
57-
SerialInterface.print("i=");
58-
SerialInterface.print(i);
57+
PRINT("i=");
58+
PRINT(i);
5959
if (i != 0) {
60-
SerialInterface.print(" prev pos=");
61-
SerialInterface.print(prev_pos);
60+
PRINT(" prev pos=");
61+
PRINT(prev_pos);
6262
}
63-
SerialInterface.print(" old pos=");
64-
SerialInterface.print(seq->s32_1);
65-
SerialInterface.print(" curr pos=");
66-
SerialInterface.println(pos);
63+
PRINT(" old pos=");
64+
PRINT(seq->s32_1);
65+
PRINT(" curr pos=");
66+
PRINTLN(pos);
6767
stepper->stopMove();
6868
seq->state = TEST_STATE_ERROR;
6969
return true;

examples/StepperDemo/test_seq_12.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ bool test_seq_12(FastAccelStepper *stepper, struct test_seq_s *seq,
5151
stepper->applySpeedAcceleration();
5252
stepper->stopMove();
5353
} else {
54-
SerialInterface.print("Speed changes 64us <=> ");
55-
SerialInterface.print(64 + seq->s16_1);
56-
SerialInterface.println("us");
54+
PRINT("Speed changes 64us <=> ");
55+
PRINT(64 + seq->s16_1);
56+
PRINTLN("us");
5757
seq->state = 2;
5858
}
5959
}

examples/StepperDemo/test_seq_13.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ bool test_seq_13(FastAccelStepper *stepper, struct test_seq_s *seq,
1818
case 3:
1919
res = stepper->addQueueEntry(&cmd_step);
2020
if (res > 1) {
21-
SerialInterface.print(res);
22-
SerialInterface.print(' ');
21+
PRINT(res);
22+
PRINT(' ');
2323
} else {
2424
seq->u32_1 = time_ms;
2525
seq->state++;
@@ -28,7 +28,7 @@ bool test_seq_13(FastAccelStepper *stepper, struct test_seq_s *seq,
2828
case 4:
2929
if (time_ms - seq->u32_1 >= 20) {
3030
if (stepper->getCurrentPosition() != 4) {
31-
SerialInterface.println("not all raw commands executed");
31+
PRINTLN("not all raw commands executed");
3232
seq->state = TEST_STATE_ERROR;
3333
}
3434
return true;

0 commit comments

Comments
 (0)