-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
99 lines (88 loc) · 2.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var net = require('net');
var Put = require('put');
/** @constructor */
function PiFastGpio() {}
/**
* Connects to the pigpio daemon.
*
* This method has to be called before it's possible to do anything with
* the GPIO pins.
*
* The callback will be called when the connection has been established or
* if an error occurs.
*
* @param {string} host - The host to connect to
* @param {number} port - The port on the host to connect to
* @param {function} cb - Callback function
*/
PiFastGpio.prototype.connect = function(host, port, cb) {
this.client = net.connect({host: host, port: port});
this.client.on('connect', function() {
// Disable the Nagle algoritm
this.client.setNoDelay(true);
cb();
}.bind(this));
this.client.on('error', function(e) {
cb(e);
});
};
/**
* Half-closes the connection. We might still get some data from the server.
*/
PiFastGpio.prototype.close = function() {
this.client.end();
};
/**
* Starts (500-2500) or stops (0) servo pulses on the given gpio pin.
*
* The selected pulsewidth will continue to be transmitted until
* changed by a subsequent call to set_servo_pulsewidth.
*
* The pulsewidths supported by servos varies and should probably
* be determined by experiment. A value of 1500 should always be
* safe and represents the mid-point of rotation.
*
* You can DAMAGE a servo if you command it to move beyond its
* limits.
*
* gpio.setServoPulsewidth(17, 0) # off
* gpio.setServoPulsewidth(17, 1000) # safe anti-clockwise
* gpio.setServoPulsewidth(17, 1500) # centre
* gpio.setServoPulsewidth(17, 2000) # safe clockwise
*
* @param {number} userGpio The number of the gpio to address. 0-31.
* @param {number} pulsewidth The servo pulsewidth to generate
* 0 (off),
* 500 (most anti-clockwise) - 2500 (most clockwise).
*/
PiFastGpio.prototype.setServoPulsewidth = function(userGpio, pulsewidth) {
var cmd = Put()
.word32le(8) // _PI_CMD_SERVO
.word32le(userGpio)
.word32le(pulsewidth)
.word32le(0); // Not used
this.client.write(cmd.buffer());
};
/**
* Starts (non-zero dutycycle) or stops (0) PWM pulses on the gpio.
*
* pi.set_PWM_dutycycle(4, 0) # PWM off
* pi.set_PWM_dutycycle(4, 64) # PWM 1/4 on
* pi.set_PWM_dutycycle(4, 128) # PWM 1/2 on
* pi.set_PWM_dutycycle(4, 192) # PWM 3/4 on
* pi.set_PWM_dutycycle(4, 255) # PWM full on
*
* @param {number} userGpio The number of the gpio to address. 0-31
* @param {number} dutycycle The pwm dutycycle to use.
* 0 (off),
* 255 (full on)
*/
PiFastGpio.prototype.setPwmDutycycle = function(userGpio, dutycycle) {
var cmd = Put()
.word32le(5) // _PI_CMD_PWM
.word32le(userGpio)
.word32le(dutycycle)
.word32le(0);
this.client.write(cmd.buffer());
};
module.exports = PiFastGpio;