@@ -14,10 +14,10 @@ var errors = {
14
14
} ;
15
15
16
16
var 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 } } ,
21
21
{ id : "D4" , modes : [ 0 , 1 ] } ,
22
22
{ id : "D5" , modes : [ 0 , 1 ] } ,
23
23
{ id : "D6" , modes : [ 0 , 1 ] } ,
@@ -26,14 +26,14 @@ var pins = [
26
26
{ id : "" , modes : [ ] } ,
27
27
{ id : "" , modes : [ ] } ,
28
28
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 } } ,
31
31
{ id : "A2" , modes : [ 0 , 1 , 2 ] } ,
32
32
{ 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 } } ,
37
37
] ;
38
38
39
39
var modes = Object . freeze ( {
@@ -252,6 +252,7 @@ function Particle(opts) {
252
252
name : pin . id ,
253
253
supportedModes : pin . modes ,
254
254
mode : pin . modes [ 0 ] ,
255
+ pwm : pin . pwm ,
255
256
value : 0
256
257
} ;
257
258
} ) ;
@@ -479,10 +480,9 @@ Particle.prototype.pinMode = function(pin, mode) {
479
480
return this ;
480
481
} ;
481
482
482
- [ "analogWrite" , "digitalWrite" , "servoWrite" ] . forEach ( function ( fn ) {
483
+ [ "analogWrite" , "digitalWrite" ] . forEach ( function ( fn ) {
483
484
var isAnalog = fn === "analogWrite" ;
484
- var isServo = fn === "servoWrite" ;
485
- var action = isAnalog ? 0x02 : ( isServo ? 0x41 : 0x01 ) ;
485
+ var action = isAnalog ? 0x02 : 0x01 ;
486
486
487
487
Particle . prototype [ fn ] = function ( pin , value ) {
488
488
var state = priv . get ( this ) ;
@@ -502,6 +502,67 @@ Particle.prototype.pinMode = function(pin, mode) {
502
502
} ;
503
503
} ) ;
504
504
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
+
505
566
// TODO: Define protocol for gather this information.
506
567
[ "analogRead" , "digitalRead" ] . forEach ( function ( fn ) {
507
568
var isAnalog = fn === "analogRead" ;
@@ -812,6 +873,11 @@ Particle.prototype.close = function() {
812
873
state . server . close ( ) ;
813
874
} ;
814
875
876
+ function scale ( value , inMin , inMax , outMin , outMax ) {
877
+ return ( value - inMin ) * ( outMax - outMin ) /
878
+ ( inMax - inMin ) + outMin ;
879
+ }
880
+
815
881
function constrain ( value , lower , upper ) {
816
882
return Math . min ( upper , Math . max ( lower , value ) ) ;
817
883
}
0 commit comments