forked from ctlbox/controlbox-cpp
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathValueActuator.h
122 lines (96 loc) · 2.26 KB
/
ValueActuator.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
#pragma once
#include "Values.h"
#include "Actuator.h"
/**
*
*/
class LogicActuator : public Value
{
/**
* The number of actuators that must return true for this one to return true.
* 0 - this functions as a NAND gate
* 1 - functions as a OR gate
* 2 - functions as a AND gate
*/
uint8_t count;
Value* v1; Value* v2;
private:
uint8_t active(Value* v) {
uint8_t data = 0;
BufferDataOut out(&data, 1);
if (v!=NULL)
v->readTo(out);
return data;
}
public:
LogicActuator(uint8_t _count, Value* _v1, Value* _v2)
: count(_count), v1(_v1), v2(_v2) {}
/**
* The value is 0 if not active, or non-zero if active.
*/
void writeFrom(DataIn& out) {
// doesn't support overrides - what to do here?
// - change base Actautor interface to ActuatorOutput (supports query state not setting state)
// - allow value to be written: 0 off, 1 on, -1 logic
}
void readTo(DataOut& out) {
out.write(isActive());
}
// todo - if there are other simple truth values ensure this class is refactored to avoid code-duplication
uint8_t streamSize() { return 1; }
bool isActive() {
return active(v1) + active(v2) == count;
}
/**
* data is a sequence of
*/
static Object* create(ObjectDefinition& def)
{
LogicActuator* result = new LogicActuator(
def.in->next(),
(Value*)lookupUserObject(*def.in),
(Value*)lookupUserObject(*def.in)
);
return result;
}
};
#ifdef ARDUINO
class DigitalPinActuator : public Actuator
{
private:
bool invert;
uint8_t pin;
bool active;
public:
DigitalPinActuator(uint8_t pin, bool invert) {
this->invert = invert;
this->pin = pin;
setActive(false);
pinMode(pin, OUTPUT);
}
void readTo(DataOut& out) {
out.write(isActive());
}
void writeMaskedFrom(DataIn& in, DataIn& mask) {
setActive(in.next() & mask.next());
}
uint8_t streamSize() {
return 1;
}
inline void setActive(bool active) {
this->active = active;
digitalWrite(pin, active^invert ? HIGH : LOW);
}
bool isActive() { return active; }
/*
* Params: 1 - byte
* IPPPPPPP
* I = invert (0 not inverted, 1 inverted)
* P = bits for pin number.
*/
static Object* create(ObjectDefinition& def) {
uint8_t v = def.in->next();
return new_object(DigitalPinActuator(v&0x7f, v&0x80));
}
};
#endif