Digital IO
Arduino/libraries/DigitalIO/PinIO.h
Go to the documentation of this file.
00001 /* Arduino DigitalIO Library
00002  * Copyright (C) 2013 by William Greiman
00003  *
00004  * This file is part of the Arduino DigitalIO Library
00005  *
00006  * This Library is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with the Arduino DigitalIO Library.  If not, see
00018  * <http://www.gnu.org/licenses/>.
00019  */
00028 #ifndef PinIO_h
00029 #define PinIO_h
00030 #include <util/atomic.h>
00031 #include <avr/io.h>
00032 //------------------------------------------------------------------------------
00037 class PinIO {
00038  public:
00040   PinIO() : bit_(0), mask_(0XFF) {}
00041   explicit PinIO(uint8_t pin);
00042   bool begin(uint8_t pin);
00043   void config(bool mode, bool data);
00044   //----------------------------------------------------------------------------
00046   inline __attribute__((always_inline))
00047   bool read() {return *pinReg_ & bit_;}
00048   //----------------------------------------------------------------------------
00054   inline __attribute__((always_inline))
00055   void toggle() {*pinReg_ = bit_;}
00056   //============================================================================
00063   inline __attribute__((always_inline))
00064   void highI() {writeI(1);}
00071   inline __attribute__((always_inline))
00072   void lowI() {writeI(0);}
00082   inline __attribute__((always_inline))
00083   void modeI(bool mode) {
00084     volatile uint8_t* ddrReg = pinReg_ + 1;
00085     *ddrReg = mode ? *ddrReg | bit_ : *ddrReg & mask_;
00086   }
00095   inline __attribute__((always_inline))
00096   void writeI(bool level) {
00097     *portReg_ = level ? *portReg_ | bit_ : *portReg_ & mask_;
00098   }
00099   //============================================================================
00106   inline __attribute__((always_inline))
00107   void high() {ATOMIC_BLOCK(ATOMIC_FORCEON) {highI();}}
00114   inline __attribute__((always_inline))
00115   void low() {ATOMIC_BLOCK(ATOMIC_FORCEON) {lowI();}}
00125   inline __attribute__((always_inline))
00126   void mode(bool pinMode) {ATOMIC_BLOCK(ATOMIC_FORCEON) {modeI(pinMode);}}
00135   inline __attribute__((always_inline))
00136   void write(bool level) {ATOMIC_BLOCK(ATOMIC_FORCEON) {writeI(level);}}
00137   //----------------------------------------------------------------------------
00138  private:
00139   uint8_t bit_;
00140   uint8_t mask_;
00141   volatile uint8_t* pinReg_;
00142   volatile uint8_t* portReg_;
00143 };
00144 #endif  // PinIO_h
00145 
 All Classes Files Functions Variables Defines