-
Notifications
You must be signed in to change notification settings - Fork 0
/
PinIO.cpp
executable file
·69 lines (68 loc) · 2.22 KB
/
PinIO.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Arduino DigitalIO Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino DigitalIO Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino DigitalIO Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Digital AVR port I/O with runtime pin number.
*
* @defgroup runtimeDigital Runtime Pin I/O
* @details Two Wire Interface library.
* @{
*/
#include <PinIO.h>
#include <util/atomic.h>
#include <Arduino.h>
//==============================================================================
/** Constructor
* @param[in] pin Pin assigned to this object.
*/
PinIO::PinIO(uint8_t pin) {
begin(pin);
}
//------------------------------------------------------------------------------
/** Initialize pin bit mask and port address.
* @param[in] pin Arduino board pin number.
* @return true for success or false if invalid pin number.
*/
bool PinIO::begin(uint8_t pin) {
if (pin >= NUM_DIGITAL_PINS) return false;
uint8_t port = digitalPinToPort(pin);
pinReg_ = portInputRegister(port);
bit_ = digitalPinToBitMask(pin);
mask_ = ~bit_;
portReg_ = pinReg_ + 2;
return true;
}
//------------------------------------------------------------------------------
/** Configure the pin
*
* @param[in] mode Configure as output mode if true else input mode.
* @param[in] data For output mode set pin high if true else low.
* For input mode enable 20K pullup if true else Hi-Z.
*
* This function may be used with interrupts enabled or disabled.
* The previous interrupt state will be restored.
*/
void PinIO::config(bool mode, bool data) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
modeI(mode);
writeI(data);
}
}
/** @} */