### Split Engine Initialization and Script Execution in JerryScript Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/03.API-EXAMPLE.md Shows how to manually initialize, execute, and clean up the JerryScript engine. This provides more control, allowing for setup of native objects or interaction with JavaScript functions. Requires 'jerry-api.h'. ```c #include #include "jerry-api.h" int main (int argc, char *argv[]) { const jerry_char_t script[] = "print ('Hello, World!');"; size_t script_size = strlen ((const char *) script); /* Initialize engine */ jerry_init (JERRY_INIT_EMPTY); /* Setup Global scope code */ jerry_value_t parsed_code = jerry_parse (script, script_size, false); if (!jerry_value_has_error_flag (parsed_code)) { /* Execute the parsed source code in the Global scope */ jerry_value_t ret_value = jerry_run (parsed_code); /* Returned value must be freed */ jerry_release_value (ret_value); } /* Parsed source code must be freed */ jerry_release_value (parsed_code); /* Cleanup engine */ jerry_cleanup (); return 0; } ``` -------------------------------- ### OpenOCD Debug Session Setup (Bash) Source: https://context7.com/coredevices/pebbleos/llms.txt This bash script provides commands for initiating an OpenOCD debug session with GDB. It involves starting the OpenOCD server in one terminal and then connecting to it with GDB in another. Basic GDB commands for debugging are also included. ```bash # Start OpenOCD server (keeps running) ./waf openocd # In another terminal, start GDB ./waf gdb # GDB commands for debugging: # (gdb) break main # (gdb) continue # (gdb) backtrace ``` -------------------------------- ### Run JerryScript 'Hello, World!' Example Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/riot-stm32f4/README.md This is a simple JerryScript code snippet that prints 'Hello, World!' to the console. It's used as a basic test case after flashing the JerryScript environment onto the RIOT-OS board. ```javascript print ('Hello, World!'); ``` -------------------------------- ### Install Go and newt Build Tool Source: https://github.com/coredevices/pebbleos/blob/main/third_party/nimble/syscfg/README.md Installs the Go programming language and the `newt` build tool, which is essential for building Mynewt projects. Ensure Go is installed before running these commands. The PATH environment variable is updated to include the newt executable. ```shell go install mynewt.apache.org/newt/newt@latest export PATH=${PATH}:`go env GOPATH`/bin ``` -------------------------------- ### Rocky.js Watchface Application Example Source: https://context7.com/coredevices/pebbleos/llms.txt A basic example of a watchface application written in JavaScript using Rocky.js. It demonstrates drawing the time and date on the screen and handling minute change events. ```javascript // app.js - Rocky.js watchface var rocky = require('rocky'); rocky.on('draw', function(event) { var ctx = event.context; var d = new Date(); // Clear display ctx.clearRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight); // Draw time ctx.fillStyle = 'white'; ctx.textAlign = 'center'; ctx.font = '42px bold Gothic'; var hours = d.getHours(); var minutes = d.getMinutes(); var timeStr = hours + ':' + (minutes < 10 ? '0' : '') + minutes; ctx.fillText(timeStr, ctx.canvas.clientWidth / 2, ctx.canvas.clientHeight / 2); // Draw date ctx.font = '18px Gothic'; var dateStr = d.toLocaleDateString(); ctx.fillText(dateStr, ctx.canvas.clientWidth / 2, ctx.canvas.clientHeight / 2 + 30); }); rocky.on('minutechange', function(event) { rocky.requestDraw(); }); // Initialize rocky.requestDraw(); ``` -------------------------------- ### Interact with JavaScript Global Object in JerryScript Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/03.API-EXAMPLE.md Illustrates how to interact with the JerryScript JavaScript environment from C. This example sets a property ('s') on the global object and then executes a script that accesses this property. Requires 'jerry-api.h'. ```c #include #include "jerry-api.h" int main (int argc, char *argv[]) { const jerry_char_t str[] = "Hello, World!"; const jerry_char_t script[] = "print (s);"; /* Initializing JavaScript environment */ jerry_init (JERRY_INIT_EMPTY); /* Getting pointer to the Global object */ jerry_value_t global_object = jerry_get_global_object (); /* Constructing strings */ jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "s"); jerry_value_t prop_value = jerry_create_string (str); /* Setting the string value as a property of the Global object */ jerry_set_property (global_object, prop_name, prop_value); /* Releasing string values, as it is no longer necessary outside of engine */ jerry_release_value (prop_name); jerry_release_value (prop_value); /* Releasing the Global object */ jerry_release_value (global_object); /* Now starting script that would output value of just initialized field */ jerry_value_t eval_ret = jerry_eval (script, strlen ((const char *) script), false); /* Free JavaScript value, returned by eval */ jerry_release_value (eval_ret); /* Freeing engine */ jerry_cleanup (); return 0; } ``` -------------------------------- ### Configure and Build PebbleOS Firmware (Waf) Source: https://context7.com/coredevices/pebbleos/llms.txt This snippet demonstrates how to configure and build the PebbleOS firmware for various target hardware using the Waf build system. It includes steps for dependency installation, configuration with board options, and different build targets like standard firmware, recovery firmware, and bundled firmware. Dependencies are installed using pip within a virtual environment. ```bash # Install dependencies (Ubuntu) sudo apt install clang gcc gcc-multilib git gettext python3-dev python3-venv openocd python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt # Configure for specific board ./waf configure --board asterix # Options: asterix, snowy_bb2, robert_evt, silk, spalding, etc. # Build firmware ./waf build # Build with options ./waf configure --board snowy_bb2 --qemu --js-engine rocky --log-level debug ./waf build # Build recovery firmware (PRF) ./waf build_prf # Build and bundle firmware ./waf build bundle ``` -------------------------------- ### Build mbed Binary with Yotta Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/mbed/readme.md This rule configures the Yotta target, installs mbed-drivers and other dependencies, and finally creates a binary file (jerry.bin) for the specified Yotta target. The binary is generated at targets/mbed/build/$(YOTTA_TARGET)/source/. ```makefile make -f targets/mbed/Makefile.mbed board=$(TARGET) yotta ``` -------------------------------- ### Execute JavaScript Directly with JerryScript Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/03.API-EXAMPLE.md Demonstrates the simplest way to run a JavaScript string within a C application using JerryScript. It initializes the engine, runs a script, and cleans up automatically. Requires the 'jerry-api.h' header. ```c #include #include "jerry-api.h" int main (int argc, char *argv[]) { const jerry_char_t script[] = "print ('Hello, World!');"; size_t script_size = strlen ((const char *) script); bool ret_value = jerry_run_simple (script, script_size, JERRY_INIT_EMPTY); return (ret_value ? 0 : 1); } ``` -------------------------------- ### Download and Setup Repo for Curie BSP Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/curie_bsp/README.md This snippet outlines the steps to download the 'repo' tool, make it executable, and add it to the system's PATH, preparing the environment for managing Curie BSP source code. It requires a Linux environment and internet access. ```shell mkdir ~/bin wget http://commondatastorage.googleapis.com/git-repo-downloads/repo -O ~/bin/repo chmod a+x ~/bin/repo ``` ```shell PATH=$PATH:~/bin ``` -------------------------------- ### Add New Target Configuration to Makefile Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/mbed/readme.md Example snippet showing how to add a new board configuration to the Makefile. This involves adding the board name to TARGET_LIST and defining YOTTA_TARGET and TARGET_DIR for the new board. ```makefile ifeq ($(TARGET), k64f) YOTTA_TARGET = frdm-k64f-gcc TARGET_DIR ?= /media/$(USER)/MBED else ifeq ($(TARGET), stm32f4) YOTTA_TARGET = ``` -------------------------------- ### Get Current Time - C Implementation Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/05.PORT-API.md Provides the default C implementation for retrieving the current system time in milliseconds. It uses `gettimeofday` to get the current time and calculates the time in milliseconds from the seconds and microseconds components. Returns 0 on error. Dependencies include ``. ```c #include /** * Default implementation of jerry_port_get_current_time. */ double jerry_port_get_current_time () { struct timeval tv; if (gettimeofday (&tv, NULL) != 0) { return 0; } return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0; } /* jerry_port_get_current_time */ ``` -------------------------------- ### Simple PebbleOS Watchface in C Source: https://context7.com/coredevices/pebbleos/llms.txt A basic example of a watchface application developed for PebbleOS using the C SDK. It demonstrates the core components: initializing the window and text layer, updating the time display, handling tick events for minute updates, and managing the application lifecycle with init, app_event_loop, and deinit. ```c #include static Window *window; static TextLayer *text_layer; static void update_time() { time_t temp = time(NULL); struct tm *tick_time = localtime(&temp); static char s_buffer[8]; strftime(s_buffer, sizeof(s_buffer), clock_is_24h_style() ? "%H:%M" : "%I:%M", tick_time); text_layer_set_text(text_layer, s_buffer); } static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { update_time(); } static void init() { window = window_create(); window_stack_push(window, true); Layer *window_layer = window_get_root_layer(window); GRect bounds = layer_get_bounds(window_layer); text_layer = text_layer_create( GRect(0, bounds.size.h/2 - 20, bounds.size.w, 40)); text_layer_set_background_color(text_layer, GColorBlack); text_layer_set_text_color(text_layer, GColorWhite); text_layer_set_font(text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD)); text_layer_set_text_alignment(text_layer, GTextAlignmentCenter); layer_add_child(window_layer, text_layer_get_layer(text_layer)); tick_timer_service_subscribe(MINUTE_UNIT, tick_handler); update_time(); } static void deinit() { text_layer_destroy(text_layer); window_destroy(window); } int main(void) { init(); app_event_loop(); deinit(); } ``` -------------------------------- ### Prepare Zephyr Environment for JerryScript Build Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/zephyr/README.md Configures the Zephyr build environment by sourcing the Zephyr environment script and setting necessary environment variables for the GCC toolchain and SDK installation directory. This is a prerequisite for building JerryScript with Zephyr. ```bash cd zephyr-project source zephyr-env.sh export ZEPHYR_GCC_VARIANT=zephyr export ZEPHYR_SDK_INSTALL_DIR= ``` -------------------------------- ### Interact with JerryScript via Serial Terminal Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/zephyr/README.md Demonstrates the interactive JavaScript console provided by JerryScript running on Zephyr. Shows example outputs including build information and successful execution of simple and more complex JavaScript functions. ```javascript JerryScript build: Aug 12 2016 17:12:55 JerryScript API 1.0 Zephyr version 1.4.0 js> var test=0; for (t=100; t<1000; t++) test+=t; print ('Hi JS World! '+test); Hi JS World! 494550 ``` ```javascript js> function hello(t) {t=t*10;return t}; print("result"+hello(10.5)); ``` -------------------------------- ### Build JerryScript for Curie BSP Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/curie_bsp/README.md This section covers the build process for JerryScript on the Curie BSP. It involves generating Makefiles using a Python script, performing a one-time setup for necessary tools, and then compiling the final image. The output is a flashable firmware image. Dependencies include the Python script and the Curie BSP project structure. ```shell python setup.py ``` ```shell make -C wearable_device_sw/projects/curie_bsp_jerry/ one_time_setup ``` ```shell mkdir out && cd $_ make -f ../wearable_device_sw/projects/curie_bsp_jerry/Makefile setup make image ``` -------------------------------- ### Flash Binary to mbed Board Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/mbed/readme.md This command flashes the compiled JerryScript binary onto the connected mbed board. It automatically detects the board or uses STLink-v2 for STM32 boards. Ensure the board is mounted or STLink-v2 is installed. ```makefile make -f targets/mbed/Makefile.mbed board=$(TARGET) flash ``` -------------------------------- ### Flash Firmware and Start Serial Terminal Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/curie_bsp/README.md This snippet demonstrates how to flash the compiled firmware onto the Arduino 101 board using a JTAG debugger and how to connect to the board's serial console. It uses the 'make flash' command with specific configurations and 'screen' to establish a serial connection. Successful flashing and connection allow for interaction with the device. ```shell make flash FLASH_CONFIG=jtag_full ``` ```shell screen ttyUSB0 115200 ``` -------------------------------- ### Pebble App Event Loop (C) Source: https://github.com/coredevices/pebbleos/blob/main/sdk/docs/common.dox Defines the main entry point for a Pebble app and its event loop. The `app_event_loop()` function manages event handling and continues until the app is ready to exit. It requires setup before execution and cleanup afterwards. ```c int main(void) { // do set up here // Enter the main event loop. This will block until the app is ready to exit. app_event_loop(); // do clean up here } ``` -------------------------------- ### Execute JavaScript on Arduino 101 via Serial Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/curie_bsp/README.md This example shows how to execute JavaScript code on the Arduino 101 board after connecting via a serial terminal. It illustrates the command format 'js e ' and shows the expected output, including execution results and debug information. This is used for testing JavaScript functionality on the device. ```shell js e print ('Hello World!'); ``` -------------------------------- ### FFT Output Generation Pseudo Code Source: https://github.com/coredevices/pebbleos/blob/main/src/fw/services/normal/activity/docs/index.md Details the process of generating FFT output for step identification. It involves subtracting the mean from 125 samples per axis, zero-extending to 128 samples, computing the FFT, calculating the amplitude for each frequency (64 outputs per axis), and combining the amplitudes across axes to get the overall energy per frequency. ```pseudo for i = 0 to 63: energy[i] = sqrt(amp_x[i]^2 + amp_y[i]^2 + amp_z[i]^2) ``` -------------------------------- ### Get Time Zone - C Implementation Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/05.PORT-API.md Provides the default C implementation for retrieving the time zone information. It utilizes `gettimeofday` to fetch system time and timezone details, populating a `jerry_time_zone_t` structure. Dependencies include `` and `jerry-port.h`. Returns `false` on error. ```c #include #include "jerry-port.h" /** * Default implementation of jerry_port_get_time_zone. */ bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p) { struct timeval tv; struct timezone tz; /* gettimeofday may not fill tz, so zero-initializing */ tz.tz_minuteswest = 0; tz.tz_dsttime = 0; if (gettimeofday (&tv, &tz) != 0) { return false; } tz_p->offset = tz.tz_minuteswest; tz_p->daylight_saving_time = tz.tz_dsttime > 0 ? 1 : 0; return true; } /* jerry_port_get_time_zone */ ``` -------------------------------- ### Mocking JavaScript Date Object with TimeShift.js Source: https://github.com/coredevices/pebbleos/blob/main/applib-targets/emscripten/timeshift-js/README.md Demonstrates how to override the global Date object with TimeShift.Date to control time and timezone for testing. Includes examples of setting specific times, timezone offsets, and retrieving mocked values. Also shows how to use the original Date object and a helper method for description. ```javascript new Date().toString(); // Original Date object // "Fri Aug 09 2013 23:37:42 GMT+0300 (EEST)" Date = TimeShift.Date; // Overwrite Date object new Date().toString(); // "Fri Aug 09 2013 23:37:43 GMT+0300" TimeShift.setTimezoneOffset(-60); // Set timezone to GMT+0100 (note the sign) new Date().toString(); // "Fri Aug 09 2013 21:37:44 GMT+0100" TimeShift.setTime(1328230923000); // Set the time to 2012-02-03 01:02:03 GMT new Date().toString(); // "Fri Feb 03 2012 02:02:03 GMT+0100" TimeShift.setTimezoneOffset(0); // Set timezone to GMT new Date().toString(); // "Fri Feb 03 2012 01:02:03 GMT" TimeShift.getTime(); // Get overridden values // 1328230923000 TimeShift.getTimezoneOffset(); // 0 TimeShift.setTime(undefined); // Reset to current time new Date().toString(); // "Fri Aug 09 2013 20:37:45 GMT" new Date().desc(); // Helper method // "utc=Fri, 09 Aug 2013 20:37:46 GMT local=Fri, 09 Aug 2013 20:37:46 GMT offset=0" new TimeShift.OriginalDate().toString(); // Use original Date object // "Fri Aug 09 2013 23:37:47 GMT+0300 (EEST)" ``` -------------------------------- ### Initialize and Sync Curie BSP Repository Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/curie_bsp/README.md This section details the process of initializing the Curie BSP repository using 'repo init' and then downloading all the source files with 'repo sync'. It requires the specified manifest URL and can be parallelized with the '-j' flag. The output includes the downloaded source code. ```shell mkdir Curie_BSP && cd $_ repo init -u https://github.com/CurieBSP/manifest repo sync -j 5 -d ``` -------------------------------- ### Flash JerryScript to STM32F4-Discovery Board Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/riot-stm32f4/README.md This command flashes the built JerryScript image onto the STM32F4-Discovery board using the RIOT-OS build system. ```makefile make -f ./targets/riot-stm32f4/Makefile.riot flash ``` -------------------------------- ### PebbleOS QEMU Emulator Workflow (Waf) Source: https://context7.com/coredevices/pebbleos/llms.txt This section outlines the process of using the QEMU emulator to test PebbleOS firmware without physical hardware. It involves configuring the build for QEMU support, building the firmware, creating QEMU-specific images, bundling for QEMU, launching the emulator, connecting to its console, and debugging with GDB. ```bash # Configure with QEMU support ./waf configure --board snowy_bb2 --qemu # Build firmware ./waf build # Create QEMU images ./waf qemu_image_micro ./waf qemu_image_spi # Bundle for QEMU ./waf bundle_qemu # Launch emulator ./waf qemu_launch # In another terminal, connect to console ./waf qemu_console --qemu_host localhost:12345 # Debug with GDB ./waf qemu_gdb ``` -------------------------------- ### Get Own Property Descriptor Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/02.API-REFERENCE.md Constructs a property descriptor for a specified property of an object. ```APIDOC ## jerry_get_own_property_descriptor ### Description Construct property descriptor from specified property. ### Method bool ### Endpoint N/A (C function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **obj_val** (jerry_value_t) - Required - object value - **prop_name_val** (jerry_value_t) - Required - property name - **prop_desc_p** (jerry_property_descriptor_t *) - Required - pointer to property descriptor ### Request Example ```c { jerry_value_t global_obj_val = jerry_get_global_object (); jerry_property_descriptor_t prop_desc; jerry_init_property_descriptor_fields (&prop_desc); jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_prop"); jerry_get_own_property_descriptor (global_obj_val, prop_name, &prop_desc); jerry_release_value (prop_name); // ... usage of property descriptor jerry_free_property_descriptor_fields (&prop_desc); jerry_release_value (global_obj_val); } ``` ### Response #### Success Response (200) - **return value** (bool) - true, if the property descriptor was successfully retrieved. #### Response Example N/A ``` -------------------------------- ### Build JerryScript for RIOT-OS Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/riot-stm32f4/README.md This command builds JerryScript for the RIOT-OS target on the STM32F4-Discovery board. It generates core and math libraries and copies them to the target directory. Finally, it creates an executable ELF file. ```makefile # assume you are in harmony folder cd jerryscript make -f ./targets/riot-stm32f4/Makefile.riot ``` -------------------------------- ### Apply JERRY_CONST_DATA Attribute in C Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/esp8266/readme.md An example demonstrating how to apply the JERRY_CONST_DATA attribute to a static constant array in C code. This attribute instructs the compiler to store constants in ROM, thereby saving RAM. ```c static const lit_magic_size_t lit_magic_string_sizes[] JERRY_CONST_DATA = ``` -------------------------------- ### Get Jerry Boolean Value (C) Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/02.API-REFERENCE.md Retrieves the raw boolean value from a jerry_value_t. This function requires the input value to be a boolean type. It returns the C-style boolean representation. ```c bool jerry_get_boolean_value (const jerry_value_t value); { jerry_value_t value; ... // create or acquire value if (jerry_value_is_boolean (value)) { bool raw_value = jerry_get_boolean_value (value); ... // usage of raw value } jerry_release_value (value); } ``` -------------------------------- ### Initialize Property Descriptor Fields in C Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/02.API-REFERENCE.md Initializes the fields of a jerry_property_descriptor_t structure. This function is crucial before using the descriptor for defining or getting properties. It ensures that the descriptor is in a safe, initial state. ```c void jerry_init_property_descriptor_fields (jerry_property_descriptor_t *prop_desc_p); // Example Usage: jerry_property_descriptor_t prop_desc; jerry_init_property_descriptor_fields (&prop_desc); ... // usage of prop_desc jerry_free_property_descriptor_fields (&prop_desc); ``` -------------------------------- ### Configure Toolchain and BLE Firmware for Curie BSP Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/curie_bsp/README.md This snippet describes how to set up the necessary toolchain and BLE firmware for the IntelĀ® Curie BSP. It involves downloading a toolchain tarball, setting the TOOLCHAIN_DIR environment variable, and extracting a BLE firmware package into a specific directory. Dependencies include the toolchain and firmware files. ```shell export TOOLCHAIN_DIR='path to files of the toolchain' ``` ```shell tar -xzf issm-toolchain-linux-2016-05-12.tar.gz -C wearable_device_sw/external/toolchain --strip-components=1 ``` ```shell tar -xzf curie-ble-v3.1.1.tar.gz -C wearable_device_sw/packages ``` -------------------------------- ### Get JerryScript String Length Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/02.API-REFERENCE.md Determines the length of a JerryScript string in characters. If the input value is not a string, the function returns zero. This is distinct from 'jerry_get_string_size' as it counts characters rather than bytes. ```c jerry_length_t jerry_get_string_length (const jerry_value_t value); ``` ```c { const jerry_char_t char_array[] = "a string"; jerry_value_t string = jerry_create_string (char_array); jerry_length_t string_length = jerry_get_string_length (string); ... // usage of string_length jerry_release_value (string); } ``` -------------------------------- ### Clean JerryScript Build Artifacts Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/riot-stm32f4/README.md This command cleans the build artifacts generated by the JerryScript build process for RIOT-OS. ```makefile make -f ./targets/riot-stm32f4/Makefile.riot clean ``` -------------------------------- ### Get JerryScript Array Length Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/02.API-REFERENCE.md Retrieves the length (number of elements) of a JerryScript array object. If the provided JerryScript value is not an array, this function returns 0. This is crucial for iterating over array elements. ```c uint32_t jerry_get_array_length (const jerry_value_t value); ``` ```c { jerry_value_t value; ... // create or acquire value uint32_t len = jerry_get_array_length (value); jerry_release_value (value); } ``` -------------------------------- ### Random Number Generation (C) Source: https://github.com/coredevices/pebbleos/blob/main/sdk/docs/common.dox Provides functions for generating pseudo-random integers and seeding the generator. `rand()` returns a number between 0 and `RAND_MAX`, and `srand()` initializes the sequence. The generator is automatically seeded on app start. ```c int rand(); void srand(unsigned int seed); ``` -------------------------------- ### Initialize Rocky Simulator and Load Watchface App (JavaScript) Source: https://github.com/coredevices/pebbleos/blob/main/applib-targets/emscripten/html/index.html This snippet demonstrates how to initialize the Rocky Simulator, bind it to an HTML canvas element, and dynamically load a watchface application script. It handles loading the script from a URL query parameter or a default 'js/tictoc.js' file, allowing for flexible testing of different watchfaces. ```javascript var rockySimulator = new RockySimulator({ canvas: document.getElementById("pebble") }); var rocky = _rocky; function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split('&'); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split('='); if (decodeURIComponent(pair[0]) == variable) { return decodeURIComponent(pair[1]); } } } var src = getQueryVariable('src'); if (src !== '') { src = src || 'js/tictoc.js'; var script = document.createElement('script'); script.src = src; document.getElementsByTagName('html')[0].appendChild(script); } ``` -------------------------------- ### Execute JavaScript using eval-mode in JerryScript Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/03.API-EXAMPLE.md Demonstrates executing multiple, independent JavaScript code snippets sequentially within the same JerryScript execution environment using `jerry_eval`. This allows for state persistence between script executions. Requires 'jerry-api.h'. ```c #include #include "jerry-api.h" int main (int argc, char *argv[]) { const jerry_char_t script_1[] = "var s = 'Hello, World!';"; const jerry_char_t script_2[] = "print (s);"; /* Initialize engine */ jerry_init (JERRY_INIT_EMPTY); jerry_value_t eval_ret; /* Evaluate script1 */ eval_ret = jerry_eval (script_1, strlen ((const char *) script_1), false); /* Free JavaScript value, returned by eval */ jerry_release_value (eval_ret); /* Evaluate script2 */ eval_ret = jerry_eval (script_2, strlen ((const char *) script_2), false); /* Free JavaScript value, returned by eval */ jerry_release_value (eval_ret); /* Cleanup engine */ jerry_cleanup (); return 0; } ``` -------------------------------- ### Initialize Mynewt Project and Update NimBLE Submodule Source: https://github.com/coredevices/pebbleos/blob/main/third_party/nimble/syscfg/README.md Initializes the Mynewt project and ensures the correct NimBLE version is used by cloning it from the submodule. This step is crucial for setting up the project environment and dependencies. ```shell newt upgrade --shallow=1 # Use NimBLE version from submodule rm -rf repos/apache-mynewt-nimble git clone ../mynewt-nimble repos/apache-mynewt-nimble ``` -------------------------------- ### Clone JerryScript Repository Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/README.md This command clones the JerryScript project from its GitHub repository. It's the first step in obtaining the source code for building and using the engine. ```bash git clone https://github.com/Samsung/jerryscript.git cd jerryscript ``` -------------------------------- ### Get Property by Index - C Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/02.API-REFERENCE.md Retrieves the value of a property from an object using its numerical index. The returned value must be freed using jerry_release_value when no longer needed. It takes the object value and the index as input. ```c jerry_value_t object; ... // create or acquire object jerry_value_t value = jerry_get_property_by_index (object, 5); ... jerry_release_value (value); jerry_release_value (object); ``` -------------------------------- ### Build JerryScript for ESP8266 using Makefile Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/targets/esp8266/readme.md Commands to clean and build JerryScript for the ESP8266 platform using the provided Makefile. Assumes the ESP-IoT-RTOS-SDK is patched and the build environment is set up. Output files are directed to the BIN_PATH. ```bash cd ~/harmony/jerryscript # clean build make -f ./targets/esp8266/Makefile.esp8266 clean # or just normal build make -f ./targets/esp8266/Makefile.esp8266 ``` -------------------------------- ### Get JerryScript String Size Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/docs/02.API-REFERENCE.md Obtains the size of a JerryScript string in bytes. If the provided JerryScript value is not a string, this function returns zero. The size is useful for allocating appropriate buffer space for string manipulation. ```c jerry_size_t jerry_get_string_size (const jerry_value_t value); ``` ```c { const jerry_char_t char_array[] = "a string"; jerry_value_t string = jerry_create_string (char_array); jerry_size_t string_size = jerry_get_string_size (string); ... // usage of string_size jerry_release_value (string); } ``` -------------------------------- ### Waf Build System Commands for Device Management Source: https://context7.com/coredevices/pebbleos/llms.txt Waf build system commands for managing the device. Includes commands for debugging recovery firmware, resetting the device via OpenOCD, and erasing device flash. ```bash # Debug recovery firmware ./waf gdb_prf # Reset device via OpenOCD ./waf reset # Erase device flash ./waf bork ``` -------------------------------- ### JS Tooling: Define PebbleOS Path2D Object Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/js_tooling/test.html Defines the `Path2D` object for PebbleOS, used to represent drawing paths. It includes methods for setting the starting point (`moveTo`) and adding line segments (`lineTo`). The coordinates are rounded for efficiency. ```javascript \/\*global Rocky:true *\/\r\n\r\nif (typeof (Rocky) === \'undefined\') {\r\n Rocky = {};\r\n}\r\n\r\n(function() {\r\n Rocky.Path2D = function() {\r\n };\r\n\r\n var rounded = function(v) {\r\n return Math.round(v \* 8) \/ 8;\r\n };\r\n\r\n \/\/ TODO: implement this correctly\r\n Rocky.Path2D.prototype.moveTo = function(x, y) {\r\n this.p0 = [rounded(x), rounded(y)];\r\n };\r\n Rocky.Path2D.prototype.lineTo = function(x, y) {\r\n this.p1 = [rounded(x), rounded(y)];\r\n };\r\n\r\n})(); ``` -------------------------------- ### Build JerryScript Engine Source: https://github.com/coredevices/pebbleos/blob/main/third_party/jerryscript/jerryscript/README.md This command initiates the build process for the JerryScript engine using the provided Python build script. Ensure you are in the root directory of the cloned repository before running this command. ```python python tools/build.py ```