Import of the watch repository from Pebble

This commit is contained in:
Matthieu Jeanson 2024-12-12 16:43:03 -08:00 committed by Katharine Berry
commit 3b92768480
10334 changed files with 2564465 additions and 0 deletions

View file

@ -0,0 +1,131 @@
/* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-util/logging.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"
#include "mbed.h"
/**
* DigitalOut#write (native JavaScript method)
*
* Writes a binary value to a DigitalOut.
*
* @param value 1 or 0, specifying whether the output pin is high or low,
* respectively
* @returns undefined, or an error if invalid arguments are provided.
*/
DECLARE_CLASS_FUNCTION(DigitalOut, write) {
CHECK_ARGUMENT_COUNT(DigitalOut, write, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(DigitalOut, write, 0, number);
// Extract native DigitalOut pointer
uintptr_t ptr_val;
jerry_get_object_native_handle(this_obj, &ptr_val);
DigitalOut* native_ptr = reinterpret_cast<DigitalOut*>(ptr_val);
int arg0 = jerry_get_number_value(args[0]);
native_ptr->write(arg0);
return jerry_create_undefined();
}
/**
* DigitalOut#read (native JavaScript method)
*
* Reads the current status of a DigitalOut
*
* @returns 1 if the pin is currently high, or 0 if the pin is currently low.
*/
DECLARE_CLASS_FUNCTION(DigitalOut, read) {
CHECK_ARGUMENT_COUNT(DigitalOut, read, (args_count == 0));
// Extract native DigitalOut pointer
uintptr_t ptr_val;
jerry_get_object_native_handle(this_obj, &ptr_val);
DigitalOut* native_ptr = reinterpret_cast<DigitalOut*>(ptr_val);
int result = native_ptr->read();
return jerry_create_number(result);
}
/**
* DigitalOut#is_connected (native JavaScript method)
*
* @returns 0 if the DigitalOut is set to NC, or 1 if it is connected to an
* actual pin
*/
DECLARE_CLASS_FUNCTION(DigitalOut, is_connected) {
CHECK_ARGUMENT_COUNT(DigitalOut, is_connected, (args_count == 0));
// Extract native DigitalOut pointer
uintptr_t ptr_val;
jerry_get_object_native_handle(this_obj, &ptr_val);
DigitalOut* native_ptr = reinterpret_cast<DigitalOut*>(ptr_val);
int result = native_ptr->is_connected();
return jerry_create_number(result);
}
/**
* DigitalOut#destructor
*
* Called if/when the DigitalOut is GC'ed.
*/
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut)(const uintptr_t native_handle) {
delete reinterpret_cast<DigitalOut*>(native_handle);
}
/**
* DigitalOut (native JavaScript constructor)
*
* @param pin_name mbed pin to connect the DigitalOut to.
* @param value (optional) Initial value of the DigitalOut.
* @returns a JavaScript object representing a DigitalOut.
*/
DECLARE_CLASS_CONSTRUCTOR(DigitalOut) {
CHECK_ARGUMENT_COUNT(DigitalOut, __constructor, (args_count == 1 || args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS(DigitalOut, __constructor, 0, number);
CHECK_ARGUMENT_TYPE_ON_CONDITION(DigitalOut, __constructor, 1, number, (args_count == 2));
uintptr_t native_ptr;
// Call correct overload of DigitalOut::DigitalOut depending on the
// arguments passed.
PinName pin_name = PinName(jerry_get_number_value(args[0]));
switch (args_count) {
case 1:
native_ptr = (uintptr_t) new DigitalOut(pin_name);
break;
case 2:
int value = static_cast<int>(jerry_get_number_value(args[1]));
native_ptr = (uintptr_t) new DigitalOut(pin_name, value);
break;
}
// create the jerryscript object
jerry_value_t js_object = jerry_create_object();
jerry_set_object_native_handle(js_object, native_ptr, NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut));
// attach methods
ATTACH_CLASS_FUNCTION(js_object, DigitalOut, write);
ATTACH_CLASS_FUNCTION(js_object, DigitalOut, read);
ATTACH_CLASS_FUNCTION(js_object, DigitalOut, is_connected);
return js_object;
}

View file

@ -0,0 +1,270 @@
/* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-util/logging.h"
#include "jerryscript-mbed-drivers/I2C-js.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"
#include "mbed.h"
/**
* I2C#frequency (native JavaScript method)
*
* Set the frequency of the I2C bus.
*
* @param frequency New I2C Frequency
*/
DECLARE_CLASS_FUNCTION(I2C, frequency) {
CHECK_ARGUMENT_COUNT(I2C, frequency, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, frequency, 0, number);
// Unwrap native I2C object
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
I2C* native_ptr = reinterpret_cast<I2C*>(native_handle);
int hz = jerry_get_number_value(args[0]);
native_ptr->frequency(hz);
return jerry_create_undefined();
}
/**
* I2C#read (native JavaScript method)
*
* Read data from the I2C bus.
*
* @overload I2C#read(int)
* Read a single byte from the I2C bus
*
* @param ack indicates if the byte is to be acknowledged (1 => acknowledge)
*
* @returns array: Data read from the I2C bus
*
* @overload I2C#read(int, array, int)
* Read a series of bytes from the I2C bus
*
* @param address I2C address to read from
* @param data Array to read into
* @param length Length of data to read
*
* @returns array: Data read from the I2C bus
*/
DECLARE_CLASS_FUNCTION(I2C, read) {
CHECK_ARGUMENT_COUNT(I2C, read, (args_count == 1 || args_count == 3 || args_count == 4));
if (args_count == 1) {
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 0, number);
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
I2C *native_ptr = reinterpret_cast<I2C*>(native_handle);
int data = jerry_get_number_value(args[0]);
int result = native_ptr->read(data);
return jerry_create_number(result);
} else {
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 0, number);
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 1, array);
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 2, number);
CHECK_ARGUMENT_TYPE_ON_CONDITION(I2C, read, 3, boolean, (args_count == 4));
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
I2C *native_ptr = reinterpret_cast<I2C*>(native_handle);
const uint32_t data_len = jerry_get_array_length(args[1]);
int address = jerry_get_number_value(args[0]);
int length = jerry_get_number_value(args[2]);
char *data = new char[data_len];
bool repeated = false;
if (args_count == 4) {
repeated = jerry_get_boolean_value(args[3]);
}
int result = native_ptr->read(address, data, length, repeated);
jerry_value_t out_array = jerry_create_array(data_len);
for (uint32_t i = 0; i < data_len; i++) {
jerry_value_t val = jerry_create_number(double(data[i]));
jerry_set_property_by_index(out_array, i, val);
jerry_release_value(val);
}
delete[] data;
if (result == 0) {
// ACK
return out_array;
} else {
// NACK
const char *error_msg = "NACK received from I2C bus";
jerry_release_value(out_array);
return jerry_create_error(JERRY_ERROR_COMMON, reinterpret_cast<const jerry_char_t *>(error_msg));
}
}
}
/**
* I2C#write (native JavaScript method)
*
* @overload I2C#write(int)
* Write a single byte to the I2C bus
*
* @param data Data byte to write to the I2C bus
*
* @returns 1 on success, 0 on failure
*
* @overload I2C#write(int, array, int, bool)
* Write an array of data to a certain address on the I2C bus
*
* @param address 8-bit I2C slave address
* @param data Array of bytes to send
* @param length Length of data to write
* @param repeated (optional) If true, do not send stop at end.
*
* @returns 0 on success, non-0 on failure
*/
DECLARE_CLASS_FUNCTION(I2C, write) {
CHECK_ARGUMENT_COUNT(I2C, write, (args_count == 1 || args_count == 3 || args_count == 4));
if (args_count == 1) {
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 0, number);
// Extract native I2C object
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
I2C *native_ptr = reinterpret_cast<I2C*>(native_handle);
// Unwrap arguments
int data = jerry_get_number_value(args[0]);
int result = native_ptr->write(data);
return jerry_create_number(result);
} else {
// 3 or 4
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 0, number);
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 1, array);
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 2, number);
CHECK_ARGUMENT_TYPE_ON_CONDITION(I2C, write, 3, boolean, (args_count == 4));
// Extract native I2C object
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
I2C *native_ptr = reinterpret_cast<I2C*>(native_handle);
// Unwrap arguments
int address = jerry_get_number_value(args[0]);
const uint32_t data_len = jerry_get_array_length(args[1]);
int length = jerry_get_number_value(args[2]);
bool repeated = args_count == 4 && jerry_get_boolean_value(args[3]);
// Construct data byte array
char *data = new char[data_len];
for (uint32_t i = 0; i < data_len; i++) {
data[i] = jerry_get_number_value(jerry_get_property_by_index(args[1], i));
}
int result = native_ptr->write(address, data, length, repeated);
// free dynamically allocated resources
delete[] data;
return jerry_create_number(result);
}
}
/**
* I2C#start (native JavaScript method)
*
* Creates a start condition on the I2C bus.
*/
DECLARE_CLASS_FUNCTION(I2C, start)
{
CHECK_ARGUMENT_COUNT(I2C, start, (args_count == 0));
// Extract native I2C object
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
I2C *native_ptr = reinterpret_cast<I2C*>(native_handle);
native_ptr->start();
return jerry_create_undefined();
}
/**
* I2C#stop (native JavaScript method)
*
* Creates a stop condition on the I2C bus.
*/
DECLARE_CLASS_FUNCTION(I2C, stop)
{
CHECK_ARGUMENT_COUNT(I2C, stop, (args_count == 0));
// Extract native I2C object
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
I2C *native_ptr = reinterpret_cast<I2C*>(native_handle);
native_ptr->stop();
return jerry_create_undefined();
}
/**
* I2C#destructor
*
* Called if/when the I2C object is GC'ed.
*/
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(I2C) (uintptr_t handle) {
delete reinterpret_cast<I2C*>(handle);
}
/**
* I2C (native JavaScript constructor)
*
* @param sda mbed pin for I2C data
* @param scl mbed pin for I2C clock
* @returns a JavaScript object representing the I2C bus.
*/
DECLARE_CLASS_CONSTRUCTOR(I2C) {
CHECK_ARGUMENT_COUNT(I2C, __constructor, (args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, __constructor, 0, number);
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, __constructor, 1, number);
int sda = jerry_get_number_value(args[0]);
int scl = jerry_get_number_value(args[1]);
uintptr_t native_handle = (uintptr_t)new I2C((PinName)sda, (PinName)scl);
jerry_value_t js_object = jerry_create_object();
jerry_set_object_native_handle(js_object, native_handle, NAME_FOR_CLASS_NATIVE_DESTRUCTOR(I2C));
ATTACH_CLASS_FUNCTION(js_object, I2C, frequency);
ATTACH_CLASS_FUNCTION(js_object, I2C, read);
ATTACH_CLASS_FUNCTION(js_object, I2C, write);
ATTACH_CLASS_FUNCTION(js_object, I2C, start);
ATTACH_CLASS_FUNCTION(js_object, I2C, stop);
return js_object;
}

View file

@ -0,0 +1,218 @@
/* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-util/logging.h"
#include "jerryscript-mbed-event-loop/EventLoop.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"
#include "mbed.h"
/**
* InterruptIn#rise (native JavaScript method)
*
* Register a rise callback for an InterruptIn
*
* @param cb Callback function, or null to detach previously attached callback.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, rise) {
CHECK_ARGUMENT_COUNT(InterruptIn, rise, (args_count == 1));
// Detach the rise callback when InterruptIn::rise(null) is called
if (jerry_value_is_null(args[1])) {
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
InterruptIn *this_interruptin = (InterruptIn*) native_handle;
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_rise");
jerry_value_t cb_func = jerry_get_property(this_obj, property_name);
jerry_release_value(property_name);
// Only drop the callback if it exists
if (jerry_value_is_function(cb_func)) {
// Ensure that the EventLoop frees memory used by the callback.
mbed::js::EventLoop::getInstance().dropCallback(cb_func);
}
this_interruptin->rise(0);
return jerry_create_undefined();
}
// Assuming we actually have a callback now...
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, rise, 0, function);
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
InterruptIn *this_interruptin = reinterpret_cast<InterruptIn*>(native_handle);
jerry_value_t f = args[0];
// Pass the function to EventLoop.
mbed::Callback<void()> cb = mbed::js::EventLoop::getInstance().wrapFunction(f);
this_interruptin->rise(cb);
// Keep track of our callback internally.
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_rise");
jerry_set_property(this_obj, property_name, f);
jerry_release_value(property_name);
return jerry_create_undefined();
}
/**
* InterruptIn#fall (native JavaScript method)
*
* Register a fall callback for an InterruptIn
*
* @param cb Callback function, or null to detach previously attached callback.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, fall) {
CHECK_ARGUMENT_COUNT(InterruptIn, fall, (args_count == 1));
// Detach the fall callback when InterruptIn::fall(null) is called
if (jerry_value_is_null(args[1])) {
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
InterruptIn *this_interruptin = (InterruptIn*) native_handle;
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_fall");
jerry_value_t cb_func = jerry_get_property(this_obj, property_name);
jerry_release_value(property_name);
// Only drop the callback if it exists
if (jerry_value_is_function(cb_func)) {
// Ensure that the EventLoop frees memory used by the callback.
mbed::js::EventLoop::getInstance().dropCallback(cb_func);
}
this_interruptin->fall(0);
return jerry_create_undefined();
}
// Assuming we actually have a callback now...
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, fall, 0, function);
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
InterruptIn *this_interruptin = reinterpret_cast<InterruptIn*>(native_handle);
jerry_value_t f = args[0];
// Pass the function to EventLoop.
mbed::Callback<void()> cb = mbed::js::EventLoop::getInstance().wrapFunction(f);
this_interruptin->fall(cb);
// Keep track of our callback internally.
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_fall");
jerry_set_property(this_obj, property_name, f);
jerry_release_value(property_name);
return jerry_create_undefined();
}
/**
* InterruptIn#mode (native JavaScript method)
*
* Set the mode of the InterruptIn pin.
*
* @param mode PullUp, PullDown, PullNone
*/
DECLARE_CLASS_FUNCTION(InterruptIn, mode) {
CHECK_ARGUMENT_COUNT(InterruptIn, mode, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, mode, 0, number);
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
InterruptIn *native_ptr = reinterpret_cast<InterruptIn*>(native_handle);
int pull = jerry_get_number_value(args[0]);
native_ptr->mode((PinMode)pull);
return jerry_create_undefined();
}
/**
* InterruptIn#disable_irq (native JavaScript method)
*
* Disable IRQ. See InterruptIn.h in mbed-os sources for more details.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, disable_irq) {
CHECK_ARGUMENT_COUNT(InterruptIn, disable_irq, (args_count == 0));
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
InterruptIn *native_ptr = reinterpret_cast<InterruptIn*>(native_handle);
native_ptr->disable_irq();
return jerry_create_undefined();
}
/**
* InterruptIn#enable_irq (native JavaScript method)
*
* Enable IRQ. See InterruptIn.h in mbed-os sources for more details.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, enable_irq) {
CHECK_ARGUMENT_COUNT(InterruptIn, enable_irq, (args_count == 0));
uintptr_t native_handle;
jerry_get_object_native_handle(this_obj, &native_handle);
InterruptIn *native_ptr = reinterpret_cast<InterruptIn*>(native_handle);
native_ptr->enable_irq();
return jerry_create_undefined();
}
/**
* InterruptIn#destructor
*
* Called if/when the InterruptIn object is GC'ed.
*/
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn) (uintptr_t handle) {
InterruptIn *native_ptr = reinterpret_cast<InterruptIn*>(handle);
native_ptr->rise(0);
native_ptr->fall(0);
delete native_ptr;
}
/**
* InterruptIn (native JavaScript constructor)
*
* @param pin PinName
*
* @returns JavaScript object wrapping InterruptIn native object
*/
DECLARE_CLASS_CONSTRUCTOR(InterruptIn) {
CHECK_ARGUMENT_COUNT(InterruptIn, __constructor, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, __constructor, 0, number);
int pin = jerry_get_number_value(args[0]);
uintptr_t native_handle = (uintptr_t)new InterruptIn((PinName)pin);
jerry_value_t js_object = jerry_create_object();
jerry_set_object_native_handle(js_object, native_handle, NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn));
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, rise);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, fall);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, mode);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, enable_irq);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, disable_irq);
return js_object;
}

View file

@ -0,0 +1,34 @@
/* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-drivers/assert-js.h"
/**
* assert (native JavaScript function)
*
* @param condition The condition to assert to be true
* @returns undefined if the assertion passes, returns an error if the assertion
* fails.
*/
DECLARE_GLOBAL_FUNCTION(assert) {
CHECK_ARGUMENT_COUNT(global, assert, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(global, assert, 0, boolean);
if (!jerry_get_boolean_value(args[0])) {
const char* error_msg = "Assertion failed";
return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t*>(error_msg));
}
return jerry_create_undefined();
}

View file

@ -0,0 +1,25 @@
/* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-drivers/gc-js.h"
/**
* gc (native JavaScript function)
*
* Manually cause JerryScript to run garbage collection.
*/
DECLARE_GLOBAL_FUNCTION(gc) {
jerry_gc();
return jerry_create_undefined();
}

View file

@ -0,0 +1,36 @@
/* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-drivers/setInterval-js.h"
#include "jerryscript-mbed-event-loop/EventLoop.h"
/**
* setInterval (native JavaScript function)
*
* Call a JavaScript function at fixed intervals.
*
* @param function Function to call
* @param interval Time between function calls, in ms.
*/
DECLARE_GLOBAL_FUNCTION(setInterval) {
CHECK_ARGUMENT_COUNT(global, setInterval, (args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS(global, setInterval, 0, function);
CHECK_ARGUMENT_TYPE_ALWAYS(global, setInterval, 1, number);
jerry_acquire_value(args[0]);
int interval = int(jerry_get_number_value(args[1]));
mbed::js::EventLoop::getInstance().getQueue().call_every(interval, jerry_call_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
return jerry_create_undefined();
}

View file

@ -0,0 +1,37 @@
/* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-drivers/setTimeout-js.h"
#include "jerryscript-mbed-event-loop/EventLoop.h"
/**
* setTimeout (native JavaScript function)
*
* Call a JavaScript function once, after a fixed time period.
*
* @param function Function to call
* @param wait_time Time before function is called, in ms.
*/
DECLARE_GLOBAL_FUNCTION(setTimeout) {
CHECK_ARGUMENT_COUNT(global, setTimeout, (args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS(global, setTimeout, 0, function);
CHECK_ARGUMENT_TYPE_ALWAYS(global, setTimeout, 1, number);
jerry_acquire_value(args[0]);
int interval = int(jerry_get_number_value(args[1]));
mbed::js::EventLoop::getInstance().getQueue().call_in(interval, jerry_call_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
return jerry_create_undefined();
}