oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
i2c.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#ifdef CONFIG_OVE_I2C
17
18#include <ove/i2c.h>
19#include <ove/types.hpp>
20
21namespace ove {
22
29class I2c {
30public:
31 explicit I2c(const struct ove_i2c_cfg &cfg) {
32#ifdef CONFIG_OVE_ZERO_HEAP
33 int err = ove_i2c_init(&handle_, &storage_, &cfg);
34#else
35 int err = ove_i2c_create(&handle_, &cfg);
36#endif
37 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
38 }
39
40 ~I2c() {
41 if (!handle_) return;
42#ifdef CONFIG_OVE_ZERO_HEAP
43 ove_i2c_deinit(handle_);
44#else
45 ove_i2c_destroy(handle_);
46#endif
47 }
48
49 I2c(const I2c &) = delete;
50 I2c &operator=(const I2c &) = delete;
51
52#ifdef CONFIG_OVE_ZERO_HEAP
53 I2c(I2c &&) = delete;
54 I2c &operator=(I2c &&) = delete;
55#else
56 I2c(I2c &&o) noexcept : handle_(o.handle_) { o.handle_ = nullptr; }
57 I2c &operator=(I2c &&o) noexcept {
58 if (this != &o) {
59 if (handle_) ove_i2c_destroy(handle_);
60 handle_ = o.handle_;
61 o.handle_ = nullptr;
62 }
63 return *this;
64 }
65#endif
66
67 [[nodiscard]] int write(uint16_t addr, const void *data, size_t len,
68 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
69 return ove_i2c_write(handle_, addr, data, len, timeout_ms);
70 }
71
72 [[nodiscard]] int read(uint16_t addr, void *buf, size_t len,
73 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
74 return ove_i2c_read(handle_, addr, buf, len, timeout_ms);
75 }
76
77 [[nodiscard]] int write_read(uint16_t addr,
78 const void *tx, size_t tx_len,
79 void *rx, size_t rx_len,
80 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
81 return ove_i2c_write_read(handle_, addr,
82 tx, tx_len, rx, rx_len, timeout_ms);
83 }
84
85 [[nodiscard]] int reg_write(uint16_t addr, uint8_t reg,
86 const void *data, size_t len,
87 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
88 return ove_i2c_reg_write(handle_, addr, reg, data, len,
89 timeout_ms);
90 }
91
92 [[nodiscard]] int reg_read(uint16_t addr, uint8_t reg,
93 void *buf, size_t len,
94 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
95 return ove_i2c_reg_read(handle_, addr, reg, buf, len,
96 timeout_ms);
97 }
98
99 [[nodiscard]] int probe(uint16_t addr,
100 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
101 return ove_i2c_probe(handle_, addr, timeout_ms);
102 }
103
104 ove_i2c_t native_handle() const { return handle_; }
105
106private:
107 ove_i2c_t handle_ = nullptr;
108#ifdef CONFIG_OVE_ZERO_HEAP
109 ove_i2c_storage_t storage_{};
110#endif
111};
112
113} /* namespace ove */
114
115#endif /* CONFIG_OVE_I2C */
void write(const char *data, unsigned int len)
Writes a buffer of bytes to the console output.
Definition console.hpp:71
int read(const char *key, void *buf, size_t len, size_t *out)
Reads the value associated with a key from NVS.
Definition nvs.hpp:55
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.