This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HID: remove double buffering from inputs, move common behavior to bas…
…e app
- Loading branch information
1 parent
204fe11
commit 3f3785f
Showing
15 changed files
with
240 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "app_base.hpp" | ||
|
||
void app_base::stop() | ||
{ | ||
sending_sem_.release(); | ||
} | ||
|
||
void app_base::send(const std::span<const uint8_t> &buffer) | ||
{ | ||
if (!active()) { | ||
return; | ||
} | ||
if (!sending_sem_.try_acquire_for(SEMAPHORE_RESET_TIMEOUT)) { | ||
return; | ||
} | ||
std::copy(buffer.begin(), buffer.end(), in_buffer_.data() + sizeof(in_id_)); | ||
auto result = send_report(in_buffer_); | ||
if (result != hid::result::OK) { | ||
sending_sem_.release(); | ||
} | ||
} | ||
|
||
void app_base::in_report_sent(const std::span<const uint8_t> &data) | ||
{ | ||
if (data.front() != in_id_) { | ||
return; | ||
} | ||
sending_sem_.release(); | ||
} | ||
|
||
void app_base::get_report(hid::report::selector select, const std::span<uint8_t> &buffer) | ||
{ | ||
if (select != hid::report::selector(hid::report::type::INPUT, in_id_)) { | ||
return; | ||
} | ||
assert(buffer.size() >= in_buffer_.size()); | ||
|
||
memcpy(buffer.data(), in_buffer_.data(), in_buffer_.size()); | ||
send_report(buffer.subspan(0, in_buffer_.size())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef __APP_BASE_HEADER__ | ||
#define __APP_BASE_HEADER__ | ||
|
||
#include "hid/application.hpp" | ||
#include "hid/rdf/descriptor.hpp" | ||
#include "hid/report_protocol.hpp" | ||
#include "port/zephyr/semaphore.hpp" | ||
#include <chrono> | ||
|
||
class app_base : public hid::application { | ||
public: | ||
bool active() const; | ||
|
||
protected: | ||
static constexpr std::chrono::milliseconds SEMAPHORE_RESET_TIMEOUT{100}; | ||
|
||
template <typename T> | ||
static hid::report_protocol rp() | ||
{ | ||
static constexpr const auto rd{T::report_desc()}; | ||
constexpr hid::report_protocol rp{rd}; | ||
return rp; | ||
} | ||
template <typename T, typename TReport> | ||
constexpr app_base([[maybe_unused]] T *t, TReport &in_report_buffer) | ||
: application(rp<T>()), | ||
in_id_(in_report_buffer.ID), | ||
in_buffer_(in_report_buffer.data(), sizeof(TReport)) | ||
{} | ||
|
||
void stop() override; | ||
void set_report(hid::report::type type, const std::span<const uint8_t> &data) override {} | ||
void get_report(hid::report::selector select, const std::span<uint8_t> &buffer) override; | ||
void in_report_sent(const std::span<const uint8_t> &data) override; | ||
void send(const std::span<const uint8_t> &buffer); | ||
|
||
const hid::report::id in_id_; | ||
const std::span<uint8_t> in_buffer_; | ||
os::zephyr::binary_semaphore sending_sem_{1}; | ||
}; | ||
|
||
#endif // __APP_BASE_HEADER__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.