-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_app.dart
61 lines (53 loc) · 1.73 KB
/
example_app.dart
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
import 'dart:async';
import 'dart:io';
import 'package:rpi_gpio/gpio.dart';
import 'debouncer.dart';
const dutyCycleValues = [100, 50, 100, 25, 100, 10, 100];
Future runExample(Gpio gpio, {Duration? blink, int? debounce}) async {
blink ??= const Duration(milliseconds: 500);
debounce ??= 250;
// Blink the LED 3 times for each PWM level
final led = gpio.output(15); // GPIO 22
final pwmLed = gpio.pwm(12); // GPIO 18
for (var dutyCycle in dutyCycleValues) {
pwmLed.dutyCycle = dutyCycle;
print('PWM Led brightness $dutyCycle %');
for (int count = 0; count < 3; ++count) {
led.value = true;
// sleep blocks this thread, but not the RpiGpio isolate
// so polling and pulse width modulation continue to operate
sleep(blink);
led.value = false;
sleep(blink);
}
}
pwmLed.dutyCycle = 0; // off
// Get the current button state
final button = gpio.input(11, Pull.up); // GPIO 17
print('Button state: ${await button.value}');
// Wait for the button to be pressed 3 times
bool lastValue = true;
int count = 0;
final completer = Completer();
final subscription = button.values
.transform(Debouncer(lastValue, debounce))
.listen((bool newValue) {
print('New button state: $newValue');
if (lastValue == false && newValue) {
++count;
if (count == 3) {
completer.complete();
}
}
led.value = lastValue = newValue;
});
print('Waiting for 3 button presses on GPIO 17 pin 11...');
await completer.future.timeout(const Duration(seconds: 15), onTimeout: () {
print('Stopped waiting for button presses');
});
led.value = false;
// Cleanup before exit
await subscription.cancel();
await gpio.dispose();
print('Complete');
}