atMETEO
An ATmega based weather station
pin.h
Go to the documentation of this file.
1 /*
2  * atMETEO - An ATmega based weather station
3  * Copyright (C) 2014-2015 Christian Fetzer
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #pragma once
21 
59 #include <external/avr_io_cpp.h>
60 
61 #include "lib/utils.h"
62 
63 namespace Avr
64 {
65 
77 template <typename DDR, typename PORT, typename PIN>
78 struct DigitalIo
79 {
80 protected:
81  static DDR m_DDR;
82  static PORT m_PORT;
83  static PIN m_PIN;
84 };
85 
86 #if defined(DDRA) || defined (DOXYGEN)
87 
90 using DigitalIoA = DigitalIo<
91  AVR_IOR_PARAM(DDRA), AVR_IOR_PARAM(PORTA), AVR_IOR_PARAM(PINA)>;
92 #endif
93 #if defined(DDRB) || defined (DOXYGEN)
94 
97 using DigitalIoB = DigitalIo<
98  AVR_IOR_PARAM(DDRB), AVR_IOR_PARAM(PORTB), AVR_IOR_PARAM(PINB)>;
99 #endif
100 #if defined(DDRC) || defined (DOXYGEN)
101 
104 using DigitalIoC = DigitalIo<
105  AVR_IOR_PARAM(DDRC), AVR_IOR_PARAM(PORTC), AVR_IOR_PARAM(PINC)>;
106 #endif
107 #if defined(DDRD) || defined (DOXYGEN)
108 
111 using DigitalIoD = DigitalIo<
112  AVR_IOR_PARAM(DDRD), AVR_IOR_PARAM(PORTD), AVR_IOR_PARAM(PIND)>;
113 #endif
114 
120 template <class TDigitalIo, uint8_t pinNumber>
121 struct OutputConfiguration : public TDigitalIo
122 {
126  void on() { Sensors::bitSet(this->m_PORT, pinNumber); }
127 
131  void off() { Sensors::bitClear(this->m_PORT, pinNumber); }
132 
137  void set(bool enable)
138  {
139  Sensors::bitWrite(this->m_PORT, pinNumber, enable);
140  }
141 
145  void flip() { Sensors::bitFlip(this->m_PORT, pinNumber); }
146 };
147 
154 template <class TDigitalIo, uint8_t pinNumber>
155 struct OutputConfigurationInverted : public TDigitalIo
156 {
160  void on() { Sensors::bitClear(this->m_PORT, pinNumber); }
161 
165  void off() { Sensors::bitSet(this->m_PORT, pinNumber); }
166 
171  void set(bool enable)
172  {
173  Sensors::bitWrite(this->m_PORT, pinNumber, !enable);
174  }
175 
179  void flip() { Sensors::bitFlip(this->m_PORT, pinNumber); }
180 };
181 
185 template <class TDigitalIo, uint8_t pinNumber>
187 {
188 };
189 
195 template <class TDigitalIo, uint8_t pinNumber>
196 struct InputConfiguration : public TDigitalIo
197 {
202  bool isSet() const { return Sensors::bitRead(this->m_PIN, pinNumber); }
203 };
204 
208 template <class TDigitalIo, uint8_t pinNumber>
210 {
211 };
212 
216 template<class TDigitalIo, uint8_t pinNumber>
217 struct OutputPin :
218  public OutputConfiguration<TDigitalIo, pinNumber>,
219  public InputConfigurationDisabled<TDigitalIo, pinNumber>
220 {
221  OutputPin() { Sensors::bitSet(this->m_DDR, pinNumber); }
222 };
223 
227 template <class TDigitalIo, uint8_t pinNumber>
229  public OutputConfigurationInverted<TDigitalIo, pinNumber>,
230  public InputConfigurationDisabled<TDigitalIo, pinNumber>
231 {
232  OutputPinInverted() { Sensors::bitSet(this->m_DDR, pinNumber); }
233 };
234 
238 template <class TDigitalIo, uint8_t pinNumber>
239 struct InputPin :
240  public OutputConfigurationDisabled<TDigitalIo, pinNumber>,
241  public InputConfiguration<TDigitalIo, pinNumber>
242 {
243  InputPin() { Sensors::bitClear(this->m_DDR, pinNumber); }
244 };
245 
249 template <class TDigitalIo, uint8_t pinNumber>
251  public OutputConfiguration<TDigitalIo, pinNumber>,
252  public InputConfiguration<TDigitalIo, pinNumber>
253 {
254  InputOutputPin() { setOutput(); }
255 
259  void setOutput()
260  {
263  }
264 
268  void setInput()
269  {
272  }
273 };
274  // \addtogroup libtarget_pin
276 
277 } // namespace Avr
bool isSet() const
Determines if the input pin is set.
Definition: pin.h:202
Input / Output pin (initially configured as output pin).
Definition: pin.h:250
void bitWrite(T &value, uint8_t bit, bool bitValue)
Writes the bit in the given value.
Definition: utils.h:106
void flip()
Flips output pin.
Definition: pin.h:179
void on()
Turns output pin on.
Definition: pin.h:126
Digital I/O register configuration.
Definition: pin.h:78
Output configuration parameter for pin (disabled).
Definition: pin.h:186
void off()
Turns output pin off.
Definition: pin.h:131
void on()
Turns output pin on.
Definition: pin.h:160
Input configuration parameter for pin (disabled).
Definition: pin.h:209
void bitFlip(T &value, uint8_t bit)
Flips the bit in the given value.
Definition: utils.h:79
Bit manipulation utilities.
Inverted output pin (active low configuration).
Definition: pin.h:228
Namespace containing all symbols of the AVR C++ utilities library.
Definition: adc.h:48
Output pin (normal mode).
Definition: pin.h:217
Input configuration parameter for pin (normal mode).
Definition: pin.h:196
bool bitRead(T &value, uint8_t bit)
Returns the bit in the given value.
Definition: utils.h:92
void bitSet(T &value, uint8_t bit)
Sets the bit in the given value to 1.
Definition: utils.h:54
void flip()
Flips output pin.
Definition: pin.h:145
Output configuration parameter for pin (for inverted output pins / active low configuration).
Definition: pin.h:155
Output configuration parameter for pin (normal mode).
Definition: pin.h:121
void off()
Turns output pin off.
Definition: pin.h:165
void setInput()
Use pin as input pin.
Definition: pin.h:268
Input pin.
Definition: pin.h:239
void setOutput()
Use pin as output pin.
Definition: pin.h:259
void bitClear(T &value, uint8_t bit)
Sets the bit in the given value to 0.
Definition: utils.h:67