/
tentacle.cpp
109 lines (83 loc) · 2.08 KB
/
tentacle.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
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
#include "tentacle.h"
// Make library cross-compatiable
// with Arduino, GNU C++ for tests, and Spark.
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#elif defined(SPARK)
#include "application.h"
#endif
Tentacle::Tentacle(size_t numPins) {
this->numPins = numPins;
configuredPinActions = new Tentacle::Action[numPins];
resetPinActions();
}
Tentacle::Action* Tentacle::getConfiguredPinActions() {
return configuredPinActions;
}
void Tentacle::resetPinActions() {
for(int i = 0; i < numPins; i++) {
configuredPinActions[i] = Tentacle::Action_ignore;
}
}
Tentacle::~Tentacle() {
delete configuredPinActions;
}
Tentacle& Tentacle::configurePin(int number, Tentacle::Action action) {
if( number < 0 || number >= numPins) {
return *this;
}
configuredPinActions[number] = action;
if(action == Tentacle::Action_ignore) {
return *this;
}
setMode(number, action);
return *this;
}
Tentacle& Tentacle::configurePins(Tentacle::Action* actions) {
for(int i = 0; i < numPins; i++) {
configurePin(i, actions[i]);
}
return *this;
}
int Tentacle::processPin(int number, int value) {
if( number < 0 || number >= numPins) {
return -1;
}
Tentacle::Action action = configuredPinActions[number];
switch(action) {
case Tentacle::Action_digitalWrite:
digitalWrite(number, value);
return value;
break;
case Tentacle::Action_pwmWrite:
case Tentacle::Action_analogWrite:
analogWrite(number, value);
return value;
break;
default:
break;
}
return processPin(number);
}
int Tentacle::processPin(int number) {
if( number < 0 || number >= numPins) {
return -1;
}
Tentacle::Action action = configuredPinActions[number];
switch(action) {
case Tentacle::Action_digitalRead:
case Tentacle::Action_digitalReadPullup:
return digitalRead(number);
break;
case Tentacle::Action_analogRead:
case Tentacle::Action_analogReadPullup:
return analogRead(number);
break;
default:
break;
}
return -1;
}
int Tentacle::getNumPins() const {
return numPins;
}