-
Notifications
You must be signed in to change notification settings - Fork 65
/
requestCounter.device.nut
148 lines (117 loc) · 3.08 KB
/
requestCounter.device.nut
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
class MAX72XX {
displays = [
"\x01",
"\x02",
"\x03",
"\x04",
"\x05",
"\x06",
"\x07",
"\x08"
];
digits = [
"\x7E", //0
"\x30", //1
"\x6D", //2
"\x79", //3
"\x33", //4
"\x5B", //5
"\x5F", //6
"\x70", //7
"\x7F", //8
"\x73", //9
"\x00" //ALLOFF
];
OFF = 10;
_spi = null;
_latch = null;
constructor(spi, latchPin) {
this._spi = spi;
this._spi.configure(SIMPLEX_TX | MSB_FIRST, 400)
this._latch = latchPin;
this._latch.configure(DIGITAL_OUT);
this._latch.write(0);
setup();
}
function setup() {
// leave shutdown mode
_spi.write("\x0C\x01");
latch();
// scanner limit
_spi.write("\x0B\x07")
latch();
// no decode
_spi.write("\x09\x00");
latch();
// no test mode
_spi.write("\x0F\x00");
latch();
// set brightness
_spi.write("\x0A\x0F")
latch();
// turn all digits off
foreach(d in displays) {
_spi.write(d+digits[OFF]);
latch();
}
}
function latch() {
_latch.write(1);
imp.sleep(0.01)
_latch.write(0);
}
function writeDigit(d,v) {
_spi.write(displays[d] + digits[v]);
latch();
}
function writeNum(num, firstDigit = 0, lastDigit = 7) {
local numString = num.tostring();
foreach(k,v in numString) {
local digit = numString.len() - k - 1 + firstDigit;
if (digit <= lastDigit) writeDigit(numString.len()-k-1+firstDigit, (v-48));
}
for(local i = lastDigit; i > numString.len() - 1 + lastDigit; i--) {
writeDigit(i, OFF);
}
}
}
class Servo {
_servo = null;
_min = null;
_max = null;
constructor (servo, period = 0.02, min = 0.03, max = 0.1) {
this._servo = servo;
this._servo.configure(PWM_OUT, period, min);
this._min = min;
this._max = max;
}
// expects a value between 0.0 and 1.0
function setPosition(value) {
local scaledValue = value * (_max-_min) + _min;
this._servo.write(scaledValue);
}
// expects a value between -75.0 and 75.0
function setDegrees(value) {
if (value < -75) value = -75;
if (value > 75) value = 75;
local scaledValue = (value + 56) / 151.0 * (_max-_min) + _min;
this._servo.write(scaledValue);
}
}
left <- 0;
right <- 0;
agent.on("request", function(data) {
if (data == "left") left ++;
else if (data == "right") right++;
update();
});
function update() {
// set displays
display.writeNum(right, 0, 3);
display.writeNum(left, 4, 7);
// set servo
servo.setDegrees(right-left);
}
display <- MAX72XX(hardware.spi257, hardware.pin1);
servo <- Servo(hardware.pin2);
update();