Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 5d277d6

Browse files
Jimmy Huanggrgustaf
authored andcommitted
[sensor] Implements new W3C Generic Sensor API (#1837)
In the latest W3C spec, onchange event handler has been renamed to onreading, and the state property is no longer exposed in the sensor object. Fixes #1829 Signed-off-by: Jimmy Huang <[email protected]>
1 parent 5cd3746 commit 5d277d6

11 files changed

+66
-110
lines changed

docs/sensors.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ specific API functions.
2828
####Sensor Interface
2929
```javascript
3030
interface Sensor {
31-
readonly attribute SensorState state; // current state of Sensor object
31+
readonly attribute boolean activated; // whether the sensor is activated or not
32+
readonly attribute boolean hasReading; // whether the sensor has readings available
3233
readonly attribute double timestamp; // timestamp of the latest reading in milliseconds
3334
attribute double frequency; // sampling frequency in hertz
3435
void start(); // starts the sensor
3536
void stop(); // stops the sensor
36-
attribute ChangeCallback onchange; // callback handler for change events
37+
attribute ChangeCallback onreading; // callback handler for change events
3738
attribute ActivateCallback onactivate; // callback handler for activate events
3839
attribute ErrorCallback onerror; // callback handler for error events
3940
};
@@ -42,14 +43,6 @@ dictionary SensorOptions {
4243
double frequency; // desire frequency, default is 20 if unset
4344
};
4445

45-
enum SensorState {
46-
"unconnected",
47-
"activating",
48-
"activated",
49-
"idle",
50-
"errored"
51-
};
52-
5346
interface SensorErrorEvent {
5447
attribute Error error;
5548
};
@@ -112,15 +105,15 @@ dictionary TemperatureSensorOptions : SensorOptions {
112105
API Documentation
113106
-----------------
114107

115-
### onchange
116-
`Sensor.onchange`
108+
### onreading
109+
`Sensor.onreading`
117110

118-
The onchange attribute is an EventHandler which is called whenever a new reading is available.
111+
The onreading attribute is an EventHandler, which is called whenever a new reading is available.
119112

120113
### onactivate
121114
`Sensor.onactivate`
122115

123-
The onactivate attribute is an EventHandler which is called when this.[[state]] transitions from "activating" to "activated".
116+
The onactivate attribute is an EventHandler which is called when the sensor is activated after calling start().
124117

125118
### onerror
126119
`Sensor.onerror`
@@ -130,7 +123,7 @@ The onactivate attribute is an EventHandler which is called whenever an exceptio
130123
### start
131124
`void Sensor.start()`
132125

133-
Starts the sensor instance, the sensor will get callback on onchange whenever there's a new reading available.
126+
Starts the sensor instance, the sensor will get callback on onreading whenever there's a new reading available.
134127

135128
### stop
136129
`void Sensor.stop()`

modules/GenericSensor.js

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2016-2017, Intel Corporation.
1+
// Copyright (c) 2016-2018, Intel Corporation.
22

33
// JavaScript library for the tests sensor case
44

@@ -54,40 +54,31 @@ function GenericSensor() {
5454
+ total + " passed");
5555
}
5656

57-
var currentState = null;
57+
var isActivated = false;
5858
var changeFlag = false;
5959
var errorFlag = true;
60-
var defaultState, startState, stopState, middleState, middleNum;
61-
var middleNumX, middleNumY, middleNumZ, tmpTimestamp;
60+
var middleNum, middleNumX, middleNumY, middleNumZ, tmpTimestamp;
6261

6362
genericSensor.test = function(sensor, sensorType) {
6463
assert(typeof sensor === "object" && sensor !== null,
6564
"sensor: be defined");
6665

67-
currentState = sensor.state;
68-
defaultState = sensor.state;
66+
isActivated = sensor.activated;
67+
assert(typeof isActivated === "boolean",
68+
"sensor: activated as " + isActivated);
69+
console.log("activated: " + isActivated);
6970

70-
assert(typeof currentState === "string" && currentState !== null,
71-
"sensor: current state as '" + currentState + "'");
72-
73-
console.log("currentstate: " + currentState);
74-
75-
middleState = sensor.state;
76-
sensor.state = middleState + "love";
77-
assert(sensor.state === middleState,
78-
"sensor: state is readonly property");
71+
sensor.activated = !sensor.activated;
72+
assert(sensor.activated === isActivated,
73+
"sensor: activated is readonly property ");
7974

8075
sensor.onactivate = function() {
81-
currentState = sensor.state
82-
console.log("currentstate: " + currentState);
83-
84-
assert(currentState === "activated",
85-
"sensor: state is activated");
86-
76+
console.log("activated: " + sensor.activated);
77+
assert(sensor.activated === true, "sensor: sensor is activated");
8778
changeFlag = true;
8879
};
8980

90-
sensor.onchange = function() {
81+
sensor.onreading = function() {
9182
tmpTimestamp = sensor.timestamp;
9283

9384
if (changeFlag === true) {
@@ -174,21 +165,12 @@ function GenericSensor() {
174165
sensor.start();
175166

176167
setTimeout(function() {
177-
startState = sensor.state;
178-
assert(defaultState !== startState &&
179-
defaultState === "unconnected" &&
180-
startState === "activated",
181-
"sensor: be started");
168+
assert(sensor.activated === true, "sensor: is activated");
182169
}, 1000);
183170

184171
setTimeout(function() {
185172
sensor.stop();
186-
187-
stopState = sensor.state;
188-
assert(startState !== stopState &&
189-
stopState === "idle" &&
190-
startState === "activated",
191-
"sensor: be stopped");
173+
assert(sensor.activated === false, "sensor: is stopped");
192174
}, 20000);
193175

194176
setTimeout(function() {

samples/AmbientLight.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2016-2017, Intel Corporation.
1+
// Copyright (c) 2016-2018, Intel Corporation.
22

33
// Test code to use the AmbientLightSensor (subclass of Generic Sensor) API
44
// to communicate with the Grove Light sensor on the Arduino 101
@@ -32,7 +32,7 @@ var sensor = new AmbientLightSensor({
3232
pin: 'A2'
3333
});
3434

35-
sensor.onchange = function() {
35+
sensor.onreading = function() {
3636
var val = sensor.illuminance;
3737
if (val <= 1) {
3838
console.log("(very dark): " + sensor.illuminance);

samples/BMI160Accelerometer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2016-2017, Intel Corporation.
1+
// Copyright (c) 2016-2018, Intel Corporation.
22

33
// Test code to use the Accelerometer (subclass of Generic Sensor) API
44
// to communicate with the BMI160 inertia sensor on the Arduino 101
@@ -11,7 +11,7 @@ var sensor = new Accelerometer({
1111
frequency: updateFrequency
1212
});
1313

14-
sensor.onchange = function() {
14+
sensor.onreading = function() {
1515
console.log("acceleration (m/s^2): " +
1616
" x=" + sensor.x +
1717
" y=" + sensor.y +

samples/BMI160Gyroscope.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2016-2017, Intel Corporation.
1+
// Copyright (c) 2016-2018, Intel Corporation.
22

33
// Test code to use the Gyroscope (subclass of Generic Sensor) API
44
// to communicate with the BMI160 inertia sensor on the Arduino 101
@@ -11,7 +11,7 @@ var sensor = new Gyroscope({
1111
frequency: updateFrequency
1212
});
1313

14-
sensor.onchange = function() {
14+
sensor.onreading = function() {
1515
console.log("rotation (rad/s): " +
1616
" x=" + sensor.x +
1717
" y=" + sensor.y +

samples/BMI160Temperature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017, Intel Corporation.
1+
// Copyright (c) 2017-2018, Intel Corporation.
22

33
// Test code to use the TemperatureSensor (subclass of Generic Sensor) API
44
// to communicate with the BMI160 inertia sensor on the Arduino 101
@@ -12,7 +12,7 @@ var sensor = new TemperatureSensor({
1212
frequency: updateFrequency
1313
});
1414

15-
sensor.onchange = function() {
15+
sensor.onreading = function() {
1616
console.log("BMI160 temperature (celsius): " + sensor.celsius);
1717
};
1818

samples/FXOS8700Accelerometer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017, Intel Corporation.
1+
// Copyright (c) 2017-2018, Intel Corporation.
22

33
// Test code to use the Accelerometer (subclass of Generic Sensor) API
44
// to communicate with the FXOS8700 6-Axis Xtrinsic Sensor on the FRDM-K64F
@@ -11,7 +11,7 @@ var sensor = new Accelerometer({
1111
frequency: updateFrequency
1212
});
1313

14-
sensor.onchange = function() {
14+
sensor.onreading = function() {
1515
console.log("acceleration (m/s^2): " +
1616
" x=" + sensor.x +
1717
" y=" + sensor.y +

samples/FXOS8700Magnetometer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017, Intel Corporation.
1+
// Copyright (c) 2017-2018, Intel Corporation.
22

33
// Test code to use the Magnetometer (subclass of Generic Sensor) API
44
// to communicate with the FXOS8700 6-Axis Xtrinsic Sensor on the FRDM-K64F
@@ -11,7 +11,7 @@ var sensor = new Magnetometer({
1111
frequency: updateFrequency
1212
});
1313

14-
sensor.onchange = function() {
14+
sensor.onreading = function() {
1515
console.log("magnetic field (μT): " +
1616
" x=" + sensor.x +
1717
" y=" + sensor.y +

samples/SensorBLEDemo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ var gyro = new Gyroscope({
9999
accel.start();
100100
gyro.start();
101101

102-
accel.onchange = function() {
102+
accel.onreading = function() {
103103
SensorCharacteristic.valueChange(1, accel.x, accel.y, accel.z);
104104
};
105105

106-
gyro.onchange = function() {
106+
gyro.onreading = function() {
107107
SensorCharacteristic.valueChange(0, gyro.x, gyro.y, gyro.z);
108108
};
109109

src/zjs_sensor.c

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -96,61 +96,45 @@ void zjs_sensor_free_instance(sensor_instance_t *instance)
9696

9797
sensor_state_t zjs_sensor_get_state(jerry_value_t obj)
9898
{
99-
const int BUFLEN = 20;
100-
char buffer[BUFLEN];
101-
if (zjs_obj_get_string(obj, "state", buffer, BUFLEN)) {
102-
if (strequal(buffer, "unconnected"))
103-
return SENSOR_STATE_UNCONNECTED;
104-
if (strequal(buffer, "idle"))
105-
return SENSOR_STATE_IDLE;
106-
if (strequal(buffer, "activating"))
107-
return SENSOR_STATE_ACTIVATING;
108-
if (strequal(buffer, "activated"))
109-
return SENSOR_STATE_ACTIVATED;
110-
if (strequal(buffer, "errored"))
111-
return SENSOR_STATE_ERRORED;
112-
else
113-
ERR_PRINT("invalid state set %s\n", buffer);
114-
} else {
115-
ERR_PRINT("state is undefined\n");
99+
ZJS_GET_HANDLE_OR_NULL(obj, sensor_handle_t, handle, sensor_type_info);
100+
if (!handle) {
101+
ZJS_ASSERT(false, "no handle found");
102+
return SENSOR_STATE_ERRORED;
116103
}
117-
118-
zjs_obj_add_readonly_string(obj, "state", "errored");
119-
return SENSOR_STATE_ERRORED;
104+
return handle->state;
120105
}
121106

122107
void zjs_sensor_set_state(jerry_value_t obj, sensor_state_t state)
123108
{
124-
sensor_state_t old_state = zjs_sensor_get_state(obj);
109+
ZJS_GET_HANDLE_OR_NULL(obj, sensor_handle_t, handle, sensor_type_info);
110+
if (!handle) {
111+
ZJS_ASSERT(false, "no handle found");
112+
return;
113+
}
114+
115+
sensor_state_t old_state = handle->state;
125116
if (old_state == state) {
126117
return;
127118
}
128119

129120
// update state property and trigger onactivate event if necessary
130-
const char *state_str = NULL;
131121
switch (state) {
132122
case SENSOR_STATE_UNCONNECTED:
133-
state_str = "unconnected";
134-
break;
135123
case SENSOR_STATE_IDLE:
136-
state_str = "idle";
137-
break;
138124
case SENSOR_STATE_ACTIVATING:
139-
state_str = "activating";
125+
case SENSOR_STATE_ERRORED:
126+
zjs_obj_add_readonly_boolean(obj, "activated", false);
127+
zjs_obj_add_readonly_boolean(obj, "hasReading", false);
140128
break;
141129
case SENSOR_STATE_ACTIVATED:
142-
state_str = "activated";
143-
break;
144-
case SENSOR_STATE_ERRORED:
145-
state_str = "errored";
130+
zjs_obj_add_readonly_boolean(obj, "activated", true);
146131
break;
147132

148133
default:
149134
// should never get here
150135
ERR_PRINT("invalid state\n");
151136
return;
152137
}
153-
zjs_obj_add_readonly_string(obj, "state", state_str);
154138

155139
if (old_state == SENSOR_STATE_ACTIVATING &&
156140
state == SENSOR_STATE_ACTIVATED) {
@@ -167,16 +151,6 @@ void zjs_sensor_set_state(jerry_value_t obj, sensor_state_t state)
167151
}
168152
}
169153

170-
sensor_handle_t *handle = NULL;
171-
const jerry_object_native_info_t *tmp;
172-
if (!jerry_get_object_native_pointer(obj, (void **)&handle, &tmp)) {
173-
ERR_PRINT("no handle found\n");
174-
return;
175-
}
176-
if (tmp != &sensor_type_info) {
177-
ERR_PRINT("handle type did not match\n");
178-
return;
179-
}
180154
handle->state = state;
181155
}
182156

@@ -185,13 +159,20 @@ void zjs_sensor_trigger_change(jerry_value_t obj)
185159
u64_t timestamp = k_uptime_get();
186160
zjs_obj_add_readonly_number(obj, "timestamp", ((double)timestamp));
187161

188-
ZVAL func = zjs_get_property(obj, "onchange");
162+
// When the first reading is triggered, set hasReading to true
163+
bool has_reading = false;
164+
zjs_obj_get_boolean(obj, "hasReading", &has_reading);
165+
if (!has_reading) {
166+
zjs_obj_add_readonly_boolean(obj, "hasReading", true);
167+
}
168+
169+
ZVAL func = zjs_get_property(obj, "onreading");
189170
if (jerry_value_is_function(func)) {
190171
ZVAL event = zjs_create_object();
191-
// if onchange exists, call it
172+
// if onreading exists, call it
192173
ZVAL rval = jerry_call_function(func, obj, NULL, 0);
193174
if (jerry_value_has_error_flag(rval)) {
194-
ERR_PRINT("calling onchange\n");
175+
ERR_PRINT("Error calling onreading\n");
195176
}
196177
}
197178
}
@@ -352,9 +333,10 @@ ZJS_DECL_FUNC_ARGS(zjs_sensor_create,
352333
jerry_value_t sensor_obj = zjs_create_object();
353334
ZVAL null_val = jerry_create_null();
354335

336+
zjs_obj_add_readonly_boolean(sensor_obj, "activated", false);
337+
zjs_obj_add_readonly_boolean(sensor_obj, "hasReading", false);
355338
zjs_set_readonly_property(sensor_obj, "timestamp", null_val);
356339
zjs_obj_add_number(sensor_obj, "frequency", frequency);
357-
zjs_obj_add_readonly_string(sensor_obj, "state", "unconnected");
358340
jerry_set_prototype(sensor_obj, zjs_sensor_prototype);
359341

360342
handle->sensor_obj = jerry_acquire_value(sensor_obj);

src/zjs_sensor_board_k64f.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ int zjs_sensor_board_create(sensor_handle_t *handle)
111111
return -1;
112112
}
113113

114-
ZJS_PRINT("handle->controller->name %s\n", handle->controller->name);
115-
114+
DBG_PRINT("handle->controller->name %s\n", handle->controller->name);
116115
return 0;
117116
}
118117

0 commit comments

Comments
 (0)