Skip to content

Commit

Permalink
Reading settings works
Browse files Browse the repository at this point in the history
  • Loading branch information
malcx95 committed Aug 5, 2024
1 parent aa7d5d0 commit a7b5345
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 26 deletions.
2 changes: 1 addition & 1 deletion core/firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void Firmware::update()
mouse_state.reset();
if (!loaded_keymap)
{
const bool success = keyboard::KeyMapLoader::load_from_sd_else_default(device, keymap);
const bool success = keyboard::KeyMapLoader::load_from_sd_else_default(device, keymap, settings);
if (success)
{
backlight.signal_success();
Expand Down
2 changes: 2 additions & 0 deletions core/firmware.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "backlight/schemes/wave.h"
#include "keyboard/keymap.h"
#include "keyboard/keyscan.h"
#include "keyboard/settings.h"

namespace simulator { class SimulatorWindow; }

Expand All @@ -26,6 +27,7 @@ class Firmware
keyboard::KeyMap keymap;
keyboard::KeyboardScanResult keyboard_scan_result;
keyboard::MouseState mouse_state;
keyboard::Settings settings;

bool loaded_keymap = false;

Expand Down
109 changes: 106 additions & 3 deletions core/keyboard/keymap_loader.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,60 @@
#include "keymap_loader.h"
#include "keyutils.h"
#include "../util/buffer_utils.h"
#include <cstdint>
#include <iostream>


namespace core::keyboard
{


const uint64_t SETTINGS_START_WORD = 0xDEADBEEFDEADBEEF;

const uint16_t MOUSE_SPEED_NUMBER = 0;
const uint16_t MOUSE_ACCELERATION_NUMBER = 1;
const uint16_t HIGHLIGHT_LAYER_KEYS_NUMBER = 2;


struct DataProperties
{
uint16_t* settings_start;
int keymap_size;
int settings_size;
};


DataProperties find_keymap_settings_split(const uint16_t* data, int size)
{
DataProperties properties;
properties.settings_start = nullptr;
properties.keymap_size = 0;
properties.settings_size = 0;

for (int i = 0; i < size - 1; i++)
{
const uint64_t word =
data[i + 3]
| (static_cast<uint64_t>(data[i + 2]) << 16)
| (static_cast<uint64_t>(data[i + 1]) << 32)
| (static_cast<uint64_t>(data[i]) << 48);
if (word == SETTINGS_START_WORD)
{
properties.settings_start = const_cast<uint16_t*>(&data[i + 4]);
properties.keymap_size = i;
properties.settings_size = size - i - 4;
break;
}
}

properties.keymap_size = properties.settings_start == nullptr
? size
: properties.keymap_size;

return properties;
}


void KeyMapLoader::load_default(KeyMap& keymap)
{
// initialize all actions to null
Expand Down Expand Up @@ -89,7 +137,7 @@ bool KeyMapLoader::check_checksum(const uint16_t* data, int size)
}


bool KeyMapLoader::load_from_sd_else_default(Device& device, KeyMap& keymap)
bool KeyMapLoader::load_from_sd_else_default(Device& device, KeyMap& keymap, Settings& settings)
{
char* ascii_buffer;
uint32_t num_read_ascii_chars;
Expand All @@ -105,9 +153,15 @@ bool KeyMapLoader::load_from_sd_else_default(Device& device, KeyMap& keymap)
{
const uint16_t* data = reinterpret_cast<const uint16_t*>(buffer);
const int size = num_read_bytes / 2;
const bool success = deserialize_keymap(data, size, keymap);
const auto properties = find_keymap_settings_split(data, size);
const bool keymap_success = deserialize_keymap(data, properties.keymap_size, keymap);
bool settings_success = true;
if (properties.settings_start != nullptr)
{
settings_success = deserialize_settings(properties.settings_start, properties.settings_size, settings);
}
delete[] buffer;
if (success)
if (keymap_success && settings_success)
{
return true;
}
Expand All @@ -117,6 +171,54 @@ bool KeyMapLoader::load_from_sd_else_default(Device& device, KeyMap& keymap)
}


bool KeyMapLoader::deserialize_settings(const uint16_t* data, int size, Settings& settings)
{
if (!check_settings_checksum(data, size))
{
return false;
}

const int num_settings = data[0];

const uint16_t* settings_ptr = &data[2];
for (int i = 0; i < num_settings * 2; i += 2)
{
const uint16_t setting_number = settings_ptr[i];
const uint16_t setting_value = settings_ptr[i + 1];

if (setting_number == MOUSE_SPEED_NUMBER)
{
settings.mouse_speed = setting_value;
}
else if (setting_number == MOUSE_ACCELERATION_NUMBER)
{
settings.mouse_acceleration = setting_value;
}
else if (setting_number == HIGHLIGHT_LAYER_KEYS_NUMBER)
{
settings.highlight_layer_keys = setting_value;
}
}

return true;
}


bool KeyMapLoader::check_settings_checksum(const uint16_t* data, int size)
{
if (size < 2)
{
return false;
}
uint16_t checksum = 0;
for (int i = 2; i < size; i++)
{
checksum = (checksum + data[i]) % common::constants::CHECKSUM_PERIOD;
}
return checksum == data[1];
}


bool KeyMapLoader::deserialize_keymap(const uint16_t* data, int size, KeyMap& keymap)
{
if (!check_checksum(data, size))
Expand Down Expand Up @@ -146,6 +248,7 @@ bool KeyMapLoader::deserialize_keymap(const uint16_t* data, int size, KeyMap& ke
const uint16_t layer = data[i++];
const uint16_t row = data[i++];
const uint16_t col = data[i++];
std::cout << row << " " << col << std::endl;

const uint16_t sequence_length = data[i++];

Expand Down
6 changes: 5 additions & 1 deletion core/keyboard/keymap_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdint.h>
#include "keymap.h"
#include "settings.h"


namespace core::keyboard
Expand All @@ -21,15 +22,18 @@ class KeyMapLoader
* @param device The device to load the keymap for.
* @return True if the keymap was loaded from the SD card, false if the default keymap was loaded.
*/
static bool load_from_sd_else_default(Device& device, KeyMap& keymap);
static bool load_from_sd_else_default(Device& device, KeyMap& keymap, Settings& settings);

static bool deserialize_keymap(const uint16_t* data, int size, KeyMap& keymap);

private:
static bool deserialize_settings(const uint16_t* data, int size, Settings& settings);
static bool check_checksum(const uint16_t* data, int size);
static bool check_sequence_lengths(const uint16_t* data, int size);
static void load_default(KeyMap& keymap);

static bool check_settings_checksum(const uint16_t* data, int size);

};

}
15 changes: 15 additions & 0 deletions core/keyboard/settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once


namespace core::keyboard
{

struct Settings
{
int mouse_speed;
int mouse_acceleration;

bool highlight_layer_keys;
};

}
47 changes: 26 additions & 21 deletions simulator/keyboard_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,6 @@ void KeyboardState::draw_row_and_col_state(
{
window.draw(col_rect);
}

const auto action = keymap.get_action(keymap.current_layer, row, col);
if (action != nullptr && action->is_single_key())
{
sf::Text text;
text.setFont(font);
text.setCharacterSize(20);
text.setFillColor(sf::Color::White);
const auto code = action->sequence[0].key;
if (KEY_CODES.contains(code))
{
text.setString(KEY_CODES.at(code));
}
else
{
text.setString(std::to_string(code));
}
text.setPosition(x + KEY_SIZE/4.0, y + KEY_SIZE/4.0);
window.draw(text);
}
}


Expand Down Expand Up @@ -138,7 +118,32 @@ void KeyboardState::draw(sf::RenderWindow& window)
const auto y = key.description->y * (KEY_SIZE + KEY_PADDING) + OFFSET_Y;
rectangle.setPosition(x, y);
window.draw(rectangle);
draw_row_and_col_state(window, key.description->row, key.description->col, x, y);

const auto row = key.description->row;
const auto col = key.description->col;

draw_row_and_col_state(window, row, col, x, y);

const auto action = keymap.get_action(keymap.current_layer, row, col);
if (action != nullptr && action->is_single_key())
{
sf::Text text;
text.setFont(font);
text.setCharacterSize(20);
text.setFillColor(sf::Color::White);
const auto code = action->sequence[0].key;

if (KEY_CODES.contains(code))
{
text.setString(KEY_CODES.at(code));
}
else
{
text.setString(std::to_string(code));
}
text.setPosition(x + KEY_SIZE/4.0, y + KEY_SIZE/4.0);
window.draw(text);
}
}
}

Expand Down

0 comments on commit a7b5345

Please sign in to comment.