Speeduino
Loading...
Searching...
No Matches
int16_byte.h
Go to the documentation of this file.
1
6#pragma once
7
8
9#include <stdint.h>
10
11#ifdef USE_LIBDIVIDE
12#include "src/libdivide/libdivide.h"
13#endif
14
16typedef uint8_t byte;
17
25{
26public:
27
37 constexpr int16_byte(uint8_t factor
38#ifdef USE_LIBDIVIDE
39 , const libdivide::libdivide_s16_t &divider
40#endif
41 )
42 : _factor(factor)
43#ifdef USE_LIBDIVIDE
44 , _divider(divider)
45#endif
46 {
47 }
48
50#ifdef USE_LIBDIVIDE
51 inline byte to_byte(int16_t value) const { return _factor==1 ? value : _factor==2 ? value>>1 : (byte)libdivide::libdivide_s16_do(value, &_divider); }
52#else
53 inline byte to_byte(int16_t value) const { return (byte)(value/_factor); }
54#endif
56 inline int16_t from_byte( byte in ) const { return (int16_t)in * _factor; }
57
58private:
59 uint8_t _factor;
60#ifdef USE_LIBDIVIDE
61 libdivide::libdivide_s16_t _divider;
62#endif
63};
64
Represents a 16-bit value as a byte. Useful for I/O.
Definition: int16_byte.h:25
int16_t from_byte(byte in) const
Convert from a byte.
Definition: int16_byte.h:56
byte to_byte(int16_t value) const
Convert to a byte.
Definition: int16_byte.h:53
constexpr int16_byte(uint8_t factor)
Construct.
Definition: int16_byte.h:37
uint8_t byte
Byte type. This is not defined in any C or C++ standard header.
Definition: int16_byte.h:16