-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArduinoIOPins.h
198 lines (157 loc) · 5.15 KB
/
ArduinoIOPins.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef ARDUINOIOPINS_H_
#define ARDUINOIOPINS_H_
#if ARDUINO < 100
#include "WProgram.h"
#else
#include "Arduino.h"
#endif
/** Basic Arduino pin methods class */
class ArduinoPin {
protected :
/** pin number */
uint8_t arduinoPin;
public :
/** Default constructor */
ArduinoPin(){}
/** Pin setting included constructor. */
ArduinoPin(uint8_t pin) : arduinoPin(pin){}
/** Destructor */
virtual ~ArduinoPin(){}
// Pin setting
/** Pin mode setting */
void setPinMode(uint8_t mode){pinMode(arduinoPin,mode);}
/** Pin and mode setting */
void setPin(uint8_t pin){arduinoPin = pin;}
// Pin init
/** Pin and mode setting */
void initPin(uint8_t pin, uint8_t mode){setPin(pin);setPinMode(mode);}
/** Pin initialization as Output */
void initOutput(uint8_t pin) {initPin(pin,OUTPUT);}
/** Pin initialization as Input */
void initInput(uint8_t pin) {initPin(pin,INPUT);}
/** Sets pin mode to OUTPUT */
void initOutput(){setPinMode(OUTPUT);}
/** Sets pin mode to INPUT */
void initInput() {setPinMode(INPUT);}
// Value Reading
/** digitalRead */
virtual int readDigital(){return digitalRead(arduinoPin);}
/** analogRead */
virtual int readAnalog(){return analogRead(arduinoPin);}
// Value Writing
/** digitalWrite */
virtual void writeDigital(uint8_t value){digitalWrite(arduinoPin,value);}
/** analogWrite (PWM) */
virtual void writeAnalog(uint8_t value){analogWrite(arduinoPin,value);}
};
/**
* Digital input pin abstraction. Includes methods for input reading and change detection. Input state is whatched.
* - Use isHigh() or isLow() to check input.
* - inputChanged(), raisingChaned() and fallingChanged() will check for input changes.
*/
class DigitalInputPin : public ArduinoPin{
public :
uint8_t lastInput;
/** Default constructor */
DigitalInputPin () : ArduinoPin(){}
/** Pin setting included constructor. */
DigitalInputPin(uint8_t pin) : ArduinoPin(pin){initInput();}
virtual ~DigitalInputPin(){}
/** Checks if input is HIGH */
bool isHigh(){lastInput = readDigital();return lastInput;}
/** Checks if input is LOW */
bool isLow(){return !isHigh();}
// Change detection
/** Checks if input state changed */
bool inputChanged(){
bool i = lastInput;
return (i!=isHigh());
}
/** Checks if input state changed from LOW to HIGH */
bool raisingChanged(){
bool i = lastInput;
return i == false ? isHigh(): false;
}
/** Checks if input state changed from HIGH to LOW */
bool fallingChanged(){
bool i = lastInput;
return i == true ? isLow(): false;
}
};
/** Analog pin extension. By now, just average reading extension included.*/
class AnalogInputPin : public ArduinoPin{
public :
/** Default constructor */
AnalogInputPin () : ArduinoPin(){}
/** Pin setting included constructor. */
AnalogInputPin(uint8_t pin) : ArduinoPin(pin){initInput();}
/** Destructor */
virtual ~AnalogInputPin(){}
/**
* Gets an average meausure given the number of samples. Reading is static.
* @param nSamples Number of consecutive samples to take for the meausure
*/
int readAverage(unsigned int nSamples){
unsigned long value = 0;
unsigned int n=0;
while(n++ < nSamples){value += (readAnalog());}
return value/nSamples;
}
/**
* Gets an average meausure given the number of samples and sample period. Reading is static.
* @param nSamples Number of consecutive samples to take.
* @param period Sample period un miliseconds
* TODO fix
*/
int readAverage(unsigned int nSamples,unsigned int period){
unsigned long value = 0;
unsigned long micros = 1000*period - 215;
unsigned int n=0;
while(n++ < nSamples){value += (readAnalog());delayMicroseconds(micros);}
return value/nSamples;
}
uint8_t to8bit(int n){return map(n,0,1024,0,255);}
uint8_t readto8bit(){return to8bit(readAnalog());}
};
/** Digital output pin abstraction. This pin class whatches for output state when tracking is needed.
* - Value writing will be stored.
* - high() and low() methods write HIGH or LOW to output.
* - changeState() Inverts the output state.
*
*/
class DigitalOutputPin : public ArduinoPin{
protected :
uint8_t outputState;
void storeOutputValue(uint8_t newValue){outputState = newValue;}
public :
/** Default constructor */
DigitalOutputPin () : ArduinoPin(){}
/** Pin setting included constructor. */
DigitalOutputPin(uint8_t pin) : ArduinoPin(pin){initOutput();}
virtual ~DigitalOutputPin(){}
// Output writing.
/** Writes and stores state to output */
virtual void writeDigital(uint8_t state){
storeOutputValue(state);
ArduinoPin::writeDigital(outputState);
}
/** Writes and stores PWM value on output */
virtual void writeAnalog(uint8_t value){
storeOutputValue(value);
ArduinoPin::writeAnalog(outputState);
}
/** Sets output to high */
void high(){writeDigital(HIGH);}
/** Sets output to low */
void low(){writeDigital(LOW);}
/** Inverts the state of the output */
void changeState(){writeDigital(!outputState);}
// Output checking.
/** Checks if output was set to High */
virtual bool isHigh(){return outputState;}
/** Checks if output was set to Low */
virtual bool isLow(){return !outputState;}
/** Returns last stored value/state */
uint8_t outputValue(){ return outputState;}
};
#endif /* ARDUINOIOPINS_H_ */