C/C++ API Reference
Loading...
Searching...
No Matches
address.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
15#pragma once
16
17#include <cstdint>
18
19namespace pw::i2c {
20
22
42class Address {
43 public:
44 static constexpr uint8_t kMaxSevenBitAddress = (1 << 7) - 1;
45 static constexpr uint16_t kMaxTenBitAddress = (1 << 10) - 1;
46
58 template <uint16_t kAddress>
59 static constexpr Address TenBit() {
60 static_assert(kAddress <= kMaxTenBitAddress);
61 return Address(kAddress, Mode::kTenBit, kAlreadyCheckedAddress);
62 }
63
75 static Address TenBit(uint16_t address) {
76 return Address(address, Mode::kTenBit);
77 }
78
91 template <uint8_t kAddress>
92 static constexpr Address SevenBit() {
93 static_assert(kAddress <= kMaxSevenBitAddress);
94 return Address(kAddress, Mode::kSevenBit, kAlreadyCheckedAddress);
95 }
96
108 static Address SevenBit(uint16_t address) {
109 return Address(address, Mode::kSevenBit);
110 }
111
137 [[deprecated("Use one of the factory methods above to construct safely.")]]
138 explicit Address(uint16_t address);
139
146 uint8_t GetSevenBit() const;
147
151 [[deprecated("Use GetAddress() and IsTenBit() as appropriate.")]]
152 uint16_t GetTenBit() const {
153 return address_;
154 }
155
162 uint16_t GetAddress() const { return address_; }
163
167 friend bool operator==(const Address& a1, const Address& a2);
168
173 constexpr bool IsTenBit() const { return mode_ == Mode::kTenBit; }
174
175 private:
176 enum class Mode : uint8_t {
177 kSevenBit = 0,
178 kTenBit = 1,
179 };
180
181 Address(uint16_t address, Mode mode);
182
183 enum AlreadyCheckedAddress { kAlreadyCheckedAddress };
184 constexpr Address(uint16_t address, Mode mode, AlreadyCheckedAddress)
185 : address_(address), mode_(mode) {}
186
187 uint16_t address_ : 10;
188 Mode mode_ : 1;
189};
190
191// Compares two address objects.
192inline bool operator==(const Address& a1, const Address& a2) {
193 return a1.address_ == a2.address_ && a1.mode_ == a2.mode_;
194}
195
196} // namespace pw::i2c
Definition: address.h:42
static constexpr Address SevenBit()
Definition: address.h:92
uint16_t GetTenBit() const
Definition: address.h:152
uint16_t GetAddress() const
Definition: address.h:162
uint8_t GetSevenBit() const
Address(uint16_t address)
constexpr bool IsTenBit() const
Definition: address.h:173
friend bool operator==(const Address &a1, const Address &a2)
Definition: address.h:192
bool operator==(const Address &a1, const Address &a2)
Definition: address.h:192
static Address SevenBit(uint16_t address)
Definition: address.h:108
static Address TenBit(uint16_t address)
Definition: address.h:75
static constexpr Address TenBit()
Definition: address.h:59
Cross-platform I2C library.
Definition: address.h:19