Skip to content

Commit

Permalink
feat(base_peripheral): Allow delay in separated write then read
Browse files Browse the repository at this point in the history
* Add `separate_write_then_read_delay` (default 0) into `BasePeripheral::Config`
* Add `void set_separate_write_then_read_delay(...)` public method
* In `write_then_read(...)`, if using separate `write` then `read`, use `std::this_thread::sleep_for(...)` if the configured delay is > 0
  • Loading branch information
finger563 committed Feb 13, 2024
1 parent b4e301f commit ff46ee8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components/base_peripheral/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
idf_component_register(
INCLUDE_DIRS "include"
REQUIRES base_component)
REQUIRES base_component pthread)
25 changes: 25 additions & 0 deletions components/base_peripheral/include/base_peripheral.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once

#include <chrono>
#include <cstdint>
#include <functional>
#include <mutex>
#include <string>
#include <thread>

#include "base_component.hpp"

Expand Down Expand Up @@ -67,6 +69,10 @@ class BasePeripheral : public BaseComponent {
nullptr}; ///< Function to read data at a specific address from the peripheral
write_then_read_fn write_then_read{
nullptr}; ///< Function to write then read data from the peripheral
std::chrono::milliseconds separate_write_then_read_delay{
0}; ///< The delay between the write and read operations in write_then_read in
///< milliseconds if the write_then_read function is not set to a custom function
///< and the write and read functions are separate
};

/// Probe the peripheral
Expand Down Expand Up @@ -146,6 +152,21 @@ class BasePeripheral : public BaseComponent {
base_config_.write_then_read = write_then_read;
}

/// Set the delay between the write and read operations in write_then_read
/// \param delay The delay between the write and read operations in
/// write_then_read
/// \note This function is thread safe
/// \note This should rarely be used, as the delay is usually set in the
/// constructor. If you need to change the delay, consider using the
/// set_config function instead.
/// \note This delay is only used if the write_then_read function is not set to
/// a custom function and the write and read functions are separate
/// functions.
void set_separate_write_then_read_delay(std::chrono::milliseconds delay) {
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
base_config_.separate_write_then_read_delay = delay;
}

/// Set the configuration for the peripheral
/// \param config The configuration for the peripheral
/// \note This function is thread safe
Expand Down Expand Up @@ -322,6 +343,10 @@ class BasePeripheral : public BaseComponent {
if (!base_config_.write(base_config_.address, write_data, write_length)) {
ec = std::make_error_code(std::errc::io_error);
} else {
// if there is a delay, wait for it
if (base_config_.separate_write_then_read_delay.count() > 0) {
std::this_thread::sleep_for(base_config_.separate_write_then_read_delay);
}
// read the data
if (!base_config_.read(base_config_.address, read_data, read_length)) {
ec = std::make_error_code(std::errc::io_error);
Expand Down

0 comments on commit ff46ee8

Please sign in to comment.