-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplePinBit.hpp
More file actions
111 lines (87 loc) · 2.63 KB
/
simplePinBit.hpp
File metadata and controls
111 lines (87 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef TMP_SIMPLE_PIN_BIT_HPP
#define TMP_SIMPLE_PIN_BIT_HPP
// ----------------------------------------------------------------------------------------------------
#include "simplePin.hpp"
#include <stddef.h>
#include <stdint.h>
// ----------------------------------------------------------------------------------------------------
/**
* @brief PinBit simulates a HW pin via single bit in a byte - readonly.
* Pleaes note that the referenced memory needs to be static, so its address can be passed as a
* template parameter.
* The bits are numbered from 0 to 7.
*/
template <uint8_t Bit_,
uint8_t const * Byte_,
size_t ByteOffset_ = 0>
class SimplePinBitRead : public SimplePin
{
public:
// static uint8_t constexpr Bit = Bit_;
// static uint8_t * constexpr Byte = Byte_;
// static size_t constexpr ByteOffset = ByteOffset_;
static_assert(Bit_ < 8);
static void initialize()
{
// intentionally empty
}
static void deinitialize()
{
// intentionally empty
}
static State get()
{
return ((value_() & bitMask_) == (0b1 << Bit_)) ? State::One : State::Zero;
}
protected:
static uint8_t constexpr bitMask_ = (0b1 << Bit_);
private:
static inline uint8_t const & value_()
{
return *(Byte_ + ByteOffset_);
}
SimplePinBitRead() = delete;
};
// ----------------------------------------------------------------------------------------------------
/**
* @brief PinBit simulates a HW pin via single bit in a byte.
* Pleaes note that the referenced memory needs to be static, so its address can be passed as a
* template parameter.
* The bits are numbered from 0 to 7.
*/
template <uint8_t Bit_,
uint8_t * Byte_,
size_t ByteOffset_ = 0>
class SimplePinBit : public SimplePinBitRead<Bit_, const_cast<uint8_t const *>(Byte_), ByteOffset_>
{
public:
typedef SimplePinBitRead<Bit_, const_cast<uint8_t const *>(Byte_), ByteOffset_> Base;
static void set(SimplePin::State const state)
{
switch (state)
{
case Base::State::Zero:
{
value_() &= ~SimplePinBit::bitMask_;
break;
}
case SimplePin::State::One:
{
value_() |= SimplePinBit::bitMask_;
break;
}
}
}
static void toggle()
{
value_() ^= SimplePinBit::bitMask_;
}
private:
static inline uint8_t & value_()
{
return *(Byte_ + ByteOffset_);
}
SimplePinBit() = delete;
};
// ----------------------------------------------------------------------------------------------------
#endif // TMP_SIMPLE_PIN_BIT_HPP