Skip to content

Commit

Permalink
update hid-rp
Browse files Browse the repository at this point in the history
  • Loading branch information
benedekkupper committed Feb 22, 2024
1 parent ea4a631 commit 02bf42b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 58 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
etl
hid-rp
)

add_subdirectory(examples)
32 changes: 0 additions & 32 deletions c2usb/c2usb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,6 @@ enum class result : int
NO_CONNECTION = -ENOTCONN,
};

/// @brief Lookup unsigned integer of matching size
template <std::size_t SIZE, class T = void>
struct sized_unsigned
{};

template <std::size_t SIZE>
struct sized_unsigned<SIZE, std::enable_if_t<SIZE == 1>>
{
typedef std::uint8_t type;
};
template <std::size_t SIZE>
struct sized_unsigned<SIZE, std::enable_if_t<SIZE == 2>>
{
typedef std::uint16_t type;
};
template <std::size_t SIZE>
struct sized_unsigned<SIZE, std::enable_if_t<SIZE == 4>>
{
typedef std::uint32_t type;
};
template <std::size_t SIZE>
struct sized_unsigned<SIZE, std::enable_if_t<SIZE == 8>>
{
typedef std::uint64_t type;
};

template <std::size_t SIZE>
using sized_unsigned_t = typename sized_unsigned<SIZE>::type;

template <typename T>
concept IntegerConvertable = std::is_convertible_v<T, sized_unsigned_t<sizeof(T)>>;

/// @brief The interface base class is used by interface subclasses,
/// that contain only abstract virtual functions, and no member data.
/// This library limits multiple inheritance to interface subclasses.
Expand Down
2 changes: 1 addition & 1 deletion c2usb/port/zephyr/bluetooth/gatt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <magic_enum.hpp>
#include <zephyr/bluetooth/gatt.h>

#include "c2usb.hpp"
#include "sized_unsigned.hpp"

namespace bluetooth::zephyr
{
Expand Down
1 change: 1 addition & 0 deletions c2usb/usb/df/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <new>
#include <string_view>

#include "sized_unsigned.hpp"
#include "usb/control.hpp"
#include "usb/df/transfer.hpp"

Expand Down
18 changes: 9 additions & 9 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
set(C2USB_EXAMPLES_PUBLIC_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/leds_saving_keyboard.hpp
PARENT_SCOPE
add_library(${PROJECT_NAME}-example-hid)
target_sources(${PROJECT_NAME}-example-hid
PUBLIC
leds_saving_keyboard.hpp
)

set(C2USB_EXAMPLES_PRIVATE_HEADERS
PARENT_SCOPE
target_include_directories(${PROJECT_NAME}-example-hid
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

set(C2USB_EXAMPLES_SOURCES
PARENT_SCOPE
target_link_libraries(${PROJECT_NAME}-example-hid PUBLIC
${PROJECT_NAME}
)
21 changes: 6 additions & 15 deletions examples/leds_saving_keyboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,11 @@ class leds_saving_keyboard : public hid::application
}

leds_saving_keyboard(hid::page::keyboard_keypad key, hid::page::leds led)
: hid::application(report_prot()),
key_(static_cast<uint8_t>(key)),
led_mask_(1 << (static_cast<uint8_t>(led) - 1))
: hid::application(report_prot()), key_(key), led_(led)
{}
auto send_key(bool set)
{
if (set)
{
keys_buffer_.scancodes[0] = key_;
}
else
{
keys_buffer_.scancodes[0] = 0;
}
keys_buffer_.scancodes.set(key_, set);
return send_report(&keys_buffer_);
}
void start(hid::protocol prot) override
Expand All @@ -59,15 +50,15 @@ class leds_saving_keyboard : public hid::application
{
auto* out_report = reinterpret_cast<const kb_leds_report*>(data.data());

if ((out_report->leds & led_mask_) != 0)
if (out_report->leds.test(led_))
{
send_key(true);
}
receive_report(&leds_buffer_);
}
void in_report_sent(const std::span<const uint8_t>& data) override
{
if (keys_buffer_.scancodes[0] == key_)
if (keys_buffer_.scancodes.test(key_))
{
send_key(false);
}
Expand All @@ -80,8 +71,8 @@ class leds_saving_keyboard : public hid::application

private:
C2USB_USB_TRANSFER_ALIGN(keys_report, keys_buffer_){};
const uint8_t key_{};
const uint8_t led_mask_{};
const hid::page::keyboard_keypad key_{};
const hid::page::leds led_{};
C2USB_USB_TRANSFER_ALIGN(kb_leds_report, leds_buffer_){};
hid::protocol prot_{};
};
Expand Down

0 comments on commit 02bf42b

Please sign in to comment.