Skip to content

Commit

Permalink
Add mouse controls, tested in simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
malcx95 committed Jul 17, 2024
1 parent e110bb3 commit aa7d5d0
Show file tree
Hide file tree
Showing 22 changed files with 650 additions and 265 deletions.
7 changes: 5 additions & 2 deletions core/firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "../common/constants.h"
#include "backlight/schemes/wave.h"
#include "keyboard/communication.h"
#include "keyboard/keymap_loader.h"

namespace core
{
Expand All @@ -19,9 +20,10 @@ Firmware::Firmware(Device& device) :

void Firmware::update()
{
mouse_state.reset();
if (!loaded_keymap)
{
const bool success = keymap.load_from_sd_else_default(device);
const bool success = keyboard::KeyMapLoader::load_from_sd_else_default(device, keymap);
if (success)
{
backlight.signal_success();
Expand All @@ -34,8 +36,9 @@ void Firmware::update()
}
device.start_timer();
key_scanner.scan(keyboard_scan_result);
keymap.translate_keyboard_scan_result(keyboard_scan_result, key_queue);
keymap.translate_keyboard_scan_result(keyboard_scan_result, key_queue, mouse_state);
keyboard::communication::send_key_report(key_queue, device);
keyboard::communication::send_mouse_commands(mouse_state, device);
backlight.update(keyboard_scan_result);

const uint32_t elapsed = device.get_timer_micros();
Expand Down
1 change: 1 addition & 0 deletions core/firmware.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Firmware
keyboard::KeyQueue key_queue;
keyboard::KeyMap keymap;
keyboard::KeyboardScanResult keyboard_scan_result;
keyboard::MouseState mouse_state;

bool loaded_keymap = false;

Expand Down
5 changes: 5 additions & 0 deletions core/keyboard/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ bool Action::is_single_key() const
return sequence_length == 1;
}

bool Action::is_mouse_action() const
{
return is_single_key() && util::key_is_mouse_key(sequence[0].key);
}

}
2 changes: 2 additions & 0 deletions core/keyboard/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct Action
uint8_t sequence_length;

bool is_single_key() const;

bool is_mouse_action() const;
};

}
Expand Down
36 changes: 36 additions & 0 deletions core/keyboard/communication.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
#include "communication.h"
#include <utility>

namespace core::keyboard::communication
{

void send_mouse_commands(MouseState& mouse_state, Device& device)
{
const auto movement = mouse_state.get_movement();
if (movement.moving)
{
device.mouse_move(movement.dx, movement.dy, movement.wheel);
}

const auto left_button_state = mouse_state.get_button_state(MouseButton::LEFT);
const auto middle_button_state = mouse_state.get_button_state(MouseButton::MIDDLE);
const auto right_button_state = mouse_state.get_button_state(MouseButton::RIGHT);

const auto buttons = {
std::make_pair(DeviceMouseButton::LEFT, left_button_state),
std::make_pair(DeviceMouseButton::MIDDLE, middle_button_state),
std::make_pair(DeviceMouseButton::RIGHT, right_button_state)
};

for (const auto&[button_code, button_state] : buttons)
{
switch (button_state)
{
case ButtonState::JUST_PRESSED:
device.mouse_press(button_code);
break;
case ButtonState::JUST_RELEASED:
device.mouse_release(button_code);
break;
default:
break;
}
}
}


void send_key_report(KeyQueue& key_queue, Device& device)
{
if (key_queue.size() > 0)
Expand Down
4 changes: 4 additions & 0 deletions core/keyboard/communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace core::keyboard::communication
{

void send_mouse_commands(MouseState& mouseState, Device& device);

void send_key_report(KeyQueue& key_queue, Device& device);

}

Loading

0 comments on commit aa7d5d0

Please sign in to comment.