M5Utility 0.0.2 git rev:5c1a751
Loading...
Searching...
No Matches
misc.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UTILITY_MISC_HPP
11#define M5_UTILITY_MISC_HPP
12
13#include <cstdint>
14
15namespace m5 {
16namespace utility {
17
19inline bool isValidI2CAddress(const uint16_t addr)
20{
21 if (addr <= 0x7F) { // 7 bit
22 return (addr >= 0x08 && addr <= 0x77);
23 }
24 return addr <= 0x3FF; // 10 bit
25}
26
28inline uint8_t reverseBitOrder(const uint8_t u8)
29{
30#if defined(__clang__) && 0
31#pragma message "Using clang builtin"
32 return __builtin_bitreverse8(u8);
33#else
34 uint8_t v{u8};
35 v = ((v & 0xF0) >> 4) | ((v & 0x0F) << 4);
36 v = ((v & 0xCC) >> 2) | ((v & 0x33) << 2);
37 v = ((v & 0xAA) >> 1) | ((v & 0x55) << 1);
38 return v;
39#endif
40}
41
43inline uint16_t reverseBitOrder(const uint16_t u16)
44{
45#if defined(__clang__) && 0
46#pragma message "Using clang builtin"
47 return __builtin_bitreverse16(u16);
48#else
49 uint16_t v{u16};
50 v = ((v & 0xFF00) >> 8) | ((v & 0x00FF) << 8);
51 v = ((v & 0xF0F0) >> 4) | ((v & 0x0F0F) << 4);
52 v = ((v & 0xCCCC) >> 2) | ((v & 0x3333) << 2);
53 v = ((v & 0xAAAA) >> 1) | ((v & 0x5555) << 1);
54 return v;
55#endif
56}
57
58} // namespace utility
59} // namespace m5
60#endif
bool isValidI2CAddress(const uint16_t addr)
Valid I2C address?
Definition misc.hpp:19
uint8_t reverseBitOrder(const uint8_t u8)
Reversing the bit order.
Definition misc.hpp:28
Top level namespace of M5.
Definition bit_segment.hpp:17
For utilities.