cewkomator/firmware/src/debug.cpp

137 lines
3.1 KiB
C++

#include <Arduino.h>
#include "constants.h"
#include "buzzer.h"
#include "screen.h"
#include "keyboard.h"
#include "stepper.h"
#include "input.h"
#include "characters.h"
#if DEBUG_I2C
#include <I2CScanner.h>
I2CScanner scanner;
#endif
void setupDebug() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(1500000);
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");
Serial.println(" q - quick accelerate");
} 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();
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());
} else if (key == 'q') {
quickAccelerate();
} else if (key == '\\') {
lcd.clear();
lcd.write(CHAR_INVERTED_A);
lcd.write(CHAR_INVERTED_B);
lcd.write(CHAR_INVERTED_C);
lcd.write(CHAR_INVERTED_D);
lcd.write(CHAR_ENCODER);
lcd.write(CHAR_ENCODER_SWITCH);
}
}
void debugHandleInput(Input input) {
handleDebugFunctions(input.debugKey);
#if DEBUG_PRINT_EVENTS
if (input.key) {
Serial.print("Key pressed: ");
Serial.print(allKeys[input.key]);
auto digit = getPressedDigit(input.key);
if (digit != -1) {
Serial.print(", digit: ");
Serial.print(digit);
} else {
Serial.print(" (Not a digit)");
}
Serial.println();
}
if (input.switchEvent) {
Serial.println(input.switchEvent == SWITCH_PRESSED ? "Pressed" : "Released");
}
if (input.encoderEvent) {
Serial.println(input.encoderEvent == ENCODER_CLOCKWISE ? "Clockwise" : "Counter-clockwise");
}
#endif
}