Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE.md file. | |
| 4 library gpio_mock; | |
| 5 | |
| 6 import 'package:gpio/gpio.dart'; | |
| 7 | |
| 8 /// Mock implementation of the GPIO interface. | |
| 9 class MockGPIO extends GPIOBase { | |
| 10 Function _setPin; | |
| 11 Function _getPin; | |
| 12 | |
| 13 List<bool> _pinValues; | |
| 14 | |
| 15 MockGPIO([int pins = GPIO.defaultPins]) : super(pins) { | |
| 16 _pinValues = new List.filled(pins, false); | |
| 17 } | |
| 18 | |
| 19 /// The simulated values of the pins. | |
| 20 /// | |
| 21 /// The default ´getPin´ will return the value in this `List`. The default | |
| 22 /// ´setPin´ will update it. Initially all values are ´false´. | |
| 23 List<bool> get pinValues => _pinValues; | |
| 24 | |
| 25 void setMode(int pin, Mode mode) { | |
| 26 checkPinRange(pin); | |
|
wibling
2015/10/05 07:43:37
NIT: You could enforce the mode on get/set pin, e.
| |
| 27 print('Setting mode for pin $pin to $mode'); | |
| 28 } | |
| 29 | |
| 30 Mode getMode(int pin) { | |
| 31 checkPinRange(pin); | |
| 32 print('Reading mode of pin $pin'); | |
| 33 } | |
| 34 | |
| 35 void setPin(int pin, bool value) { | |
| 36 checkPinRange(pin); | |
| 37 if (_setPin == null) { | |
| 38 _pinValues[pin] = value; | |
| 39 print('Writing pin $pin value $value'); | |
| 40 } else { | |
| 41 _setPin(pin, value); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 bool getPin(int pin) { | |
| 46 checkPinRange(pin); | |
| 47 if (_getPin == null) { | |
| 48 bool value = _pinValues[pin]; | |
| 49 print('Reading pin $pin value $value'); | |
| 50 return value; | |
| 51 } else { | |
| 52 return _getPin(pin); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 /// Register ´callback´ to be called for ´setPin´. | |
| 57 void registerSetPin(void callback(int pin, bool value)) { | |
| 58 _setPin = callback; | |
| 59 } | |
| 60 | |
| 61 /// Register ´callback´ to be called for ´getPin´. | |
| 62 void registerGetPin(bool callback(int pin)) { | |
| 63 _getPin = callback; | |
| 64 } | |
| 65 } | |
| OLD | NEW |