From 5c55665b73552d8e237d73e3b2b3e4048db67718 Mon Sep 17 00:00:00 2001 From: Nicolas FRANCOIS Date: Tue, 13 Jan 2015 18:03:28 +0100 Subject: [PATCH] internal must be export --- example/digital_read_write.dart | 3 +- lib/firmata.dart | 1 + lib/firmata_chrome.dart | 2 + lib/src/chrome/adapter.dart | 4 +- lib/src/internal/board.dart | 68 ++++++++++++++++++++++++--------- lib/src/native/adapter.dart | 4 +- 6 files changed, 57 insertions(+), 25 deletions(-) diff --git a/example/digital_read_write.dart b/example/digital_read_write.dart index c68023c..a9cab67 100644 --- a/example/digital_read_write.dart +++ b/example/digital_read_write.dart @@ -13,7 +13,6 @@ // limitations under the License. import 'package:firmata/firmata.dart'; -import 'dart:async'; final P1 = 2; final P2 = 3; @@ -24,7 +23,7 @@ final L4 = 7; main() { print('Diduino start ...'); - detect().then((board) { + detect().then((Board board) { print("connected"); print('Firmware: ${board.firmware.name}-${board.firmware.major}.${board.firmware.minor}'); diff --git a/lib/firmata.dart b/lib/firmata.dart index c43f290..89171ab 100644 --- a/lib/firmata.dart +++ b/lib/firmata.dart @@ -17,6 +17,7 @@ library firmata_native; import 'dart:io'; import 'dart:async'; import 'src/firmata_internal.dart'; +export 'src/firmata_internal.dart' show PinModes, Board, FirmataVersion; import 'package:serial_port/serial_port.dart'; part 'src/native/adapter.dart'; diff --git a/lib/firmata_chrome.dart b/lib/firmata_chrome.dart index 49ce09a..a898254 100644 --- a/lib/firmata_chrome.dart +++ b/lib/firmata_chrome.dart @@ -17,5 +17,7 @@ library firmata_chrome; import 'dart:async'; import 'src/firmata_internal.dart'; import 'package:chrome/chrome_app.dart'; +export 'src/firmata_internal.dart' show PinModes, Board, FirmataVersion; part 'src/chrome/adapter.dart'; + diff --git a/lib/src/chrome/adapter.dart b/lib/src/chrome/adapter.dart index 013336e..7ebf93f 100644 --- a/lib/src/chrome/adapter.dart +++ b/lib/src/chrome/adapter.dart @@ -24,7 +24,7 @@ Future detect() { // TODO mac OS filter ? final selected = ports.first; final adapter = new ChromeSerialPortAdapter(selected.path); - final board = new Board(adapter); + final board = new BoardImpl(adapter); board.open().then((_) => completer.complete(board)); }); return completer.future; @@ -34,7 +34,7 @@ Future detect() { Future fromPortName(String portName) { final completer = new Completer(); final adapter = new ChromeSerialPortAdapter(portName); - final board = new Board(adapter); + final board = new BoardImpl(adapter); board.open().then((_) => completer.complete(board)); return completer.future; } diff --git a/lib/src/internal/board.dart b/lib/src/internal/board.dart index 575b392..ed63fcc 100644 --- a/lib/src/internal/board.dart +++ b/lib/src/internal/board.dart @@ -59,8 +59,54 @@ abstract class SerialPortAdapter { } -/// Represent the arduino board -class Board { +/// The arduino board +abstract class Board { + + /// Send SYSTEM_RESET to arduino + Future reset(); + + /// Asks the arduino to set the pin to a certain mode. + Future pinMode(int pin, int mode); + + /// Getter for firmware information + FirmataVersion get firmware; + + /// Resquest a QUERY_FIRMWARE call + Future queryFirmware(); + + /// Request a CAPABILITY_RESPONSE call + Future queryCapability(); + + /// Asks the arduino to tell us its analog pin mapping + Future queryAnalogMapping(); + + /// Close the connection + Future close(); + + /// Asks the arduino to write a value to a digital pin + Future digitalWrite(int pin, int value); + + /// Stream that sent state from digital value + Stream get onDigitalRead; + + /// Stream that sent analogic value + Stream get onAnalogRead; + + /// Read the digatal value from pin; + int digitalRead(int pin); + + /// Asks the arduino to write an analog message. + Future analogWrite(int pin, int value); + + /// Read the analog value from pin; + int analogRead(int pin); + + /// Asks the arduino to move a servo + Future servoWrite(int pin, int angle); + +} + +class BoardImpl { /// Adapter to SerialPort final SerialPortAdapter adapter; @@ -78,10 +124,8 @@ class Board { FirmataVersion _firmware; - Board(this.adapter); + BoardImpl(this.adapter); - /// Open communication with Arduino. - /// Please DO NOT call yourself this method dans use helper construction to start communication with Arduino Future open() { final completer = new Completer(); adapter.open().then((_) { @@ -116,33 +160,25 @@ class Board { }); } - /// Asks the arduino to set the pin to a certain mode. Future pinMode(int pin, int mode) { _pins[pin] = mode; return adapter.write([PIN_MODE, pin, mode]); } - /// Getter for firmware information FirmataVersion get firmware => _firmware; - /// Send SYSTEM_RESET to arduino Future reset() => adapter.write([SYSTEM_RESET]); - /// Resquest a QUERY_FIRMWARE call Future queryFirmware() => adapter.write([START_SYSEX, QUERY_FIRMWARE, END_SYSEX]); //Future queryPinState(int pin) => _serialPort.write(([START_SYSEX, PIN_STATE_QUERY, pin, END_SYSEX])); - /// Request a CAPABILITY_RESPONSE call Future queryCapability() => adapter.write([START_SYSEX, CAPABILITY_QUERY, END_SYSEX]); - /// Asks the arduino to tell us its analog pin mapping Future queryAnalogMapping() => adapter.write([START_SYSEX, ANALOG_MAPPING_QUERY, END_SYSEX]); - /// Close the connection Future close() => adapter.close(); - /// Asks the arduino to write a value to a digital pin Future digitalWrite(int pin, int value) { final portNumber = (pin >> 3) & 0x0F; if (value == 0) { @@ -153,7 +189,6 @@ class Board { return adapter.write([DIGITAL_MESSAGE | portNumber, _digitalOutputData[portNumber] & 0x7F, _digitalOutputData[portNumber] >> 7]); } - /// Stream that sent state from digital value Stream get onDigitalRead { if(digitalReadStream == null){ digitalReadStream = _digitalReadController.stream.asBroadcastStream(); @@ -161,7 +196,6 @@ class Board { return digitalReadStream; } - /// Stream that sent analogic value Stream get onAnalogRead { if(_analogReadStream == null){ _analogReadStream = _analogReadController.stream.asBroadcastStream(); @@ -169,19 +203,15 @@ class Board { return _analogReadStream; } - /// Read the digatal value from pin; int digitalRead(int pin) => _digitalInputData.containsKey(pin) ? _digitalInputData[pin] : 0; - /// Asks the arduino to write an analog message. Future analogWrite(int pin, int value) { pinMode(pin, PinModes.PWM); return adapter.write([ANALOG_MESSAGE | (pin & 0x0F), value & 0x7F, value >> 7]); } - /// Read the analog value from pin int analogRead(int pin) => _analogInputData.containsKey(pin) ? _analogInputData[pin] : 0; - /// Asks the arduino to move a servo Future servoWrite(int pin, int angle) { pinMode(pin, PinModes.SERVO); return adapter.write([ANALOG_MESSAGE | (pin & 0x0F), angle & 0x7F, angle >> 7]); diff --git a/lib/src/native/adapter.dart b/lib/src/native/adapter.dart index 0a897ee..ae7867c 100644 --- a/lib/src/native/adapter.dart +++ b/lib/src/native/adapter.dart @@ -24,7 +24,7 @@ Future detect() { completer.completeError("Impossible to detect Arduino board on usb."); } else { final adapter = new NativeSerialPortAdapter(avaibles.first); - final board = new Board(adapter); + final board = new BoardImpl(adapter); board.open().then((_) => completer.complete(board)); } }); @@ -37,7 +37,7 @@ bool _isMacPortName(String name) => name.startsWith("/dev/tty") && name.contains Future fromPortName(String portName) { final completer = new Completer(); final adapter = new NativeSerialPortAdapter(portName); - final board = new Board(adapter); + final board = new BoardImpl(adapter); board.open().then((_) => completer.complete(board)); return completer.future; }