-
Notifications
You must be signed in to change notification settings - Fork 65
/
example1.device.nut
60 lines (48 loc) · 1.67 KB
/
example1.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
// -----------------------------------------------------------------------------
// PIN mux
irtrx <- hardware.uart1289;
irpwm <- hardware.pin7;
btn1 <- hardware.pin1;
btn2 <- hardware.pin2;
led <- hardware.pin5;
// -----------------------------------------------------------------------------
// Handle the stage change for button 1
function btn1_change() {
// Debounce (give the button a chance to settle)
imp.sleep(0.01);
// Read the button state and if down (value of 1) then
if (btn1.read()) {
// -----[ Blink using blocking sleep ]-----
// Turn the LED on
led.write(1);
// Wait a half second
imp.sleep(0.5);
// Turn the LED off again
led.write(0);
// Wait a half second
imp.sleep(0.5);
// -----[ Blink using non-blocking timer ]-----
// Turn the LED on
led.write(1);
// Wait a half second
imp.wakeup(0.5, function() {
// Turn the LED off again
led.write(0);
})
}
}
// Handle the stage change for button 2
function btn2_change() {
// Debounce (give the button a chance to settle)
imp.sleep(0.01);
// Set the LED state to match the button state
led.write(btn2.read());
}
// -----------------------------------------------------------------------------
imp.enableblinkup(true);
// Configure the LED pin as digital output and initialise its value to 0
led.configure(DIGITAL_OUT, 0);
// Configure button 1 as an input and set an event handler
btn1.configure(DIGITAL_IN_PULLDOWN, btn1_change);
// Configure button 2 as an input and set an event handler
btn2.configure(DIGITAL_IN_PULLDOWN, btn2_change);