Skip to content

Commit

Permalink
✨ Add CH9329 driver for mouse and keyboard control (#8)
Browse files Browse the repository at this point in the history
* ✨ Add CH9329 driver for mouse control

* bug fixes and absolute mouse demo

* change button logic

* add doxygen comments

* add keyboard general commands

* add keyboard media command

* split keyboard media and acpi commands

* comment out new demos in cmake

* Update demo to use nunchuck buttons as kb buttons

* add get info command

* work in progress, more commands

* more work in progress

* more work in progress

* take out config/parameter commands for later

* change requests

* more change requests

* bug fixes

* more change requests
  • Loading branch information
MaliaLabor authored Oct 7, 2024
1 parent 84b8883 commit 0ba945d
Show file tree
Hide file tree
Showing 13 changed files with 1,146 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ libhal_test_and_make_library(
LIBRARY_NAME libhal-input

SOURCES
src/ch9329.cpp
src/input.cpp
src/gamepad/nunchuck.cpp

TEST_SOURCES
tests/gamepad/nunchuck.test.cpp
tests/ch9329.test.cpp
tests/input.test.cpp
tests/main.test.cpp
)
Binary file not shown.
Binary file added datasheets/ch9329/CH9329DS1.PDF
Binary file not shown.
3 changes: 3 additions & 0 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ libhal_build_demos(
DEMOS
input
nunchuck
# ch9329_nunchuck_mouse
# ch9329_absolute_mouse
# ch9329_keyboard

INCLUDES
.
Expand Down
75 changes: 75 additions & 0 deletions demos/applications/ch9329_absolute_mouse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cstdint>

#include <libhal-input/ch9329.hpp>
#include <libhal-input/gamepad/nunchuck.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/i2c.hpp>

#include <resource_list.hpp>

void application(resource_list& p_map)
{
using namespace std::chrono_literals;
using namespace hal::literals;

auto& clock = *p_map.clock.value();
auto& console = *p_map.console.value();
auto& uart3 = *p_map.uart3.value();

hal::input::ch9329 usb_control(uart3);
uint16_t width = 3840;
uint16_t height = 2160;

hal::input::ch9329::mouse_absolute abs_mouse_control(width, height);

hal::print(console, "Demo Application Starting...\n\n");
hal::print<32>(console, "Screen Width: %li ", width);
hal::print<32>(console, "Screen Height: %li \n", height);

while (true) {
// top left corner
abs_mouse_control.position(1, 1);
usb_control.send(abs_mouse_control);
hal::delay(clock, 1s);

// middle screen
abs_mouse_control.position(width / 2, height / 2);
usb_control.send(abs_mouse_control);
hal::delay(clock, 1s);

// bottom right
abs_mouse_control.position(width, height);
usb_control.send(abs_mouse_control);
hal::delay(clock, 1s);

// bottom left
abs_mouse_control.position(1, height);
usb_control.send(abs_mouse_control);
hal::delay(clock, 1s);

// middle screen
abs_mouse_control.position(width / 2, height / 2);
usb_control.send(abs_mouse_control);
hal::delay(clock, 1s);

// top right
abs_mouse_control.position(width, 1);
usb_control.send(abs_mouse_control);
hal::delay(clock, 1s);
}
}
55 changes: 55 additions & 0 deletions demos/applications/ch9329_keyboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libhal-input/ch9329.hpp>
#include <libhal-input/gamepad/nunchuck.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/i2c.hpp>

#include <resource_list.hpp>

void application(resource_list& p_map)
{
using namespace std::chrono_literals;
using namespace hal::literals;

auto& clock = *p_map.clock.value();
auto& console = *p_map.console.value();
auto& uart3 = *p_map.uart3.value();

hal::input::ch9329 usb_control(uart3);

hal::input::ch9329::keyboard_general keyboard_control;

hal::print(console, "Demo Application Starting...\n\n");

while (true) {
keyboard_control.press_normal_key(normal_key::a, 1);
usb_control.send(keyboard_control);
hal::delay(clock, 1ms);

keyboard_control.release_normal_key(normal_key::a);
usb_control.send(keyboard_control);
hal::delay(clock, 2s);

keyboard_control.press_normal_key(normal_key::back_space, 1);
usb_control.send(keyboard_control);
hal::delay(clock, 1ms);

keyboard_control.release_normal_key(normal_key::back_space);
usb_control.send(keyboard_control);
hal::delay(clock, 2s);
}
}
50 changes: 50 additions & 0 deletions demos/applications/ch9329_nunchuck_mouse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libhal-input/ch9329.hpp>
#include <libhal-input/gamepad/nunchuck.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/i2c.hpp>

#include <resource_list.hpp>

void application(resource_list& p_map)
{
using namespace std::chrono_literals;
using namespace hal::literals;

constexpr auto sensitivity = 16;
auto& clock = *p_map.clock.value();
auto& console = *p_map.console.value();
auto& uart3 = *p_map.uart3.value();
auto& i2c = *p_map.i2c.value();

hal::input::nunchuck nunchuck(i2c);
hal::input::ch9329 usb_control(uart3);
hal::input::ch9329::keyboard_general kb_control;
hal::input::ch9329::mouse_relative rel_mouse_control;

hal::print(console, "Demo Application Starting...\n\n");
while (true) {
auto data = nunchuck.read();
std::int8_t x = (data.joystick_x() - 128) / sensitivity;
std::int8_t y = -(data.joystick_y() - 128) / sensitivity;
rel_mouse_control.move(x, y)
.left_button(data.c_button())
.right_button(data.z_button());
usb_control.send(rel_mouse_control);
hal::delay(clock, 1ms);
}
}
6 changes: 6 additions & 0 deletions demos/platforms/stm32f103c8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ resource_list initialize_platform()
hal::serial::settings{
.baud_rate = 115200,
});
static hal::stm32f1::uart uart3(hal::port<3>,
hal::buffer<128>,
hal::serial::settings{
.baud_rate = 9600,
});

static hal::stm32f1::output_pin led('C', 13);
static hal::stm32f1::output_pin sda('B', 7);
Expand All @@ -55,6 +60,7 @@ resource_list initialize_platform()
return {
.reset = +[]() { hal::cortex_m::reset(); },
.console = &uart1,
.uart3 = &uart3,
.clock = &counter,
.status_led = &led,
.i2c = &bit_bang_i2c,
Expand Down
1 change: 1 addition & 0 deletions demos/resource_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct resource_list
{
hal::callback<void()> reset;
std::optional<hal::serial*> console = std::nullopt;
std::optional<hal::serial*> uart3 = std::nullopt;
std::optional<hal::steady_clock*> clock = std::nullopt;
std::optional<hal::output_pin*> status_led = std::nullopt;
// Add more driver interfaces here ...
Expand Down
Loading

0 comments on commit 0ba945d

Please sign in to comment.