From 34d7dd50dc4b167139003bc9a0960a4b945025ab Mon Sep 17 00:00:00 2001 From: Nicholas Rushton Date: Mon, 20 May 2013 23:54:17 -0700 Subject: [PATCH 1/8] adding sound --- .npmignore | 2 + index.js | 1 + lib/board.js | 3 +- lib/sound.js | 13 +++ package.json | 27 ++++- src/du/du.ino | 269 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 308 insertions(+), 7 deletions(-) create mode 100644 .npmignore create mode 100644 lib/sound.js create mode 100644 src/du/du.ino diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..9daa824 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +.DS_Store +node_modules diff --git a/index.js b/index.js index a30f8f9..eb80996 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ module.exports = { Button: require('./lib/button'), Servo: require('./lib/servo'), Sensor: require('./lib/sensor'), + Sound: require('./lib/sound'), Ping: require('./lib/ping'), PIR: require('./lib/pir') }; diff --git a/lib/board.js b/lib/board.js index a1acf89..770d4c8 100644 --- a/lib/board.js +++ b/lib/board.js @@ -12,7 +12,6 @@ var events = require('events'), var Board = function (options) { this.log('info', 'initializing'); this.debug = options && options.debug || false; - this.baudrate = options && options.baudrate || 115200; this.writeBuffer = []; var self = this; @@ -84,7 +83,7 @@ Board.prototype.detect = function (callback) { if (possible.slice(0, 2) !== 'cu') { try { temp = new serial.SerialPort('/dev/' + possible, { - baudrate: self.baudrate, + baudrate: 115200, parser: serial.parsers.readline('\n') }); } catch (e) { diff --git a/lib/sound.js b/lib/sound.js new file mode 100644 index 0000000..b251ed0 --- /dev/null +++ b/lib/sound.js @@ -0,0 +1,13 @@ +var Sound = function (options) { + if (!options || !options.board) throw new Error('Must supply required options to LED'); + this.board = options.board; + this.pin = options.pin || 8; + this.board.pinMode(this.pin, 'out'); +} + +Sound.prototype.tone = function(tone,duration) { + this.board.write('95' + this.board.lpad(2, '0', this.pin) + this.board.lpad(6,'0',tone)); + this.board.write('96' + this.board.lpad(2, '0', this.pin) + this.board.lpad(6,'0',duration)); +} + +module.exports = Sound; diff --git a/package.json b/package.json index 7c7b79b..7360912 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,22 @@ { - "author": "Cam Pedersen (http://campedersen.com/)", + "author": { + "name": "Cam Pedersen", + "email": "diffference@gmail.com", + "url": "http://campedersen.com/" + }, "contributors": [ - { "name": "Rick Waldron", "email": "waldron.rick@gmail.com" }, - { "name": "Leonhardt Wille", "email": "wille@riverlabs.de" }, - { "name": "Seiya Konno", "email": "nulltask@gmail.com" } + { + "name": "Rick Waldron", + "email": "waldron.rick@gmail.com" + }, + { + "name": "Leonhardt Wille", + "email": "wille@riverlabs.de" + }, + { + "name": "Seiya Konno", + "email": "nulltask@gmail.com" + } ], "name": "duino", "description": "Arduino framework for mad scientists", @@ -25,5 +38,9 @@ "serialport": "*", "colors": "*" }, - "devDependencies": {} + "devDependencies": {}, + "readme": "# duino\n\nA framework for working with Arduinos in node.js\n\n![arduino](http://i.imgur.com/eFq84.jpg)\n\n# install\n\n npm install duino\n\n# usage\n\n````javascript\nvar arduino = require('duino'),\n board = new arduino.Board();\n\nvar led = new arduino.Led({\n board: board,\n pin: 13\n});\n\nled.blink();\n````\n\n# what ಠ_ಠ\n\nThe way this works is simple (in theory, not in practice). The Arduino listens for low-level signals over a serial port, while we abstract all of the logic on the Node side.\n\n1. Plug in your Arduino\n2. Upload the C code at `./src/du.ino` to it\n3. Write a simple **duino** script\n4. ?????\n5. Profit!\n\n# libraries\n\n##board\n\nRight now, the board library will attempt to autodiscover the Arduino. I'm going to make it configurable, don't worry.\n\n````javascript\nvar board = new arduino.Board({\n debug: true\n});\n````\n\nDebug mode is off by default. Turning it on will enable verbose logging in your terminal, and tell the Arduino board to echo everthing back to you. You will get something like this:\n\n![debug](http://i.imgur.com/gBYZA.png)\n\nThe **board** object is an EventEmitter. You can listen for the following events:\n\n* `data` messages from the serial port, delimited by newlines\n* `connected` when the serial port has connected\n* `ready` when all internal post-connection logic has finished and the board is ready to use\n\n````javascript\nboard.on('ready', function(){\n // do stuff\n});\n\nboard.on('data', function(m){\n console.log(m);\n}\n````\n\n###board.serial\n\nLow-level access to the serial connection to the board\n\n###board.write(msg)\n\nWrite a message to the board, wrapped in predefined delimiters (! and .)\n\n###board.pinMode(pin, mode)\n\nSet the mode for a pin. `mode` is either `'in'` or `'out'`\n\n###board.digitalWrite(pin, val)\n\nWrite one of the following to a pin:\n\n####board.HIGH and board.LOW\n\nConstants for use in low-level digital writes\n\n###board.analogWrite(pin,val)\n\nWrite a value between 0-255 to a pin\n\n##led\n\n````javascript\nvar led = new arduino.Led({\n board: board,\n pin: 13\n});\n````\n\nPin will default to 13.\n\n###led.on()\n\nTurn the LED on\n\n###led.off()\n\nTurn the LED off\n\n###led.blink(interval)\n\nBlink the LED at `interval` ms. Defaults to 1000\n\n###led.fade(interval)\n\nFade the to full brightness then back to minimal brightness in `interval` ms. Defaults to 2000\n\n###led.bright\n\nCurrent brightness of the LED\n\n##piezo\n\n````javascript\nvar led = new arduino.Piezo({\n board: board,\n pin: 13\n});\n````\nPin will default to 13.\n\n###piezo.note(note, duration)\n\nPlay a pre-calculated note for a given duration (in milliseconds).\n\n`note` must be a string, one of `d`, `e`, `f`, `g`, `a`, `b`, or `c` (must be lowercase)\n\n###piezo.tone(tone, duration)\n\nWrite a square wave to the piezo element.\n\n`tone` and `duration` must be integers. See code comments for math on `tone` generation.\n\n##button\n\n````javascript\nvar button = new arduino.Button({\n board: board,\n pin: 13\n});\n````\nPin will default to 13.\n\nButtons are simply EventEmitters. They will emit the events `up` and `down`. You may also access their `down` property.\n\n````javascript\nbutton.on('down', function(){\n // delete the database!\n console.log('BOOM');\n});\n\nsetInterval(function(){\n console.log(button.down);\n}, 1000);\n````\n\n##servo\n\n````javascript\nvar servo = new arduino.Servo({\n board: board\n});\n\nservo.write(0);\nservo.write(180);\n````\nPin will default to 9. (Arduino PWM default)\n\n###servo.sweep()\n\nIncrement position from 0 to 180.\n\n###servo.write(pos)\n\nInstruct the servo to immediately go to a position from 0 to 180.\n\n##motor\n\n##potentiometer\n\n# protocol\n\nEach message sent to the Arduino board by the **board** class has 8 bytes.\n\nA full message looks like this:\n\n !0113001.\n\n`!` Start\n`01` Command (digitalWrite)\n`13` Pin number\n`001` Value (high)\n`.` Stop\n\nI was drunk. It works.\n\n##command\n\nWhat is implemented right now:\n\n* `00` pinMode\n* `01` digitalWrite\n* `02` digitalRead\n* `03` analogWrite\n* `04` analogRead\n* `99` debug\n\n##pin\n\nPins can be sent as an integer or a string(`1`, `2`, `\"3\"`, `\"A0\"`)\n\n##value\n\n* `board.LOW`(`0`)\n* `board.HIGH`(`255`)\n* integer/string from `0`-`255` for PWM pins\n\n# license\n\n(The MIT License)\n\nCopyright (c) 2011 Cam Pedersen \n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n", + "readmeFilename": "README.md", + "_id": "duino@0.0.7", + "_from": "duino@0.0.7" } diff --git a/src/du/du.ino b/src/du/du.ino new file mode 100644 index 0000000..6345823 --- /dev/null +++ b/src/du/du.ino @@ -0,0 +1,269 @@ +#include + + +bool debug = true; + +int index = 0; + +char messageBuffer[12]; +char cmd[3]; +char pin[3]; +char val[4]; +char aux[4]; +char tne[7]; +char dur[7]; + +Servo servo; + +void setup() { + Serial.begin(115200); +} + +void loop() { + while(Serial.available() > 0) { + char x = Serial.read(); + if (x == '!') index = 0; // start + else if (x == '.') process(); // end + else messageBuffer[index++] = x; + } +} + +/* + * Deal with a full message and determine function to call + */ +void process() { + index = 0; + + strncpy(cmd, messageBuffer, 2); + cmd[2] = '\0'; + strncpy(pin, messageBuffer + 2, 2); + pin[2] = '\0'; + + if (atoi(cmd) == 95) { + strncpy(tne, messageBuffer + 4, 6); + tne[6] = '\0'; + } + else if (atoi(cmd) == 96) { + strncpy(dur, messageBuffer + 4, 6); + dur[6] = '\0'; + } + else if (atoi(cmd) > 90) { + strncpy(val, messageBuffer + 4, 2); + val[2] = '\0'; + strncpy(aux, messageBuffer + 6, 3); + aux[3] = '\0'; + } else { + strncpy(val, messageBuffer + 4, 3); + val[4] = '\0'; + strncpy(aux, messageBuffer + 7, 3); + aux[4] = '\0'; + } + + if (debug) { + Serial.println(messageBuffer); + } + int cmdid = atoi(cmd); + + // Serial.println(cmd); + // Serial.println(pin); + // Serial.println(val); + // Serial.println(aux); + + switch(cmdid) { + case 0: sm(pin,val); break; + case 1: dw(pin,val); break; + case 2: dr(pin,val); break; + case 3: aw(pin,val); break; + case 4: ar(pin,val); break; + case 96: handleSoundDuration(pin,dur); break; + case 97: handlePing(pin,val,aux); break; + case 98: handleServo(pin,val,aux); break; + case 99: toggleDebug(val); break; + default: break; + } +} + +void handleSoundDuration(char *pin, char *dur) { + Serial.println("SOUND"); + Serial.println(tne); + Serial.println(dur); + tone(atoi(pin),atoi(tne), atoi(dur)); +} +/* + * Toggle debug mode + */ +void toggleDebug(char *val) { + if (atoi(val) == 0) { + debug = false; + Serial.println("goodbye"); + } else { + debug = true; + Serial.println("hello"); + } +} + +/* + * Set pin mode + */ +void sm(char *pin, char *val) { + if (debug) Serial.println("sm"); + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + if (atoi(val) == 0) { + pinMode(p, OUTPUT); + } else { + pinMode(p, INPUT); + } +} + +/* + * Digital write + */ +void dw(char *pin, char *val) { + if (debug) Serial.println("dw"); + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + pinMode(p, OUTPUT); + if (atoi(val) == 0) { + digitalWrite(p, LOW); + } else { + digitalWrite(p, HIGH); + } +} + +/* + * Digital read + */ +void dr(char *pin, char *val) { + if (debug) Serial.println("dr"); + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + pinMode(p, INPUT); + int oraw = digitalRead(p); + char m[7]; + sprintf(m, "%02d::%02d", p,oraw); + Serial.println(m); +} + +/* + * Analog read + */ +void ar(char *pin, char *val) { + if(debug) Serial.println("ar"); + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + pinMode(p, INPUT); // don't want to sw + int rval = analogRead(p); + char m[8]; + sprintf(m, "%s::%03d", pin, rval); + Serial.println(m); +} + +void aw(char *pin, char *val) { + if(debug) Serial.println("aw"); + int p = getPin(pin); + pinMode(p, OUTPUT); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + analogWrite(p,atoi(val)); +} + +int getPin(char *pin) { //Converts to A0-A5, and returns -1 on error + int ret = -1; + if(pin[0] == 'A' || pin[0] == 'a') { + switch(pin[1]) { + case '0': ret = A0; break; + case '1': ret = A1; break; + case '2': ret = A2; break; + case '3': ret = A3; break; + case '4': ret = A4; break; + case '5': ret = A5; break; + default: break; + } + } else { + ret = atoi(pin); + if(ret == 0 && (pin[0] != '0' || pin[1] != '0')) { + ret = -1; + } + } + return ret; +} + +/* + * Handle Ping commands + * fire, read + */ +void handlePing(char *pin, char *val, char *aux) { + if (debug) Serial.println("ss"); + int p = getPin(pin); + + if(p == -1) { if(debug) Serial.println("badpin"); return; } + Serial.println("got signal"); + + // 01(1) Fire and Read + if (atoi(val) == 1) { + char m[16]; + + pinMode(p, OUTPUT); + digitalWrite(p, LOW); + delayMicroseconds(2); + digitalWrite(p, HIGH); + delayMicroseconds(5); + digitalWrite(p, LOW); + + Serial.println("ping fired"); + + pinMode(p, INPUT); + sprintf(m, "%s::read::%08d", pin, pulseIn(p, HIGH)); + Serial.println(m); + + delay(50); + } +} + +/* + * Handle Servo commands + * attach, detach, write, read, writeMicroseconds, attached + */ +void handleServo(char *pin, char *val, char *aux) { + if (debug) Serial.println("ss"); + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + Serial.println("signal: servo"); + + // 00(0) Detach + if (atoi(val) == 0) { + servo.detach(); + char m[12]; + sprintf(m, "%s::detached", pin); + Serial.println(m); + + // 01(1) Attach + } else if (atoi(val) == 1) { + // servo.attach(p, 750, 2250); + servo.attach(p); + char m[12]; + sprintf(m, "%s::attached", pin); + Serial.println(m); + + // 02(2) Write + } else if (atoi(val) == 2) { + Serial.println("writing to servo"); + Serial.println(atoi(aux)); + // Write to servo + servo.write(atoi(aux)); + delay(15); + + // TODO: Experiment with microsecond pulses + // digitalWrite(pin, HIGH); // start the pulse + // delayMicroseconds(pulseWidth); // pulse width + // digitalWrite(pin, LOW); // stop the pulse + + // 03(3) Read + } else if (atoi(val) == 3) { + Serial.println("reading servo"); + int sval = servo.read(); + char m[13]; + sprintf(m, "%s::read::%03d", pin, sval); + Serial.println(m); + } +} From 23ee1e2250f918bacf7640484af60cb3487a2a53 Mon Sep 17 00:00:00 2001 From: Nicholas Rushton Date: Tue, 21 May 2013 00:24:18 -0700 Subject: [PATCH 2/8] adding notes --- lib/sound.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/sound.js b/lib/sound.js index b251ed0..be1440f 100644 --- a/lib/sound.js +++ b/lib/sound.js @@ -1,3 +1,4 @@ +var notes = {"C0":"16","C#0":"17","D0":"18","D#0":"19","E0":"20","F0":"21","F#0":"23","G0":"24","G#0":"25","A0":"27","A#0":"29","B0":"30","C1":"32","C#1":"34","D1":"36","D#1":"38","E1":"41","F1":"43","F#1":"46","G1":"49","G#1":"51","A1":"55","A#1":"58","B1":"61","C2":"65","C#2":"69","D2":"73","D#2":"77","E2":"82","F2":"87","F#2":"92","G2":"98","G#2":"103","A2":"110","A#2":"116","B2":"123","C3":"130","C#3":"138","D3":"146","D#3":"155","E3":"164","F3":"174","F#3":"185","G3":"196","G#3":"207","A3":"220","A#3":"233","B3":"246","C4":"261","C#4":"277","D4":"293","D#4":"311","E4":"329","F4":"349","F#4":"369","G4":"392","G#4":"415","A4":"440","A#4":"466","B4":"493","C5":"523","C#5":"554","D5":"587","D#5":"622","E5":"659","F5":"698","F#5":"739","G5":"783","G#5":"830","A5":"880","A#5":"932","B5":"987","C6":"1046","C#6":"1108","D6":"1174","D#6":"1244","E6":"1318","F6":"1396","F#6":"1479","G6":"1567","G#6":"1661","A6":"1760","A#6":"1864","B6":"1975","C7":"2093","C#7":"2217","D7":"2349","D#7":"2489","E7":"2637","F7":"2793","F#7":"2959","G7":"3135","G#7":"3322","A7":"3520","A#7":"3729","B7":"3951","C8":"4186","C#8":"4434","D8":"4698","D#8":"4978"}; var Sound = function (options) { if (!options || !options.board) throw new Error('Must supply required options to LED'); this.board = options.board; @@ -10,4 +11,8 @@ Sound.prototype.tone = function(tone,duration) { this.board.write('96' + this.board.lpad(2, '0', this.pin) + this.board.lpad(6,'0',duration)); } +Sound.prototype.note = function(note, duration) { + this.tone(notes[note],duration); +} + module.exports = Sound; From 3e562fbae4cae504245cf1af4ae747b5925bbf42 Mon Sep 17 00:00:00 2001 From: Nicholas Rushton Date: Thu, 23 May 2013 23:18:27 -0700 Subject: [PATCH 3/8] restoring configable baudrate --- lib/board.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/board.js b/lib/board.js index 770d4c8..cfa34c9 100644 --- a/lib/board.js +++ b/lib/board.js @@ -12,6 +12,7 @@ var events = require('events'), var Board = function (options) { this.log('info', 'initializing'); this.debug = options && options.debug || false; + this.baudrate = options && options.baudrate || 115200; this.writeBuffer = []; var self = this; @@ -83,7 +84,7 @@ Board.prototype.detect = function (callback) { if (possible.slice(0, 2) !== 'cu') { try { temp = new serial.SerialPort('/dev/' + possible, { - baudrate: 115200, + baudrate: self.buadrate, parser: serial.parsers.readline('\n') }); } catch (e) { From 782b18e0dc190d5e5bf6b7776fff048a35cc6947 Mon Sep 17 00:00:00 2001 From: Nicholas Rushton Date: Thu, 23 May 2013 23:20:29 -0700 Subject: [PATCH 4/8] typo squish --- lib/board.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/board.js b/lib/board.js index cfa34c9..a1acf89 100644 --- a/lib/board.js +++ b/lib/board.js @@ -84,7 +84,7 @@ Board.prototype.detect = function (callback) { if (possible.slice(0, 2) !== 'cu') { try { temp = new serial.SerialPort('/dev/' + possible, { - baudrate: self.buadrate, + baudrate: self.baudrate, parser: serial.parsers.readline('\n') }); } catch (e) { From 8d84abaa0881afaea6fe65ee3259a8854cc4534a Mon Sep 17 00:00:00 2001 From: Nicholas Rushton Date: Thu, 23 May 2013 23:22:48 -0700 Subject: [PATCH 5/8] moving accidental file to correct location --- src/du.ino | 18 +++- src/du/du.ino | 269 -------------------------------------------------- 2 files changed, 16 insertions(+), 271 deletions(-) delete mode 100644 src/du/du.ino diff --git a/src/du.ino b/src/du.ino index 2427435..fa8e918 100644 --- a/src/du.ino +++ b/src/du.ino @@ -1,7 +1,7 @@ #include -bool debug = false; +bool debug = true; int index = 0; @@ -10,6 +10,8 @@ char cmd[3]; char pin[3]; char val[4]; char aux[4]; +char tne[7]; +char dur[7]; Servo servo; @@ -37,7 +39,15 @@ void process() { strncpy(pin, messageBuffer + 2, 2); pin[2] = '\0'; - if (atoi(cmd) > 90) { + if (atoi(cmd) == 95) { + strncpy(tne, messageBuffer + 4, 6); + tne[6] = '\0'; + } + else if (atoi(cmd) == 96) { + strncpy(dur, messageBuffer + 4, 6); + dur[6] = '\0'; + } + else if (atoi(cmd) > 90) { strncpy(val, messageBuffer + 4, 2); val[2] = '\0'; strncpy(aux, messageBuffer + 6, 3); @@ -65,6 +75,7 @@ void process() { case 2: dr(pin,val); break; case 3: aw(pin,val); break; case 4: ar(pin,val); break; + case 96: handleSoundDuration(pin,dur); break; case 97: handlePing(pin,val,aux); break; case 98: handleServo(pin,val,aux); break; case 99: toggleDebug(val); break; @@ -72,6 +83,9 @@ void process() { } } +void handleSoundDuration(char *pin, char *dur) { + tone(atoi(pin),atoi(tne), atoi(dur)); +} /* * Toggle debug mode */ diff --git a/src/du/du.ino b/src/du/du.ino deleted file mode 100644 index 6345823..0000000 --- a/src/du/du.ino +++ /dev/null @@ -1,269 +0,0 @@ -#include - - -bool debug = true; - -int index = 0; - -char messageBuffer[12]; -char cmd[3]; -char pin[3]; -char val[4]; -char aux[4]; -char tne[7]; -char dur[7]; - -Servo servo; - -void setup() { - Serial.begin(115200); -} - -void loop() { - while(Serial.available() > 0) { - char x = Serial.read(); - if (x == '!') index = 0; // start - else if (x == '.') process(); // end - else messageBuffer[index++] = x; - } -} - -/* - * Deal with a full message and determine function to call - */ -void process() { - index = 0; - - strncpy(cmd, messageBuffer, 2); - cmd[2] = '\0'; - strncpy(pin, messageBuffer + 2, 2); - pin[2] = '\0'; - - if (atoi(cmd) == 95) { - strncpy(tne, messageBuffer + 4, 6); - tne[6] = '\0'; - } - else if (atoi(cmd) == 96) { - strncpy(dur, messageBuffer + 4, 6); - dur[6] = '\0'; - } - else if (atoi(cmd) > 90) { - strncpy(val, messageBuffer + 4, 2); - val[2] = '\0'; - strncpy(aux, messageBuffer + 6, 3); - aux[3] = '\0'; - } else { - strncpy(val, messageBuffer + 4, 3); - val[4] = '\0'; - strncpy(aux, messageBuffer + 7, 3); - aux[4] = '\0'; - } - - if (debug) { - Serial.println(messageBuffer); - } - int cmdid = atoi(cmd); - - // Serial.println(cmd); - // Serial.println(pin); - // Serial.println(val); - // Serial.println(aux); - - switch(cmdid) { - case 0: sm(pin,val); break; - case 1: dw(pin,val); break; - case 2: dr(pin,val); break; - case 3: aw(pin,val); break; - case 4: ar(pin,val); break; - case 96: handleSoundDuration(pin,dur); break; - case 97: handlePing(pin,val,aux); break; - case 98: handleServo(pin,val,aux); break; - case 99: toggleDebug(val); break; - default: break; - } -} - -void handleSoundDuration(char *pin, char *dur) { - Serial.println("SOUND"); - Serial.println(tne); - Serial.println(dur); - tone(atoi(pin),atoi(tne), atoi(dur)); -} -/* - * Toggle debug mode - */ -void toggleDebug(char *val) { - if (atoi(val) == 0) { - debug = false; - Serial.println("goodbye"); - } else { - debug = true; - Serial.println("hello"); - } -} - -/* - * Set pin mode - */ -void sm(char *pin, char *val) { - if (debug) Serial.println("sm"); - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - if (atoi(val) == 0) { - pinMode(p, OUTPUT); - } else { - pinMode(p, INPUT); - } -} - -/* - * Digital write - */ -void dw(char *pin, char *val) { - if (debug) Serial.println("dw"); - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - pinMode(p, OUTPUT); - if (atoi(val) == 0) { - digitalWrite(p, LOW); - } else { - digitalWrite(p, HIGH); - } -} - -/* - * Digital read - */ -void dr(char *pin, char *val) { - if (debug) Serial.println("dr"); - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - pinMode(p, INPUT); - int oraw = digitalRead(p); - char m[7]; - sprintf(m, "%02d::%02d", p,oraw); - Serial.println(m); -} - -/* - * Analog read - */ -void ar(char *pin, char *val) { - if(debug) Serial.println("ar"); - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - pinMode(p, INPUT); // don't want to sw - int rval = analogRead(p); - char m[8]; - sprintf(m, "%s::%03d", pin, rval); - Serial.println(m); -} - -void aw(char *pin, char *val) { - if(debug) Serial.println("aw"); - int p = getPin(pin); - pinMode(p, OUTPUT); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - analogWrite(p,atoi(val)); -} - -int getPin(char *pin) { //Converts to A0-A5, and returns -1 on error - int ret = -1; - if(pin[0] == 'A' || pin[0] == 'a') { - switch(pin[1]) { - case '0': ret = A0; break; - case '1': ret = A1; break; - case '2': ret = A2; break; - case '3': ret = A3; break; - case '4': ret = A4; break; - case '5': ret = A5; break; - default: break; - } - } else { - ret = atoi(pin); - if(ret == 0 && (pin[0] != '0' || pin[1] != '0')) { - ret = -1; - } - } - return ret; -} - -/* - * Handle Ping commands - * fire, read - */ -void handlePing(char *pin, char *val, char *aux) { - if (debug) Serial.println("ss"); - int p = getPin(pin); - - if(p == -1) { if(debug) Serial.println("badpin"); return; } - Serial.println("got signal"); - - // 01(1) Fire and Read - if (atoi(val) == 1) { - char m[16]; - - pinMode(p, OUTPUT); - digitalWrite(p, LOW); - delayMicroseconds(2); - digitalWrite(p, HIGH); - delayMicroseconds(5); - digitalWrite(p, LOW); - - Serial.println("ping fired"); - - pinMode(p, INPUT); - sprintf(m, "%s::read::%08d", pin, pulseIn(p, HIGH)); - Serial.println(m); - - delay(50); - } -} - -/* - * Handle Servo commands - * attach, detach, write, read, writeMicroseconds, attached - */ -void handleServo(char *pin, char *val, char *aux) { - if (debug) Serial.println("ss"); - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - Serial.println("signal: servo"); - - // 00(0) Detach - if (atoi(val) == 0) { - servo.detach(); - char m[12]; - sprintf(m, "%s::detached", pin); - Serial.println(m); - - // 01(1) Attach - } else if (atoi(val) == 1) { - // servo.attach(p, 750, 2250); - servo.attach(p); - char m[12]; - sprintf(m, "%s::attached", pin); - Serial.println(m); - - // 02(2) Write - } else if (atoi(val) == 2) { - Serial.println("writing to servo"); - Serial.println(atoi(aux)); - // Write to servo - servo.write(atoi(aux)); - delay(15); - - // TODO: Experiment with microsecond pulses - // digitalWrite(pin, HIGH); // start the pulse - // delayMicroseconds(pulseWidth); // pulse width - // digitalWrite(pin, LOW); // stop the pulse - - // 03(3) Read - } else if (atoi(val) == 3) { - Serial.println("reading servo"); - int sval = servo.read(); - char m[13]; - sprintf(m, "%s::read::%03d", pin, sval); - Serial.println(m); - } -} From a331030a070368f5339fb00cb8fb942b5ec7b688 Mon Sep 17 00:00:00 2001 From: Nicholas Rushton Date: Thu, 23 May 2013 23:25:24 -0700 Subject: [PATCH 6/8] renaming sound function to something more sensible --- src/du.ino | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/du.ino b/src/du.ino index fa8e918..657a70a 100644 --- a/src/du.ino +++ b/src/du.ino @@ -75,7 +75,7 @@ void process() { case 2: dr(pin,val); break; case 3: aw(pin,val); break; case 4: ar(pin,val); break; - case 96: handleSoundDuration(pin,dur); break; + case 96: handleSound(pin,dur); break; case 97: handlePing(pin,val,aux); break; case 98: handleServo(pin,val,aux); break; case 99: toggleDebug(val); break; @@ -83,9 +83,7 @@ void process() { } } -void handleSoundDuration(char *pin, char *dur) { - tone(atoi(pin),atoi(tne), atoi(dur)); -} + /* * Toggle debug mode */ @@ -264,3 +262,10 @@ void handleServo(char *pin, char *val, char *aux) { Serial.println(m); } } + +/** + * handles sound using native tone function + */ +void handleSound(char *pin, char *dur) { + tone(atoi(pin),atoi(tne), atoi(dur)); +} From 13b398a27b8b3a3e27b8c12a0cb7f5c4955f8351 Mon Sep 17 00:00:00 2001 From: Nicholas Rushton Date: Thu, 23 May 2013 23:27:33 -0700 Subject: [PATCH 7/8] turning off debug --- src/du.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/du.ino b/src/du.ino index 657a70a..5b1303b 100644 --- a/src/du.ino +++ b/src/du.ino @@ -1,7 +1,7 @@ #include -bool debug = true; +bool debug = false; int index = 0; From b3a52f6a19ba3a829090d14857b982beea497c8a Mon Sep 17 00:00:00 2001 From: Nicholas Rushton Date: Thu, 23 May 2013 23:32:15 -0700 Subject: [PATCH 8/8] updating contributor list --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index 7360912..a15a0cd 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,10 @@ { "name": "Seiya Konno", "email": "nulltask@gmail.com" + }, + { + "name": "Nicholas Rushton", + "email": "nick.rushton@hotmail.com" } ], "name": "duino",