Update oXs_out_frsky.cpp
[openXsensor.git] / locator_receiver / utility / DigitalOutput.h
blob03ddedbcb8ec11b9d0a034987190f83b824444f5
1 /* Arduino SSD1306Ascii Library
2 * Copyright (C) 2015 by William Greiman
4 * This file is part of the Arduino SSD1306Ascii Library
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with the Arduino SSD1306Ascii Library. If not, see
18 * <http://www.gnu.org/licenses/>.
20 /**
21 * @file DigitalOutput.h
22 * @brief Faster version of digitalWrite().
24 #ifndef DigitalOutput_h
25 #define DigitalOutput_h
27 #include <Arduino.h>
28 #ifdef __AVR__
29 #include <util/atomic.h>
30 #include <avr/io.h>
31 /**
32 * @class DigitalOutput
33 * @brief Faster version of digitalWrite().
35 class DigitalOutput {
36 public:
37 /**
38 * @breif initialize the digital pin.
40 * @param[in] pin The Arduino pin number.
42 void begin(uint8_t pin) {
43 uint8_t port = digitalPinToPort(pin);
44 m_portReg = portOutputRegister(port);
45 m_bit = digitalPinToBitMask(pin);
46 m_mask = ~m_bit;
47 pinMode(pin, OUTPUT);
49 /**
50 * @brief Set the level of a digital pin.
52 * @param[in] level The value to be set.
54 inline __attribute__((always_inline))
55 void write(bool level) {ATOMIC_BLOCK(ATOMIC_FORCEON) {writeI(level);}}
56 /**
57 * @brief Set the level of a digital pin.
59 * @param[in] level The value to be set.
60 * @note This function must be called with interrupts disabled.
62 inline __attribute__((always_inline))
63 void writeI(bool level) {
64 *m_portReg = level ? *m_portReg | m_bit : *m_portReg & m_mask;
67 private:
68 uint8_t m_bit;
69 uint8_t m_mask;
70 volatile uint8_t* m_portReg;
72 #else // _AVR_
73 /**
74 * @class DigitalOutput
75 * @brief Non AVR version of digitalWrite().
77 class DigitalOutput {
78 public:
79 /**
80 * @brief initialize the digital pin.
82 * @param[in] pin The Arduino pin number.
84 void begin(uint8_t pin) {
85 m_pin = pin;
86 pinMode(m_pin, OUTPUT);
88 /**
89 * @brief Set the level of a digital pin.
91 * @param[in] level The value to be set.
93 inline __attribute__((always_inline))
94 void write(bool level) {
95 digitalWrite(m_pin, level);
97 private:
98 uint8_t m_pin;
100 #endif // _AVR_
101 #endif // DigitalOutput_h