Skip to content

Commit

Permalink
generalize PRINT/PRINTLN via Serial/USBSerial
Browse files Browse the repository at this point in the history
  • Loading branch information
gin66 committed Aug 25, 2024
1 parent d43012f commit 0a91fad
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 135 deletions.
165 changes: 79 additions & 86 deletions examples/StepperDemo/StepperDemo.ino

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions examples/StepperDemo/generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,25 @@
#endif

#if defined(CONFIG_IDF_TARGET_ESP32C3) && (ARDUINO_USB_MODE == 1)
#define SerialInterface USBSerial
#define PRINT_INIT() \
USBSerial.begin(115200); \
while (!Serial) { \
/* wait for USB serial port to connect */ \
}
#define PRINTLN USBSerial.println
#define PRINT USBSerial.println
#define POLL_CHAR_IF_ANY(ch) \
if (USBSerial.available()) { \
ch = USBSerial.read(); \
}
#else
#define SerialInterface Serial
#define PRINT_INIT() Serial.begin(115200);
#define PRINTLN Serial.println
#define PRINT Serial.println
#define POLL_CHAR_IF_ANY(ch) \
if (Serial.available()) { \
ch = Serial.read(); \
}
#endif

#endif
6 changes: 3 additions & 3 deletions examples/StepperDemo/test_seq_07.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bool test_seq_07(FastAccelStepper *stepper, struct test_seq_s *seq,
case 1:
if ((stepper->rampState() & RAMP_STATE_MASK) == RAMP_STATE_COAST) {
int32_t dt = time_ms - seq->u32_1;
SerialInterface.println(dt);
PRINTLN(dt);
// 779 esp, 805 avr (neu 810 avr), 820: esp32 with rmt, 812: esp32 with
// rmt second channel
if (abs(dt - 792) > 30) {
Expand Down Expand Up @@ -43,11 +43,11 @@ bool test_seq_07(FastAccelStepper *stepper, struct test_seq_s *seq,
case 4:
if (!stepper->isRunning()) {
int32_t dt = time_ms - seq->u32_1;
SerialInterface.println(dt);
PRINTLN(dt);
if (abs(dt - 1495) > 30) {
seq->state = TEST_STATE_ERROR;
}
SerialInterface.println(stepper->getPositionAfterCommandsCompleted());
PRINTLN(stepper->getPositionAfterCommandsCompleted());
if (stepper->getPositionAfterCommandsCompleted() != 0) {
seq->state = TEST_STATE_ERROR;
}
Expand Down
22 changes: 11 additions & 11 deletions examples/StepperDemo/test_seq_08.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool test_seq_08(FastAccelStepper *stepper, struct test_seq_s *seq,
case 0: // INIT
srand(135);
if (!stepper->attachToPulseCounter(7)) {
SerialInterface.println("Error attaching to pulse counter");
PRINTLN("Error attaching to pulse counter");
seq->state = TEST_STATE_ERROR;
return true;
}
Expand All @@ -27,10 +27,10 @@ bool test_seq_08(FastAccelStepper *stepper, struct test_seq_s *seq,
int16_t pcnt = stepper->readPulseCounter();
int32_t spos = stepper->getPositionAfterCommandsCompleted();
if ((pcnt & 0x3fff) != (spos & 0x3fff)) {
SerialInterface.print("stepper pos=");
SerialInterface.print(spos);
SerialInterface.print(" real pos=");
SerialInterface.println(pcnt);
PRINT("stepper pos=");
PRINT(spos);
PRINT(" real pos=");
PRINTLN(pcnt);

seq->state = TEST_STATE_ERROR;
return true;
Expand Down Expand Up @@ -69,12 +69,12 @@ bool test_seq_08(FastAccelStepper *stepper, struct test_seq_s *seq,
move = -move;
}

SerialInterface.print("speed=");
SerialInterface.print(speed);
SerialInterface.print(" accel=");
SerialInterface.print(accel);
SerialInterface.print(" move=");
SerialInterface.println(move);
PRINT("speed=");
PRINT(speed);
PRINT(" accel=");
PRINT(accel);
PRINT(" move=");
PRINTLN(move);
stepper->setSpeedInUs(speed);
stepper->setAcceleration(accel);
stepper->move(move);
Expand Down
16 changes: 8 additions & 8 deletions examples/StepperDemo/test_seq_09.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ bool test_seq_09(FastAccelStepper *stepper, struct test_seq_s *seq,
srand(135);
#if defined(SUPPORT_ESP32_PULSE_COUNTER)
if (!stepper->attachToPulseCounter(7)) {
SerialInterface.println("Error attaching to pulse counter");
PRINTLN("Error attaching to pulse counter");
seq->state = TEST_STATE_ERROR;
return true;
}
Expand All @@ -28,10 +28,10 @@ bool test_seq_09(FastAccelStepper *stepper, struct test_seq_s *seq,
uint32_t accel = rand() % (AMAX * 4);
accel = accel >> ((accel % 4) + 2);
accel = accel + AMIN;
SerialInterface.print("speed=");
SerialInterface.print(speed);
SerialInterface.print(" accel=");
SerialInterface.println(accel);
PRINT("speed=");
PRINT(speed);
PRINT(" accel=");
PRINTLN(accel);
stepper->setSpeedInUs(speed);
stepper->setAcceleration(accel);
if (rand() & 1) {
Expand All @@ -58,8 +58,8 @@ bool test_seq_09(FastAccelStepper *stepper, struct test_seq_s *seq,
#if defined(SUPPORT_ESP32_PULSE_COUNTER)
int16_t old = seq->s16_1;
seq->s16_1 = stepper->readPulseCounter();
SerialInterface.print("Steps needed for stop=");
SerialInterface.println(old - seq->s16_1);
PRINT("Steps needed for stop=");
PRINTLN(old - seq->s16_1);
#endif
seq->state++;
}
Expand All @@ -68,7 +68,7 @@ bool test_seq_09(FastAccelStepper *stepper, struct test_seq_s *seq,
if (time_ms - seq->u32_1 >= 100) {
#if defined(SUPPORT_ESP32_PULSE_COUNTER)
if (seq->s16_1 != stepper->readPulseCounter()) {
SerialInterface.println("Step AFTER stop");
PRINTLN("Step AFTER stop");
}
#endif
seq->u32_1 = time_ms;
Expand Down
6 changes: 3 additions & 3 deletions examples/StepperDemo/test_seq_10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ bool test_seq_10(FastAccelStepper *stepper, struct test_seq_s *seq,
stepper->applySpeedAcceleration();
stepper->stopMove();
} else {
SerialInterface.print("Speed changes 64us <=> ");
SerialInterface.print(64 + seq->s16_1);
SerialInterface.println("us");
PRINT("Speed changes 64us <=> ");
PRINT(64 + seq->s16_1);
PRINTLN("us");
seq->state = 2;
}
}
Expand Down
32 changes: 16 additions & 16 deletions examples/StepperDemo/test_seq_11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ bool test_seq_11(FastAccelStepper *stepper, struct test_seq_s *seq,
for (uint16_t i = 0; i <= 1000; i++) {
int32_t pos = stepper->getCurrentPosition();
if (pos < seq->s32_1) {
SerialInterface.print("i=");
SerialInterface.print(i);
PRINT("i=");
PRINT(i);
if (i != 0) {
SerialInterface.print(" prev pos=");
SerialInterface.print(prev_pos);
PRINT(" prev pos=");
PRINT(prev_pos);
}
SerialInterface.print(" old pos=");
SerialInterface.print(seq->s32_1);
SerialInterface.print(" curr pos=");
SerialInterface.println(pos);
PRINT(" old pos=");
PRINT(seq->s32_1);
PRINT(" curr pos=");
PRINTLN(pos);
stepper->stopMove();
seq->state = TEST_STATE_ERROR;
return true;
Expand All @@ -54,16 +54,16 @@ bool test_seq_11(FastAccelStepper *stepper, struct test_seq_s *seq,
for (uint16_t i = 0; i <= 1000; i++) {
int32_t pos = stepper->getCurrentPosition();
if (pos > seq->s32_1) {
SerialInterface.print("i=");
SerialInterface.print(i);
PRINT("i=");
PRINT(i);
if (i != 0) {
SerialInterface.print(" prev pos=");
SerialInterface.print(prev_pos);
PRINT(" prev pos=");
PRINT(prev_pos);
}
SerialInterface.print(" old pos=");
SerialInterface.print(seq->s32_1);
SerialInterface.print(" curr pos=");
SerialInterface.println(pos);
PRINT(" old pos=");
PRINT(seq->s32_1);
PRINT(" curr pos=");
PRINTLN(pos);
stepper->stopMove();
seq->state = TEST_STATE_ERROR;
return true;
Expand Down
6 changes: 3 additions & 3 deletions examples/StepperDemo/test_seq_12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ bool test_seq_12(FastAccelStepper *stepper, struct test_seq_s *seq,
stepper->applySpeedAcceleration();
stepper->stopMove();
} else {
SerialInterface.print("Speed changes 64us <=> ");
SerialInterface.print(64 + seq->s16_1);
SerialInterface.println("us");
PRINT("Speed changes 64us <=> ");
PRINT(64 + seq->s16_1);
PRINTLN("us");
seq->state = 2;
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/StepperDemo/test_seq_13.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ bool test_seq_13(FastAccelStepper *stepper, struct test_seq_s *seq,
case 3:
res = stepper->addQueueEntry(&cmd_step);
if (res > 1) {
SerialInterface.print(res);
SerialInterface.print(' ');
PRINT(res);
PRINT(' ');
} else {
seq->u32_1 = time_ms;
seq->state++;
Expand All @@ -28,7 +28,7 @@ bool test_seq_13(FastAccelStepper *stepper, struct test_seq_s *seq,
case 4:
if (time_ms - seq->u32_1 >= 20) {
if (stepper->getCurrentPosition() != 4) {
SerialInterface.println("not all raw commands executed");
PRINTLN("not all raw commands executed");
seq->state = TEST_STATE_ERROR;
}
return true;
Expand Down

0 comments on commit 0a91fad

Please sign in to comment.