From 559065ae097633739dbd4a9ca04c0dbdbb1f3f25 Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Wed, 10 Jan 2024 15:36:12 +0100 Subject: [PATCH 01/12] Add a browser platform support --- engine/HAL/BROWSER/hal_script.py | 53 ++++ engine/HAL/BROWSER/luos_hal.c | 253 ++++++++++++++++++ engine/HAL/BROWSER/luos_hal.h | 59 ++++ engine/HAL/BROWSER/luos_hal_config.h | 74 +++++ examples/projects/browser/led/README.md | 38 +++ .../projects/browser/led/lib/Led/README.md | 18 ++ examples/projects/browser/led/lib/Led/led.c | 129 +++++++++ examples/projects/browser/led/lib/Led/led.h | 28 ++ .../projects/browser/led/lib/Led/library.json | 14 + examples/projects/browser/led/node_config.h | 96 +++++++ examples/projects/browser/led/platformio.ini | 28 ++ examples/projects/browser/led/src/main.c | 16 ++ network/ws_network/HAL/ARDUINO/hal_script.py | 8 + network/ws_network/HAL/BROWSER/ws_hal.c | 60 +++++ network/ws_network/HAL/BROWSER/ws_hal.h | 23 ++ .../ws_network/HAL/BROWSER/ws_hal_config.h | 12 + 16 files changed, 909 insertions(+) create mode 100644 engine/HAL/BROWSER/hal_script.py create mode 100644 engine/HAL/BROWSER/luos_hal.c create mode 100644 engine/HAL/BROWSER/luos_hal.h create mode 100644 engine/HAL/BROWSER/luos_hal_config.h create mode 100644 examples/projects/browser/led/README.md create mode 100644 examples/projects/browser/led/lib/Led/README.md create mode 100644 examples/projects/browser/led/lib/Led/led.c create mode 100644 examples/projects/browser/led/lib/Led/led.h create mode 100644 examples/projects/browser/led/lib/Led/library.json create mode 100644 examples/projects/browser/led/node_config.h create mode 100644 examples/projects/browser/led/platformio.ini create mode 100644 examples/projects/browser/led/src/main.c create mode 100644 network/ws_network/HAL/ARDUINO/hal_script.py create mode 100644 network/ws_network/HAL/BROWSER/ws_hal.c create mode 100644 network/ws_network/HAL/BROWSER/ws_hal.h create mode 100644 network/ws_network/HAL/BROWSER/ws_hal_config.h diff --git a/engine/HAL/BROWSER/hal_script.py b/engine/HAL/BROWSER/hal_script.py new file mode 100644 index 000000000..0aad8b622 --- /dev/null +++ b/engine/HAL/BROWSER/hal_script.py @@ -0,0 +1,53 @@ +#!/usr/bin/python +# This sript is used to modify the platformio build environment to use cheerp instead + +import os +import platform +# Local env +Import("env") + # Global env, use it if you call this script from a library.json file +genv = DefaultEnvironment() + + # Get the cheerp bin folder +cheerp_bin_path = None +if platform.system() == 'Windows': + cheerp_bin_path = 'c:/cheerp/bin' +if platform.system() == 'Linux': + cheerp_bin_path = '/opt/cheerp/bin' +if platform.system() == 'Darwin': + cheerp_bin_path = '/Applications/cheerp/bin' + +if platform.system() == 'Darwin': + # Create a symlink to Cheerp clang++ in gcc + try: + os.symlink(cheerp_bin_path + '/clang++', cheerp_bin_path + '/gcc') + except FileExistsError: + pass + + # Create a symlink to Cheerp clang++ in g++ + try: + os.symlink(cheerp_bin_path + '/clang++', cheerp_bin_path + '/g++') + except FileExistsError: + pass + +# Add the cheerp fake gcc path at the beginning of the PATH environment variable than platformio will use it by default +current_path = os.environ.get('PATH', '') +os.environ['PATH'] = f"{cheerp_bin_path}{os.pathsep}{current_path}" + +# Print the path used when calling gcc +print("GCC used by platformio is :" + os.popen("which gcc").read()) + +for e in [env, genv]: + # Add the cheerp-wasm target to the compiler flags + e.Append(CCFLAGS=["--target=cheerp-wasm"]) + + # Add the cheerp-wasm target to the linker flags + e.Append(LINKFLAGS=["--target=cheerp-wasm"]) + + # Replace the ar and ranlib commands with the appropriate llvm-ar command + e.Replace(AR=cheerp_bin_path + "/llvm-ar", + RANLIB=cheerp_bin_path + "/llvm-ar s") + + # Replace the output filename with the appropriate extension + e.Replace(PROGNAME="program.bc") + diff --git a/engine/HAL/BROWSER/luos_hal.c b/engine/HAL/BROWSER/luos_hal.c new file mode 100644 index 000000000..3771e3873 --- /dev/null +++ b/engine/HAL/BROWSER/luos_hal.c @@ -0,0 +1,253 @@ +/****************************************************************************** + * @file luosHAL + * @brief Luos Hardware Abstration Layer. Describe Low layer fonction + * @Family x86/Linux/Mac + * @author Luos + * @version 0.0.0 + ******************************************************************************/ +#include "luos_hal.h" + +#include +#include +#include +#include +#include + +pthread_mutex_t mutex_msg_alloc = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t mutex_luos = PTHREAD_MUTEX_INITIALIZER; + +/******************************************************************************* + * Function + ******************************************************************************/ +static void LuosHAL_SystickInit(void); +static void LuosHAL_FlashInit(void); +static void LuosHAL_FlashEraseLuosMemoryInfo(void); + +/////////////////////////Luos Library Needed function/////////////////////////// + +/****************************************************************************** + * @brief Luos HAL general initialisation + * @param None + * @return None + ******************************************************************************/ +void LuosHAL_Init(void) +{ + { + // Systick Initialization + LuosHAL_SystickInit(); + + // Flash Initialization + LuosHAL_FlashInit(); + + // start timestamp + LuosHAL_StartTimestamp(); + } +} + +/****************************************************************************** + * @brief Luos HAL general disable IRQ + * @param None + * @return None + ******************************************************************************/ +void LuosHAL_SetIrqState(bool Enable) +{ +} + +/****************************************************************************** + * @brief Luos HAL general systick tick at 1ms initialize + * @param None + * @return tick Counter + ******************************************************************************/ +static void LuosHAL_SystickInit(void) +{ +} + +/****************************************************************************** + * @brief Luos HAL general systick tick at 1ms + * @param None + * @return tick Counter + ******************************************************************************/ +uint32_t LuosHAL_GetSystick(void) +{ + struct timespec time; + uint32_t ms; // Milliseconds + time_t s; // Seconds +#ifdef linux + clock_gettime(CLOCK_BOOTTIME, &time); +#else + clock_gettime(CLOCK_MONOTONIC, &time); +#endif + s = time.tv_sec; + ms = round(time.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds + if (ms > 999) + { + s++; + ms = 0; + } + ms += s * 1000; + return ms; +} + +/****************************************************************************** + * @brief Luos GetTimestamp + * @param None + * @return uint64_t + ******************************************************************************/ +uint64_t LuosHAL_GetTimestamp(void) +{ + struct timespec time; +#ifdef linux + clock_gettime(CLOCK_BOOTTIME, &time); +#else + clock_gettime(CLOCK_MONOTONIC, &time); +#endif + volatile uint64_t timestamp = time.tv_nsec + time.tv_sec * 1000000000; + return timestamp; +} + +/****************************************************************************** + * @brief Luos start Timestamp + * @param None + * @return None + ******************************************************************************/ +void LuosHAL_StartTimestamp(void) +{ +} + +/****************************************************************************** + * @brief Luos stop Timestamp + * @param None + * @return None + ******************************************************************************/ +void LuosHAL_StopTimestamp(void) +{ +} + +/****************************************************************************** + * @brief Flash Initialisation + * @param None + * @return None + ******************************************************************************/ +static void LuosHAL_FlashInit(void) +{ + for (uint16_t i = 0; i < FLASH_PAGE_NUMBER; i++) + { + for (uint16_t j = 0; j < FLASH_PAGE_SIZE; j++) + { + stub_flash_x86[i][j] = 0; + } + } +} + +/****************************************************************************** + * @brief Erase flash page where Luos keep permanente information + * @param None + * @return None + ******************************************************************************/ +static void LuosHAL_FlashEraseLuosMemoryInfo(void) +{ +} + +/****************************************************************************** + * @brief Write flash page where Luos keep permanente information + * @param Address page / size to write / pointer to data to write + * @return + ******************************************************************************/ +void LuosHAL_FlashWriteLuosMemoryInfo(uint32_t addr, uint16_t size, uint8_t *data) +{ +} + +/****************************************************************************** + * @brief read information from page where Luos keep permanente information + * @param Address info / size to read / pointer callback data to read + * @return + ******************************************************************************/ +void LuosHAL_FlashReadLuosMemoryInfo(uint32_t addr, uint16_t size, uint8_t *data) +{ + memset(data, 0xFF, size); +} + +/****************************************************************************** + * @brief Set boot mode in shared flash memory + * @param + * @return + ******************************************************************************/ +void LuosHAL_SetMode(uint8_t mode) +{ +} + +/****************************************************************************** + * @brief Save node ID in shared flash memory + * @param Address, node_id + * @return + ******************************************************************************/ +void LuosHAL_SaveNodeID(uint16_t node_id) +{ +} + +/****************************************************************************** + * @brief software reboot the microprocessor + * @param + * @return + ******************************************************************************/ +void LuosHAL_Reboot(void) +{ +} + +#ifdef BOOTLOADER_CONFIG +/****************************************************************************** + * @brief DeInit Bootloader peripherals + * @param + * @return + ******************************************************************************/ +void LuosHAL_DeInit(void) +{ +} + +/****************************************************************************** + * @brief DeInit Bootloader peripherals + * @param + * @return + ******************************************************************************/ +typedef void (*pFunction)(void); /*!< Function pointer definition */ + +void LuosHAL_JumpToApp(uint32_t app_addr) +{ +} + +/****************************************************************************** + * @brief Return bootloader mode saved in flash + * @param + * @return + ******************************************************************************/ +uint8_t LuosHAL_GetMode(void) +{ +} + +/****************************************************************************** + * @brief Get node id saved in flash memory + * @param Address + * @return node_id + ******************************************************************************/ +uint16_t LuosHAL_GetNodeID(void) +{ +} + +/****************************************************************************** + * @brief erase sectors in flash memory + * @param Address, size + * @return + ******************************************************************************/ +void LuosHAL_EraseMemory(uint32_t address, uint16_t size) +{ +} + +/****************************************************************************** + * @brief Save binary data in shared flash memory + * @param Address, size, data[] + * @return + ******************************************************************************/ +void LuosHAL_ProgramFlash(uint32_t address, uint16_t size, uint8_t *data) +{ +} +#endif diff --git a/engine/HAL/BROWSER/luos_hal.h b/engine/HAL/BROWSER/luos_hal.h new file mode 100644 index 000000000..048a0b86f --- /dev/null +++ b/engine/HAL/BROWSER/luos_hal.h @@ -0,0 +1,59 @@ +/****************************************************************************** + * @file luosHAL + * @brief Luos Hardware Abstration Layer. Describe Low layer fonction + * @Family x86/Linux/Mac + * @author Luos + * @version 0.0.0 + ******************************************************************************/ +#ifndef _LUOSHAL_H_ +#define _LUOSHAL_H_ + +#include +#include +#include "luos_hal_config.h" + +/******************************************************************************* + * Definitions + ******************************************************************************/ +#ifndef _CRITICAL + #define _CRITICAL +#endif + +#define LUOS_UUID ((uint32_t *)0x00000001) + +#define ADDRESS_ALIASES_FLASH ADDRESS_LAST_PAGE_FLASH +#define ADDRESS_BOOT_FLAG_FLASH (ADDRESS_LAST_PAGE_FLASH + PAGE_SIZE) - 4 + +/******************************************************************************* + * Variables + ******************************************************************************/ + +/******************************************************************************* + * Function + ******************************************************************************/ +void LuosHAL_Init(void); +void LuosHAL_SetIrqState(bool Enable); +uint32_t LuosHAL_GetSystick(void); +void LuosHAL_FlashWriteLuosMemoryInfo(uint32_t addr, uint16_t size, uint8_t *data); +void LuosHAL_FlashReadLuosMemoryInfo(uint32_t addr, uint16_t size, uint8_t *data); + +// bootloader functions +void LuosHAL_SetMode(uint8_t mode); +void LuosHAL_Reboot(void); +void LuosHAL_SaveNodeID(uint16_t); + +#ifdef BOOTLOADER_CONFIG +void LuosHAL_DeInit(void); +void LuosHAL_JumpToApp(uint32_t); +uint8_t LuosHAL_GetMode(void); +uint16_t LuosHAL_GetNodeID(void); +void LuosHAL_EraseMemory(uint32_t, uint16_t); +void LuosHAL_ProgramFlash(uint32_t, uint16_t, uint8_t *); +#endif + +// timestamp functions +uint64_t LuosHAL_GetTimestamp(void); +void LuosHAL_StartTimestamp(void); +void LuosHAL_StopTimestamp(void); + +#endif /* _LUOSHAL_H_ */ diff --git a/engine/HAL/BROWSER/luos_hal_config.h b/engine/HAL/BROWSER/luos_hal_config.h new file mode 100644 index 000000000..f2b7929ff --- /dev/null +++ b/engine/HAL/BROWSER/luos_hal_config.h @@ -0,0 +1,74 @@ +/****************************************************************************** + * @file luosHAL_Config + * @brief This file allow you to configure LuosHAL according to your design + * this is the default configuration created by Luos team for this MCU Family + * Do not modify this file if you want to ovewrite change define in you project + * @Family x86 + * @author Luos + * @version 0.0.0 + ******************************************************************************/ +#ifndef _LUOSHAL_CONFIG_H_ +#define _LUOSHAL_CONFIG_H_ + +#ifndef MCUFREQ + #define MCUFREQ 100000000 // MCU frequence +#endif + +/******************************************************************************* + * DEFINE STUB FLASH FOR X86 + ******************************************************************************/ +#ifndef FLASH_PAGE_SIZE + #define FLASH_PAGE_SIZE 0x100 +#endif +#ifndef FLASH_PAGE_NUMBER + #define FLASH_PAGE_NUMBER 8 +#endif +static uint32_t stub_flash_x86[FLASH_PAGE_NUMBER][FLASH_PAGE_SIZE]; +static uint32_t *last_page_stub_flash_x86 = &stub_flash_x86[FLASH_PAGE_NUMBER - 1][FLASH_PAGE_SIZE]; + +/******************************************************************************* + * DEFINE THREAD MUTEX LOCKING AND UNLOCKING FUNCTIONS + ******************************************************************************/ +#include +extern pthread_mutex_t mutex_msg_alloc; +extern pthread_mutex_t mutex_luos; + +#ifndef MSGALLOC_MUTEX_LOCK + #define MSGALLOC_MUTEX_LOCK pthread_mutex_lock(&mutex_msg_alloc); +#endif +#ifndef MSGALLOC_MUTEX_UNLOCK + #define MSGALLOC_MUTEX_UNLOCK pthread_mutex_unlock(&mutex_msg_alloc); +#endif + +#ifndef LUOS_MUTEX_LOCK + #define LUOS_MUTEX_LOCK pthread_mutex_lock(&mutex_luos); +#endif +#ifndef LUOS_MUTEX_UNLOCK + #define LUOS_MUTEX_UNLOCK pthread_mutex_unlock(&mutex_luos); +#endif +/******************************************************************************* + * FLASH CONFIG + ******************************************************************************/ +#ifndef PAGE_SIZE + #define PAGE_SIZE (uint32_t) FLASH_PAGE_SIZE +#endif +#ifndef ADDRESS_LAST_PAGE_FLASH + #define ADDRESS_LAST_PAGE_FLASH (uint32_t) last_page_stub_flash_x86 +#endif + +/******************************************************************************* + * BOOTLOADER CONFIG + ******************************************************************************/ +#define FLASH_END FLASH_SIZE - 1 + +#ifndef END_ERASE_BOOTLOADER + #define END_ERASE_BOOTLOADER (uint32_t)0x08020000 +#endif +#ifndef SHARED_MEMORY_ADDRESS + #define SHARED_MEMORY_ADDRESS (uint32_t)0x0801F800 +#endif +#ifndef APP_ADDRESS + #define APP_ADDRESS (uint32_t)0x0800C800 +#endif + +#endif /* _LUOSHAL_CONFIG_H_ */ diff --git a/examples/projects/browser/led/README.md b/examples/projects/browser/led/README.md new file mode 100644 index 000000000..bcdcb1452 --- /dev/null +++ b/examples/projects/browser/led/README.md @@ -0,0 +1,38 @@ +Luos logo + +![](https://github.com/Luos-io/luos_engine/actions/workflows/build.yml/badge.svg) +[![](https://img.shields.io/github/license/Luos-io/Luos)](https://github.com/Luos-io/luos_engine/blob/master/LICENSE) + +[![](https://img.shields.io/badge/Luos-Documentation-34A3B4)](https://www.luos.io/docs/) +[![](http://certified.luos.io)](https://luos.io) +[![PlatformIO Registry](https://badges.registry.platformio.org/packages/luos/library/luos_engine.svg)](https://registry.platformio.org/libraries/luos_engine/luos_engine) + +[![](https://img.shields.io/discord/902486791658041364?label=Discord&logo=discord&style=social)](http://bit.ly/JoinLuosDiscord) +[![](https://img.shields.io/reddit/subreddit-subscribers/Luos?style=social)](https://www.reddit.com/r/Luos) +[![](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Unleash%20electronic%20devices%20as%20microservices%20thanks%20to%20Luos&https://luos.io&via=Luos_io&hashtags=embeddedsystems,electronics,microservices,api) +[![](https://img.shields.io/badge/LinkedIn-Share-0077B5?style=social&logo=linkedin)](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fgithub.com%2Fluos-io) + +# Led project example :bulb: + +This project demonstrate how to make and use a simple button through Luos. Feel free to use electronics and code example as you want. + +## How to compile the code :computer: + +1. Install [Sheerp](https://labs.leaningtech.com/cheerp/installation/windows-macos) +2. Download and install [Platformio](https://platformio.org/platformio-ide) +3. Open this folder into Platformio +4. Build (Platformio will do the rest) + +## How to open the electronic design :electric_plug: + +You can open [a working example electronic design](https://github.com/Luos-io/luos_engine/tree/main/examples/hardware) with Kicad. This design use Luos_components library for more information to install and use it read [our doc](https://www.luos.io/docs/). + +## Linked driver + +This project is linked to the [Button driver](../../Drivers/button). + +## Don't hesitate to read [our documentation](https://www.luos.io/docs/), or to post your questions/issues on the [Luos' Forum](https://community.luos.io). :books: + +[![](https://img.shields.io/discourse/topics?server=https%3A%2F%2Fcommunity.luos.io&logo=Discourse)](https://community.luos.io) +[![](https://img.shields.io/badge/Luos-Documentation-34A3B4)](https://www.luos.io/docs/) +[![](https://img.shields.io/badge/LinkedIn-Follow%20us-0077B5?style=flat&logo=linkedin)](https://www.linkedin.com/company/luos) diff --git a/examples/projects/browser/led/lib/Led/README.md b/examples/projects/browser/led/lib/Led/README.md new file mode 100644 index 000000000..12168b996 --- /dev/null +++ b/examples/projects/browser/led/lib/Led/README.md @@ -0,0 +1,18 @@ +Luos logo + +[![](http://certified.luos.io)](https://luos.io) +[![](https://img.shields.io/github/license/Luos-io/Examples)]( +https://github.com/Luos-io/Examples/blob/master/LICENSE) + +[![](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Unleash%20electronic%20devices%20as%20microservices%20thanks%20to%20Luos&https://luos.io&via=Luos_io&hashtags=embeddedsystems,electronics,microservices,api) +[![](https://img.shields.io/badge/LinkedIn-Share-0077B5?style=social&logo=linkedin)](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fgithub.com%2Fluos-io) + +# LED driver +Driver for using an RGB LED in your projects with Luos. + +# Link project +This driver is linked to the [LED project](../../Projects/Led). + +[![](https://img.shields.io/discourse/topics?server=https%3A%2F%2Fcommunity.luos.io&logo=Discourse)](https://community.luos.io) +[![](https://img.shields.io/badge/Luos-Documentation-34A3B4)](https://www.luos.io/docs/) +[![](https://img.shields.io/badge/LinkedIn-Follow%20us-0077B5?style=flat&logo=linkedin)](https://www.linkedin.com/company/luos) diff --git a/examples/projects/browser/led/lib/Led/led.c b/examples/projects/browser/led/lib/Led/led.c new file mode 100644 index 000000000..5b7fa2ff6 --- /dev/null +++ b/examples/projects/browser/led/lib/Led/led.c @@ -0,0 +1,129 @@ +/****************************************************************************** + * @file Led + * @brief driver example a simple Led + * @author Luos + * @version 0.0.0 + ******************************************************************************/ +#include +#include +#ifdef _WIN32 + #include +#else + #include +#endif +#include "led.h" +#include + +/******************************************************************************* + * Definitions + ******************************************************************************/ + +/******************************************************************************* + * Variables + ******************************************************************************/ +uint8_t Led_last_state = 0; +const char led_ON[768] = " \\ | /\n" + " ___________________________.-.________________\n" + " / - |***| - /;\n" + " / ________________ - |***| - //\n" + " / / /; [` - ') //\n" + " / / MCU // `---' //\n" + " / / // | | //\n" + " / /______________ // //\n" + " / '---------------' //\n" + " / //\n" + "/_____________________________________________//\n" + "`---------------------------------------------'\n"; + +const char led_OFF[768] = "\n" + " ___________________________.-.________________\n" + " / | | /;\n" + " / ________________ | | //\n" + " / / /; [` - ') //\n" + " / / MCU // `---' //\n" + " / / // | | //\n" + " / /______________ // //\n" + " / '---------------' //\n" + " / //\n" + "/_____________________________________________//\n" + "`---------------------------------------------'\n"; + +/******************************************************************************* + * Function + ******************************************************************************/ +static void Led_MsgHandler(service_t *service, const msg_t *msg); + +void clear_screen(void) +{ +#ifdef _WIN32 + system("cls"); +#else + // Assume POSIX + system("clear"); +#endif +} + +/****************************************************************************** + * @brief init must be call in project init + * @param None + * @return None + ******************************************************************************/ +void Led_Init(void) +{ + revision_t revision = {.major = 1, .minor = 0, .build = 0}; + Luos_CreateService(Led_MsgHandler, STATE_TYPE, "led", revision); + clear_screen(); + printf("LED service running.\n\n"); + printf(led_OFF); +} + +/****************************************************************************** + * @brief loop must be call in project loop + * @param None + * @return None + ******************************************************************************/ +void Led_Loop(void) {} + +/****************************************************************************** + * @brief Msg manager callback when a msg receive for this service + * @param service destination + * @param Msg receive + * @return None + ******************************************************************************/ +static void Led_MsgHandler(service_t *service, const msg_t *msg) +{ + if (msg->header.cmd == IO_STATE) + { + if (msg->data[0] != Led_last_state) + { + Led_last_state = msg->data[0]; + clear_screen(); + printf("LED service running.\n\n"); + if (Led_last_state == LED_OFF) + { + printf(led_OFF); + } + else if (Led_last_state == LED_ON) + { + printf(led_ON); + } + else + { + printf("[UNKWNOWN STATE] LED can only be ON or OFF...\n"); + } + } + else if (msg->data[0] == LED_ON) + { + + clear_screen(); + printf("LED service running. => LED is already ON\n\n"); + printf(led_ON); + } + else if (msg->data[0] == LED_OFF) + { + clear_screen(); + printf("LED service running. => LED is already OFF\n\n"); + printf(led_OFF); + } + } +} diff --git a/examples/projects/browser/led/lib/Led/led.h b/examples/projects/browser/led/lib/Led/led.h new file mode 100644 index 000000000..5efb98688 --- /dev/null +++ b/examples/projects/browser/led/lib/Led/led.h @@ -0,0 +1,28 @@ +/****************************************************************************** + * @file Led + * @brief driver example a simple Led + * @author Luos + * @version 0.0.0 + ******************************************************************************/ +#ifndef LED_H +#define LED_H + +#include "luos_engine.h" + +/******************************************************************************* + * Definitions + ******************************************************************************/ +#define LED_OFF 0 +#define LED_ON 1 + +/******************************************************************************* + * Variables + ******************************************************************************/ + +/******************************************************************************* + * Function + ******************************************************************************/ +void Led_Init(void); +void Led_Loop(void); + +#endif /* LED_H */ diff --git a/examples/projects/browser/led/lib/Led/library.json b/examples/projects/browser/led/lib/Led/library.json new file mode 100644 index 000000000..53d03f7ca --- /dev/null +++ b/examples/projects/browser/led/lib/Led/library.json @@ -0,0 +1,14 @@ +{ + "name": "led", + "keywords": "robus,network,microservice,luos,operating system,os,embedded,communication,container,ST", + "description": "a simple button driver", + "version": "0.7.0", + "authors": { + "name": "Luos", + "url": "https://luos.io" + }, + "licence": "MIT", + "dependencies": { + "luos_engine": "^3.1.0" + } +} diff --git a/examples/projects/browser/led/node_config.h b/examples/projects/browser/led/node_config.h new file mode 100644 index 000000000..721108be8 --- /dev/null +++ b/examples/projects/browser/led/node_config.h @@ -0,0 +1,96 @@ +/****************************************************************************** + * @file node_config.h + * @brief This file allow you to use standard preprocessor definitions to + * configure your project, Luos and Luos HAL libraries + * + * # Introduction + * This file is for the luos user. You may here configure your project and + * define your custom Luos service and custom Luos command for your product + * + * Luos libraries offer a minimal standard configuration to optimize + * memory usage. In some case you have to modify standard value to fit + * with your need concerning among of data transiting through the network + * or network speed for example + * + * Luos libraries can be use with a lot a MCU family. Luos compagny give you + * a default configuration, for specific MCU family, in robus_hal_config.h. + * This configuration can be modify here to fit with you design by + * preprocessor definitions of MCU Hardware needs + * + * # Usage + * This file should be place a the root folder of your project and include + * where build flag preprocessor definitions are define in your IDE + * -include node_config.h + * + * @author Luos + * @version 0.0.0 + ******************************************************************************/ +#ifndef _NODE_CONFIG_H_ +#define _NODE_CONFIG_H_ + +/******************************************************************************* + * PROJECT DEFINITION + *******************************************************************************/ + +/******************************************************************************* + * LUOS LIBRARY DEFINITION + ******************************************************************************* + * Define | Default Value | Description + * :---------------------|------------------------------------------------------ + * MAX_LOCAL_SERVICE_NUMBER | 5 | Service number in the node + * MAX_NODE_NUMBER. | 20 | Node number in the device + * MSG_BUFFER_SIZE | 3*SIZE_MSG_MAX (405 Bytes) | Size in byte of the Luos buffer TX and RX + * MAX_MSG_NB | 2*MAX_LOCAL_SERVICE_NUMBER | Message number in Luos buffer + * NBR_PORT | 2 | PTP Branch number Max 8 + * NBR_RETRY | 10 | Send Retry number in case of NACK or collision + ******************************************************************************/ +#define MAX_LOCAL_SERVICE_NUMBER 1 +#define MAX_LOCAL_PROFILE_NUMBER 1 +#define MAX_MSG_NB 10 + +/******************************************************************************* + * LUOS HAL LIBRARY DEFINITION +******************************************************************************* + * Define | Description + * :-----------------------|----------------------------------------------- + * MCUFREQ | Put your the MCU frequency (value in Hz) + * TIMERDIV | Timer divider clock (see your clock configuration) + * USE_CRC_HW | define to 0 if there is no Module CRC in your MCU + * USE_TX_IT | define to 1 to not use DMA transfers for Luos Tx + * + * PORT_CLOCK_ENABLE | Enable clock for port + * PTPx | A,B,C,D etc. PTP Branch Pin/Port/IRQ + * TX_LOCK_DETECT | Disable by default use if not busy flag in USART Pin/Port/IRQ + * RX_EN | Rx enable for driver RS485 always on Pin/Port + * TX_EN | Tx enable for driver RS485 Pin/Port + * COM_TX | Tx USART Com Pin/Port/Alternate + * COM_RX | Rx USART Com Pin/Port/Alternate + * PINOUT_IRQHANDLER | Callback function for Pin IRQ handler + + * ROBUS_COM_CLOCK_ENABLE | Enable clock for USART + * ROBUS_COM | USART number + * ROBUS_COM_IRQ | USART IRQ number + * ROBUS_COM_IRQHANDLER | Callback function for USART IRQ handler + + * ROBUS_DMA_CLOCK_ENABLE | Enable clock for DMA + * ROBUS_DMA | DMA number + * ROBUS_DMA_CHANNEL | DMA channel (depending on MCU DMA may need special config) + + * ROBUS_TIMER_CLOCK_ENABLE | Enable clock for Timer + * ROBUS_TIMER | Timer number + * ROBUS_TIMER_IRQ | Timer IRQ number + * ROBUS_TIMER_IRQHANDLER | Callback function for Timer IRQ handler +******************************************************************************/ + +/******************************************************************************* + * FLASH CONFIGURATION FOR APP WITH BOOTLOADER + ******************************************************************************** + * Define | Default Value | Description + * :---------------------|------------------------------------------------------ + * BOOT_START_ADDRESS | FLASH_BASE = 0x8000000 | Start address of Bootloader in flash + * SHARED_MEMORY_ADDRESS | 0x0800C000 | Start address of shared memory to save boot flag + * APP_START_ADDRESS | 0x0800C800 | Start address of application with bootloader + * APP_END_ADDRESS | FLASH_BANK1_END=0x0801FFFF | End address of application with bootloader + ******************************************************************************/ + +#endif /* _NODE_CONFIG_H_ */ diff --git a/examples/projects/browser/led/platformio.ini b/examples/projects/browser/led/platformio.ini new file mode 100644 index 000000000..17e899950 --- /dev/null +++ b/examples/projects/browser/led/platformio.ini @@ -0,0 +1,28 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html +[platformio] + +[env:browser] +lib_ldf_mode =off +lib_extra_dirs = + $PROJECT_DIR/../../../../../ + $PROJECT_DIR/../../../../network/ +platform = native +lib_deps = + Led + ws_network + luos_engine@^3.1.0 +build_unflags = -Os +build_flags = + -I inc + -include node_config.h + -O3 + -D LUOSHAL=BROWSER + -D WSHAL=BROWSER diff --git a/examples/projects/browser/led/src/main.c b/examples/projects/browser/led/src/main.c new file mode 100644 index 000000000..623636771 --- /dev/null +++ b/examples/projects/browser/led/src/main.c @@ -0,0 +1,16 @@ +#include "luos_engine.h" +// #include "ws_network.h" +#include "led.h" + +int main(void) +{ + Luos_Init(); + // Ws_Init(); + Led_Init(); + while (1) + { + Luos_Loop(); + // Ws_Loop(); + Led_Loop(); + } +} diff --git a/network/ws_network/HAL/ARDUINO/hal_script.py b/network/ws_network/HAL/ARDUINO/hal_script.py new file mode 100644 index 000000000..bdcb3594b --- /dev/null +++ b/network/ws_network/HAL/ARDUINO/hal_script.py @@ -0,0 +1,8 @@ +# Import("env") +# platform = env.PioPlatform() +# libs=env.GetProjectOption("lib_deps") +# libs.append("u0078867/Arduino-Websocket-Fast@^1.0.0") +# print(libs) +# env_section = "env:" + env["PIOENV"] +# platform.config.set(env_section, "lib_deps", libs) +# print("ending hal_script.py") diff --git a/network/ws_network/HAL/BROWSER/ws_hal.c b/network/ws_network/HAL/BROWSER/ws_hal.c new file mode 100644 index 000000000..171b87ef0 --- /dev/null +++ b/network/ws_network/HAL/BROWSER/ws_hal.c @@ -0,0 +1,60 @@ +/****************************************************************************** + * @file robusHAL + * @brief Robus Hardware Abstration Layer. Describe Low layer fonction + * @Family x86/Linux/Mac + * @author Luos + * @version 0.0.0 + ******************************************************************************/ +#include "ws_hal.h" +#include "_ws_network.h" +#include "luos_utils.h" + +/******************************************************************************* + * Definitions + ******************************************************************************/ + +/******************************************************************************* + * Variables + ******************************************************************************/ + +static const char *s_url = WS_NETWORK_BROKER_ADDR; + +/******************************************************************************* + * Function + ******************************************************************************/ + +/////////////////////////Luos Library Needed function/////////////////////////// + +/****************************************************************************** + * @brief Luos HAL general initialisation + * @param None + * @return None + ******************************************************************************/ +void WsHAL_Init(void) +{ + + // Create a client WebSocket instance to s_url +} + +/****************************************************************************** + * @brief Luos HAL general loop + * @param None + * @return None + ******************************************************************************/ +void WsHAL_Loop(void) +{ + // Get the messages and put them on the reception function + // Ws_Reception(uint8_t *msg, (uint32_t)messageSize); + // If you have a callback you can create a callback instead of this function and call it here +} + +/****************************************************************************** + * @brief Transmit data + * @param data to send + * @param size of data to send + * @return None + ******************************************************************************/ +void WsHAL_Send(const uint8_t *data, uint16_t size) +{ + // Send data to the websocket in binary mode +} diff --git a/network/ws_network/HAL/BROWSER/ws_hal.h b/network/ws_network/HAL/BROWSER/ws_hal.h new file mode 100644 index 000000000..255bd800b --- /dev/null +++ b/network/ws_network/HAL/BROWSER/ws_hal.h @@ -0,0 +1,23 @@ +/****************************************************************************** + * @file ws_hal + * @brief Websocket Hardware Abstration Layer. Describe Low layer fonctions + * @author Luos + * @version 0.0.0 + ******************************************************************************/ +#ifndef _WSHAL_H_ +#define _WSHAL_H_ + +#include + +/******************************************************************************* + * Variables + ******************************************************************************/ + +/******************************************************************************* + * Function + ******************************************************************************/ +void WsHAL_Init(void); // Init the Websocket communication +void WsHAL_Loop(void); // Do your loop stuff if needed +void WsHAL_Send(const uint8_t *data, uint16_t size); // Send data + +#endif /* _WSHAL_H_ */ diff --git a/network/ws_network/HAL/BROWSER/ws_hal_config.h b/network/ws_network/HAL/BROWSER/ws_hal_config.h new file mode 100644 index 000000000..75d858273 --- /dev/null +++ b/network/ws_network/HAL/BROWSER/ws_hal_config.h @@ -0,0 +1,12 @@ +/****************************************************************************** + * @file ws_HAL_config + * @brief This file allow you to configure ws HAL according to your system + * this is the default configuration created by Luos team for this target + * Do not modify this file if you want to ovewrite change define it in your project node_config.h + * @author Luos + * @version 0.0.0 + ******************************************************************************/ +#ifndef _WS_CONFIG_H_ +#define _WS_CONFIG_H_ + +#endif /* _WS_CONFIG_H_ */ From 81f1379302347b01cec684577348403df2f9d31c Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Tue, 23 Jan 2024 14:09:11 +0100 Subject: [PATCH 02/12] Compile Luos_engine as a wasm lib --- engine/HAL/BROWSER/hal_script.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/engine/HAL/BROWSER/hal_script.py b/engine/HAL/BROWSER/hal_script.py index 0aad8b622..10e7837f1 100644 --- a/engine/HAL/BROWSER/hal_script.py +++ b/engine/HAL/BROWSER/hal_script.py @@ -8,6 +8,20 @@ # Global env, use it if you call this script from a library.json file genv = DefaultEnvironment() + +def shared_lib(source, target, env): + # Try to find the luos_engine.a somwhere on $BUILD_DIR/*/ archive and save it to a libPath variable + libPath = None + for root, dirs, files in os.walk(env.subst("$BUILD_DIR")): + for file in files: + if file.endswith("luos_engine.a"): + libPath = os.path.join(root, file) + break + if libPath is not None: + break + # Convert the luos_engine.a archive to a wasm shared library + env.Execute("gcc -O2 -o $BUILD_DIR/libluos_engine.bc " + libPath) + # Get the cheerp bin folder cheerp_bin_path = None if platform.system() == 'Windows': @@ -51,3 +65,6 @@ # Replace the output filename with the appropriate extension e.Replace(PROGNAME="program.bc") +# Add the shared_lib callback to the buildprog post action +print("Adding the shared_lib callback to the buildprog post action") +genv.AddPostAction("$PROGPATH", shared_lib) From 6a73658faf79b95ab0643c2c347c3092ae7e1293 Mon Sep 17 00:00:00 2001 From: Benjamin Christau Date: Tue, 23 Jan 2024 17:28:11 +0100 Subject: [PATCH 03/12] Update Luos_engine HAL script --- engine/HAL/BROWSER/hal_script.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/engine/HAL/BROWSER/hal_script.py b/engine/HAL/BROWSER/hal_script.py index 10e7837f1..e75c770d3 100644 --- a/engine/HAL/BROWSER/hal_script.py +++ b/engine/HAL/BROWSER/hal_script.py @@ -20,7 +20,7 @@ def shared_lib(source, target, env): if libPath is not None: break # Convert the luos_engine.a archive to a wasm shared library - env.Execute("gcc -O2 -o $BUILD_DIR/libluos_engine.bc " + libPath) + env.Execute("gcc -cheerp-pretty-code -O2 -o $BUILD_DIR/libluos_engine.js " + libPath) # Get the cheerp bin folder cheerp_bin_path = None @@ -56,14 +56,14 @@ def shared_lib(source, target, env): e.Append(CCFLAGS=["--target=cheerp-wasm"]) # Add the cheerp-wasm target to the linker flags - e.Append(LINKFLAGS=["--target=cheerp-wasm"]) + e.Append(LINKFLAGS=["--target=cheerp-wasm", "-cheerp-pretty-code"]) # Replace the ar and ranlib commands with the appropriate llvm-ar command e.Replace(AR=cheerp_bin_path + "/llvm-ar", RANLIB=cheerp_bin_path + "/llvm-ar s") # Replace the output filename with the appropriate extension - e.Replace(PROGNAME="program.bc") + e.Replace(PROGNAME="program.js") # Add the shared_lib callback to the buildprog post action print("Adding the shared_lib callback to the buildprog post action") From 65e6f00586beb587787d8791b2a818fdba788614 Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Tue, 23 Jan 2024 17:44:15 +0100 Subject: [PATCH 04/12] Expose packages functions --- engine/HAL/BROWSER/hal_script.py | 25 +++++++++------------ engine/core/inc/luos_utils.h | 4 ++++ engine/core/src/luos_engine.c | 4 ++-- examples/projects/browser/led/lib/Led/led.c | 4 ++-- network/ws_network/src/ws_network.c | 4 ++-- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/engine/HAL/BROWSER/hal_script.py b/engine/HAL/BROWSER/hal_script.py index e75c770d3..d74718180 100644 --- a/engine/HAL/BROWSER/hal_script.py +++ b/engine/HAL/BROWSER/hal_script.py @@ -8,7 +8,6 @@ # Global env, use it if you call this script from a library.json file genv = DefaultEnvironment() - def shared_lib(source, target, env): # Try to find the luos_engine.a somwhere on $BUILD_DIR/*/ archive and save it to a libPath variable libPath = None @@ -31,18 +30,17 @@ def shared_lib(source, target, env): if platform.system() == 'Darwin': cheerp_bin_path = '/Applications/cheerp/bin' -if platform.system() == 'Darwin': - # Create a symlink to Cheerp clang++ in gcc - try: - os.symlink(cheerp_bin_path + '/clang++', cheerp_bin_path + '/gcc') - except FileExistsError: - pass +# Create a symlink to Cheerp clang++ in gcc +try: + os.symlink(cheerp_bin_path + '/clang++', cheerp_bin_path + '/gcc') +except FileExistsError: + pass - # Create a symlink to Cheerp clang++ in g++ - try: - os.symlink(cheerp_bin_path + '/clang++', cheerp_bin_path + '/g++') - except FileExistsError: - pass +# Create a symlink to Cheerp clang++ in g++ +try: + os.symlink(cheerp_bin_path + '/clang++', cheerp_bin_path + '/g++') +except FileExistsError: + pass # Add the cheerp fake gcc path at the beginning of the PATH environment variable than platformio will use it by default current_path = os.environ.get('PATH', '') @@ -53,7 +51,7 @@ def shared_lib(source, target, env): for e in [env, genv]: # Add the cheerp-wasm target to the compiler flags - e.Append(CCFLAGS=["--target=cheerp-wasm"]) + e.Append(CCFLAGS=["--target=cheerp-wasm", "-Wno-cheerp", "-DPUBLIC=__attribute__\(\(cheerp_jsexport\)\)"]) # Add the cheerp-wasm target to the linker flags e.Append(LINKFLAGS=["--target=cheerp-wasm", "-cheerp-pretty-code"]) @@ -66,5 +64,4 @@ def shared_lib(source, target, env): e.Replace(PROGNAME="program.js") # Add the shared_lib callback to the buildprog post action -print("Adding the shared_lib callback to the buildprog post action") genv.AddPostAction("$PROGPATH", shared_lib) diff --git a/engine/core/inc/luos_utils.h b/engine/core/inc/luos_utils.h index aab906f2e..1127359b8 100644 --- a/engine/core/inc/luos_utils.h +++ b/engine/core/inc/luos_utils.h @@ -22,6 +22,10 @@ #define LUOS_ASSERT(expr) #endif +#ifndef PUBLIC + #define PUBLIC +#endif + /* This structure is used to manage node assertion informations */ typedef struct __attribute__((__packed__)) diff --git a/engine/core/src/luos_engine.c b/engine/core/src/luos_engine.c index b90444dae..7fc9dabd6 100644 --- a/engine/core/src/luos_engine.c +++ b/engine/core/src/luos_engine.c @@ -34,7 +34,7 @@ static inline void Luos_PackageLoop(void); * @param None * @return None ******************************************************************************/ -void Luos_Init(void) +PUBLIC void Luos_Init(void) { Service_Init(); Node_Init(); @@ -56,7 +56,7 @@ void Luos_Init(void) * @param None * @return None ******************************************************************************/ -void Luos_Loop(void) +PUBLIC void Luos_Loop(void) { static uint32_t last_loop_date; phy_job_t *job = NULL; diff --git a/examples/projects/browser/led/lib/Led/led.c b/examples/projects/browser/led/lib/Led/led.c index 5b7fa2ff6..dfb37daf7 100644 --- a/examples/projects/browser/led/lib/Led/led.c +++ b/examples/projects/browser/led/lib/Led/led.c @@ -68,7 +68,7 @@ void clear_screen(void) * @param None * @return None ******************************************************************************/ -void Led_Init(void) +PUBLIC void Led_Init(void) { revision_t revision = {.major = 1, .minor = 0, .build = 0}; Luos_CreateService(Led_MsgHandler, STATE_TYPE, "led", revision); @@ -82,7 +82,7 @@ void Led_Init(void) * @param None * @return None ******************************************************************************/ -void Led_Loop(void) {} +PUBLIC void Led_Loop(void) {} /****************************************************************************** * @brief Msg manager callback when a msg receive for this service diff --git a/network/ws_network/src/ws_network.c b/network/ws_network/src/ws_network.c index 8c3bd8a02..aa514dea1 100644 --- a/network/ws_network/src/ws_network.c +++ b/network/ws_network/src/ws_network.c @@ -92,7 +92,7 @@ volatile wait_ack_t ping_status = INACTIVE; // This flag indicate the status of * @param None * @return None ******************************************************************************/ -void Ws_Init(void) +PUBLIC void Ws_Init(void) { // Instantiate the phy struct phy_ws = Phy_Create(Ws_JobHandler, Ws_RunTopology, Ws_Reset); @@ -117,7 +117,7 @@ void Ws_Reset(luos_phy_t *phy_ptr) * @param None * @return None ******************************************************************************/ -void Ws_Loop(void) +PUBLIC void Ws_Loop(void) { WsHAL_Loop(); } From 68358214dc1fb77b2b3b4360e449f93323e6575c Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Tue, 23 Jan 2024 19:04:14 +0100 Subject: [PATCH 05/12] Remove the main from the wasm example --- examples/projects/browser/led/src/main.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/examples/projects/browser/led/src/main.c b/examples/projects/browser/led/src/main.c index 623636771..e82436004 100644 --- a/examples/projects/browser/led/src/main.c +++ b/examples/projects/browser/led/src/main.c @@ -1,16 +1 @@ -#include "luos_engine.h" -// #include "ws_network.h" -#include "led.h" - -int main(void) -{ - Luos_Init(); - // Ws_Init(); - Led_Init(); - while (1) - { - Luos_Loop(); - // Ws_Loop(); - Led_Loop(); - } -} +// This file is only used for compilation purpose, you can use the compiled packages using JS instead. From 645a3d1ad58795abbc92c127054a8429904b30f1 Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Wed, 24 Jan 2024 10:31:12 +0100 Subject: [PATCH 06/12] WS HAL for BROWSER move to CPP --- .../ws_network/HAL/BROWSER/{ws_hal.c => ws_hal.cpp} | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) rename network/ws_network/HAL/BROWSER/{ws_hal.c => ws_hal.cpp} (93%) diff --git a/network/ws_network/HAL/BROWSER/ws_hal.c b/network/ws_network/HAL/BROWSER/ws_hal.cpp similarity index 93% rename from network/ws_network/HAL/BROWSER/ws_hal.c rename to network/ws_network/HAL/BROWSER/ws_hal.cpp index 171b87ef0..4d0c9fb34 100644 --- a/network/ws_network/HAL/BROWSER/ws_hal.c +++ b/network/ws_network/HAL/BROWSER/ws_hal.cpp @@ -1,14 +1,20 @@ /****************************************************************************** - * @file robusHAL - * @brief Robus Hardware Abstration Layer. Describe Low layer fonction + * @file wsHAL + * @brief WebSocket Hardware Abstration Layer. Describe Low layer fonction * @Family x86/Linux/Mac * @author Luos * @version 0.0.0 ******************************************************************************/ +#ifdef __cplusplus +extern "C" +{ +#endif #include "ws_hal.h" #include "_ws_network.h" #include "luos_utils.h" - +#ifdef __cplusplus +} +#endif /******************************************************************************* * Definitions ******************************************************************************/ From 309ece4da9f0f8ea6983c43963be21c5a704f33d Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Wed, 24 Jan 2024 10:37:08 +0100 Subject: [PATCH 07/12] Remove useless luos_engine shared lib compilation in the end. --- engine/HAL/BROWSER/hal_script.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/engine/HAL/BROWSER/hal_script.py b/engine/HAL/BROWSER/hal_script.py index d74718180..13af7b567 100644 --- a/engine/HAL/BROWSER/hal_script.py +++ b/engine/HAL/BROWSER/hal_script.py @@ -8,19 +8,6 @@ # Global env, use it if you call this script from a library.json file genv = DefaultEnvironment() -def shared_lib(source, target, env): - # Try to find the luos_engine.a somwhere on $BUILD_DIR/*/ archive and save it to a libPath variable - libPath = None - for root, dirs, files in os.walk(env.subst("$BUILD_DIR")): - for file in files: - if file.endswith("luos_engine.a"): - libPath = os.path.join(root, file) - break - if libPath is not None: - break - # Convert the luos_engine.a archive to a wasm shared library - env.Execute("gcc -cheerp-pretty-code -O2 -o $BUILD_DIR/libluos_engine.js " + libPath) - # Get the cheerp bin folder cheerp_bin_path = None if platform.system() == 'Windows': @@ -62,6 +49,3 @@ def shared_lib(source, target, env): # Replace the output filename with the appropriate extension e.Replace(PROGNAME="program.js") - -# Add the shared_lib callback to the buildprog post action -genv.AddPostAction("$PROGPATH", shared_lib) From 409d501fd8602789aaa671cd7d1cdb5f4ba95551 Mon Sep 17 00:00:00 2001 From: Benjamin Christau Date: Thu, 25 Jan 2024 11:45:38 +0100 Subject: [PATCH 08/12] Manage websocket in wasm --- .gitignore | 2 + engine/HAL/BROWSER/hal_script.py | 2 +- .../browser/led/lib/Led/{led.c => led.cpp} | 10 ++- .../projects/browser/led/package-lock.json | 36 ++++++++++ examples/projects/browser/led/package.json | 17 +++++ examples/projects/browser/led/platformio.ini | 1 + examples/projects/browser/led/src/main.js | 19 +++++ .../native/gate_wscom/.docker/Dockerfile | 4 +- .../projects/native/gate_wscom/platformio.ini | 2 +- network/ws_network/HAL/BROWSER/ws_hal.cpp | 71 +++++++++++++++++-- 10 files changed, 149 insertions(+), 15 deletions(-) rename examples/projects/browser/led/lib/Led/{led.c => led.cpp} (98%) create mode 100644 examples/projects/browser/led/package-lock.json create mode 100644 examples/projects/browser/led/package.json create mode 100644 examples/projects/browser/led/src/main.js diff --git a/.gitignore b/.gitignore index ab8f54688..596f449cd 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,5 @@ examples/projects/ESP32/button/sdkconfig.esp32dev examples/projects/ESP32/led/sdkconfig.esp32dev tool_services/pipe/WS/ARDUINO/arduinoWebSockets/ mongoose/ +examples/projects/browser/**/node_modules/ +*.pyc diff --git a/engine/HAL/BROWSER/hal_script.py b/engine/HAL/BROWSER/hal_script.py index 13af7b567..c0d3ec5c6 100644 --- a/engine/HAL/BROWSER/hal_script.py +++ b/engine/HAL/BROWSER/hal_script.py @@ -41,7 +41,7 @@ e.Append(CCFLAGS=["--target=cheerp-wasm", "-Wno-cheerp", "-DPUBLIC=__attribute__\(\(cheerp_jsexport\)\)"]) # Add the cheerp-wasm target to the linker flags - e.Append(LINKFLAGS=["--target=cheerp-wasm", "-cheerp-pretty-code"]) + e.Append(LINKFLAGS=["--target=cheerp-wasm", "-cheerp-make-module=commonjs", "-cheerp-pretty-code"]) # Replace the ar and ranlib commands with the appropriate llvm-ar command e.Replace(AR=cheerp_bin_path + "/llvm-ar", diff --git a/examples/projects/browser/led/lib/Led/led.c b/examples/projects/browser/led/lib/Led/led.cpp similarity index 98% rename from examples/projects/browser/led/lib/Led/led.c rename to examples/projects/browser/led/lib/Led/led.cpp index dfb37daf7..2fa3b6f2d 100644 --- a/examples/projects/browser/led/lib/Led/led.c +++ b/examples/projects/browser/led/lib/Led/led.cpp @@ -14,6 +14,8 @@ #include "led.h" #include +#include + /******************************************************************************* * Definitions ******************************************************************************/ @@ -55,12 +57,8 @@ static void Led_MsgHandler(service_t *service, const msg_t *msg); void clear_screen(void) { -#ifdef _WIN32 - system("cls"); -#else - // Assume POSIX - system("clear"); -#endif + // clear the console + printf("\x1B[2J\x1B[H"); } /****************************************************************************** diff --git a/examples/projects/browser/led/package-lock.json b/examples/projects/browser/led/package-lock.json new file mode 100644 index 000000000..bc1833cd8 --- /dev/null +++ b/examples/projects/browser/led/package-lock.json @@ -0,0 +1,36 @@ +{ + "name": "led", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "led", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "ws": "^8.16.0" + } + }, + "node_modules/ws": { + "version": "8.16.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz", + "integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + } + } +} diff --git a/examples/projects/browser/led/package.json b/examples/projects/browser/led/package.json new file mode 100644 index 000000000..04f360a0a --- /dev/null +++ b/examples/projects/browser/led/package.json @@ -0,0 +1,17 @@ +{ + "name": "led", + "version": "1.0.0", + "description": "Led project example :bulb:", + "main": "src/main.js", + "directories": { + "lib": "lib" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "ws": "^8.16.0" + } +} diff --git a/examples/projects/browser/led/platformio.ini b/examples/projects/browser/led/platformio.ini index 17e899950..3a0be9ae8 100644 --- a/examples/projects/browser/led/platformio.ini +++ b/examples/projects/browser/led/platformio.ini @@ -26,3 +26,4 @@ build_flags = -O3 -D LUOSHAL=BROWSER -D WSHAL=BROWSER + -D WS_NETWORK_BROKER_ADDR=\"ws://127.0.0.1:8000/ws\" diff --git a/examples/projects/browser/led/src/main.js b/examples/projects/browser/led/src/main.js new file mode 100644 index 000000000..e1ef7487c --- /dev/null +++ b/examples/projects/browser/led/src/main.js @@ -0,0 +1,19 @@ +const program = require('../.pio/build/browser/program'); +program + .then(({ Luos_Init, Led_Init, Luos_Loop, Led_Loop, Ws_Init, Ws_Loop }) => { + Luos_Init(); + Ws_Init(); + Led_Init(); + + const mainLoop = () => { + Luos_Loop(); + Ws_Loop(); + Led_Loop(); + }; + + return new Promise(() => setInterval(mainLoop, 0)); + }) + .catch((err) => { + console.error('Error', err); + process.exit(-1); + }); diff --git a/examples/projects/native/gate_wscom/.docker/Dockerfile b/examples/projects/native/gate_wscom/.docker/Dockerfile index a69947544..74390b2e4 100644 --- a/examples/projects/native/gate_wscom/.docker/Dockerfile +++ b/examples/projects/native/gate_wscom/.docker/Dockerfile @@ -13,7 +13,7 @@ RUN pip install --upgrade pip setuptools wheel platformio && \ rm -rf /root/.cache/pip RUN platformio run \ - --environment native \ + --environment native_ws \ -d ./examples/projects/native/gate_wscom -CMD [ "./examples/projects/native/gate_wscom/.pio/build/native/program"] +CMD [ "./examples/projects/native/gate_wscom/.pio/build/native_ws/program"] diff --git a/examples/projects/native/gate_wscom/platformio.ini b/examples/projects/native/gate_wscom/platformio.ini index b65ad3cb4..157201f26 100644 --- a/examples/projects/native/gate_wscom/platformio.ini +++ b/examples/projects/native/gate_wscom/platformio.ini @@ -64,5 +64,5 @@ build_flags = -D GATEFORMAT=TinyJSON -D PIPEMODE=WS -D PIPEHAL=native - -D PIPE_WS_SERVER_ADDR=\"ws://localhost:9342\" ; Watch out you need to escape the " using \ + -D PIPE_WS_SERVER_ADDR=\"ws://127.0.0.1:9342\" ; Watch out you need to escape the " using \ build_type = debug diff --git a/network/ws_network/HAL/BROWSER/ws_hal.cpp b/network/ws_network/HAL/BROWSER/ws_hal.cpp index 4d0c9fb34..24cdd18c3 100644 --- a/network/ws_network/HAL/BROWSER/ws_hal.cpp +++ b/network/ws_network/HAL/BROWSER/ws_hal.cpp @@ -15,6 +15,12 @@ extern "C" #ifdef __cplusplus } #endif + +#include +#include + +using namespace client; + /******************************************************************************* * Definitions ******************************************************************************/ @@ -24,11 +30,66 @@ extern "C" ******************************************************************************/ static const char *s_url = WS_NETWORK_BROKER_ADDR; +[[cheerp::genericjs]] WebSocket *clientWebsocket; /******************************************************************************* * Function ******************************************************************************/ +[[cheerp::genericjs]] void openEventCallback() +{ + console.log("Websocket opened on", s_url); +} + +[[cheerp::genericjs]] void closeEventCallback() +{ + console.log("Websocket closed."); +} + +[[cheerp::genericjs]] void errorEventCallback() +{ + console.log("Websocket error."); +} + +void WsHAL_ReceptionWeb(const std::vector &data) +{ + Ws_Reception((uint8_t *)data.data(), data.size()); +} + +[[cheerp::genericjs]] void WsHAL_SendWeb(const std::vector &data) +{ + // Convert the data to ArrayBufferView* + ArrayBufferView *arrayBufferView = cheerp::MakeTypedArray(data.data(), data.size()); + clientWebsocket->send(arrayBufferView); +} + +[[cheerp::genericjs]] void WsHAL_InitWeb() +{ + __asm__("const WebSocket = require('ws')"); + clientWebsocket = new WebSocket(s_url); + clientWebsocket->addEventListener( + "open", + cheerp::Callback(openEventCallback)); + clientWebsocket->addEventListener( + "close", + cheerp::Callback(closeEventCallback)); + clientWebsocket->addEventListener( + "error", + cheerp::Callback(errorEventCallback)); + + clientWebsocket->addEventListener( + "message", + cheerp::Callback( + [](MessageEvent *e) + { + auto *dataArray = (Uint8Array *)e->get_data(); + std::vector wasmData(dataArray->get_length()); // allocate some linear memory + Uint8Array *wasmDataArray = cheerp::MakeTypedArray(wasmData.data(), wasmData.size()); // create a Uint8Array wrapper ove the linear memory held by the vector + wasmDataArray->set(dataArray); // copy the data with the builtin set() function. A for() loop with element-wise copy would do but this is faster + WsHAL_ReceptionWeb(wasmData); // NOTE: this now takes a const std::vector& (or a std::vector if you want to pass ownership) + })); +} + /////////////////////////Luos Library Needed function/////////////////////////// /****************************************************************************** @@ -38,8 +99,8 @@ static const char *s_url = WS_NETWORK_BROKER_ADDR; ******************************************************************************/ void WsHAL_Init(void) { - // Create a client WebSocket instance to s_url + WsHAL_InitWeb(); } /****************************************************************************** @@ -49,9 +110,7 @@ void WsHAL_Init(void) ******************************************************************************/ void WsHAL_Loop(void) { - // Get the messages and put them on the reception function - // Ws_Reception(uint8_t *msg, (uint32_t)messageSize); - // If you have a callback you can create a callback instead of this function and call it here + // Nothing to do here } /****************************************************************************** @@ -62,5 +121,7 @@ void WsHAL_Loop(void) ******************************************************************************/ void WsHAL_Send(const uint8_t *data, uint16_t size) { - // Send data to the websocket in binary mode + // Convert the data and size to a std::vector + std::vector wasmData(data, data + size); + WsHAL_SendWeb(wasmData); } From ef35d08015182b0d8bd57eb369d008c8d7c3d2ef Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Tue, 13 Feb 2024 17:30:31 +0100 Subject: [PATCH 09/12] Improve ws Js dependancy management --- .gitignore | 1 - .../projects/browser/led/package-lock.json | 36 ------------------- examples/projects/browser/led/package.json | 17 --------- network/ws_network/HAL/BROWSER/hal_script.py | 7 ++++ 4 files changed, 7 insertions(+), 54 deletions(-) delete mode 100644 examples/projects/browser/led/package-lock.json delete mode 100644 examples/projects/browser/led/package.json create mode 100644 network/ws_network/HAL/BROWSER/hal_script.py diff --git a/.gitignore b/.gitignore index 596f449cd..35e2b0226 100644 --- a/.gitignore +++ b/.gitignore @@ -39,5 +39,4 @@ examples/projects/ESP32/button/sdkconfig.esp32dev examples/projects/ESP32/led/sdkconfig.esp32dev tool_services/pipe/WS/ARDUINO/arduinoWebSockets/ mongoose/ -examples/projects/browser/**/node_modules/ *.pyc diff --git a/examples/projects/browser/led/package-lock.json b/examples/projects/browser/led/package-lock.json deleted file mode 100644 index bc1833cd8..000000000 --- a/examples/projects/browser/led/package-lock.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "name": "led", - "version": "1.0.0", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "led", - "version": "1.0.0", - "license": "ISC", - "dependencies": { - "ws": "^8.16.0" - } - }, - "node_modules/ws": { - "version": "8.16.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz", - "integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==", - "engines": { - "node": ">=10.0.0" - }, - "peerDependencies": { - "bufferutil": "^4.0.1", - "utf-8-validate": ">=5.0.2" - }, - "peerDependenciesMeta": { - "bufferutil": { - "optional": true - }, - "utf-8-validate": { - "optional": true - } - } - } - } -} diff --git a/examples/projects/browser/led/package.json b/examples/projects/browser/led/package.json deleted file mode 100644 index 04f360a0a..000000000 --- a/examples/projects/browser/led/package.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "led", - "version": "1.0.0", - "description": "Led project example :bulb:", - "main": "src/main.js", - "directories": { - "lib": "lib" - }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "dependencies": { - "ws": "^8.16.0" - } -} diff --git a/network/ws_network/HAL/BROWSER/hal_script.py b/network/ws_network/HAL/BROWSER/hal_script.py new file mode 100644 index 000000000..2918db096 --- /dev/null +++ b/network/ws_network/HAL/BROWSER/hal_script.py @@ -0,0 +1,7 @@ +#!/usr/bin/python +# This sript is used to import network dependancies + +# Install ws Js dependancy using `npm install ws` +Import("env") + +env.Execute("npm install --prefix $BUILD_DIR ws") From d5a4db3e01569dd8127d8bd3e85704ec74be9bc5 Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Tue, 13 Feb 2024 17:58:27 +0100 Subject: [PATCH 10/12] enable execution on browser --- engine/HAL/BROWSER/hal_script.py | 4 +- examples/projects/browser/led/src/main.html | 11 +++++ .../browser/led/src/{main.js => main.mjs} | 13 +++--- network/ws_network/HAL/BROWSER/ws_hal.cpp | 45 ++++++++++++++----- 4 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 examples/projects/browser/led/src/main.html rename examples/projects/browser/led/src/{main.js => main.mjs} (52%) diff --git a/engine/HAL/BROWSER/hal_script.py b/engine/HAL/BROWSER/hal_script.py index c0d3ec5c6..75d01550f 100644 --- a/engine/HAL/BROWSER/hal_script.py +++ b/engine/HAL/BROWSER/hal_script.py @@ -41,11 +41,11 @@ e.Append(CCFLAGS=["--target=cheerp-wasm", "-Wno-cheerp", "-DPUBLIC=__attribute__\(\(cheerp_jsexport\)\)"]) # Add the cheerp-wasm target to the linker flags - e.Append(LINKFLAGS=["--target=cheerp-wasm", "-cheerp-make-module=commonjs", "-cheerp-pretty-code"]) + e.Append(LINKFLAGS=["--target=cheerp-wasm", "-cheerp-make-module=es6", "-cheerp-pretty-code"]) # Replace the ar and ranlib commands with the appropriate llvm-ar command e.Replace(AR=cheerp_bin_path + "/llvm-ar", RANLIB=cheerp_bin_path + "/llvm-ar s") # Replace the output filename with the appropriate extension - e.Replace(PROGNAME="program.js") + e.Replace(PROGNAME="program.mjs") diff --git a/examples/projects/browser/led/src/main.html b/examples/projects/browser/led/src/main.html new file mode 100644 index 000000000..e9b7b373e --- /dev/null +++ b/examples/projects/browser/led/src/main.html @@ -0,0 +1,11 @@ + + + + + Luos_engine led example + + + +

Luos_engine led example

+ + diff --git a/examples/projects/browser/led/src/main.js b/examples/projects/browser/led/src/main.mjs similarity index 52% rename from examples/projects/browser/led/src/main.js rename to examples/projects/browser/led/src/main.mjs index e1ef7487c..94f3e3e16 100644 --- a/examples/projects/browser/led/src/main.js +++ b/examples/projects/browser/led/src/main.mjs @@ -1,5 +1,6 @@ -const program = require('../.pio/build/browser/program'); -program +import program from "../.pio/build/browser/program.mjs"; + +program() .then(({ Luos_Init, Led_Init, Luos_Loop, Led_Loop, Ws_Init, Ws_Loop }) => { Luos_Init(); Ws_Init(); @@ -11,9 +12,11 @@ program Led_Loop(); }; - return new Promise(() => setInterval(mainLoop, 0)); + return new Promise(() => setInterval(mainLoop, 10)); }) .catch((err) => { - console.error('Error', err); - process.exit(-1); + console.error("Error", err); + if (typeof window === "undefined") { + process.exit(-1); + } }); diff --git a/network/ws_network/HAL/BROWSER/ws_hal.cpp b/network/ws_network/HAL/BROWSER/ws_hal.cpp index 24cdd18c3..5bc523542 100644 --- a/network/ws_network/HAL/BROWSER/ws_hal.cpp +++ b/network/ws_network/HAL/BROWSER/ws_hal.cpp @@ -21,6 +21,11 @@ extern "C" using namespace client; +namespace [[cheerp::genericjs]] client +{ + Promise *import(const String &path); +} + /******************************************************************************* * Definitions ******************************************************************************/ @@ -63,10 +68,10 @@ void WsHAL_ReceptionWeb(const std::vector &data) clientWebsocket->send(arrayBufferView); } -[[cheerp::genericjs]] void WsHAL_InitWeb() +[[cheerp::genericjs]] void WsHAL_AddWsEventHandlers() { - __asm__("const WebSocket = require('ws')"); clientWebsocket = new WebSocket(s_url); + clientWebsocket->set_binaryType("arraybuffer"); clientWebsocket->addEventListener( "open", cheerp::Callback(openEventCallback)); @@ -76,18 +81,34 @@ void WsHAL_ReceptionWeb(const std::vector &data) clientWebsocket->addEventListener( "error", cheerp::Callback(errorEventCallback)); - clientWebsocket->addEventListener( "message", - cheerp::Callback( - [](MessageEvent *e) - { - auto *dataArray = (Uint8Array *)e->get_data(); - std::vector wasmData(dataArray->get_length()); // allocate some linear memory - Uint8Array *wasmDataArray = cheerp::MakeTypedArray(wasmData.data(), wasmData.size()); // create a Uint8Array wrapper ove the linear memory held by the vector - wasmDataArray->set(dataArray); // copy the data with the builtin set() function. A for() loop with element-wise copy would do but this is faster - WsHAL_ReceptionWeb(wasmData); // NOTE: this now takes a const std::vector& (or a std::vector if you want to pass ownership) - })); + cheerp::Callback([](MessageEvent *e) + { + auto *dataBuffer = (ArrayBuffer *)e->get_data(); + auto *dataArray = new Uint8Array(dataBuffer); + std::vector wasmData(dataArray->get_length()); + Uint8Array *wasmDataArray = cheerp::MakeTypedArray(wasmData.data(), wasmData.size()); + wasmDataArray->set(dataArray); + WsHAL_ReceptionWeb(wasmData); })); +} + +[[cheerp::genericjs]] void WsHAL_InitWeb() +{ + bool isBrowser; + __asm__("typeof %1 !== 'undefined'" : "=r"(isBrowser) : "r"(&client::window)); + if (isBrowser) + { + WsHAL_AddWsEventHandlers(); + } + else + { + client::import("ws") + ->then(cheerp::Callback([](client::Object *lib) + { __asm__("globalThis.WebSocket=%0.WebSocket" ::"r"(lib)); })) + ->then(cheerp::Callback([]() + { WsHAL_AddWsEventHandlers(); })); + } } /////////////////////////Luos Library Needed function/////////////////////////// From bd07e067a497e151f7073447857204ac7ff24d02 Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Thu, 22 Feb 2024 10:48:06 +0100 Subject: [PATCH 11/12] Add a small HTML to the led example on browser --- examples/projects/browser/led/lib/Led/led.cpp | 70 +++++++++++++++++-- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/examples/projects/browser/led/lib/Led/led.cpp b/examples/projects/browser/led/lib/Led/led.cpp index 2fa3b6f2d..e09e61523 100644 --- a/examples/projects/browser/led/lib/Led/led.cpp +++ b/examples/projects/browser/led/lib/Led/led.cpp @@ -50,10 +50,16 @@ const char led_OFF[768] = "\n" "/_____________________________________________//\n" "`---------------------------------------------'\n"; +using namespace client; + +static bool isBrowser; +[[cheerp::genericjs]] HTMLElement *ledDisplay; + /******************************************************************************* * Function ******************************************************************************/ -static void Led_MsgHandler(service_t *service, const msg_t *msg); +static void +Led_MsgHandler(service_t *service, const msg_t *msg); void clear_screen(void) { @@ -61,6 +67,39 @@ void clear_screen(void) printf("\x1B[2J\x1B[H"); } +[[cheerp::genericjs]] void define_browser() +{ + __asm__("typeof %1 !== 'undefined'" : "=r"(isBrowser) : "r"(&client::window)); +} + +[[cheerp::genericjs]] void browser_init() +{ + client::HTMLElement *body = client::document.get_body(); + HTMLElement *board = document.createElement("div"); + board->setAttribute("style", "width: 200px; height: 100px; background-color: #449944; display: flex; border-radius: 5%; border: 2px solid #000000;"); + body->appendChild(board); + HTMLElement *chip = document.createElement("div"); + chip->setAttribute("style", "margin: 25px; width: 50px; height: 50px; background-color: #101010; border-radius: 10%; color: #FFFFFF; text-align: center; line-height: 50px; font-size: 15px; font-weight: bold;"); + chip->appendChild(document.createTextNode("MCU")); + board->appendChild(chip); + ledDisplay = document.createElement("div"); + ledDisplay->setAttribute("style", "margin: 30px; width: 30px; height: 30px; background-color: #F0F0F0; border-radius: 50%"); + board->appendChild(ledDisplay); +} + +[[cheerp::genericjs]] void browser_display(bool state) +{ + if (state) + { + ledDisplay->setAttribute("style", "margin: 30px; width: 30px; height: 30px; background-color: #00ff00; border-radius: 50%"); + } + else + { + + ledDisplay->setAttribute("style", "margin: 30px; width: 30px; height: 30px; background-color: #F0F0F0; border-radius: 50%"); + } +} + /****************************************************************************** * @brief init must be call in project init * @param None @@ -68,11 +107,20 @@ void clear_screen(void) ******************************************************************************/ PUBLIC void Led_Init(void) { + define_browser(); revision_t revision = {.major = 1, .minor = 0, .build = 0}; Luos_CreateService(Led_MsgHandler, STATE_TYPE, "led", revision); clear_screen(); printf("LED service running.\n\n"); - printf(led_OFF); + if (isBrowser) + { + browser_init(); + browser_display(false); + } + else + { + printf(led_OFF); + } } /****************************************************************************** @@ -99,11 +147,25 @@ static void Led_MsgHandler(service_t *service, const msg_t *msg) printf("LED service running.\n\n"); if (Led_last_state == LED_OFF) { - printf(led_OFF); + if (isBrowser) + { + browser_display(false); + } + else + { + printf(led_OFF); + } } else if (Led_last_state == LED_ON) { - printf(led_ON); + if (isBrowser) + { + browser_display(true); + } + else + { + printf(led_ON); + } } else { From 887473e0600acc5aeb310795248688ce67a24d3c Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Thu, 22 Feb 2024 12:52:04 +0100 Subject: [PATCH 12/12] Update the browser led Readme --- examples/projects/browser/led/README.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/projects/browser/led/README.md b/examples/projects/browser/led/README.md index bcdcb1452..5f1b169ea 100644 --- a/examples/projects/browser/led/README.md +++ b/examples/projects/browser/led/README.md @@ -12,9 +12,9 @@ [![](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Unleash%20electronic%20devices%20as%20microservices%20thanks%20to%20Luos&https://luos.io&via=Luos_io&hashtags=embeddedsystems,electronics,microservices,api) [![](https://img.shields.io/badge/LinkedIn-Share-0077B5?style=social&logo=linkedin)](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fgithub.com%2Fluos-io) -# Led project example :bulb: +# Led project web assembly example :bulb: -This project demonstrate how to make and use a simple button through Luos. Feel free to use electronics and code example as you want. +This project demonstrate how to make and use a simple led in web assembly (wasm) through Luos. ## How to compile the code :computer: @@ -23,13 +23,12 @@ This project demonstrate how to make and use a simple button through Luos. Feel 3. Open this folder into Platformio 4. Build (Platformio will do the rest) -## How to open the electronic design :electric_plug: +## How to use :electric_plug: -You can open [a working example electronic design](https://github.com/Luos-io/luos_engine/tree/main/examples/hardware) with Kicad. This design use Luos_components library for more information to install and use it read [our doc](https://www.luos.io/docs/). +- Run `npx http-server -o ./src/main.html` to execute the example in your browser. +- Run `node src/main.mjs` to execute the example in your terminal. -## Linked driver - -This project is linked to the [Button driver](../../Drivers/button). +Like other examples you will need to have other services interacting with it. ## Don't hesitate to read [our documentation](https://www.luos.io/docs/), or to post your questions/issues on the [Luos' Forum](https://community.luos.io). :books: