OLD | NEW |
1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 // | 4 // |
5 // Remember those red running lights KITT had in Knight Rider? | 5 // Remember those red running lights KITT had in Knight Rider? |
6 // https://www.youtube.com/watch?v=Mo8Qls0HnWo | 6 // https://www.youtube.com/watch?v=Mo8Qls0HnWo |
7 // | 7 // |
8 // This sample recreates those with a chain of LEDs running right and left: | 8 // This sample recreates those with a chain of LEDs running right and left: |
9 // https://storage.googleapis.com/fletch-archive/images/knight-rider.mp4 | 9 // https://storage.googleapis.com/fletch-archive/images/knight-rider.mp4 |
10 // | 10 // |
11 // For breadboard layout and connections to the Pi, see: | 11 // For breadboard layout and connections to the Pi, see: |
12 // https://storage.googleapis.com/fletch-archive/images/k-r-schematic.png | 12 // https://storage.googleapis.com/fletch-archive/images/k-r-schematic.png |
13 | 13 |
14 import 'package:gpio/gpio.dart'; | 14 import 'package:gpio/gpio.dart'; |
| 15 import 'package:raspberry_pi/raspberry_pi.dart'; |
15 import 'package:os/os.dart'; | 16 import 'package:os/os.dart'; |
16 | 17 |
17 main() { | 18 main() { |
| 19 // Initialize Raspberry Pi |
| 20 RaspberryPi pi = new RaspberryPi(); |
| 21 |
18 // Array constant containing the GPIO pins of the connected LEDs. | 22 // Array constant containing the GPIO pins of the connected LEDs. |
19 // You can add more LEDs simply by extending the list. Make sure | 23 // You can add more LEDs simply by extending the list. Make sure |
20 // the pins are listed in the order the LEDs are connected. | 24 // the pins are listed in the order the LEDs are connected. |
21 List<int> leds = [26, 19, 13, 6]; | 25 List<int> leds = [26, 19, 13, 6]; |
22 | 26 |
23 // Initialize the lights controller class. | 27 // Initialize the lights controller class. |
24 Lights lights = new Lights(leds); | 28 Lights lights = new Lights(pi.memoryMappedGPIO, leds); |
25 lights.init(); | 29 lights.init(); |
26 | 30 |
27 // Alternate between running left and right in a continuous loop. | 31 // Alternate between running left and right in a continuous loop. |
28 const int waitTime = 100; | 32 const int waitTime = 100; |
29 while (true) { | 33 while (true) { |
30 lights.runLightLeft(waitTime); | 34 lights.runLightLeft(waitTime); |
31 lights.runLightRight(waitTime); | 35 lights.runLightRight(waitTime); |
32 } | 36 } |
33 } | 37 } |
34 | 38 |
35 class Lights { | 39 class Lights { |
36 List<int> leds; | 40 final GPIO _gpio; |
37 PiMemoryMappedGPIO _gpio = new PiMemoryMappedGPIO(); | 41 final List<int> leds; |
38 | 42 |
39 Lights(this.leds); | 43 Lights(this._gpio, this.leds); |
40 | 44 |
41 // Initializes GPIO and configures the pins. | 45 // Initializes GPIO and configures the pins. |
42 void init() { | 46 void init() { |
43 leds.forEach((pin) => _gpio.setMode(pin, Mode.output)); | 47 leds.forEach((pin) => _gpio.setMode(pin, Mode.output)); |
44 leds.forEach((pin) => _gpio.setPin(pin, false)); | 48 leds.forEach((pin) => _gpio.setPin(pin, false)); |
45 } | 49 } |
46 | 50 |
47 // Iterates though the lights in increasing order, and sets the LEDs using | 51 // Iterates though the lights in increasing order, and sets the LEDs using |
48 // a helper function. Pauses [waitTime] milliseconds before returning. | 52 // a helper function. Pauses [waitTime] milliseconds before returning. |
49 void runLightLeft(int waitTime) { | 53 void runLightLeft(int waitTime) { |
(...skipping 15 matching lines...) Expand all Loading... |
65 // Sets LED [ledToEnable] to true, and all others to false. | 69 // Sets LED [ledToEnable] to true, and all others to false. |
66 void _setLeds(int ledToEnable) { | 70 void _setLeds(int ledToEnable) { |
67 var state; | 71 var state; |
68 | 72 |
69 for (int i = 0; i < leds.length; i++) { | 73 for (int i = 0; i < leds.length; i++) { |
70 bool state = (i == ledToEnable); | 74 bool state = (i == ledToEnable); |
71 _gpio.setPin(leds[i], state); | 75 _gpio.setPin(leds[i], state); |
72 } | 76 } |
73 } | 77 } |
74 } | 78 } |
OLD | NEW |