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
135 [[deprecated("Use one of the factory methods above to construct safely.")]]
136 explicit Address(uint16_t address);
137
144 uint8_t GetSevenBit() const;
145
149 [[deprecated("Use GetAddress() and IsTenBit() as appropriate.")]]
150 uint16_t GetTenBit() const {
151 return address_;
152 }
153
160 uint16_t GetAddress() const { return address_; }
161
165 friend bool operator==(const Address& a1, const Address& a2);
166
171 constexpr bool IsTenBit() const { return mode_ == Mode::kTenBit; }
172
173 private:
174 enum class Mode : uint8_t {
175 kSevenBit = 0,
176 kTenBit = 1,
177 };
178
179 Address(uint16_t address, Mode mode);
180
181 enum AlreadyCheckedAddress { kAlreadyCheckedAddress };
182 constexpr Address(uint16_t address, Mode mode, AlreadyCheckedAddress)
183 : address_(address), mode_(mode) {}
184
185 uint16_t address_ : 10;
186 Mode mode_ : 1;
187};
188
189// Compares two address objects.
190inline bool operator==(const Address& a1, const Address& a2) {
191 return a1.address_ == a2.address_ && a1.mode_ == a2.mode_;
192}
193
194} // namespace pw::i2c
Definition: address.h:42
static constexpr Address SevenBit()
Definition: address.h:92
uint16_t GetTenBit() const
Definition: address.h:150
uint16_t GetAddress() const
Definition: address.h:160
uint8_t GetSevenBit() const
Address(uint16_t address)
constexpr bool IsTenBit() const
Definition: address.h:171
friend bool operator==(const Address &a1, const Address &a2)
Definition: address.h:190
bool operator==(const Address &a1, const Address &a2)
Definition: address.h:190
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