-
Notifications
You must be signed in to change notification settings - Fork 29
/
beaglebone.js
94 lines (75 loc) · 2.37 KB
/
beaglebone.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
'use strict';
var LinuxIO = require('linux-io'),
util = require('util'),
boardInfo = require('./board-info'),
AnalogInput = require('./analog-input'),
PwmOutput = require('./pwm-output'),
slots = require('./slots'),
pinmux = require('./pinmux');
var PWM_PERIOD = 1e9 / 2000, // 2000Hz
SERVO_PERIOD = 1e9 / 50, // 50Hz
PULSES_PER_MICROSEC = 1e9 / 1e6;
function BeagleBone() {
var pins = boardInfo.pins();
LinuxIO.call(this, {
name: 'BeagleBone-IO',
pins: pins,
defaultI2cBus: 2,
defaultLed: 'USR3',
aref: 1.8,
vref: 3.3
});
// Analog inputs are enabled by default on Debian Stretch but not on Jessie
if (!AnalogInput.isEnabled()) {
slots.add('BB-ADC');
}
setImmediate(function () {
this.emit('connect');
this.emit('ready');
}.bind(this));
}
util.inherits(BeagleBone, LinuxIO);
BeagleBone.prototype._pinModeInput = function(pinData) {
pinmux.state(pinData.custom.id, 'gpio');
BeagleBone.super_.prototype._pinModeInput.call(this, pinData);
};
BeagleBone.prototype._pinModeOutput = function(pinData) {
pinmux.state(pinData.custom.id, 'gpio');
BeagleBone.super_.prototype._pinModeOutput.call(this, pinData);
};
BeagleBone.prototype._pinModeAnalog = function(pinData) {
pinData.custom.analogInput = new AnalogInput(pinData.analogChannel);
};
BeagleBone.prototype._pinModePwm = function(pinData) {
pinData.custom.pwmOutput = new PwmOutput(pinData, PWM_PERIOD);
};
BeagleBone.prototype._pinModeServo = function(pinData) {
pinData.custom.pwmOutput = new PwmOutput(pinData, SERVO_PERIOD);
pinData.servoConfig = {
min: 400, // 450
max: 2600 // 2270
};
};
BeagleBone.prototype._analogRead = function(pinData, callback) {
setImmediate(function () {
try {
// Raw value is 0 to 4095 but J5 expects 0 to 1023.
callback(null, pinData.custom.analogInput.rawValue() >> 2);
} catch (err) {
callback(err);
}
});
};
BeagleBone.prototype._pwmWriteSync = function(pinData, value) {
var dutyCycle = Math.round(value / 255 * PWM_PERIOD);
pinData.custom.pwmOutput.pwmWrite(dutyCycle);
};
BeagleBone.prototype._servoWriteSync = function(pinData, value) {
var min = pinData.servoConfig.min,
max = pinData.servoConfig.max,
dutyCycle = Math.round(
(min + (value / 180) * (max - min)) * PULSES_PER_MICROSEC
);
pinData.custom.pwmOutput.pwmWrite(dutyCycle);
};
module.exports = BeagleBone;