diff --git a/common/constants.h b/common/constants.h index d453b9d..f1d044e 100644 --- a/common/constants.h +++ b/common/constants.h @@ -20,7 +20,6 @@ const uint8_t COL_PINS[] = 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 12, 11, 10, 9, 8, 7, 6 }; - const uint8_t ROW_PINS[] = { 5, 4, 3, 2, 1, 0 @@ -31,4 +30,6 @@ const uint16_t CHECKSUM_PERIOD = 65500; const uint32_t DEFAULT_BLINK_PERIOD = 500; const uint32_t DEFAULT_FADE_TIME = 1000; +static const char* KEYMAP_FILENAME = "Configuration.zkb"; + } diff --git a/core/backlight/backlight.cpp b/core/backlight/backlight.cpp index edf8318..5cd7f02 100644 --- a/core/backlight/backlight.cpp +++ b/core/backlight/backlight.cpp @@ -43,4 +43,13 @@ void Backlight::signal_failure() } } +void Backlight::signal_success() +{ + for (uint8_t i = 0; i < common::constants::TOTAL_NUM_LEDS; ++i) + { + LEDState& state = led_states[i]; + state.start_fade(device, colors::GREEN, 10000); + } +} + } diff --git a/core/backlight/backlight.h b/core/backlight/backlight.h index c90bbe3..1f4b570 100644 --- a/core/backlight/backlight.h +++ b/core/backlight/backlight.h @@ -19,6 +19,7 @@ class Backlight void increment_scheme(); void signal_failure(); + void signal_success(); private: Color update_fade(LEDState& state); diff --git a/core/firmware.cpp b/core/firmware.cpp index 43efce8..d53460b 100644 --- a/core/firmware.cpp +++ b/core/firmware.cpp @@ -15,16 +15,23 @@ Firmware::Firmware(Device& device) : wave{device}, backlight{device, schemes, 1} -{ - const bool success = keymap.load_from_sd_else_default(device); - if (!success) - { - backlight.signal_failure(); - } -} +{ } void Firmware::update() { + if (!loaded_keymap) + { + const bool success = keymap.load_from_sd_else_default(device); + if (success) + { + backlight.signal_success(); + } + else + { + backlight.signal_failure(); + } + loaded_keymap = true; + } device.start_timer(); keyboard::KeyboardScanResult result; key_scanner.scan(result); @@ -37,6 +44,24 @@ void Firmware::update() { device.sleep_micros(CYCLE_TIME_MICROS - elapsed); } + + // stty 9600 -F /dev/ttyACM0 + if (device.serial_data_available()) + { + char* buffer; + uint32_t num_read_bytes; + device.serial_read(buffer, num_read_bytes); + const bool success = device.sd_write(common::constants::KEYMAP_FILENAME, buffer, num_read_bytes); + delete[] buffer; + if (success) + { + backlight.signal_success(); + } + else + { + backlight.signal_failure(); + } + } } } diff --git a/core/firmware.h b/core/firmware.h index 8977ab6..79c449d 100644 --- a/core/firmware.h +++ b/core/firmware.h @@ -25,6 +25,8 @@ class Firmware keyboard::KeyQueue key_queue; keyboard::KeyMap keymap; + bool loaded_keymap = false; + // Backlight schemes backlight::schemes::Wave wave; diff --git a/core/keyboard/keymap.cpp b/core/keyboard/keymap.cpp index f449162..400569a 100644 --- a/core/keyboard/keymap.cpp +++ b/core/keyboard/keymap.cpp @@ -5,10 +5,6 @@ namespace core::keyboard { - -const char* KEYMAP_FILENAME = "Configuration.zkb"; - - void KeyReport::add_key(uint16_t key) { if (util::key_is_valid_standard_key(key)) @@ -296,7 +292,8 @@ bool KeyMap::load_from_sd_else_default(Device& device) { char* buffer; uint32_t num_read_bytes; - const bool success = device.sd_read(KEYMAP_FILENAME, buffer, num_read_bytes); + const bool success = device.sd_read(common::constants::KEYMAP_FILENAME, + buffer, num_read_bytes); if (success) { const uint16_t* data = reinterpret_cast(buffer); diff --git a/device.h b/device.h index b6d2a22..e130fd9 100644 --- a/device.h +++ b/device.h @@ -39,6 +39,21 @@ class Device virtual void set_keyboard_media(const uint16_t media) = 0; virtual void keyboard_send() = 0; + /** + * Check if there is data available to read from the serial port. + * + * @return true if there is data available to read, false otherwise. + */ + virtual bool serial_data_available() = 0; + + /** + * Read data from the serial port. + * + * @param buffer A pointer to a buffer that will be allocated and filled with the data read from the serial port. Remember to delete[] this buffer when you're done with it. + * @param num_read_bytes The number of bytes read from the serial port. + */ + virtual void serial_read(char*& buffer, uint32_t& num_read_bytes) = 0; + /** * Initialize the SD card. * diff --git a/simulator/simulator_device.h b/simulator/simulator_device.h index e488c62..21fdc7f 100644 --- a/simulator/simulator_device.h +++ b/simulator/simulator_device.h @@ -31,6 +31,8 @@ class SimulatorDevice : public Device virtual void set_keyboard_media(const uint16_t media) override; virtual void keyboard_send() override; + virtual bool serial_data_available() override { return false; } + virtual void serial_read(char*&, uint32_t&) override { } virtual bool sd_init() override; virtual bool sd_read(const char* filename, char*& buffer, uint32_t& num_read_bytes) const override; diff --git a/teensy_device.cpp b/teensy_device.cpp index fc01a82..e1262dc 100644 --- a/teensy_device.cpp +++ b/teensy_device.cpp @@ -49,71 +49,85 @@ void TeensyDevice::serial_begin(const uint32_t baud) void TeensyDevice::serial_print(const char* str) { Serial.print(str); + Serial.flush(); } void TeensyDevice::serial_print(uint8_t val) { Serial.print(val); + Serial.flush(); } void TeensyDevice::serial_print(uint16_t val) { Serial.print(val); + Serial.flush(); } void TeensyDevice::serial_print(uint32_t val) { Serial.print(val); + Serial.flush(); } void TeensyDevice::serial_print(int8_t val) { Serial.print(val); + Serial.flush(); } void TeensyDevice::serial_print(int16_t val) { Serial.print(val); + Serial.flush(); } void TeensyDevice::serial_print(int32_t val) { Serial.print(val); + Serial.flush(); } void TeensyDevice::serial_println(const char* str) { Serial.println(str); + Serial.flush(); } void TeensyDevice::serial_println(uint8_t val) { Serial.println(val); + Serial.flush(); } void TeensyDevice::serial_println(uint16_t val) { Serial.println(val); + Serial.flush(); } void TeensyDevice::serial_println(uint32_t val) { Serial.println(val); + Serial.flush(); } void TeensyDevice::serial_println(int8_t val) { Serial.println(val); + Serial.flush(); } void TeensyDevice::serial_println(int16_t val) { Serial.println(val); + Serial.flush(); } void TeensyDevice::serial_println(int32_t val) { Serial.println(val); + Serial.flush(); } void TeensyDevice::set_keyboard_key1(const uint8_t code) @@ -161,6 +175,20 @@ void TeensyDevice::keyboard_send() Keyboard.send_now(); } +bool TeensyDevice::serial_data_available() +{ + return Serial.available() > 0; +} + +void TeensyDevice::serial_read(char*& buffer, uint32_t& num_read_bytes) +{ + num_read_bytes = Serial.available(); + serial_print("num_read_bytes: "); + serial_println(num_read_bytes); + buffer = new char[num_read_bytes]; + Serial.readBytes(buffer, num_read_bytes); +} + bool TeensyDevice::sd_init() { return SD.begin(BUILTIN_SDCARD); @@ -180,12 +208,20 @@ bool TeensyDevice::sd_read(const char* filename, char*& buffer, uint32_t& num_re return false; } -bool TeensyDevice::sd_write(const char* filename, const char* buffer, const uint32_t numBytes) +bool TeensyDevice::sd_write(const char* filename, const char* buffer, const uint32_t num_bytes) { + if (SD.exists(filename)) + { + const bool removed = SD.remove(filename); + if (!removed) + { + return false; + } + } File file = SD.open(filename, FILE_WRITE); if (file) { - file.write(buffer, numBytes); + file.write(buffer, num_bytes); file.close(); return true; } diff --git a/teensy_device.h b/teensy_device.h index 4809b66..98b44cf 100644 --- a/teensy_device.h +++ b/teensy_device.h @@ -27,9 +27,12 @@ class TeensyDevice : public Device virtual void set_keyboard_media(const uint16_t media) override; virtual void keyboard_send() override; + virtual bool serial_data_available() override; + virtual void serial_read(char*& buffer, uint32_t& num_read_bytes) override; + virtual bool sd_init() override; - virtual bool sd_read(const char* filename, char*& buffer, uint32_t& numReadBytes) const override; - virtual bool sd_write(const char* filename, const char* buffer, const uint32_t numBytes) override; + virtual bool sd_read(const char* filename, char*& buffer, uint32_t& num_read_bytes) const override; + virtual bool sd_write(const char* filename, const char* buffer, const uint32_t num_bytes) override; virtual uint16_t get_keyboard_leds() override; virtual void serial_begin(const uint32_t baud) override; diff --git a/teensy_firmware.cpp b/teensy_firmware.cpp index 90d91c8..ce4a956 100644 --- a/teensy_firmware.cpp +++ b/teensy_firmware.cpp @@ -16,6 +16,7 @@ core::Firmware firmware{device}; void setup() { + device.sd_init(); octo.begin(); pcontroller = new CTeensy4Controller(&octo); FastLED.setBrightness(255); @@ -25,6 +26,7 @@ void setup() FastLED.setMaxPowerInVoltsAndMilliamps(5, 900); delay(50); FastLED.show(); + device.serial_begin(9600); } void loop()