diff options
author | Jimmy Huang <jimmy.huang@linux.intel.com> | 2017-03-21 10:41:19 -0700 |
---|---|---|
committer | Geoff Gustafson <geoff@linux.intel.com> | 2017-03-21 10:41:19 -0700 |
commit | e1e0a280098a88d480bc87daed311fd70bd529fb (patch) | |
tree | 3f7469295967b2d24bd2a84366a5b65dc3b19892 /src | |
parent | 08e62a955dc30e8c64c8b1be1e2487dbce162900 (diff) |
[sensor] Update sensor API for readings (#843)
The readings no longer are passed to the event object
in the onchange() callback, but instead stored
as a internal property in the object itself in the
latest W3C spec.
Signed-off-by: Jimmy Huang <jimmy.huang@intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/zjs_sensor.c | 96 |
1 files changed, 58 insertions, 38 deletions
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); |