Skip to content

Commit

Permalink
Sending and saving the configuration now works
Browse files Browse the repository at this point in the history
  • Loading branch information
malcx95 committed Mar 26, 2024
1 parent 371b78c commit af66300
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 17 deletions.
3 changes: 2 additions & 1 deletion common/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";

}
9 changes: 9 additions & 0 deletions core/backlight/backlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
1 change: 1 addition & 0 deletions core/backlight/backlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Backlight
void increment_scheme();

void signal_failure();
void signal_success();

private:
Color update_fade(LEDState& state);
Expand Down
39 changes: 32 additions & 7 deletions core/firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
}
}

}
2 changes: 2 additions & 0 deletions core/firmware.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Firmware
keyboard::KeyQueue key_queue;
keyboard::KeyMap keymap;

bool loaded_keymap = false;

// Backlight schemes
backlight::schemes::Wave wave;

Expand Down
7 changes: 2 additions & 5 deletions core/keyboard/keymap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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<const uint16_t*>(buffer);
Expand Down
15 changes: 15 additions & 0 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions simulator/simulator_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 38 additions & 2 deletions teensy_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions teensy_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions teensy_firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ core::Firmware firmware{device};

void setup()
{
device.sd_init();
octo.begin();
pcontroller = new CTeensy4Controller<GRB, WS2811_800kHz>(&octo);
FastLED.setBrightness(255);
Expand All @@ -25,6 +26,7 @@ void setup()
FastLED.setMaxPowerInVoltsAndMilliamps(5, 900);
delay(50);
FastLED.show();
device.serial_begin(9600);
}

void loop()
Expand Down

0 comments on commit af66300

Please sign in to comment.