forked from WasabiFan/ev3dev-lang-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.ts
210 lines (179 loc) · 6.29 KB
/
sensor.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
///<reference path="node.d.ts" />
///<reference path="include.ts" />
///<reference path="io.ts" />
//~autogen js_generic-class-description classes.sensor>currentClass
/**
* The sensor class provides a uniform interface for using most of the
* sensors available for the EV3. The various underlying device drivers will
* create a `lego-sensor` device for interacting with the sensors.
*
* Sensors are primarily controlled by setting the `mode` and monitored by
* reading the `value<N>` attributes. Values can be converted to floating point
* if needed by `value<N>` / 10.0 ^ `decimals`.
*
* Since the name of the `sensor<N>` device node does not correspond to the port
* that a sensor is plugged in to, you must look at the `port_name` attribute if
* you need to know which port a sensor is plugged in to. However, if you don't
* have more than one sensor of each type, you can just look for a matching
* `driver_name`. Then it will not matter which port a sensor is plugged in to - your
* program will still work.
*/
//~autogen
class Sensor extends Device {
private port: string;
private sensorDeviceDir = '/sys/class/lego-sensor/';
private _deviceIndex: number = -1;
get deviceIndex(): number {
return this._deviceIndex;
}
constructor(port?: string, driverNames?: string[]) {
super();
this.port = port;
var rootPath: string;
try {
var availableDevices = fs.readdirSync(this.sensorDeviceDir);
for (var i in availableDevices) {
var file = availableDevices[i];
rootPath = path.join(this.sensorDeviceDir, file);
var portName = this.readString("port_name", rootPath);
var driverName = this.readString("driver_name", rootPath);
var satisfiesCondition = (
(port == ports.INPUT_AUTO)
|| (port == undefined)
|| (portName === port)
) && (
(driverNames == undefined || driverNames == [])
|| driverNames.indexOf(driverName) != -1
);
if (satisfiesCondition) {
this._deviceIndex = Number(file.substring('sensor'.length));
break;
}
}
if (this.deviceIndex == -1) {
this.connected = false;
return;
}
}
catch (e) {
console.log(e);
this.connected = false;
return;
}
this.connect(rootPath);
}
public getValue(valueIndex: number): number {
return this.readNumber("value" + valueIndex);
}
public getFloatValue(valueIndex: number): number {
return this.getValue(valueIndex) / Math.pow(10, this.decimals);
}
//PROPERTIES
//~autogen js_generic-get-set classes.sensor>currentClass
/**
* Sends a command to the sensor.
*/
set command(value: string) {
this.setString("command", value);
}
/**
* Returns a list of the valid commands for the sensor.
* Returns -EOPNOTSUPP if no commands are supported.
*/
get commands(): string[] {
return this.readString("commands").split(' ');
}
/**
* Returns the number of decimal places for the values in the `value<N>`
* attributes of the current mode.
*/
get decimals(): number {
return this.readNumber("decimals");
}
/**
* Returns the name of the sensor device/driver. See the list of [supported
* sensors] for a complete list of drivers.
*/
get driverName(): string {
return this.readString("driver_name");
}
/**
* Returns the current mode. Writing one of the values returned by `modes`
* sets the sensor to that mode.
*/
get mode(): string {
return this.readString("mode");
}
/**
* Returns the current mode. Writing one of the values returned by `modes`
* sets the sensor to that mode.
*/
set mode(value: string) {
this.setString("mode", value);
}
/**
* Returns a list of the valid modes for the sensor.
*/
get modes(): string[] {
return this.readString("modes").split(' ');
}
/**
* Returns the number of `value<N>` attributes that will return a valid value
* for the current mode.
*/
get numValues(): number {
return this.readNumber("num_values");
}
/**
* Returns the name of the port that the sensor is connected to, e.g. `ev3:in1`.
* I2C sensors also include the I2C address (decimal), e.g. `ev3:in1:i2c8`.
*/
get portName(): string {
return this.readString("port_name");
}
/**
* Returns the units of the measured value for the current mode. May return
* empty string
*/
get units(): string {
return this.readString("units");
}
//~autogen
}
//~autogen js_generic-class-description classes.i2cSensor>currentClass
/**
* A generic interface to control I2C-type EV3 sensors.
*/
//~autogen
class I2CSensor extends Sensor {
constructor(port?: string, driverNames?: string[]) {
super(port, driverNames);
}
//~autogen js_generic-get-set classes.i2cSensor>currentClass
/**
* Returns the firmware version of the sensor if available. Currently only
* I2C/NXT sensors support this.
*/
get fwVersion(): string {
return this.readString("fw_version");
}
/**
* Returns the polling period of the sensor in milliseconds. Writing sets the
* polling period. Setting to 0 disables polling. Minimum value is hard
* coded as 50 msec. Returns -EOPNOTSUPP if changing polling is not supported.
* Currently only I2C/NXT sensors support changing the polling period.
*/
get pollMs(): number {
return this.readNumber("poll_ms");
}
/**
* Returns the polling period of the sensor in milliseconds. Writing sets the
* polling period. Setting to 0 disables polling. Minimum value is hard
* coded as 50 msec. Returns -EOPNOTSUPP if changing polling is not supported.
* Currently only I2C/NXT sensors support changing the polling period.
*/
set pollMs(value: number) {
this.setNumber("poll_ms", value);
}
//~autogen
}