Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
NeoUtil.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 LumitronixIFlex library helper functions
3 
4 Written by Michael C. Miller.
5 
6 I invest time and resources providing this open source code,
7 please support me by dontating (see https://github.com/Makuna)
8 
9 -------------------------------------------------------------------------
10 This file is part of the LUMITRONIX_iFlex_Workshop library.
11 
12 LumitronixIFlexBus is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as
14 published by the Free Software Foundation, either version 3 of
15 the License, or (at your option) any later version.
16 
17 LumitronixIFlexBus is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Lesser General Public License for more details.
21 
22 You should have received a copy of the GNU Lesser General Public
23 License along with LumitronixIFlex. If not, see
24 <http://www.gnu.org/licenses/>.
25 -------------------------------------------------------------------------*/
26 
27 #pragma once
28 
29 // some platforms do not come with STL or properly defined one, specifically functional
30 // if you see...
31 // undefined reference to `std::__throw_bad_function_call()'
32 // ...then you can either add the platform symbol to the list so NEOPIXEBUS_NO_STL gets defined or
33 // go to boards.txt and enable c++ by adding (teensy31.build.flags.libs=-lstdc++) and set to "smallest code" option in Arduino
34 //
35 #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) || defined(STM32L432xx) || defined(STM32L476xx) || defined(ARDUINO_ARCH_SAM)
36 #define NEOPIXEBUS_NO_STL 1
37 #endif
38 
39 // some platforms do not define this standard progmem type for some reason
40 //
41 #ifndef PGM_VOID_P
42 #define PGM_VOID_P const void *
43 #endif
44 
45 #ifndef countof
46 #define countof(array) (sizeof(array)/sizeof(array[0]))
47 #endif
48 
49 class NeoUtil
50 {
51 private:
52  static constexpr uint8_t Reverse8BitsLookup[16] = {
53  0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
54  0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf };
55 
56 public:
57  inline static uint8_t Reverse8Bits(uint8_t n)
58  {
59  return (Reverse8BitsLookup[n & 0b1111] << 4) | Reverse8BitsLookup[n >> 4];
60  }
61 
62  inline static size_t RoundUp(size_t numToRound, size_t multiple)
63  {
64  return ((numToRound + multiple - 1) / multiple) * multiple;
65  }
66 
67  // alternatives that proved to be slower but left for more periodic testing
68  /*
69  // marginally slower than the table
70  static uint8_t Reverse8Bits(uint8_t b)
71  {
72  b = (b & 0b11110000) >> 4 | (b & 0b00001111) << 4;
73  b = (b & 0b11001100) >> 2 | (b & 0b00110011) << 2;
74  b = (b & 0b10101010) >> 1 | (b & 0b01010101) << 1;
75  return b;
76  }
77  */
78 
79  /* WAY TO SLOW
80  static uint8_t Reverse8Bits(uint8_t b)
81  {
82  return (b * 0x0202020202ULL & 0x010884422010ULL) % 1023;
83  }
84  */
85 };
Definition: NeoUtil.h:50
static size_t RoundUp(size_t numToRound, size_t multiple)
Definition: NeoUtil.h:62
static uint8_t Reverse8Bits(uint8_t n)
Definition: NeoUtil.h:57