@@ -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,11 @@ Particle.prototype.pinMode = function(pin, mode) {
479
480
return this ;
480
481
} ;
481
482
482
- [ "analogWrite" , "digitalWrite" , "servoWrite" ] . forEach ( function ( fn ) {
483
+
484
+
485
+ [ "analogWrite" , "digitalWrite" ] . forEach ( function ( fn ) {
483
486
var isAnalog = fn === "analogWrite" ;
484
- var isServo = fn === "servoWrite" ;
485
- var action = isAnalog ? 0x02 : ( isServo ? 0x41 : 0x01 ) ;
487
+ var action = isAnalog ? 0x02 : 0x01 ;
486
488
487
489
Particle . prototype [ fn ] = function ( pin , value ) {
488
490
var state = priv . get ( this ) ;
@@ -502,6 +504,67 @@ Particle.prototype.pinMode = function(pin, mode) {
502
504
} ;
503
505
} ) ;
504
506
507
+ Particle . prototype . servoWrite = function ( pin , value ) {
508
+ var state = priv . get ( this ) ;
509
+ var buffer ;
510
+ var offset = pin [ 0 ] === "A" ? 10 : 0 ;
511
+ var pinInt = ( String ( pin ) . replace ( / A | D / i, "" ) | 0 ) + offset ;
512
+
513
+ if ( value < 544 ) {
514
+ buffer = new Buffer ( 3 ) ;
515
+ value = constrain ( value , 0 , 180 ) ;
516
+ buffer [ 0 ] = 0x41 ;
517
+ buffer [ 1 ] = pinInt ;
518
+ buffer [ 2 ] = value ;
519
+ } else {
520
+ buffer = new Buffer ( 4 ) ;
521
+ value = constrain ( value , this . pins [ pinInt ] . pwm . servoMin , this . pins [ pinInt ] . pwm . servoMax ) ;
522
+ buffer [ 0 ] = 0x43 ;
523
+ buffer [ 1 ] = pinInt ;
524
+ buffer [ 2 ] = value & 0x7f ;
525
+ buffer [ 3 ] = value >> 7 ;
526
+ }
527
+
528
+ // console.log(buffer);
529
+ state . socket . write ( buffer ) ;
530
+ this . pins [ pinInt ] . value = value ;
531
+
532
+ return this ;
533
+ } ;
534
+
535
+ Particle . prototype . servoConfig = function ( pin , min , max ) {
536
+ var offset = pin [ 0 ] === "A" ? 10 : 0 ;
537
+ var pinInt = ( String ( pin ) . replace ( / A | D / i, "" ) | 0 ) + offset ;
538
+ var temp ;
539
+
540
+ if ( typeof pin === "object" && pin !== null ) {
541
+ temp = pin ;
542
+ pin = temp . pin ;
543
+ min = temp . min ;
544
+ max = temp . max ;
545
+ }
546
+
547
+ if ( typeof pin === "undefined" ) {
548
+ throw new Error ( "servoConfig: pin must be specified" ) ;
549
+ }
550
+
551
+ if ( typeof min === "undefined" ) {
552
+ throw new Error ( "servoConfig: min must be specified" ) ;
553
+ }
554
+
555
+ if ( typeof max === "undefined" ) {
556
+ throw new Error ( "servoConfig: max must be specified" ) ;
557
+ }
558
+
559
+ if ( this . pins [ pinInt ] . mode !== modes . SERVO ) {
560
+ this . pinMode ( pinInt , modes . SERVO ) ;
561
+ }
562
+
563
+ pins [ pinInt ] . pwm . servoMin = min ;
564
+ pins [ pinInt ] . pwm . servoMax = max ;
565
+
566
+ } ;
567
+
505
568
// TODO: Define protocol for gather this information.
506
569
[ "analogRead" , "digitalRead" ] . forEach ( function ( fn ) {
507
570
var isAnalog = fn === "analogRead" ;
@@ -812,6 +875,11 @@ Particle.prototype.close = function() {
812
875
state . server . close ( ) ;
813
876
} ;
814
877
878
+ function scale ( value , inMin , inMax , outMin , outMax ) {
879
+ return ( value - inMin ) * ( outMax - outMin ) /
880
+ ( inMax - inMin ) + outMin ;
881
+ }
882
+
815
883
function constrain ( value , lower , upper ) {
816
884
return Math . min ( upper , Math . max ( lower , value ) ) ;
817
885
}
0 commit comments