Skip to content

Commit

Permalink
internal must be export
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrancois committed Jan 13, 2015
1 parent 0dad038 commit 5c55665
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
3 changes: 1 addition & 2 deletions example/digital_read_write.dart
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

import 'package:firmata/firmata.dart';
import 'dart:async';

final P1 = 2;
final P2 = 3;
Expand All @@ -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}');
Expand Down
1 change: 1 addition & 0 deletions lib/firmata.dart
Expand Up @@ -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';
2 changes: 2 additions & 0 deletions lib/firmata_chrome.dart
Expand Up @@ -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';

4 changes: 2 additions & 2 deletions lib/src/chrome/adapter.dart
Expand Up @@ -24,7 +24,7 @@ Future<Board> 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;
Expand All @@ -34,7 +34,7 @@ Future<Board> detect() {
Future<Board> fromPortName(String portName) {
final completer = new Completer<Board>();
final adapter = new ChromeSerialPortAdapter(portName);
final board = new Board(adapter);
final board = new BoardImpl(adapter);
board.open().then((_) => completer.complete(board));
return completer.future;
}
Expand Down
68 changes: 49 additions & 19 deletions lib/src/internal/board.dart
Expand Up @@ -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<PinState> get onDigitalRead;

/// Stream that sent analogic value
Stream<PinState> 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;
Expand All @@ -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<bool>();
adapter.open().then((_) {
Expand Down Expand Up @@ -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<bool> 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) {
Expand All @@ -153,35 +189,29 @@ class Board {
return adapter.write([DIGITAL_MESSAGE | portNumber, _digitalOutputData[portNumber] & 0x7F, _digitalOutputData[portNumber] >> 7]);
}

/// Stream that sent state from digital value
Stream<PinState> get onDigitalRead {
if(digitalReadStream == null){
digitalReadStream = _digitalReadController.stream.asBroadcastStream();
}
return digitalReadStream;
}

/// Stream that sent analogic value
Stream<PinState> get onAnalogRead {
if(_analogReadStream == null){
_analogReadStream = _analogReadController.stream.asBroadcastStream();
}
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]);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/native/adapter.dart
Expand Up @@ -24,7 +24,7 @@ Future<Board> 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));
}
});
Expand All @@ -37,7 +37,7 @@ bool _isMacPortName(String name) => name.startsWith("/dev/tty") && name.contains
Future<Board> fromPortName(String portName) {
final completer = new Completer<Board>();
final adapter = new NativeSerialPortAdapter(portName);
final board = new Board(adapter);
final board = new BoardImpl(adapter);
board.open().then((_) => completer.complete(board));
return completer.future;
}
Expand Down

0 comments on commit 5c55665

Please sign in to comment.