MySensors Library & Examples  2.3.2-62-ge298769
PinIO.h
Go to the documentation of this file.
1 /* Arduino DigitalIO Library
2  * Copyright (C) 2013 by William Greiman
3  *
4  * This file is part of the Arduino DigitalIO Library
5  *
6  * This Library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with the Arduino DigitalIO Library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
28 #ifndef PinIO_h
29 #define PinIO_h
30 #if defined(__AVR__) || defined(DOXYGEN) // AVR only
31 #include <Arduino.h>
32 #include <util/atomic.h>
33 #include <avr/io.h>
34 //------------------------------------------------------------------------------
39 class PinIO
40 {
41 public:
43  // cppcheck-suppress uninitMemberVar
44  PinIO() : bit_(0), mask_(0XFF) {}
45  explicit PinIO(uint8_t pin);
46  bool begin(uint8_t pin);
47  void config(uint8_t mode, bool data);
48  //----------------------------------------------------------------------------
50  inline __attribute__((always_inline))
51  bool read()
52  {
53  return *pinReg_ & bit_;
54  }
55  //----------------------------------------------------------------------------
61  inline __attribute__((always_inline))
62  void toggle()
63  {
64  *pinReg_ = bit_;
65  }
66  //============================================================================
73  inline __attribute__((always_inline))
74  void highI()
75  {
76  writeI(1);
77  }
84  inline __attribute__((always_inline))
85  void lowI()
86  {
87  writeI(0);
88  }
99  inline __attribute__((always_inline))
100  void modeI(uint8_t mode)
101  {
102  volatile uint8_t* ddrReg = pinReg_ + 1;
103  *ddrReg = mode == OUTPUT ? *ddrReg | bit_ : *ddrReg & mask_;
104  if (mode != OUTPUT) {
105  writeI(mode == INPUT_PULLUP);
106  }
107  }
108 
117  inline __attribute__((always_inline))
118  void writeI(bool level)
119  {
120  *portReg_ = level ? *portReg_ | bit_ : *portReg_ & mask_;
121  }
122  //============================================================================
129  inline __attribute__((always_inline))
130  void high()
131  {
132  ATOMIC_BLOCK(ATOMIC_FORCEON) {
133  highI();
134  }
135  }
136 
143  inline __attribute__((always_inline))
144  void low()
145  {
146  ATOMIC_BLOCK(ATOMIC_FORCEON) {
147  lowI();
148  }
149  }
150 
162  inline __attribute__((always_inline))
163  void mode(uint8_t mode)
164  {
165  ATOMIC_BLOCK(ATOMIC_FORCEON) {
166  modeI(mode);
167  }
168  }
169 
178  inline __attribute__((always_inline))
179  void write(bool level)
180  {
181  ATOMIC_BLOCK(ATOMIC_FORCEON) {
182  writeI(level);
183  }
184  }
185  //----------------------------------------------------------------------------
186 private:
187  uint8_t bit_;
188  uint8_t mask_;
189  volatile uint8_t* pinReg_;
190  volatile uint8_t* portReg_;
191 };
192 #endif // __AVR__
193 #endif // PinIO_h
194 
PinIO::__attribute__
__attribute__((always_inline)) void lowI()
Definition: PinIO.h:84
data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:654
PinIO::__attribute__
__attribute__((always_inline)) bool read()
Definition: PinIO.h:50
PinIO
AVR port I/O with runtime pin numbers.
Definition: PinIO.h:39
config
Config file.
Definition: config.h:30
PinIO::__attribute__
__attribute__((always_inline)) void writeI(bool level)
Definition: PinIO.h:117
PinIO::__attribute__
__attribute__((always_inline)) void write(bool level)
Definition: PinIO.h:178
PinIO::__attribute__
__attribute__((always_inline)) void high()
Definition: PinIO.h:129
PinIO::__attribute__
__attribute__((always_inline)) void highI()
Definition: PinIO.h:73
PinIO::__attribute__
__attribute__((always_inline)) void modeI(uint8_t mode)
Definition: PinIO.h:99
PinIO::__attribute__
__attribute__((always_inline)) void low()
Definition: PinIO.h:143
PinIO::__attribute__
__attribute__((always_inline)) void toggle()
Definition: PinIO.h:61
PinIO::PinIO
PinIO()
Definition: PinIO.h:44
PinIO::__attribute__
__attribute__((always_inline)) void mode(uint8_t mode)
Definition: PinIO.h:162