C/C++ API Reference
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
26
51class Message {
52 public:
65 static constexpr Message WriteMessage(Address address, ConstByteSpan data) {
66 return Message(address,
67 ByteSpan(const_cast<std::byte*>(data.data()), data.size()),
68 Direction::kWrite,
69 /*no_start=*/false);
70 }
71
90 return Message(Address::SevenBit<1>(),
91 ByteSpan(const_cast<std::byte*>(data.data()), data.size()),
92 Direction::kWrite,
93 /*no_start=*/true);
94 }
95
108 static constexpr Message ReadMessage(Address address, ByteSpan data) {
109 return Message(address, data, Direction::kRead, /*no_start=*/false);
110 }
111
115 bool IsRead() const { return direction_ == Direction::kRead; }
116
123 bool IsTenBit() const { return address_.IsTenBit(); }
124
128 bool IsWriteContinuation() const { return no_start_; }
129
133 Address GetAddress() const { return address_; }
134
143 PW_ASSERT(IsRead());
144 return data_;
145 }
146
150 ConstByteSpan GetData() const { return data_; }
151
152 private:
153 enum class Direction : uint8_t {
154 kWrite,
155 kRead,
156 };
157
158 constexpr Message(const Address& address,
159 ByteSpan data,
160 Direction direction,
161 bool no_start)
162 : address_(address),
163 data_(data),
164 direction_(direction),
165 no_start_(no_start) {}
166
167 // I2c address of target device.
168 Address address_;
169
170 // Data buffer to send from or receive into.
171 ByteSpan data_;
172
173 // Message represents an i2c read.
174 Direction direction_;
175
176 // Message should be transmitted without an i2c start condition and without
177 // an address.
178 bool no_start_;
179};
180
181} // namespace pw::i2c
Definition: address.h:42
Definition: message.h:51
bool IsRead() const
Definition: message.h:115
ByteSpan GetMutableData() const
Definition: message.h:142
bool IsWriteContinuation() const
Definition: message.h:128
constexpr bool IsTenBit() const
Definition: address.h:173
static constexpr Message WriteMessageContinuation(ConstByteSpan data)
Definition: message.h:89
static constexpr Message ReadMessage(Address address, ByteSpan data)
Definition: message.h:108
Address GetAddress() const
Definition: message.h:133
ConstByteSpan GetData() const
Definition: message.h:150
static constexpr Message WriteMessage(Address address, ConstByteSpan data)
Definition: message.h:65
bool IsTenBit() const
Definition: message.h:123
Cross-platform I2C library.
Definition: address.h:19