Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
message.h
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstddef>
17#include <cstdint>
18
19#include "pw_assert/assert.h"
20#include "pw_bytes/span.h"
21#include "pw_i2c/address.h"
22
23namespace pw::i2c {
24
49class Message {
50 public:
63 static constexpr Message WriteMessage(Address address, ConstByteSpan data) {
64 return Message(address,
65 ByteSpan(const_cast<std::byte*>(data.data()), data.size()),
66 Direction::kWrite,
67 /*no_start=*/false);
68 }
69
87 static constexpr Message WriteMessageContinuation(ConstByteSpan data) {
88 return Message(Address::SevenBit<1>(),
89 ByteSpan(const_cast<std::byte*>(data.data()), data.size()),
90 Direction::kWrite,
91 /*no_start=*/true);
92 }
93
106 static constexpr Message ReadMessage(Address address, ByteSpan data) {
107 return Message(address, data, Direction::kRead, /*no_start=*/false);
108 }
109
113 bool IsRead() const { return direction_ == Direction::kRead; }
114
121 bool IsTenBit() const { return address_.IsTenBit(); }
122
126 bool IsWriteContinuation() const { return no_start_; }
127
131 Address GetAddress() const { return address_; }
132
140 ByteSpan GetMutableData() const {
141 PW_ASSERT(IsRead());
142 return data_;
143 }
144
148 ConstByteSpan GetData() const { return data_; }
149
150 private:
151 enum class Direction : uint8_t {
152 kWrite,
153 kRead,
154 };
155
156 constexpr Message(const Address& address,
157 ByteSpan data,
158 Direction direction,
159 bool no_start)
160 : address_(address),
161 data_(data),
162 direction_(direction),
163 no_start_(no_start) {}
164
165 // I2c address of target device.
166 Address address_;
167
168 // Data buffer to send from or receive into.
169 ByteSpan data_;
170
171 // Message represents an i2c read.
172 Direction direction_;
173
174 // Message should be transmitted without an i2c start condition and without
175 // an address.
176 bool no_start_;
177};
178
179} // namespace pw::i2c
Definition: address.h:39
constexpr bool IsTenBit() const
Definition: address.h:170
Definition: message.h:49
bool IsRead() const
Definition: message.h:113
ByteSpan GetMutableData() const
Definition: message.h:140
bool IsWriteContinuation() const
Definition: message.h:126
static constexpr Message WriteMessageContinuation(ConstByteSpan data)
Definition: message.h:87
static constexpr Message ReadMessage(Address address, ByteSpan data)
Definition: message.h:106
Address GetAddress() const
Definition: message.h:131
ConstByteSpan GetData() const
Definition: message.h:148
static constexpr Message WriteMessage(Address address, ConstByteSpan data)
Definition: message.h:63
bool IsTenBit() const
Definition: message.h:121