Skip to content

Commit

Permalink
update example and add removal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
finger563 committed Dec 20, 2024
1 parent 9966da6 commit c50185d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
8 changes: 6 additions & 2 deletions components/hid_service/example/main/hid_service_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ extern "C" void app_main(void) {
hid_service.set_report_map(descriptor);

// use the HID service to make an input report characteristic
auto input_report = hid_service.input_report(input_report_id);
[[maybe_unused]] auto input_report = hid_service.input_report(input_report_id);

// use the HID service to make an output report characteristic
[[maybe_unused]] auto output_report = hid_service.output_report(output_report_id);
Expand Down Expand Up @@ -217,7 +217,11 @@ extern "C" void app_main(void) {
// send an input report
auto report = gamepad_input_report.get_report();
logger.debug("Sending report data ({}): {::#02x}", report.size(), report);
input_report->notify(report);

// Get the stored pointer (we could use the one above, but this is just to
// show how to get it)
auto report_char = hid_service.input_report(input_report_id);
report_char->notify(report);

// sleep
std::this_thread::sleep_until(start + 1s);
Expand Down
60 changes: 60 additions & 0 deletions components/hid_service/include/hid_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ class HidService : public espp::BaseComponent {
return input_report_char;
}

/// @brief Remove an input report characteristic.
/// @param report_id The report ID of the input report characteristic to
/// remove.
/// @note This will delete the characteristic, and remove it from the list.
/// Any stored pointers to the characteristic will be invalid after this
/// call.
void remove_input_report(uint8_t report_id) {
std::lock_guard<std::mutex> lock(input_report_characteristics_mutex_);
auto it =
std::find_if(input_report_characteristics_.begin(), input_report_characteristics_.end(),
[report_id](const ReportCharacteristic &report_char) {
return report_char.first == report_id;
});
if (it != input_report_characteristics_.end()) {
static constexpr bool delete_char = true;
service_->removeCharacteristic(it->second, delete_char);
input_report_characteristics_.erase(it);
}
}

/// @brief Create an output report characteristic.
/// @param report_id The report ID. This should be the same as the report
/// ID in the report descriptor for the output object that
Expand Down Expand Up @@ -213,6 +233,26 @@ class HidService : public espp::BaseComponent {
return output_report_char;
}

/// @brief Remove an output report characteristic.
/// @param report_id The report ID of the output report characteristic to
/// remove.
/// @note This will delete the characteristic, and remove it from the list.
/// Any stored pointers to the characteristic will be invalid after this
/// call.
void remove_output_report(uint8_t report_id) {
std::lock_guard<std::mutex> lock(output_report_characteristics_mutex_);
auto it =
std::find_if(output_report_characteristics_.begin(), output_report_characteristics_.end(),
[report_id](const ReportCharacteristic &report_char) {
return report_char.first == report_id;
});
if (it != output_report_characteristics_.end()) {
static constexpr bool delete_char = true;
service_->removeCharacteristic(it->second, delete_char);
output_report_characteristics_.erase(it);
}
}

/// @brief Create a feature report characteristic.
/// @param report_id The report ID. This should be the same as the report
/// ID in the report descriptor for the feature object that
Expand Down Expand Up @@ -248,6 +288,26 @@ class HidService : public espp::BaseComponent {
return feature_report_char;
}

/// @brief Remove a feature report characteristic.
/// @param report_id The report ID of the feature report characteristic to
/// remove.
/// @note This will delete the characteristic, and remove it from the list.
/// Any stored pointers to the characteristic will be invalid after this
/// call.
void remove_feature_report(uint8_t report_id) {
std::lock_guard<std::mutex> lock(feature_report_characteristics_mutex_);
auto it =
std::find_if(feature_report_characteristics_.begin(), feature_report_characteristics_.end(),
[report_id](const ReportCharacteristic &report_char) {
return report_char.first == report_id;
});
if (it != feature_report_characteristics_.end()) {
static constexpr bool delete_char = true;
service_->removeCharacteristic(it->second, delete_char);
feature_report_characteristics_.erase(it);
}
}

protected:
static constexpr uint16_t SERVICE_UUID = 0x1812;
static constexpr uint16_t HID_INFORMATION_UUID = 0x2a4a;
Expand Down

0 comments on commit c50185d

Please sign in to comment.