Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit d30d5b665fc2d3a243924689a59853c699f7147a 0 parents
Tobias Schneider authored
0  README
No changes.
1  deps/node-serialport
@@ -0,0 +1 @@
+Subproject commit 9ef351e611e30884a0ba9dbaf2b22176ea67c09d
74 lib/arduino.js
@@ -0,0 +1,74 @@
+/*
+ * node-arduino: Control your Arduino with Node
+ *
+ * Copyright (c) 2010 Tobias Schneider
+ * node-arduino is freely distributable under the terms of the MIT license.
+ */
+
+var sys = require('sys')
+ , SerialPort = require(
+ '../deps/node-serialport/serialport_native/build/default/serialport_native'
+ ).SerialPort
+ ;
+
+const SERIAL_BAUDRATE = 9600;
+
+const OPC_PIN_MODE = 0x01;
+const OPC_DIGITAL_READ = 0x02;
+const OPC_DIGITAL_WRITE = 0x03;
+const OPC_ANALOG_REFERENCE = 0x04;
+const OPC_ANALOG_READ = 0x05;
+const OPC_ANALOG_WRITE = 0x06;
+
+exports.HIGH = 0x01;
+exports.LOW = 0x00;
+
+exports.INPUT = 0x00;
+exports.OUTPUT = 0x01;
+
+exports.true = 0x01;
+exports.false = 0x00;
+
+exports.EXTERNAL = 0x00;
+exports.DEFAULT = 0x01;
+exports.INTERNAL = 0x03;
+
+Board = function (path) {
+ this.sp = new SerialPort;
+
+ this.sp.on('data', function (data) {
+ sys.log(1);
+ });
+
+ this.sp.open(path, SERIAL_BAUDRATE);
+}
+
+Board.prototype = {
+ pinMode : function (pin, mode) {
+ this.sp.write(new Buffer([OPC_PIN_MODE, pin, mode]), 3);
+ }
+
+, digitalRead : function (pin) {
+ // TODO
+ }
+
+, digitalWrite : function (pin, val) {
+ this.sp.write(new Buffer([OPC_DIGITAL_WRITE, pin, val]), 3);
+ }
+
+, analogReference : function (type) {
+ this.sp.write(new Buffer([OPC_ANALOG_REFERENCE, type]), 2);
+ }
+
+, analogRead : function (pin) {
+ // TODO
+ }
+
+, analogWrite : function (pin, val) {
+ this.sp.write(new Buffer([OPC_ANALOG_WRITE, pin, val]), 3);
+ }
+};
+
+exports.connect = function (path) {
+ return new Board(path);
+};
52 src/node.pde
@@ -0,0 +1,52 @@
+/*
+ * node-arduino: Control your Arduino with Node
+ *
+ * Copyright (c) 2010 Tobias Schneider
+ * node-arduino is freely distributable under the terms of the MIT license.
+ */
+
+#define SERIAL_BAUDRATE 9600
+
+#define OPC_PIN_MODE 0x01
+#define OPC_DIGITAL_READ 0x02
+#define OPC_DIGITAL_WRITE 0x03
+#define OPC_ANALOG_REFERENCE 0x04
+#define OPC_ANALOG_READ 0x05
+#define OPC_ANALOG_WRITE 0x06
+
+void setup() {
+ Serial.begin(SERIAL_BAUDRATE);
+}
+
+void loop() {
+ while (Serial.available() > 0) {
+ switch (Serial.read()) {
+ case OPC_PIN_MODE: {
+ Serial.println("pinMode");
+ pinMode(Serial.read(), Serial.read());
+ break;
+ }
+ case OPC_DIGITAL_READ: {
+ digitalRead(Serial.read());
+ break;
+ }
+ case OPC_DIGITAL_WRITE: {
+ Serial.println("digitalWrite");
+ digitalWrite(Serial.read(), Serial.read());
+ break;
+ }
+ case OPC_ANALOG_REFERENCE: {
+ analogReference(Serial.read());
+ break;
+ }
+ case OPC_ANALOG_READ: {
+ analogRead(Serial.read());
+ break;
+ }
+ case OPC_ANALOG_WRITE: {
+ analogWrite(Serial.read(), Serial.read());
+ break;
+ }
+ }
+ }
+}
19 test/test.js
@@ -0,0 +1,19 @@
+/*
+ * node-arduino: Control your Arduino with Node
+ *
+ * Copyright (c) 2010 Tobias Schneider
+ * node-arduino is freely distributable under the terms of the MIT license.
+ */
+
+var arduino = require('../lib/arduino')
+ , board = arduino.connect('/dev/tty.usbserial-A700fkLn')
+ , val = arduino.LOW
+ ;
+
+board.pinMode(13, arduino.OUTPUT);
+
+setInterval(function () {
+ val = val == arduino.LOW ? arduino.HIGH : arduino.LOW;
+
+ board.digitalWrite(13, val);
+}, 500);
Please sign in to comment.
Something went wrong with that request. Please try again.