FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
i2c_status.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef I2C_STATUS_HH
22#define I2C_STATUS_HH
23
24#include "flash.h"
25#include "ios.h"
26#include "i2c_handler_common.h"
27
28namespace i2c
29{
34 namespace status
35 {
36 }
37}
38
39namespace i2c::status
40{
46 enum class STATUS : uint8_t
47 {
49 TRACE_ERROR = 0x01,
51 TRACE_ALL = 0xFF
52 };
53
60 template<uint8_t SIZE> class I2CStatusRecorder
61 {
62 public:
78
83 void reset()
84 {
85 index_ = 0;
86 }
87
95 template<typename OSTREAM>
96 void trace(OSTREAM& out, bool hex_status = true)
97 {
98 if (hex_status)
99 for (uint8_t i = 0; i < index_; ++i)
100 {
102 out << uint8_t(expected_[i]) << ' ';
104 out << uint8_t(actual_[i]) << '\n';
105 }
106 else
107 for (uint8_t i = 0; i < index_; ++i)
108 out << expected_[i] << ' ' << actual_[i] << '\n';
109 if (index_ >= SIZE)
110 out << F("# OVF #") << '\n';
111 index_ = 0;
112 }
113
115 void operator()(Status expected, Status actual)
116 {
117 if (index_ >= SIZE) return;
118 if ((expected != actual) || (trace_ == STATUS::TRACE_ALL))
119 {
120 expected_[index_] = expected;
121 actual_[index_++] = actual;
122 }
123 }
125
126 private:
127 Status expected_[SIZE];
128 Status actual_[SIZE];
129 uint8_t index_ = 0;
130 STATUS trace_;
131 };
132
140 template<typename OSTREAM>
142 {
143 public:
153 I2CStatusLiveLogger(OSTREAM& out, STATUS trace = STATUS::TRACE_ALL, bool hex_status = true)
154 : out_{out}, trace_{trace}, hex_status_{hex_status} {}
155
157 void operator()(Status expected, Status actual)
158 {
159 if ((expected != actual) || (trace_ == STATUS::TRACE_ALL))
160 {
161 if (hex_status_)
162 {
164 out_ << uint8_t(expected) << ' ';
166 out_ << uint8_t(actual) << '\n';
167 }
168 else
169 out_ << expected << ' ' << actual << '\n';
170 }
171 }
173
174 private:
175 OSTREAM& out_;
176 STATUS trace_;
177 bool hex_status_;
178 };
179
186 {
187 public:
192
197 {
198 return actual_;
199 }
200
205 {
206 return expected_;
207 }
208
210 void operator()(Status expected, Status actual)
211 {
212 expected_ = expected;
213 actual_ = actual;
214 }
216
217 private:
218 Status actual_ = Status::OK;
219 Status expected_ = Status::OK;
220 };
221}
222
223#endif /* I2C_STATUS_HH */
Class holding the latest I2C status.
Definition: i2c_status.h:186
Status latest_expected_status() const
Return the latest I2C expected status (may be different than actual).
Definition: i2c_status.h:204
Status latest_status() const
Return the latest I2C actual status.
Definition: i2c_status.h:196
I2CLatestStatusHolder()=default
Create an I2CLatestStatusHolder that can hold latest I2C status.
Class tracing I2C status notifications live to out.
Definition: i2c_status.h:142
I2CStatusLiveLogger(OSTREAM &out, STATUS trace=STATUS::TRACE_ALL, bool hex_status=true)
Create an I2CStatusLiveLogger that can trace live I2C notifications determined by trace.
Definition: i2c_status.h:153
Class recording I2C status notifications for later output.
Definition: i2c_status.h:61
I2CStatusRecorder(STATUS trace=STATUS::TRACE_ALL)
Create an I2CStatusRecorder that can record I2C status notifications determined by trace.
Definition: i2c_status.h:77
void trace(OSTREAM &out, bool hex_status=true)
Output all recorded I2C status notifications to out then clear all records.
Definition: i2c_status.h:96
void reset()
Clear all recorded notifications.
Definition: i2c_status.h:83
static constexpr fmtflags basefield
Bitmask constant used with setf(fmtflags, fmtflags) when changing the output base format.
Definition: ios.h:227
static constexpr fmtflags hex
Read or write integral values using hexadecimal (0..9,A..F) base format.
Definition: ios.h:218
Flash memory utilities.
#define F(ptr)
Force string constant to be stored as flash storage.
Definition: flash.h:150
Common I2C Manager API.
C++-like std::iostream facilities.
Defines API to ease I2C manager status tracing and debugging.
Definition: i2c_status.h:35
STATUS
Indicate when status should be traced.
Definition: i2c_status.h:47
@ TRACE_ERROR
Trace only status that differ (between expected and actual).
@ TRACE_ALL
Trace everything.
Define API to define and manage I2C devices.
Definition: i2c.h:51
Status
Transmission status codes.
Definition: i2c.h:67
@ OK
Code indicating the last called method executed as expected without any issue.