cewkomator/firmware/src/debug.cpp

94 lines
2.1 KiB
C++

#include <Arduino.h>
#if DEBUG_I2C
#include <I2CScanner.h>
I2CScanner scanner;
#endif
#include "constants.h"
#include "buzzer.h"
#include "screen.h"
#include "keyboard.h"
#include "stepper.h"
void setupDebug() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(2000000);
Serial.println("it's cewk-o-mator, hello!");
#if DEBUG_I2C
scanner.Init();
#endif
}
void debugScanI2C() {
#if DEBUG_I2C
scanner.Scan();
#endif
}
void fatal() {
Serial.println("Fatal error encountered. Sad trombone...");
while (true) {
sadBuzz();
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
}
}
char getDebugKey() {
if (Serial.available() > 0) {
return Serial.read();
}
return 0;
}
void handleDebugFunctions(char key) {
if (key == 'h') {
Serial.println("Debug functions:");
Serial.println(" h - print this help");
Serial.println(" s - play sounds");
Serial.println(" i - timing test");
Serial.println(" l - change stepper direction to left");
Serial.println(" r - change stepper direction to right");
Serial.println(" - - toggle stepper timer");
} else if (key == 's') {
happyBuzz();
delay(500);
sadBuzz();
delay(500);
beep();
delay(500);
tick();
} else if (key == 'i') {
auto micros1 = micros();
Serial.println("Hello, world!");
auto micros2 = micros();
Serial.print("Serial print time (microseconds): ");
Serial.println(micros2 - micros1);
micros1 = micros();
lcd.print("Hello, world!");
micros2 = micros();
Serial.print("LCD print time (microseconds): ");
Serial.println(micros2 - micros1);
micros1 = micros();
auto key = handleKeyboard();
micros2 = micros();
Serial.print("Keyboard scan time (microseconds): ");
Serial.println(micros2 - micros1);
} else if (key == 'l') {
setStepperDirection(LEFT);
} else if (key == 'r') {
setStepperDirection(RIGHT);
} else if (key == '-') {
setTimerEnabled(!isTimerEnabled());
}
}