-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasyPin.cpp
More file actions
122 lines (107 loc) · 3.19 KB
/
EasyPin.cpp
File metadata and controls
122 lines (107 loc) · 3.19 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* ----------------------------------------------------------------
* Copyright (C) 2020 Luis Acosta <zerfoinder@gmail.com>
*
* EasyPin library, This library is used to manage in a simple way all
* Arduino pins.
*
* www.github.com/Zerfoinder/EasyPin (github as default source provider)
*
* This file is part of EasyPin library.
*
* EasyPin 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.
*
* EasyPin 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 this program. If not, see <https://www.gnu.org/licenses/>.
* ----------------------------------------------------------------
*/
/**
* name: EasyPin
* version: 0.1.3
* Author: Luis Acosta <zerfoinder@gmail.com>
* Date: 2021-07-11
* Description: EasyPin is a library for simplify arduino pins management.
*/
#include <Arduino.h>
#include "EasyPin.h"
namespace zsoft::io::utilities {
EasyPin::EasyPin(int pinNumber, PinType ptype) {
_pinNumber = pinNumber;
_value = 0;
_state = false;
_type = ptype;
}
void EasyPin::init() {
switch (_type) {
case PinType::OUT:
pinMode(_pinNumber, OUTPUT);
break;
case PinType::IN:
pinMode(_pinNumber, INPUT);
break;
case PinType::PULLUP:
pinMode(_pinNumber, INPUT_PULLUP);
break;
}
}
void EasyPin::on() {
if (_type == PinType::OUT) {
_state = true;
_value = 255;
digitalWrite(_pinNumber, HIGH);
}
}
void EasyPin::off() {
if (_type == PinType::OUT) {
_state = false;
_value = 0;
digitalWrite(_pinNumber, LOW);
}
}
void EasyPin::toggle() {
if (_type == PinType::OUT) {
if (_state) {
off();
} else {
on();
}
}
}
void EasyPin::setValue(int value) {
if (_type == PinType::OUT) {
if (value <= 0) {
value = 0;
_value = value;
_state = false;
analogWrite(_pinNumber, _value);
} else {
if (value > 255) {
value = 255;
}
_value = value;
_state = true;
analogWrite(_pinNumber, _value);
}
}
}
bool EasyPin::state() {
return _state;
}
int EasyPin::value() {
return _value;
}
unsigned long EasyPin::readValue() {
unsigned long readValue = 0;
if (_type == PinType::IN || _type == PinType::PULLUP ) {
readValue = digitalRead(_pinNumber);
}
return readValue;
}
} // end namespace zsoft::io::utilities