@@ -9,13 +9,13 @@ node eg/bug.js
99``` javascript
1010var five = require (" johnny-five" );
1111
12- function Bug ( servos ) {
12+ function Bug (servos ) {
1313 var part, k;
1414
1515 this .isMoving = false ;
1616
1717 // Initialize list of body parts
18- this .parts = [ " front" , " left" , " right" , " rear" ];
18+ this .parts = [" front" , " left" , " right" , " rear" ];
1919
2020 // Servo positions
2121 this .history = [];
@@ -28,10 +28,10 @@ function Bug( servos ) {
2828
2929 k = - 1 ;
3030 // Initialize Servo properties from "servos" argument
31- while ( ++ k < this .parts .length ) {
32- part = this .parts [ k ];
31+ while (++ k < this .parts .length ) {
32+ part = this .parts [k ];
3333
34- this [ part ] = servos[ part ] || null ;
34+ this [part] = servos[part] || null ;
3535 }
3636
3737 // Unwind and wait ~10ms, center servos
@@ -43,12 +43,12 @@ function Bug( servos ) {
4343Bug .STEP_MAP = {
4444 // steps
4545 fwd: [
46- [ 10 , - 30 ],
47- [ - 15 , 30 ]
46+ [10 , - 30 ],
47+ [- 15 , 30 ]
4848 ],
4949 rev: [
50- [ - 15 , 30 ],
51- [ 15 , - 30 ]
50+ [- 15 , 30 ],
51+ [15 , - 30 ]
5252 ]
5353};
5454
@@ -60,13 +60,13 @@ Bug.prototype.idle = function() {
6060
6161 // If the bug is actually in motion,
6262 // stop the servo intervals
63- if ( this .isMoving ) {
63+ if (this .isMoving ) {
6464 this .stop ();
6565 }
6666
6767 // set to center position°
68- this .front .move ( 90 );
69- this .rear .move ( 90 );
68+ this .front .move (90 );
69+ this .rear .move (90 );
7070
7171 // return this;
7272};
@@ -79,22 +79,23 @@ Bug.prototype.idle = function() {
7979 * dir {String}
8080 * @return {Bug}
8181 */
82- Bug .prototype .step = function ( opts ) {
82+ Bug .prototype .step = function (opts ) {
8383 var dir, move, last, front, rear, step;
8484
8585 opts = opts || {};
8686
8787 // Get last move from history if any history exists
8888 // Provide a "fake" history if needed (first call)
8989 last = this .history .length ?
90- this .history [ this .history .length - 1 ] :
91- { step: 1 };
90+ this .history [this .history .length - 1 ] : {
91+ step: 1
92+ };
9293
9394 // increment the last step
9495 step = last .step + 1 ;
9596
9697 // If step is too high, step back to 0
97- if ( step > 1 ) {
98+ if (step > 1 ) {
9899 step = 0 ;
99100 }
100101
@@ -103,20 +104,20 @@ Bug.prototype.step = function( opts ) {
103104 dir = opts .dir || " fwd" ;
104105
105106 // Derive position° for next move
106- move = Bug .STEP_MAP [ dir ][ step ];
107+ move = Bug .STEP_MAP [dir][ step];
107108
108109 // Assign position° from center
109110 front = 90 + move[0 ];
110111 rear = 90 + move[1 ];
111112
112113 // Write position° to servos
113- this .front .move ( front );
114- this .rear .move ( rear );
114+ this .front .move (front);
115+ this .rear .move (rear);
115116
116117 // Allow half step or full if provided,
117118 // defaults to full
118119 // enum(false|null|undefined)
119- if ( ! opts .half ) {
120+ if (! opts .half ) {
120121 // Wait one second and move servos back to
121122 // center idling position, 90°
122123 setTimeout (function () {
@@ -129,7 +130,12 @@ Bug.prototype.step = function( opts ) {
129130 this .history .push (
130131 // NOTE: this is a great use case example for
131132 // ES.next concise object initializers
132- { dir: dir, step: step, front: front, rear: rear }
133+ {
134+ dir: dir,
135+ step: step,
136+ front: front,
137+ rear: rear
138+ }
133139 );
134140};
135141
@@ -138,9 +144,9 @@ Bug.prototype.step = function( opts ) {
138144 * @return {Bug}
139145 */
140146Bug .prototype .stop = function () {
141- Object .keys ( this .intervals ).forEach (function ( key ) {
142- if ( this .intervals [ key ] ) {
143- clearInterval ( this .intervals [ key ] );
147+ Object .keys (this .intervals ).forEach (function (key ) {
148+ if (this .intervals [key] ) {
149+ clearInterval (this .intervals [key] );
144150 }
145151 }, this );
146152 // return this;
@@ -159,15 +165,18 @@ Bug.prototype.stop = function() {
159165 */
160166 " rev"
161167
162- ].forEach (function ( dir , k ) {
168+ ].forEach (function (dir , k ) {
163169
164- Bug .prototype [ dir ] = function () {
170+ Bug .prototype [dir] = function () {
165171
166172 this .isMoving = true ;
167173
168- this .intervals [ dir ] = setInterval (function () {
169- this .step ({ dir: dir, half: true });
170- }.bind (this ), 750 );
174+ this .intervals [dir] = setInterval (function () {
175+ this .step ({
176+ dir: dir,
177+ half: true
178+ });
179+ }.bind (this ), 750 );
171180
172181 // //return this;
173182 };
0 commit comments