diff options
-rw-r--r-- | docs/sensors.md | 26 | ||||
-rw-r--r-- | modules/GenericSensor.js | 41 | ||||
-rw-r--r-- | samples/Accelerometer.js | 8 | ||||
-rw-r--r-- | samples/AmbientLight.js | 2 | ||||
-rw-r--r-- | samples/Gyroscope.js | 8 | ||||
-rw-r--r-- | samples/SensorBLEDemo.js | 8 | ||||
-rw-r--r-- | src/zjs_sensor.c | 96 |
7 files changed, 97 insertions, 92 deletions
diff --git a/docs/sensors.md b/docs/sensors.md index c994c1a..e840bc5 100644 --- a/docs/sensors.md +++ b/docs/sensors.md @@ -27,6 +27,7 @@ specific API functions. ```javascript interface Sensor { readonly attribute SensorState state; // The current state of Sensor object + attribute double frequency; // The frequency set void start(); // Starts the sensor void stop(); // Stops the sensor attribute ChangeCallback onchange; // Callback handler for change events @@ -50,7 +51,7 @@ interface SensorErrorEvent { attribute Error error; }; -callback ChangeCallback = void(SensorReading value); +callback ChangeCallback = void(); callback ActivateCallback = void(); callback ErrorCallback = void(SensorErrorEvent error); ``` @@ -58,21 +59,15 @@ callback ErrorCallback = void(SensorErrorEvent error); ```javascript [Constructor(optional AccelerometerOptions accelerometerOptions)] interface Accelerometer : Sensor { - attribute AccelerometerReading reading; attribute boolean includesGravity; -}; - -dictionary AccelerometerOptions : SensorOptions { - boolean includeGravity = true; // not supported, will throw an error if set -}; - -interface AccelerometerrReading : SensorReading { readonly attribute double x; readonly attribute double y; readonly attribute double z; }; -callback ChangeCallback = void(AccelerometerReading value); +dictionary AccelerometerOptions : SensorOptions { + boolean includeGravity = true; // not supported, will throw an error if set +}; ``` ####GyroscopeSensor Interface ```javascript @@ -86,25 +81,18 @@ interface GyroscopeSensorReading : SensorReading { readonly attribute double y; readonly attribute double z; }; - -callback ChangeCallback = void(GyroscopeReading value); ``` ####AmbientLightSensor Interface ```javascript [Constructor(optional SensorOptions sensorOptions)] interface AmbientLightSensor : Sensor { - attribute AmbientLightSensorReading reading; + readonly attribute unsigned long pin; + readonly attribute double illuminance; }; dictionary AmbientLightSensorOptions : SensorOptions { unsigned long pin; // analog pin where the light is connected }; - -interface AmbientLightSensorReading : SensorReading { - readonly attribute double illuminance; -}; - -callback ChangeCallback = void(AmbientLightSensorReading value); ``` API Documentation diff --git a/modules/GenericSensor.js b/modules/GenericSensor.js index 73880ce..12e688a 100644 --- a/modules/GenericSensor.js +++ b/modules/GenericSensor.js @@ -93,41 +93,38 @@ function GenericSensor() { if (event === "activated") changeFlag = true; }; - sensor.onchange = function(event) { + sensor.onchange = function() { if (changeFlag === true) { - assert(typeof event === "object" && event !== null, - "sensor: callback value for 'onchange'"); - if (sensorType === "AmbientLight") { - assert(typeof event.reading.illuminance === "number" && - typeof event.reading.illuminance !== null, + assert(typeof sensor.illuminance === "number" && + typeof sensor.illuminance !== null, "sensor: reading value for '" + sensorType + "'"); - middleNum = event.reading.illuminance; - event.reading.illuminance = middleNum + 1; - assert(event.reading.illuminance === middleNum, + middleNum = sensor.illuminance; + sensor.illuminance = middleNum + 1; + assert(sensor.illuminance === middleNum, "sensor: reading is readonly property"); - console.log(sensorType + ": " + event.reading.illuminance); + console.log(sensorType + ": " + sensor.illuminance); } else if (sensorType === "Accelerometer" || sensorType === "Gyroscope") { - assert(typeof event.reading.x === "number" && - typeof event.reading.x !== null && - typeof event.reading.y === "number" && - typeof event.reading.y !== null && - typeof event.reading.z === "number" && - typeof event.reading.z !== null, + assert(typeof sensor.x === "number" && + typeof sensor.x !== null && + typeof sensor.y === "number" && + typeof sensor.y !== null && + typeof sensor.z === "number" && + typeof sensor.z !== null, "sensor: reading value for '" + sensorType + "'"); - middleNum = event.reading.x; - event.reading.x = middleNum + 1; - assert(event.reading.x === middleNum, + middleNum = sensor.x; + sensor.x = middleNum + 1; + assert(sensor.x === middleNum, "sensor: reading is readonly property"); console.log(sensorType + ": " + - " x=" + event.reading.x + - " y=" + event.reading.y + - " z=" + event.reading.z); + " x=" + sensor.x + + " y=" + sensor.y + + " z=" + sensor.z); } changeFlag = false; diff --git a/samples/Accelerometer.js b/samples/Accelerometer.js index d9bb774..6d6dd6b 100644 --- a/samples/Accelerometer.js +++ b/samples/Accelerometer.js @@ -12,11 +12,11 @@ var sensor = new Accelerometer({ frequency: updateFrequency }); -sensor.onchange = function(event) { +sensor.onchange = function() { console.log("acceleration (m/s^2): " + - " x=" + event.reading.x + - " y=" + event.reading.y + - " z=" + event.reading.z); + " x=" + sensor.x + + " y=" + sensor.y + + " z=" + sensor.z); }; sensor.onstatechange = function(event) { diff --git a/samples/AmbientLight.js b/samples/AmbientLight.js index fa02c45..5ad8881 100644 --- a/samples/AmbientLight.js +++ b/samples/AmbientLight.js @@ -35,7 +35,7 @@ var sensor = new AmbientLightSensor({ }); sensor.onchange = function(event) { - var val = event.reading.illuminance; + var val = sensor.illuminance; if (val <= 1) { console.log("(very dark): " + event.reading.illuminance); } else if (val > 1 && val <= 50) { diff --git a/samples/Gyroscope.js b/samples/Gyroscope.js index 4e5373f..4a6d8cc 100644 --- a/samples/Gyroscope.js +++ b/samples/Gyroscope.js @@ -11,11 +11,11 @@ var sensor = new Gyroscope({ frequency: updateFrequency }); -sensor.onchange = function(event) { +sensor.onchange = function() { console.log("rotation (rad/s): " + - " x=" + event.reading.x + - " y=" + event.reading.y + - " z=" + event.reading.z); + " x=" + sensor.x + + " y=" + sensor.y + + " z=" + sensor.z); }; sensor.onstatechange = function(event) { diff --git a/samples/SensorBLEDemo.js b/samples/SensorBLEDemo.js index 43d0fcb..27f2d85 100644 --- a/samples/SensorBLEDemo.js +++ b/samples/SensorBLEDemo.js @@ -102,12 +102,12 @@ var gyro = new Gyroscope({ frequency: updateFrequency }); -accel.onchange = function(event) { - SensorCharacteristic.valueChange(1, event.reading.x, event.reading.y, event.reading.z); +accel.onchange = function() { + SensorCharacteristic.valueChange(1, accel.x, accel.y, accel.z); }; -gyro.onchange = function(event) { - SensorCharacteristic.valueChange(0, event.reading.x, event.reading.y, event.reading.z); +gyro.onchange = function() { + SensorCharacteristic.valueChange(0, gyro.x, gyro.y, gyro.z); }; accel.onerror = function(event) { diff --git a/src/zjs_sensor.c b/src/zjs_sensor.c index 6f3201a..cb90e30 100644 --- a/src/zjs_sensor.c +++ b/src/zjs_sensor.c @@ -215,23 +215,23 @@ static void zjs_sensor_update_reading(jerry_value_t obj, enum sensor_channel channel, void *reading) { - // update reading property and trigger onchange event - ZVAL reading_obj = jerry_create_object(); + // update object's internal properties and trigger onchange event + double x, y, z, d; switch(channel) { case SENSOR_CHAN_ACCEL_XYZ: - case SENSOR_CHAN_GYRO_XYZ: ; + case SENSOR_CHAN_GYRO_XYZ: // reading is a ptr to an array of 3 double values - double x = ((double *)reading)[0]; - double y = ((double *)reading)[1]; - double z = ((double *)reading)[2]; - zjs_obj_add_readonly_number(reading_obj, x, "x"); - zjs_obj_add_readonly_number(reading_obj, y, "y"); - zjs_obj_add_readonly_number(reading_obj, z, "z"); + x = ((double *)reading)[0]; + y = ((double *)reading)[1]; + z = ((double *)reading)[2]; + zjs_obj_add_readonly_number(obj, x, "x"); + zjs_obj_add_readonly_number(obj, y, "y"); + zjs_obj_add_readonly_number(obj, z, "z"); break; - case SENSOR_CHAN_LIGHT: ; + case SENSOR_CHAN_LIGHT: // reading is a ptr to double - double d = *((double *)reading); - zjs_obj_add_readonly_number(reading_obj, d, "illuminance"); + d = *((double *)reading); + zjs_obj_add_readonly_number(obj, d, "illuminance"); break; default: @@ -239,13 +239,11 @@ static void zjs_sensor_update_reading(jerry_value_t obj, return; } - zjs_set_property(obj, "reading", reading_obj); ZVAL func = zjs_get_property(obj, "onchange"); if (jerry_value_is_function(func)) { ZVAL event = jerry_create_object(); // if onchange exists, call it - zjs_set_property(event, "reading", reading_obj); - ZVAL rval = jerry_call_function(func, obj, &event, 1); + ZVAL rval = jerry_call_function(func, obj, NULL, 0); if (jerry_value_has_error_flag(rval)) { ERR_PRINT("calling onchange\n"); } @@ -406,7 +404,22 @@ static void zjs_sensor_onstop_c_callback(void *h, void *argv) send.data.sensor.channel = handle->channel; ZVAL reading_val = jerry_create_null(); - zjs_set_property(obj, "reading", reading_val); + jerry_value_t null_val = jerry_create_null(); + switch(handle->channel) { + case SENSOR_CHAN_ACCEL_XYZ: + case SENSOR_CHAN_GYRO_XYZ: + zjs_set_readonly_property(obj, "x", null_val); + zjs_set_readonly_property(obj, "y", null_val); + zjs_set_readonly_property(obj, "z", null_val); + break; + case SENSOR_CHAN_LIGHT: + zjs_set_readonly_property(obj, "illuminance", null_val); + break; + + default: + ERR_PRINT("unsupported sensor type\n"); + } + if (handle->channel == SENSOR_CHAN_LIGHT) { // AmbientLightSensor needs provide AIO pin value uint32_t pin; @@ -492,9 +505,19 @@ static jerry_value_t zjs_sensor_create(const jerry_value_t function_obj, ZJS_VALIDATE_ARGS(expect); double frequency = DEFAULT_SAMPLING_FREQUENCY; - bool hasPin = false; uint32_t pin; + zjs_ipm_message_t send; + send.type = TYPE_SENSOR_INIT; + send.data.sensor.channel = channel; + int error = zjs_sensor_call_remote_function(&send); + if (error != ERROR_IPM_NONE) { + return zjs_error("zjs_sensor_create failed to init\n"); + } + + // initialize object and default values + jerry_value_t sensor_obj = jerry_create_object(); + if (argc >= 1) { jerry_value_t options = argv[0]; @@ -515,35 +538,32 @@ static jerry_value_t zjs_sensor_create(const jerry_value_t function_obj, bool option_gravity; if (zjs_obj_get_boolean(options, "includeGravity", &option_gravity) && option_gravity) { - // TODO: find out if BMI160 can be configured to include gravity - ERR_PRINT("includeGravity is not supported\n"); + if (option_gravity) { + ERR_PRINT("includeGravity is not supported\n"); + } + zjs_obj_add_readonly_boolean(sensor_obj, option_gravity, + "includeGravity"); } } - if (channel == SENSOR_CHAN_LIGHT) { - hasPin = zjs_obj_get_uint32(options, "pin", &pin); + // initialize default sensor readings to null + jerry_value_t null_val = jerry_create_null(); + if (channel == SENSOR_CHAN_ACCEL_XYZ || + channel == SENSOR_CHAN_GYRO_XYZ) { + zjs_set_readonly_property(sensor_obj, "x", null_val); + zjs_set_readonly_property(sensor_obj, "y", null_val); + zjs_set_readonly_property(sensor_obj, "z", null_val); + } else if (channel == SENSOR_CHAN_LIGHT) { + if (zjs_obj_get_uint32(options, "pin", &pin)) { + zjs_obj_add_readonly_number(sensor_obj, pin, "pin"); + } + zjs_set_readonly_property(sensor_obj, "illuminance", null_val); } + jerry_release_value(null_val); } - zjs_ipm_message_t send; - send.type = TYPE_SENSOR_INIT; - send.data.sensor.channel = channel; - int error = zjs_sensor_call_remote_function(&send); - if (error != ERROR_IPM_NONE) { - return zjs_error("zjs_sensor_create failed to init\n"); - } - - // initialize object and default values - jerry_value_t sensor_obj = jerry_create_object(); - ZVAL reading_val = jerry_create_null(); zjs_obj_add_number(sensor_obj, frequency, "frequency"); zjs_obj_add_readonly_string(sensor_obj, "unconnected", "state"); - zjs_set_property(sensor_obj, "reading", reading_val); - - if (channel == SENSOR_CHAN_LIGHT && hasPin) { - zjs_obj_add_number(sensor_obj, pin, "pin"); - } - jerry_set_prototype(sensor_obj, zjs_sensor_prototype); sensor_handle_t *handle = zjs_sensor_alloc_handle(channel); |