24#include "initializer_list.h"
36 template<
typename MANAGER>
class I2CFutureHelper;
41 template<I2CMode MODE>
struct Mode {};
47 static constexpr Mode
I2C_FAST = Mode<I2CMode::FAST>{};
51 template<
bool DISABLE = true>
class DisableInterrupts
62 const uint8_t sreg_ = SREG;
64 template<
typename>
friend class I2CDevice;
66 template<>
class DisableInterrupts<false>
68 DisableInterrupts() =
default;
70 template<
typename>
friend class I2CDevice;
82 template<
typename MANAGER_>
90 using MANAGER_TRAIT = I2CManager_trait<MANAGER>;
93 MANAGER_TRAIT::IS_I2CMANAGER,
"MANAGER_ must be a valid I2C Manager type");
127 template<I2CMode MODE>
129 : device_{device}, handler_{manager}, auto_stop_flags_{I2CCommandType::flags(auto_stop, true, true)}
132 static_assert((MODE ==
I2CMode::FAST) || (MODE == MANAGER_TRAIT::MODE),
133 "MANAGER_ I2CMode must be compliant with this device best mode");
173 static constexpr I2CLightCommand read(uint8_t read_count = 0,
bool finish_future =
false,
bool stop =
false)
175 const I2CCommandType type{
false, stop, finish_future,
false};
204 const I2CCommandType type{
true, stop, finish_future,
false};
246 uint8_t num_commands = commands.size();
251 UNUSED auto outer_sync = DisableInterrupts<MANAGER_TRAIT::IS_ASYNC>{};
257 UNUSED auto inner_sync = DisableInterrupts<!MANAGER_TRAIT::IS_ASYNC>{};
259 if (!handler_.ensure_num_commands_(num_commands))
return errors::EAGAIN;
260 max_read =
future.get_future_value_size_();
261 max_write =
future.get_storage_value_size_();
265 if (MANAGER_TRAIT::IS_DEBUG && (!check_commands(max_write, max_read, commands)))
273 command.update_byte_count(max_read, max_write);
276 if (num_commands == 0)
277 command.type().add_flags(auto_stop_flags_);
279 if (!handler_.push_command_(command, device_,
future))
286 handler_.last_command_pushed_();
334 template<
typename F,
typename T = u
int8_t>
bool sync_read(T& result)
337 if (async_read<F>(
future) != 0)
return false;
338 return future.get(result);
381 constexpr uint8_t NUM_WRITES = F::NUM_WRITES;
382 constexpr uint8_t WRITE_SIZE = F::WRITE_SIZE;
384 prepare_multi_write_commands(writes, NUM_WRITES, WRITE_SIZE, stop);
406 template<
typename F,
typename T = u
int8_t>
bool sync_write(
const T& value)
409 if (async_write<F>(
future) != 0)
return false;
432 if (async_write<F>(
future) != 0)
return false;
437 static bool check_commands(
441 uint8_t total_read = 0;
442 uint8_t total_write = 0;
446 if (command.type().is_write())
447 total_write += (command.byte_count() ? command.byte_count() : max_write);
449 total_read += (command.byte_count() ? command.byte_count() : max_read);
453 return (total_write == max_write) && (total_read == max_read);
456 void prepare_multi_write_commands(I2CLightCommand* commands, uint8_t count, uint8_t write_size,
bool stop)
458 I2CLightCommand command =
write(write_size,
false, stop);
459 for (uint8_t i = 0; i < count; ++i)
460 *commands++ = command;
465 const uint8_t auto_stop_flags_;
466 friend class I2CFutureHelper<
MANAGER>;
Base class for all I2C devices.
bool sync_read(T &result)
Helper method that launches I2C commands for a simple Future performing one write followed by one rea...
void set_device(uint8_t device)
Change the I2C address of this device.
static constexpr I2CLightCommand write(uint8_t write_count=0, bool finish_future=false, bool stop=false)
Build a write I2CLightCommand that can be later pushed to the I2C Manager for proper handling.
int async_write(F &future, bool stop=true)
Helper method that asynchronously launches I2C commands for a simple Future performing only one write...
int async_multi_write(F &future, bool stop=true)
Helper method that asynchronously launches I2C commands for a simple Future performing several regist...
bool sync_write(const T &value)
Helper method that launches I2C commands for a simple Future performing only one write (typically for...
static constexpr I2CLightCommand read(uint8_t read_count=0, bool finish_future=false, bool stop=false)
Build a read I2CLightCommand that can be later pushed to the I2C Manager for proper handling.
MANAGER_ MANAGER
the type of I2C Manager that can handle this device.
int async_read(F &future, bool stop=true)
Helper method that asynchronously launches I2C commands for a simple Future performing one write foll...
bool sync_write()
Helper method that launches I2C commands for a simple Future performing only one write (typically for...
typename MANAGER::ABSTRACT_FUTURE ABSTRACT_FUTURE
The abstract base class of all futures to be defined for a device.
I2CDevice(MANAGER &manager, uint8_t device, UNUSED Mode< MODE > mode, bool auto_stop)
Create a new I2C device.
int launch_commands(ABSTRACT_FUTURE &future, utils::range< I2CLightCommand > commands)
Launch execution (asynchronously or synchronously, depending on MANAGER) of a chain of I2CLightComman...
typename MANAGER::template FUTURE< OUT, IN > FUTURE
The template base class of all futures to be defined for a device.
Light atomic I2C command as prepared by an I2C device.
Iterable class that can embed arrays or initializer lists through implicit conversion.
#define UNUSED
Specific GCC attribute to declare an argument or variable unused, so that the compiler does not emit ...
Common errors definition.
#define F(ptr)
Force string constant to be stored as flash storage.
Utility API to handle the concept of futures.
I2C API common definitions.
Utilities to convert arrays into an iterable (usable if for x: list construct).
Mode
Mode used when drawing pixels.
constexpr const int EINVAL
Invalid argument or invalid Future.
constexpr const int EPROTO
Protocol error.
constexpr const int EAGAIN
Try again.
Contains the API around Future implementation.
@ READY
The status of a Future once its output value has been fully set by a provider.
Define API to define and manage I2C devices.
static constexpr Mode I2C_STANDARD
Constant determining that best supported I2C mode for an I2CDevice is STANDARD (100kHz).
@ FAST
I2C Fast mode, less than 400KHz.
static constexpr Mode I2C_FAST
Constant determining that best supported I2C mode for an I2CDevice is FAST (400kHz).
General utilities API that have broad application in programs.