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/>.
21 * @file DigitalOutput.h
22 * @brief Faster version of digitalWrite().
24 #ifndef DigitalOutput_h
25 #define DigitalOutput_h
29 #include <util/atomic.h>
32 * @class DigitalOutput
33 * @brief Faster version of digitalWrite().
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
);
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
);}}
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
;
70 volatile uint8_t* m_portReg
;
74 * @class DigitalOutput
75 * @brief Non AVR version of digitalWrite().
80 * @brief initialize the digital pin.
82 * @param[in] pin The Arduino pin number.
84 void begin(uint8_t pin
) {
86 pinMode(m_pin
, OUTPUT
);
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
);
101 #endif // DigitalOutput_h