Skip to content

Commit

Permalink
Merge pull request #4 from EdoB/master
Browse files Browse the repository at this point in the history
Implement servoWrite and getServoPulseWidth methods + tests
  • Loading branch information
deepsyx committed Jun 26, 2018
2 parents 9f5afe7 + 0ea3037 commit 1293c0a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Gpio.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Gpio {
this.pwmValue = 0;
this.frequency = 31;
this._pwmRange = 0;
this.servoPulseWidth = Gpio.MED_SERVO_PULSE_WIDTH;

log(`[GPIO ${this.gpio} / constructor] Initializing...`);

Expand Down Expand Up @@ -119,13 +120,14 @@ class Gpio {
}

servoWrite(pulseWidth) {
log(`[GPIO ${this.gpio} / servoWrite] METHOD NOT IMPLEMENTED`);
this.servoPulseWidth = Math.min(Math.max(parseInt(pulseWidth), Gpio.MIN_SERVO_PULSE_WIDTH), Gpio.MAX_SERVO_PULSE_WIDTH);
log(`[GPIO ${this.gpio} / servoWrite] Setting pulseWidth = ${this.servoPulseWidth}`);
return this;
}

getServoPulseWidth() {
log(`[GPIO ${this.gpio} / getServoPulseWidth] METHOD NOT IMPLEMENTED`);
return null;
log(`[GPIO ${this.gpio} / getServoPulseWidth] Getting pulseWidth = ${this.servoPulseWidth}`);
return this.servoPulseWidth;
}
}

Expand Down Expand Up @@ -156,4 +158,9 @@ Gpio.MIN_GPIO = 0; // PI_MIN_GPIO;
Gpio.MAX_GPIO = 53; // PI_MAX_GPIO;
Gpio.MAX_USER_GPIO = 31; // PI_MAX_USER_GPIO;

/* gpio servo pulseWidth */
Gpio.MIN_SERVO_PULSE_WIDTH = 500; // most anti-clockwise position
Gpio.MED_SERVO_PULSE_WIDTH = 1500; // center position
Gpio.MAX_SERVO_PULSE_WIDTH = 2500; // most clockwise position

module.exports = Gpio;
10 changes: 10 additions & 0 deletions test/gpioTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,14 @@ describe("Gpio", function() {
pin.pwmFrequency(1024);
assert(pin.getPwmFrequency(), 1024);
});

it("should be able to servoWrite and getServoPulseWidth", function() {
const pin = new Gpio(13);
pin.servoWrite(1200);
assert.equal(pin.getServoPulseWidth(), 1200);
pin.servoWrite(0);
assert.equal(pin.getServoPulseWidth(), 500);
pin.servoWrite(3000);
assert.equal(pin.getServoPulseWidth(), 2500);
});
});

0 comments on commit 1293c0a

Please sign in to comment.