-
Notifications
You must be signed in to change notification settings - Fork 0
/
avrpinspecializations.hpp
136 lines (110 loc) · 3.17 KB
/
avrpinspecializations.hpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifndef AVR_PIN_SPECIALIZATIONS_HPP
#define AVR_PIN_SPECIALIZATIONS_HPP
#include "avrinputoutput.hpp"
#include "avrpin.hpp"
/** AvrPinSpecializations - Classes abstracting the internal interface for
* using Avr pins.
* These classes are stripped to their bare minimum and support only the
* necessary functionality for the respective type.
* Please note that it is currently not supported to change the type of
* the pin during execution.
*
* As however all of these are template classes, one could create typedefs
* of different types [e.g. AvrPinInput and AvrPinOutput] for the same pin
* and re-"initialize()" those anywhere in the program - please note however
* that you as the user are responsible for having these classes correctly
* initialized before using any of their member functions.
*/
namespace // anonymous
{
template<class AvrPin, AvrInputOutput::PinState pinState>
struct AvrPinOutputHelper
{
static void initialize();
static void write();
};
template<class AvrPin>
struct AvrPinOutputHelper<AvrPin, AvrInputOutput::PinState::High>
{
static void initialize()
{
AvrPin::setType(AvrInputOutput::OutputHigh);
}
static void write()
{
AvrPin::setPort();
}
};
template<class AvrPin>
struct AvrPinOutputHelper<AvrPin, AvrInputOutput::PinState::Low>
{
static void initialize()
{
AvrPin::setType(AvrInputOutput::OutputLow);
}
static void write()
{
AvrPin::clearPort();
}
};
} // namespace anonymous
template<typename AvrIoRegister, unsigned pinNumber_>
struct AvrPinInput : protected AvrPin<AvrIoRegister, pinNumber_>
{
// static void initialize() = 0;
static AvrInputOutput::PinState read()
{
return avrPin_::readPin();
}
protected:
typedef AvrPin<AvrIoRegister, pinNumber_> avrPin_;
};
template<typename AvrIoRegister, unsigned pinNumber_>
struct AvrPinInputFloating : private AvrPinInput<AvrIoRegister, pinNumber_>
{
static void initialize()
{
avrPin_::setType(AvrInputOutput::Input);
}
private:
typedef typename AvrPinInput<AvrIoRegister, pinNumber_>::avrPin_ avrPin_;
};
template<typename AvrIoRegister, unsigned pinNumber_>
struct AvrPinInputPullup : private AvrPin<AvrIoRegister, pinNumber_>
{
static void initialize()
{
avrPin_::setType(AvrInputOutput::InputPullup);
}
private:
typedef typename AvrPinInput<AvrIoRegister, pinNumber_>::avrPin_ avrPin_;
};
template<typename AvrIoRegister, unsigned pinNumber_>
struct AvrPinOutput : private AvrPin<AvrIoRegister, pinNumber_>
{
template<AvrInputOutput::PinState pinState>
static void initialize()
{
AvrPinOutputHelper<avrPin_, pinState>::initialize();
}
template<AvrInputOutput::PinState pinState>
static void write()
{
AvrPinOutputHelper<avrPin_, pinState>::write();
}
static void toggle()
{
avrPin_::togglePort();
}
// static AvrInputOutput::PinState read()
// {
// return avrPin_::readPin();
// }
static AvrInputOutput::PinState read()
{
return avrPin_::readPort();
}
private:
typedef AvrPin<AvrIoRegister, pinNumber_> avrPin_;
};
#endif // AVR_PIN_SPECIALIZATIONS_HPP