/** * ESP32 Morse decoder using trained machine learning model */ // DEFINES // IS_TRAINING should be defined when gathering training data // Otherwise, comment out to use the model to classify inputs // #define IS_TRAINING // INCLUDES // See https://github.com/LennartHennigs/Button2 #include "src/Button2.h" // Uncomment when doing classification #ifndef IS_TRAINING #include "model.h" #endif // CONSTANTS // This pin will be used to read dots and dashes - could be connected to any digital input that can be sustained - e.g. a mic, light sensor, or button const byte inputPin = 16; // This pin will be driven HIGH when input is being received const byte buzzerPin = 5; // This LED will light up when receiving character input const byte ledPin = 2; // Every letter can be represented by a maximum of four Morse dots/dashes (if including numbers, this needs to increase to five) const byte patternLength = 4; // The maximum allowed space between "dots" and "dashes" within the same character // If the time elapsed since last input was received exceeds this value, we'll move onto the next character const int intraCharacterPause = 800; // GLOBALS // A button object Button2 button; // The duration of inputs received in the current pattern float pattern[patternLength]; // The time at which the last input was received unsigned long lastReleaseTime; // Counter of how many elements have been received in the current pattern uint8_t counter; // If we're not training... #ifndef IS_TRAINING // Grab a reference to the model's classifier function exported from SciKit-Learn Eloquent::ML::Port::RandomForest classifier; #endif void setup() { // Initialise serial monitor connection Serial.begin(115200); Serial.println(__FILE__ __DATE__); // Initialise LED and buzzer pins used for user feedback pinMode(buzzerPin, OUTPUT); digitalWrite(buzzerPin, LOW); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); // Configure the button input // Anything less than 30ms we won't register as an input at all button.setDebounceTime(30); button.setPressedHandler(onButtonPress); button.setReleasedHandler(onButtonRelease); button.begin(inputPin); } // Called when a complete character pattern has been received void onCharacterReceive() { // If we're gathering training data #ifdef IS_TRAINING // Send the pattern input to the serial monitor for(int i=0; i 0 && millis() - lastReleaseTime > intraCharacterPause) { // Assume the character has ended onCharacterReceive(); } }