MaxMatrix
Drive chained 8x8 LED dot matrix modules with MAX7219.
Loading...
Searching...
No Matches
Byte.h
Go to the documentation of this file.
1// Copyright 2026 Andreas Burnickl
2// SPDX-License-Identifier: GPL-3.0-or-later
3
8
9#ifndef MAXMATRIX_SRC_UTILITY_BYTE_H_
10#define MAXMATRIX_SRC_UTILITY_BYTE_H_
11
12#include <stdint.h>
13
14#include "MaxMatrixAttributes.h"
15#include "Utility/Bit.h"
16
17namespace max_matrix {
18
37/*
38 * A class rather than the enum class of CodingStyle.md T-17: before C++17 an
39 * enum class cannot be made from a number in braces, Byte{0x3C}, and a sketch
40 * would have to write a static_cast (OIL 41). Named Byte against the names of
41 * CodingStyle.md, which give a type that mimics the standard's its name: a
42 * max_matrix::byte would meet Arduino's byte in a sketch that is using
43 * namespace max_matrix.
44 */
45class Byte {
46 public:
51 // cppcheck-suppress uninitMemberVar ; T-13, the reason is at value_
52 Byte() = default;
53
59 constexpr explicit Byte(uint8_t value) : value_{value} {}
60
62 // A member, where std::byte's to_integer() is a free function: one that
63 // reads the private bits would be a friend, which C-12 rules out.
64 MAXMATRIX_NODISCARD constexpr uint8_t ToInteger() const { return value_; }
65
67 constexpr Byte operator~() const {
68 return Byte{static_cast<uint8_t>(~value_)};
69 }
70
77 friend constexpr bool operator==(Byte left, Byte right) {
78 return left.value_ == right.value_;
79 }
80
87 friend constexpr bool operator!=(Byte left, Byte right) {
88 return !(left == right);
89 }
90
97 friend constexpr Byte operator&(Byte left, Byte right) {
98 return Byte{static_cast<uint8_t>(left.value_ & right.value_)};
99 }
100
107 friend constexpr Byte operator|(Byte left, Byte right) {
108 return Byte{static_cast<uint8_t>(left.value_ | right.value_)};
109 }
110
118 friend constexpr Byte operator<<(Byte value, uint8_t shift) {
119 return Byte{static_cast<uint8_t>(value.value_ << shift)};
120 }
121
129 friend constexpr Byte operator>>(Byte value, uint8_t shift) {
130 return Byte{static_cast<uint8_t>(value.value_ >> shift)};
131 }
132
133 private:
134 /*
135 * T-13: not initialized here, as std::byte's is not. Initialized, it made the
136 * constructor not trivial: the buffer of every MatrixOf was cleared by a loop
137 * at start, and memset() could not empty it, which cost the examples up to 66
138 * bytes of flash on the Uno. Byte{} and Byte{value} set it.
139 */
140 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init): T-13, see above
141 uint8_t value_;
142};
143
150constexpr bool ReadBit(Byte value, uint8_t bit) {
151 return Bit<uint8_t>::ReadBit(value.ToInteger(), bit);
152}
153
162constexpr Byte WriteBit(Byte value, uint8_t bit, bool set) {
163 return Byte{Bit<uint8_t>::WriteBit(value.ToInteger(), bit, set)};
164}
165
166} // namespace max_matrix
167
168#endif // MAXMATRIX_SRC_UTILITY_BYTE_H_
Single bits and groups of bits of an unsigned integer: the one place where bitwise operators stand (d...
constexpr bool ReadBit(Byte value, uint8_t bit)
Returns whether a bit is set, as the core's bitRead() for a number.
Definition Byte.h:150
constexpr Byte WriteBit(Byte value, uint8_t bit, bool set)
Returns value with one bit set or cleared, as the core's bitWrite() for a number.
Definition Byte.h:162
The attributes of a later standard, as macros, where the compiler has them.
#define MAXMATRIX_NODISCARD
Marks a function whose result must not be dropped: the compiler warns where it is.
Definition MaxMatrixAttributes.h:37
static constexpr bool ReadBit(Integer value, uint8_t position)
Returns whether the bit at position of value is set.
Definition Bit.h:87
static constexpr Integer WriteBit(Integer value, uint8_t position, bool set)
Returns value with the bit at position set or cleared.
Definition Bit.h:99
A byte of raw data: eight dots of a column or a row, or the value of a register of the MAX7219.
Definition Byte.h:45
friend constexpr bool operator==(Byte left, Byte right)
Returns whether two bytes have the same bits.
Definition Byte.h:77
Byte()=default
Makes a byte whose bits are not set to anything, as std::byte; Byte{} has none set.
friend constexpr Byte operator<<(Byte value, uint8_t shift)
Returns the bits moved shift places up; those beyond bit 7 fall out.
Definition Byte.h:118
friend constexpr Byte operator&(Byte left, Byte right)
Returns the bits set in both bytes.
Definition Byte.h:97
MAXMATRIX_NODISCARD constexpr uint8_t ToInteger() const
Returns the bits as a number.
Definition Byte.h:64
constexpr Byte operator~() const
Returns the bits that are not set in this byte.
Definition Byte.h:67
friend constexpr Byte operator|(Byte left, Byte right)
Returns the bits set in either byte.
Definition Byte.h:107
friend constexpr bool operator!=(Byte left, Byte right)
Returns whether two bytes differ in a bit.
Definition Byte.h:87
constexpr Byte(uint8_t value)
Makes a byte of the bits of value.
Definition Byte.h:59
friend constexpr Byte operator>>(Byte value, uint8_t shift)
Returns the bits moved shift places down; those below bit 0 fall out.
Definition Byte.h:129