forked from ivanseidel/ArduinoSensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigitalOut.h
100 lines (80 loc) · 1.71 KB
/
DigitalOut.h
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
/*
DigitalOut.h - A class to help with Digital Output pins.
Instantiate:
DigitalOut myOutputPin(13);
Set output:
myOutputPin = HIGH;
myOutputPin.turn(HIGH);
myOutputPin.turnOn();
For instructions, go to https://github.com/ivanseidel/Arduino-Sensors
Created by Ivan Seidel Gomes, June, 2013.
Released into the public domain.
*/
#ifndef DigitalOut_h
#define DigitalOut_h
#include "Arduino.h"
class DigitalOut
{
protected:
/*
The number of the pin
*/
int outPin;
/*
This attribute is a flag to indicate if
the output will be inverted.
If set to true, then turning "ON" actualy
turns OFF.
(Eg. of usage: Relays that activate on LOW)
*/
bool invert;
public:
/*
Pin: Analog pin to read value
Invert: Should invert the output signal?
*/
DigitalOut(int _outPin, bool _invert = false){
pinMode(_outPin, OUTPUT);
outPin = _outPin;
invert = _invert;
turn(LOW);
}
/*
Return the output signal of this pin
*/
virtual operator bool(){
return (digitalRead(outPin) ^ invert);
}
/*
Set the output to the desired value;
Use as:
digitalOutObject = true; (Turns on)
digitalOutObject = HIGH; (Turns on)
digitalOutObject = LOW; (Turns off)
*/
virtual void operator=(bool on){
turn(on);
}
/*
Set the output to HIGH or LOW depending on the boolean on
and the invert attribute
*/
virtual void turn(bool on){
#if defined(__arm__)
// A little bit faster than digitalWrite
PIO_SetOutput( g_APinDescription[outPin].pPort, g_APinDescription[outPin].ulPin, on ^ invert, 0, PIO_PULLUP ) ;
#else
digitalWrite(outPin, on ^ invert);
#endif
}
/*
Facilitate methods
*/
virtual void turnOn(){
turn(HIGH);
}
virtual void turnOff(){
turn(LOW);
}
};
#endif