UNPKG

5.96 kBJavaScriptView Raw
1var Emitter = require("events").EventEmitter;
2var util = require("util");
3var shared;
4
5function Bank(options) {
6 this.address = options.address;
7 this.io = options.io;
8 this.io.i2cConfig(options);
9}
10
11Bank.prototype.read = function(register, numBytes, callback) {
12 if (register) {
13 this.io.i2cRead(this.address, register, numBytes, callback);
14 } else {
15 this.io.i2cRead(this.address, numBytes, callback);
16 }
17};
18
19Bank.prototype.write = function(register, bytes) {
20 if (!Array.isArray(bytes)) {
21 bytes = [bytes];
22 }
23 this.io.i2cWrite(this.address, register, bytes);
24};
25
26function EVS(options) {
27 if (shared) {
28 return shared;
29 }
30
31 this.bank = {
32 a: new Bank({
33 address: EVS.BANK_A,
34 io: options.io,
35 }),
36 b: new Bank({
37 address: EVS.BANK_B,
38 io: options.io,
39 })
40 };
41
42 shared = this;
43}
44
45EVS.shieldPort = function(pin) {
46 var port = EVS[pin];
47
48 if (port === undefined) {
49 throw new Error("Invalid EVShield pin name");
50 }
51
52 var address, analog, bank, motor, mode, offset, sensor;
53 var endsWithS1 = false;
54
55 if (pin.startsWith("BA")) {
56 address = EVS.BANK_A;
57 bank = "a";
58 } else {
59 address = EVS.BANK_B;
60 bank = "b";
61 }
62
63 if (pin.includes("M")) {
64 motor = pin.endsWith("M1") ? EVS.S1 : EVS.S2;
65 }
66
67 if (pin.includes("S")) {
68 endsWithS1 = pin.endsWith("S1");
69
70 // Used for reading 2 byte integer values from raw sensors
71 analog = endsWithS1 ? EVS.S1_ANALOG : EVS.S2_ANALOG;
72 // Sensor Mode (1 or 2?)
73 mode = endsWithS1 ? EVS.S1_MODE : EVS.S2_MODE;
74 // Used for read registers
75 offset = endsWithS1 ? EVS.S1_OFFSET : EVS.S2_OFFSET;
76 // Used to address "sensor type"
77 sensor = endsWithS1 ? EVS.S1 : EVS.S2;
78 }
79
80 return {
81 address: address,
82 analog: analog,
83 bank: bank,
84 mode: mode,
85 motor: motor,
86 offset: offset,
87 port: port,
88 sensor: sensor,
89 };
90};
91
92EVS.isRawSensor = function(port) {
93 return port.analog === EVS.S1_ANALOG || port.analog === EVS.S2_ANALOG;
94};
95
96util.inherits(EVS, Emitter);
97
98EVS.prototype.setup = function(port, type) {
99 this.bank[port.bank].write(port.mode, [type]);
100};
101
102EVS.prototype.read = function(port, register, numBytes, callback) {
103
104 if (port.sensor && port.offset && !EVS.isRawSensor(port)) {
105 register += port.offset;
106 }
107
108 this.bank[port.bank].read(register, numBytes, callback);
109};
110
111EVS.prototype.write = function(port, register, data) {
112 this.bank[port.bank].write(register, data);
113};
114
115/*
116 * Shield Registers
117 */
118
119EVS.BAS1 = 0x01;
120EVS.BAS2 = 0x02;
121EVS.BBS1 = 0x03;
122EVS.BBS2 = 0x04;
123
124EVS.BAM1 = 0x05;
125EVS.BAM2 = 0x06;
126EVS.BBM1 = 0x07;
127EVS.BBM2 = 0x08;
128
129EVS.BANK_A = 0x1A;
130EVS.BANK_B = 0x1B;
131
132EVS.S1 = 0x01;
133EVS.S2 = 0x02;
134
135EVS.M1 = 0x01;
136EVS.M2 = 0x02;
137EVS.MM = 0x03;
138
139EVS.Type_NONE = 0x00;
140EVS.Type_SWITCH = 0x01;
141EVS.Type_ANALOG = 0x02;
142
143EVS.Type_I2C = 0x09;
144
145/*
146 * Sensor Mode NXT
147 */
148EVS.Type_NXT_LIGHT_REFLECTED = 0x03;
149EVS.Type_NXT_LIGHT = 0x04;
150EVS.Type_NXT_COLOR = 0x0D;
151EVS.Type_NXT_COLOR_RGBRAW = 0x04;
152EVS.Type_NXT_COLORRED = 0x0E;
153EVS.Type_NXT_COLORGREEN = 0x0F;
154EVS.Type_NXT_COLORBLUE = 0x10;
155EVS.Type_NXT_COLORNONE = 0x11;
156
157
158EVS.Type_DATABIT0_HIGH = 0x40;
159
160/*
161 * Sensor Port Controls
162 */
163EVS.S1_MODE = 0x6F;
164// EVS.S1_EV3_MODE = 0x6F;
165EVS.S1_ANALOG = 0x70;
166EVS.S1_OFFSET = 0;
167
168EVS.S2_MODE = 0xA3;
169// EVS.S2_EV3_MODE = 0x6F;
170EVS.S2_ANALOG = 0xA4;
171EVS.S2_OFFSET = 52;
172
173/*
174 * Sensor Mode EV3
175 */
176EVS.Type_EV3_LIGHT_REFLECTED = 0x00;
177EVS.Type_EV3_LIGHT = 0x01;
178EVS.Type_EV3_COLOR = 0x02;
179EVS.Type_EV3_COLOR_REFRAW = 0x03;
180EVS.Type_EV3_COLOR_RGBRAW = 0x04;
181EVS.Type_EV3_TOUCH = 0x12;
182EVS.Type_EV3 = 0x13;
183
184/*
185 * Sensor Read Registers
186 */
187EVS.Light = 0x83;
188EVS.Bump = 0x84;
189EVS.ColorMeasure = 0x83;
190EVS.Proximity = 0x83;
191EVS.Touch = 0x83;
192EVS.Ultrasonic = 0x81;
193EVS.Mode = 0x81;
194
195/*
196 * Sensor Read Byte Counts
197 */
198EVS.Light_Bytes = 2;
199EVS.Analog_Bytes = 2;
200EVS.Bump_Bytes = 1;
201EVS.ColorMeasure_Bytes = 2;
202EVS.Proximity_Bytes = 2;
203EVS.Touch_Bytes = 1;
204
205
206/*
207 * Motor selection
208 */
209EVS.Motor_1 = 0x01;
210EVS.Motor_2 = 0x02;
211EVS.Motor_Both = 0x03;
212
213/*
214 * Motor next action
215 */
216// stop and let the motor coast.
217EVS.Motor_Next_Action_Float = 0x00;
218// apply brakes, and resist change to tachometer, but if tach position is forcibly changed, do not restore position
219EVS.Motor_Next_Action_Brake = 0x01;
220// apply brakes, and restore externally forced change to tachometer
221EVS.Motor_Next_Action_BrakeHold = 0x02;
222
223EVS.Motor_Stop = 0x60;
224EVS.Motor_Reset = 0x52;
225
226/*
227 * Motor direction
228 */
229
230EVS.Motor_Reverse = 0x00;
231EVS.Motor_Forward = 0x01;
232
233/*
234 * Motor Tachometer movement
235 */
236
237// Move the tach to absolute value provided
238EVS.Motor_Move_Absolute = 0x00;
239// Move the tach relative to previous position
240EVS.Motor_Move_Relative = 0x01;
241
242/*
243 * Motor completion
244 */
245
246EVS.Motor_Completion_Dont_Wait = 0x00;
247EVS.Motor_Completion_Wait_For = 0x01;
248
249/*
250 * 0-100
251 */
252EVS.Speed_Full = 90;
253EVS.Speed_Medium = 60;
254EVS.Speed_Slow = 25;
255
256/*
257 * Motor Port Controls
258 */
259EVS.CONTROL_SPEED = 0x01;
260EVS.CONTROL_RAMP = 0x02;
261EVS.CONTROL_RELATIVE = 0x04;
262EVS.CONTROL_TACHO = 0x08;
263EVS.CONTROL_BRK = 0x10;
264EVS.CONTROL_ON = 0x20;
265EVS.CONTROL_TIME = 0x40;
266EVS.CONTROL_GO = 0x80;
267
268EVS.STATUS_SPEED = 0x01;
269EVS.STATUS_RAMP = 0x02;
270EVS.STATUS_MOVING = 0x04;
271EVS.STATUS_TACHO = 0x08;
272EVS.STATUS_BREAK = 0x10;
273EVS.STATUS_OVERLOAD = 0x20;
274EVS.STATUS_TIME = 0x40;
275EVS.STATUS_STALL = 0x80;
276
277EVS.COMMAND = 0x41;
278EVS.VOLTAGE = 0x6E;
279
280EVS.SETPT_M1 = 0x42;
281EVS.SPEED_M1 = 0x46;
282EVS.TIME_M1 = 0x47;
283EVS.CMD_B_M1 = 0x48;
284EVS.CMD_A_M1 = 0x49;
285
286EVS.SETPT_M2 = 0x4A;
287EVS.SPEED_M2 = 0x4E;
288EVS.TIME_M2 = 0x4F;
289EVS.CMD_B_M2 = 0x50;
290EVS.CMD_A_M2 = 0x51;
291
292
293/*
294 * Motor Read registers.
295 */
296EVS.POSITION_M1 = 0x52;
297EVS.POSITION_M2 = 0x56;
298EVS.STATUS_M1 = 0x5A;
299EVS.STATUS_M2 = 0x5B;
300EVS.TASKS_M1 = 0x5C;
301EVS.TASKS_M2 = 0x5D;
302
303EVS.ENCODER_PID = 0x5E;
304EVS.SPEED_PID = 0x64;
305EVS.PASS_COUNT = 0x6A;
306EVS.TOLERANCE = 0x6B;
307
308/*
309 * Built-in components
310 */
311EVS.BTN_PRESS = 0xDA;
312EVS.RGB_LED = 0xD7;
313EVS.CENTER_RGB_LED = 0xDE;
314
315
316
317module.exports = EVS;