Pigweed
 
Loading...
Searching...
No Matches
address.h
1// Copyright 2020 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 <cstdint>
17
18namespace pw::i2c {
19
39class Address {
40 public:
41 static constexpr uint8_t kMaxSevenBitAddress = (1 << 7) - 1;
42 static constexpr uint16_t kMaxTenBitAddress = (1 << 10) - 1;
43
55 template <uint16_t kAddress>
56 static constexpr Address TenBit() {
57 static_assert(kAddress <= kMaxTenBitAddress);
58 return Address(kAddress, Mode::kTenBit, kAlreadyCheckedAddress);
59 }
60
72 static Address TenBit(uint16_t address) {
73 return Address(address, Mode::kTenBit);
74 }
75
88 template <uint8_t kAddress>
89 static constexpr Address SevenBit() {
90 static_assert(kAddress <= kMaxSevenBitAddress);
91 return Address(kAddress, Mode::kSevenBit, kAlreadyCheckedAddress);
92 }
93
105 static Address SevenBit(uint16_t address) {
106 return Address(address, Mode::kSevenBit);
107 }
108
134 [[deprecated("Use one of the factory methods above to construct safely.")]]
135 explicit Address(uint16_t address);
136
143 uint8_t GetSevenBit() const;
144
148 [[deprecated("Use GetAddress() and IsTenBit() as appropriate.")]]
149 uint16_t GetTenBit() const {
150 return address_;
151 }
152
159 uint16_t GetAddress() const { return address_; }
160
164 friend bool operator==(const Address& a1, const Address& a2);
165
170 constexpr bool IsTenBit() const { return mode_ == Mode::kTenBit; }
171
172 private:
173 enum class Mode : uint8_t {
174 kSevenBit = 0,
175 kTenBit = 1,
176 };
177
178 Address(uint16_t address, Mode mode);
179
180 enum AlreadyCheckedAddress { kAlreadyCheckedAddress };
181 constexpr Address(uint16_t address, Mode mode, AlreadyCheckedAddress)
182 : address_(address), mode_(mode) {}
183
184 uint16_t address_ : 10;
185 Mode mode_ : 1;
186};
187
188// Compares two address objects.
189inline bool operator==(const Address& a1, const Address& a2) {
190 return a1.address_ == a2.address_ && a1.mode_ == a2.mode_;
191}
192
193} // namespace pw::i2c
Definition: address.h:39
static constexpr Address SevenBit()
Definition: address.h:89
uint16_t GetTenBit() const
Definition: address.h:149
uint16_t GetAddress() const
Definition: address.h:159
uint8_t GetSevenBit() const
Address(uint16_t address)
constexpr bool IsTenBit() const
Definition: address.h:170
friend bool operator==(const Address &a1, const Address &a2)
Definition: address.h:189
static Address SevenBit(uint16_t address)
Definition: address.h:105
static Address TenBit(uint16_t address)
Definition: address.h:72
static constexpr Address TenBit()
Definition: address.h:56