Digital IO
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  public:
42  PinIO() : bit_(0), mask_(0XFF) {}
43  explicit PinIO(uint8_t pin);
44  bool begin(uint8_t pin);
45  void config(uint8_t mode, bool data);
46  //----------------------------------------------------------------------------
48  inline __attribute__((always_inline))
49  bool read() {return *pinReg_ & bit_;}
50  //----------------------------------------------------------------------------
56  inline __attribute__((always_inline))
57  void toggle() {*pinReg_ = bit_;}
58  //============================================================================
65  inline __attribute__((always_inline))
66  void highI() {writeI(1);}
73  inline __attribute__((always_inline))
74  void lowI() {writeI(0);}
85  inline __attribute__((always_inline))
86  void modeI(uint8_t mode) {
87  volatile uint8_t* ddrReg = pinReg_ + 1;
88  *ddrReg = mode == OUTPUT ? *ddrReg | bit_ : *ddrReg & mask_;
89  if (mode != OUTPUT) {
90  writeI(mode == INPUT_PULLUP);
91  }
92  }
93 
102  inline __attribute__((always_inline))
103  void writeI(bool level) {
104  *portReg_ = level ? *portReg_ | bit_ : *portReg_ & mask_;
105  }
106  //============================================================================
113  inline __attribute__((always_inline))
114  void high() {ATOMIC_BLOCK(ATOMIC_FORCEON) {highI();}}
115 
122  inline __attribute__((always_inline))
123  void low() {ATOMIC_BLOCK(ATOMIC_FORCEON) {lowI();}}
124 
136  inline __attribute__((always_inline))
137  void mode(uint8_t mode) {ATOMIC_BLOCK(ATOMIC_FORCEON) {modeI(mode);}}
138 
147  inline __attribute__((always_inline))
148  void write(bool level) {ATOMIC_BLOCK(ATOMIC_FORCEON) {writeI(level);}}
149  //----------------------------------------------------------------------------
150  private:
151  uint8_t bit_;
152  uint8_t mask_;
153  volatile uint8_t* pinReg_;
154  volatile uint8_t* portReg_;
155 };
156 #endif // __AVR__
157 #endif // PinIO_h
158 
void high()
Definition: PinIO.h:114
bool read()
Definition: PinIO.h:49
PinIO()
Definition: PinIO.h:42
void write(bool level)
Definition: PinIO.h:148
void highI()
Definition: PinIO.h:66
void lowI()
Definition: PinIO.h:74
void toggle()
Definition: PinIO.h:57
void modeI(uint8_t mode)
Definition: PinIO.h:86
bool begin(uint8_t pin)
Definition: PinIO.cpp:44
static volatile uint8_t * ddrReg(uint8_t pin)
Definition: DigitalPin.h:65
void low()
Definition: PinIO.h:123
void writeI(bool level)
Definition: PinIO.h:103
void mode(uint8_t mode)
Definition: PinIO.h:137
AVR port I/O with runtime pin numbers.
Definition: PinIO.h:39
void config(uint8_t mode, bool data)
Definition: PinIO.cpp:63