-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuffin.nut
90 lines (70 loc) · 1.76 KB
/
puffin.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
uart <- hardware.uart57;
uart.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);
pressure <- hardware.pin2;
pressure.configure(ANALOG_IN);
x <- hardware.pin9;
y <- hardware.pin8;
x.configure(ANALOG_IN);
y.configure(ANALOG_IN);
// 1 is puff, 0 is sip
press <- 0;
duration <- 0;
drag <- 0; // dragging the left mouse button?
function blink() {
local xval = -(x.read() - 32768)/6000;
local yval = (y.read() - 32768)/6000;
//server.log("x: " + xval.tostring()
// + ", y: " + yval.tostring());
local oldpress = press;
local pressure_reading = pressure.read();
if (pressure_reading > 33500)
press = 1;
else if (pressure_reading < 32500)
press = 0;
else
duration = 0;
if (press != oldpress)
duration = 0;
else
duration++;
if (duration == 5) {
//server.log(duration.tostring() + " times at press " + press.tostring())
if (press == 0)
server.log("Sip!");
else {
server.log("Puff!");
drag = 0;
}
uart.write(0xFD);
uart.write(0x00);
uart.write(0x03);
uart.write(2 - press);
uart.write(0x00);
uart.write(0x00);
uart.write(0x00);
uart.write(0x00);
uart.write(0x00);
} else if (duration == 25) {
//server.log(duration.tostring() + " times at press " + press.tostring())
if (press == 0)
server.log("Long Sip!");
else {
server.log("Long Puff!");
drag = 1;
}
} else {
uart.write(0xFD);
uart.write(0x00);
uart.write(0x03);
uart.write(drag);
uart.write(xval);
uart.write(yval);
uart.write(0x00);
uart.write(0x00);
uart.write(0x00);
}
// schedule imp to wakeup in .5 seconds and do it again.
imp.wakeup(0.01, blink);
}
// start the loop
blink();