Digital IO
 All Classes Files Functions Variables Macros Groups Pages
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 onlyd
31 #include <util/atomic.h>
32 #include <avr/io.h>
33 //------------------------------------------------------------------------------
38 class PinIO {
39  public:
41  PinIO() : bit_(0), mask_(0XFF) {}
42  explicit PinIO(uint8_t pin);
43  bool begin(uint8_t pin);
44  void config(bool mode, bool data);
45  //----------------------------------------------------------------------------
47  inline __attribute__((always_inline))
48  bool read() {return *pinReg_ & bit_;}
49  //----------------------------------------------------------------------------
55  inline __attribute__((always_inline))
56  void toggle() {*pinReg_ = bit_;}
57  //============================================================================
64  inline __attribute__((always_inline))
65  void highI() {writeI(1);}
72  inline __attribute__((always_inline))
73  void lowI() {writeI(0);}
83  inline __attribute__((always_inline))
84  void modeI(bool mode) {
85  volatile uint8_t* ddrReg = pinReg_ + 1;
86  *ddrReg = mode ? *ddrReg | bit_ : *ddrReg & mask_;
87  }
96  inline __attribute__((always_inline))
97  void writeI(bool level) {
98  *portReg_ = level ? *portReg_ | bit_ : *portReg_ & mask_;
99  }
100  //============================================================================
107  inline __attribute__((always_inline))
108  void high() {ATOMIC_BLOCK(ATOMIC_FORCEON) {highI();}}
115  inline __attribute__((always_inline))
116  void low() {ATOMIC_BLOCK(ATOMIC_FORCEON) {lowI();}}
126  inline __attribute__((always_inline))
127  void mode(bool pinMode) {ATOMIC_BLOCK(ATOMIC_FORCEON) {modeI(pinMode);}}
136  inline __attribute__((always_inline))
137  void write(bool level) {ATOMIC_BLOCK(ATOMIC_FORCEON) {writeI(level);}}
138  //----------------------------------------------------------------------------
139  private:
140  uint8_t bit_;
141  uint8_t mask_;
142  volatile uint8_t* pinReg_;
143  volatile uint8_t* portReg_;
144 };
145 #endif // __AVR__
146 #endif // PinIO_h
void high()
Definition: PinIO.h:108
bool read()
Definition: PinIO.h:48
PinIO()
Definition: PinIO.h:41
void modeI(bool mode)
Definition: PinIO.h:84
void write(bool level)
Definition: PinIO.h:137
void highI()
Definition: PinIO.h:65
void lowI()
Definition: PinIO.h:73
void toggle()
Definition: PinIO.h:56
bool begin(uint8_t pin)
Definition: PinIO.cpp:44
void low()
Definition: PinIO.h:116
void writeI(bool level)
Definition: PinIO.h:97
AVR port I/O with runtime pin numbers.
Definition: PinIO.h:38
void mode(bool pinMode)
Definition: PinIO.h:127
void config(bool mode, bool data)
Definition: PinIO.cpp:63