1 | var Emitter = require("events").EventEmitter;
|
2 | var util = require("util");
|
3 | var shared;
|
4 |
|
5 | function Bank(options) {
|
6 | this.address = options.address;
|
7 | this.io = options.io;
|
8 | this.io.i2cConfig(options);
|
9 | }
|
10 |
|
11 | Bank.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 |
|
19 | Bank.prototype.write = function(register, bytes) {
|
20 | if (!Array.isArray(bytes)) {
|
21 | bytes = [bytes];
|
22 | }
|
23 | this.io.i2cWrite(this.address, register, bytes);
|
24 | };
|
25 |
|
26 | function 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 |
|
45 | EVS.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 |
|
71 | analog = endsWithS1 ? EVS.S1_ANALOG : EVS.S2_ANALOG;
|
72 |
|
73 | mode = endsWithS1 ? EVS.S1_MODE : EVS.S2_MODE;
|
74 |
|
75 | offset = endsWithS1 ? EVS.S1_OFFSET : EVS.S2_OFFSET;
|
76 |
|
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 |
|
92 | EVS.isRawSensor = function(port) {
|
93 | return port.analog === EVS.S1_ANALOG || port.analog === EVS.S2_ANALOG;
|
94 | };
|
95 |
|
96 | util.inherits(EVS, Emitter);
|
97 |
|
98 | EVS.prototype.setup = function(port, type) {
|
99 | this.bank[port.bank].write(port.mode, [type]);
|
100 | };
|
101 |
|
102 | EVS.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 |
|
111 | EVS.prototype.write = function(port, register, data) {
|
112 | this.bank[port.bank].write(register, data);
|
113 | };
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | EVS.BAS1 = 0x01;
|
120 | EVS.BAS2 = 0x02;
|
121 | EVS.BBS1 = 0x03;
|
122 | EVS.BBS2 = 0x04;
|
123 |
|
124 | EVS.BAM1 = 0x05;
|
125 | EVS.BAM2 = 0x06;
|
126 | EVS.BBM1 = 0x07;
|
127 | EVS.BBM2 = 0x08;
|
128 |
|
129 | EVS.BANK_A = 0x1A;
|
130 | EVS.BANK_B = 0x1B;
|
131 |
|
132 | EVS.S1 = 0x01;
|
133 | EVS.S2 = 0x02;
|
134 |
|
135 | EVS.M1 = 0x01;
|
136 | EVS.M2 = 0x02;
|
137 | EVS.MM = 0x03;
|
138 |
|
139 | EVS.Type_NONE = 0x00;
|
140 | EVS.Type_SWITCH = 0x01;
|
141 | EVS.Type_ANALOG = 0x02;
|
142 |
|
143 | EVS.Type_I2C = 0x09;
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | EVS.Type_NXT_LIGHT_REFLECTED = 0x03;
|
149 | EVS.Type_NXT_LIGHT = 0x04;
|
150 | EVS.Type_NXT_COLOR = 0x0D;
|
151 | EVS.Type_NXT_COLOR_RGBRAW = 0x04;
|
152 | EVS.Type_NXT_COLORRED = 0x0E;
|
153 | EVS.Type_NXT_COLORGREEN = 0x0F;
|
154 | EVS.Type_NXT_COLORBLUE = 0x10;
|
155 | EVS.Type_NXT_COLORNONE = 0x11;
|
156 |
|
157 |
|
158 | EVS.Type_DATABIT0_HIGH = 0x40;
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | EVS.S1_MODE = 0x6F;
|
164 |
|
165 | EVS.S1_ANALOG = 0x70;
|
166 | EVS.S1_OFFSET = 0;
|
167 |
|
168 | EVS.S2_MODE = 0xA3;
|
169 |
|
170 | EVS.S2_ANALOG = 0xA4;
|
171 | EVS.S2_OFFSET = 52;
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | EVS.Type_EV3_LIGHT_REFLECTED = 0x00;
|
177 | EVS.Type_EV3_LIGHT = 0x01;
|
178 | EVS.Type_EV3_COLOR = 0x02;
|
179 | EVS.Type_EV3_COLOR_REFRAW = 0x03;
|
180 | EVS.Type_EV3_COLOR_RGBRAW = 0x04;
|
181 | EVS.Type_EV3_TOUCH = 0x12;
|
182 | EVS.Type_EV3 = 0x13;
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | EVS.Light = 0x83;
|
188 | EVS.Bump = 0x84;
|
189 | EVS.ColorMeasure = 0x83;
|
190 | EVS.Proximity = 0x83;
|
191 | EVS.Touch = 0x83;
|
192 | EVS.Ultrasonic = 0x81;
|
193 | EVS.Mode = 0x81;
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | EVS.Light_Bytes = 2;
|
199 | EVS.Analog_Bytes = 2;
|
200 | EVS.Bump_Bytes = 1;
|
201 | EVS.ColorMeasure_Bytes = 2;
|
202 | EVS.Proximity_Bytes = 2;
|
203 | EVS.Touch_Bytes = 1;
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | EVS.Motor_1 = 0x01;
|
210 | EVS.Motor_2 = 0x02;
|
211 | EVS.Motor_Both = 0x03;
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | EVS.Motor_Next_Action_Float = 0x00;
|
218 |
|
219 | EVS.Motor_Next_Action_Brake = 0x01;
|
220 |
|
221 | EVS.Motor_Next_Action_BrakeHold = 0x02;
|
222 |
|
223 | EVS.Motor_Stop = 0x60;
|
224 | EVS.Motor_Reset = 0x52;
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | EVS.Motor_Reverse = 0x00;
|
231 | EVS.Motor_Forward = 0x01;
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | EVS.Motor_Move_Absolute = 0x00;
|
239 |
|
240 | EVS.Motor_Move_Relative = 0x01;
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | EVS.Motor_Completion_Dont_Wait = 0x00;
|
247 | EVS.Motor_Completion_Wait_For = 0x01;
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | EVS.Speed_Full = 90;
|
253 | EVS.Speed_Medium = 60;
|
254 | EVS.Speed_Slow = 25;
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | EVS.CONTROL_SPEED = 0x01;
|
260 | EVS.CONTROL_RAMP = 0x02;
|
261 | EVS.CONTROL_RELATIVE = 0x04;
|
262 | EVS.CONTROL_TACHO = 0x08;
|
263 | EVS.CONTROL_BRK = 0x10;
|
264 | EVS.CONTROL_ON = 0x20;
|
265 | EVS.CONTROL_TIME = 0x40;
|
266 | EVS.CONTROL_GO = 0x80;
|
267 |
|
268 | EVS.STATUS_SPEED = 0x01;
|
269 | EVS.STATUS_RAMP = 0x02;
|
270 | EVS.STATUS_MOVING = 0x04;
|
271 | EVS.STATUS_TACHO = 0x08;
|
272 | EVS.STATUS_BREAK = 0x10;
|
273 | EVS.STATUS_OVERLOAD = 0x20;
|
274 | EVS.STATUS_TIME = 0x40;
|
275 | EVS.STATUS_STALL = 0x80;
|
276 |
|
277 | EVS.COMMAND = 0x41;
|
278 | EVS.VOLTAGE = 0x6E;
|
279 |
|
280 | EVS.SETPT_M1 = 0x42;
|
281 | EVS.SPEED_M1 = 0x46;
|
282 | EVS.TIME_M1 = 0x47;
|
283 | EVS.CMD_B_M1 = 0x48;
|
284 | EVS.CMD_A_M1 = 0x49;
|
285 |
|
286 | EVS.SETPT_M2 = 0x4A;
|
287 | EVS.SPEED_M2 = 0x4E;
|
288 | EVS.TIME_M2 = 0x4F;
|
289 | EVS.CMD_B_M2 = 0x50;
|
290 | EVS.CMD_A_M2 = 0x51;
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | EVS.POSITION_M1 = 0x52;
|
297 | EVS.POSITION_M2 = 0x56;
|
298 | EVS.STATUS_M1 = 0x5A;
|
299 | EVS.STATUS_M2 = 0x5B;
|
300 | EVS.TASKS_M1 = 0x5C;
|
301 | EVS.TASKS_M2 = 0x5D;
|
302 |
|
303 | EVS.ENCODER_PID = 0x5E;
|
304 | EVS.SPEED_PID = 0x64;
|
305 | EVS.PASS_COUNT = 0x6A;
|
306 | EVS.TOLERANCE = 0x6B;
|
307 |
|
308 |
|
309 |
|
310 |
|
311 | EVS.BTN_PRESS = 0xDA;
|
312 | EVS.RGB_LED = 0xD7;
|
313 | EVS.CENTER_RGB_LED = 0xDE;
|
314 |
|
315 |
|
316 |
|
317 | module.exports = EVS;
|