Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Native sound support #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
};
18 changes: 18 additions & 0 deletions lib/sound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;
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));
}

Sound.prototype.note = function(note, duration) {
this.tone(notes[note],duration);
}

module.exports = Sound;
31 changes: 26 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
{
"author": "Cam Pedersen <[email protected]> (http://campedersen.com/)",
"author": {
"name": "Cam Pedersen",
"email": "[email protected]",
"url": "http://campedersen.com/"
},
"contributors": [
{ "name": "Rick Waldron", "email": "[email protected]" },
{ "name": "Leonhardt Wille", "email": "[email protected]" },
{ "name": "Seiya Konno", "email": "[email protected]" }
{
"name": "Rick Waldron",
"email": "[email protected]"
},
{
"name": "Leonhardt Wille",
"email": "[email protected]"
},
{
"name": "Seiya Konno",
"email": "[email protected]"
},
{
"name": "Nicholas Rushton",
"email": "[email protected]"
}
],
"name": "duino",
"description": "Arduino framework for mad scientists",
Expand All @@ -25,5 +42,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 <[email protected]>\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": "[email protected]",
"_from": "[email protected]"
}
21 changes: 20 additions & 1 deletion src/du.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ char cmd[3];
char pin[3];
char val[4];
char aux[4];
char tne[7];
char dur[7];

Servo servo;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -65,13 +75,15 @@ void process() {
case 2: dr(pin,val); break;
case 3: aw(pin,val); break;
case 4: ar(pin,val); 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;
default: break;
}
}


/*
* Toggle debug mode
*/
Expand Down Expand Up @@ -250,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));
}