@@ -14,10 +14,10 @@ var errors = {
1414} ;
1515
1616var pins = [
17- { id : "D0" , modes : [ 0 , 1 , 3 , 4 ] } ,
18- { id : "D1" , modes : [ 0 , 1 , 3 , 4 ] } ,
19- { id : "D2" , modes : [ 0 , 1 , 3 , 4 ] } ,
20- { id : "D3" , modes : [ 0 , 1 , 3 , 4 ] } ,
17+ { id : "D0" , modes : [ 0 , 1 , 3 , 4 ] , pwm : { servoMin : 600 , servoMax : 2400 } } ,
18+ { id : "D1" , modes : [ 0 , 1 , 3 , 4 ] , pwm : { servoMin : 600 , servoMax : 2400 } } ,
19+ { id : "D2" , modes : [ 0 , 1 , 3 , 4 ] , pwm : { servoMin : 600 , servoMax : 2400 } } ,
20+ { id : "D3" , modes : [ 0 , 1 , 3 , 4 ] , pwm : { servoMin : 600 , servoMax : 2400 } } ,
2121 { id : "D4" , modes : [ 0 , 1 ] } ,
2222 { id : "D5" , modes : [ 0 , 1 ] } ,
2323 { id : "D6" , modes : [ 0 , 1 ] } ,
@@ -26,14 +26,14 @@ var pins = [
2626 { id : "" , modes : [ ] } ,
2727 { id : "" , modes : [ ] } ,
2828
29- { id : "A0" , modes : [ 0 , 1 , 2 , 3 , 4 ] } ,
30- { id : "A1" , modes : [ 0 , 1 , 2 , 3 , 4 ] } ,
29+ { id : "A0" , modes : [ 0 , 1 , 2 , 3 , 4 ] , pwm : { servoMin : 600 , servoMax : 2400 } } ,
30+ { id : "A1" , modes : [ 0 , 1 , 2 , 3 , 4 ] , pwm : { servoMin : 600 , servoMax : 2400 } } ,
3131 { id : "A2" , modes : [ 0 , 1 , 2 ] } ,
3232 { id : "A3" , modes : [ 0 , 1 , 2 ] } ,
33- { id : "A4" , modes : [ 0 , 1 , 2 , 3 , 4 ] } ,
34- { id : "A5" , modes : [ 0 , 1 , 2 , 3 , 4 ] } ,
35- { id : "A6" , modes : [ 0 , 1 , 2 , 3 , 4 ] } ,
36- { id : "A7" , modes : [ 0 , 1 , 2 , 3 , 4 ] }
33+ { id : "A4" , modes : [ 0 , 1 , 2 , 3 , 4 ] , pwm : { servoMin : 1000 , servoMax : 2000 } } ,
34+ { id : "A5" , modes : [ 0 , 1 , 2 , 3 , 4 ] , pwm : { servoMin : 600 , servoMax : 2400 } } ,
35+ { id : "A6" , modes : [ 0 , 1 , 2 , 3 , 4 ] , pwm : { servoMin : 600 , servoMax : 2400 } } ,
36+ { id : "A7" , modes : [ 0 , 1 , 2 , 3 , 4 ] , pwm : { servoMin : 600 , servoMax : 2400 } } ,
3737] ;
3838
3939var modes = Object . freeze ( {
@@ -252,6 +252,7 @@ function Particle(opts) {
252252 name : pin . id ,
253253 supportedModes : pin . modes ,
254254 mode : pin . modes [ 0 ] ,
255+ pwm : pin . pwm ,
255256 value : 0
256257 } ;
257258 } ) ;
@@ -479,10 +480,9 @@ Particle.prototype.pinMode = function(pin, mode) {
479480 return this ;
480481} ;
481482
482- [ "analogWrite" , "digitalWrite" , "servoWrite" ] . forEach ( function ( fn ) {
483+ [ "analogWrite" , "digitalWrite" ] . forEach ( function ( fn ) {
483484 var isAnalog = fn === "analogWrite" ;
484- var isServo = fn === "servoWrite" ;
485- var action = isAnalog ? 0x02 : ( isServo ? 0x41 : 0x01 ) ;
485+ var action = isAnalog ? 0x02 : 0x01 ;
486486
487487 Particle . prototype [ fn ] = function ( pin , value ) {
488488 var state = priv . get ( this ) ;
@@ -502,6 +502,67 @@ Particle.prototype.pinMode = function(pin, mode) {
502502 } ;
503503} ) ;
504504
505+ Particle . prototype . servoWrite = function ( pin , value ) {
506+ var state = priv . get ( this ) ;
507+ var buffer ;
508+ var offset = pin [ 0 ] === "A" ? 10 : 0 ;
509+ var pinInt = ( String ( pin ) . replace ( / A | D / i, "" ) | 0 ) + offset ;
510+
511+ if ( value < 544 ) {
512+ buffer = new Buffer ( 3 ) ;
513+ value = constrain ( value , 0 , 180 ) ;
514+ buffer [ 0 ] = 0x41 ;
515+ buffer [ 1 ] = pinInt ;
516+ buffer [ 2 ] = value ;
517+ } else {
518+ buffer = new Buffer ( 4 ) ;
519+ value = constrain ( value , this . pins [ pinInt ] . pwm . servoMin , this . pins [ pinInt ] . pwm . servoMax ) ;
520+ buffer [ 0 ] = 0x43 ;
521+ buffer [ 1 ] = pinInt ;
522+ buffer [ 2 ] = value & 0x7f ;
523+ buffer [ 3 ] = value >> 7 ;
524+ }
525+
526+ // console.log(buffer);
527+ state . socket . write ( buffer ) ;
528+ this . pins [ pinInt ] . value = value ;
529+
530+ return this ;
531+ } ;
532+
533+ Particle . prototype . servoConfig = function ( pin , min , max ) {
534+ var offset = pin [ 0 ] === "A" ? 10 : 0 ;
535+ var pinInt = ( String ( pin ) . replace ( / A | D / i, "" ) | 0 ) + offset ;
536+ var temp ;
537+
538+ if ( typeof pin === "object" && pin !== null ) {
539+ temp = pin ;
540+ pin = temp . pin ;
541+ min = temp . min ;
542+ max = temp . max ;
543+ }
544+
545+ if ( typeof pin === "undefined" ) {
546+ throw new Error ( "servoConfig: pin must be specified" ) ;
547+ }
548+
549+ if ( typeof min === "undefined" ) {
550+ throw new Error ( "servoConfig: min must be specified" ) ;
551+ }
552+
553+ if ( typeof max === "undefined" ) {
554+ throw new Error ( "servoConfig: max must be specified" ) ;
555+ }
556+
557+ if ( this . pins [ pinInt ] . mode !== modes . SERVO ) {
558+ this . pinMode ( pinInt , modes . SERVO ) ;
559+ }
560+
561+ pins [ pinInt ] . pwm . servoMin = min ;
562+ pins [ pinInt ] . pwm . servoMax = max ;
563+
564+ } ;
565+
505566// TODO: Define protocol for gather this information.
506567[ "analogRead" , "digitalRead" ] . forEach ( function ( fn ) {
507568 var isAnalog = fn === "analogRead" ;
@@ -812,6 +873,11 @@ Particle.prototype.close = function() {
812873 state . server . close ( ) ;
813874} ;
814875
876+ function scale ( value , inMin , inMax , outMin , outMax ) {
877+ return ( value - inMin ) * ( outMax - outMin ) /
878+ ( inMax - inMin ) + outMin ;
879+ }
880+
815881function constrain ( value , lower , upper ) {
816882 return Math . min ( upper , Math . max ( lower , value ) ) ;
817883}
0 commit comments