### Blockly Toolbox Configuration Example (JSON) Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md Example `toolbox.json` structure defining a category named 'Servo' with two block types: `servo_attach` and `servo_write`, for display in the Blockly interface. ```json { "kind": "category", "name": "Servo", "contents": [ { "kind": "block", "type": "servo_attach" }, { "kind": "block", "type": "servo_write" } ] } ``` -------------------------------- ### NPM Package Configuration for Blockly Library (JSON) Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md Example `package.json` file for an Aily Blockly library, including metadata like name, nickname, author, description, version, compatibility (core, voltage), and keywords for NPM package management. ```json { "name": "@aily-project/lib-servo", // 库的包名以 @aily-project/lib- 开头 "nickname":"舵机驱动库", // 用于在库管理器中显示的名称 "author": "aily Project", "description": "舵机控制支持库,支持Arduino UNO、MEGA、ESP8266、ESP32等开发板", // 简短的介绍,不超过50字 "version": "0.0.1", "compatibility": { // 兼容性说明,支持的核心和电压 "core": [ // 如果为[],则为通用库,兼容所有开发板 "arduino:avr", "esp32:esp32", "esp32:esp32c3", "esp32:esp32s3", "renesas_uno:minima", "renesas_uno:unor4wifi" ], "voltage": [3.3,5] }, "keywords": [ // keywords可以辅助搜索库,建议其中添加分类名、函数名等 "aily", "blockly", "servo", "servo_attach", "servo_write" ], "scripts": {}, "dependencies": {}, "devDependencies": {} } ``` -------------------------------- ### C++ Servo Library Inclusion and Object Instantiation Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md Example C++ code demonstrating how to include the Servo library and declare a Servo object for Arduino projects, typically required as additional code. ```c++ #include Servo myservo; ``` -------------------------------- ### Access Board Configuration in Blockly Generator.js Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md This JavaScript snippet illustrates how to retrieve the current `board.json` configuration within the `generator.js` file using `window['boardConfig']`. It provides practical examples of how to use this configuration to implement conditional logic, such as checking the board's core (e.g., 'esp32') or its specific name (e.g., 'ai-vox'), allowing for dynamic content loading or behavior adjustments based on the target hardware. ```javascript // 判断开发板核心 if (window['boardConfig'].core.indexOf('esp32') > -1) { } // 判断开发板名称 if (window['boardConfig'].name.indexOf('ai-vox') > -1) { } ``` -------------------------------- ### Arduino Sketch Structure with Generator Injection Points (C++) Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md Illustrative C++ Arduino sketch showing the various sections where code can be injected using the Blockly generator's helper functions (e.g., macro, library, variable, object, function, setup, loop). ```c++ // [宏定义 macro] 使用generator.addMacro可向该部分添加代码 #defined PIN 3 // [库引用 library] 使用generator.addLibrary可向该部分添加代码 #include // [变量 variable] 使用generator.addVariable可向其中添加代码 static const int servoPin = 4;1 // [对象 object] 使用generator.addObject可向该部分添加代码 Servo servo1; // [函数 function] 使用generator.addFunction可向该部分添加代码 void setup() { // [初始化 setup Being] 使用generator.addSetupBegin可向该部分添加代码 servo1.attach(servoPin); // [初始化 setup] 使用generator.addSetup可向该部分添加代码(系统默认,不建议库使用) Serial.begin(115200); // [初始化 setup End] 使用generator.addSetupEnd可向该部分添加代码 } void loop() { // [主程序 loop Begin] 使用generator.addLoopBegin可向该部分添加代码 // [主程序 loop] 使用generator.addLoop可向该部分添加代码(系统默认,不建议库使用) for(int posDegrees = 0; posDegrees <= 180; posDegrees++) { servo1.write(posDegrees); Serial.println(posDegrees); delay(20); } for(int posDegrees = 180; posDegrees >= 0; posDegrees--) { servo1.write(posDegrees); Serial.println(posDegrees); delay(20); } // [主程序 loop End] 使用generator.addLoopEnd可向该部分添加代码 } ``` -------------------------------- ### Blockly Generator Function Definition Best Practice (JavaScript) Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md JavaScript example demonstrating how to define functions within the `Arduino` object in Blockly generators to avoid creating global functions. ```javascript Arduino.newFunction = function(value) { return 'ok'; }; ``` -------------------------------- ### Blockly Library Optimization and Simplification Guidelines (APIDOC) Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md Guidelines for optimizing and simplifying Blockly library usage, including automatic code injection, simplified blocks for common tasks, I2C address handling, and callback function integration. ```APIDOC Optimization/Simplification Guidelines: 1. Automatic Pin Mode Initialization: - When calling digitalWrite/digitalRead, automatically add pinMode to setup. 2. Simplified Library Initialization: - For libraries like Servo, provide a simplified block that directly controls the servo on a specified pin, in addition to attach/write blocks. 3. I2C Device Address Handling: - If only one I2C address, do not display it in the block. - If multiple addresses, use field_dropdown for selection, with default as first option. 4. Callback Function Integration: - For functions with callback parameters (e.g., onebutton library's button.attachDoubleClick(doubleClick)), create a block for the callback function (doubleClick). - Automatically add attach code (e.g., button.attachDoubleClick(doubleClick)) to setup. - Automatically add tick code (e.g., button.tick()) to loop. ``` -------------------------------- ### Blockly Generator Helper Functions API Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md API documentation for the helper functions available in the Blockly generator, used to inject code into specific sections of the generated Arduino sketch. ```APIDOC addMacro(tag, code); addLibrary(tag, code); addVariable(tag, code); addObject(tag, code); addFunction(tag, code); addSetupBegin(tag, code); addSetup(tag, code); //系统默认,不建议库使用 addSetupEnd(tag, code); addLoopBegin(tag, code); addLoop(tag, code); //系统默认,不建议库使用 addLoopEnd(tag, code); ``` -------------------------------- ### Aily Blockly Library Naming Convention (APIDOC) Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md Documentation for the naming conventions of Aily Blockly libraries, distinguishing between general libraries and core libraries. ```APIDOC Library Naming Convention: General Libraries: @aily-project/lib-xxxx (e.g., @aily-project/lib-servo) Core Libraries: @aily-project/lib-core-xxxx (e.g., @aily-project/lib-core-io) ``` -------------------------------- ### Define a Custom Blockly Block with Extension Link Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md This JSON snippet illustrates the definition of a custom Blockly block named 'blinker_init_wifi'. It includes a dropdown field for selecting WiFi modes and specifies an 'extensions' property to link this block to a JavaScript extension, allowing for custom behavior. This structure is fundamental for integrating new functionalities into the Blockly visual programming environment. ```json { "type": "blinker_init_wifi", "message0": "初始化Blinker WiFi模式 %1", "args0": [ { "type": "field_dropdown", "name": "MODE", "options": [ [ "手动配网", "手动配网" ], [ "EspTouch V2", "EspTouchV2" ] ] } ], "extensions": ["blinker_init_wifi_extension"], "previousStatement": null, "nextStatement": null, "colour": "#03A9F4", "inputsInline": false } ``` -------------------------------- ### Register a Blockly Extension in JavaScript Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md This JavaScript code demonstrates the process of registering a Blockly extension. It includes a crucial check to prevent duplicate loading by unregistering the extension if it's already registered, ensuring clean and predictable behavior. The `Blockly.Extensions.register` function is then used to define the extension's logic, which would contain the actual implementation details for the linked block. ```javascript // 避免重复加载 if (Blockly.Extensions.isRegistered('blinker_init_wifi_extension')) { Blockly.Extensions.unregister('blinker_init_wifi_extension'); } Blockly.Extensions.register('blinker_init_wifi_extension', function() { // 实现代码 }) ``` -------------------------------- ### Blockly Generator Function for Servo Attach (JavaScript) Source: https://github.com/ailyproject/aily-blockly-libraries/blob/main/库规范.md JavaScript code for a Blockly generator function (`servo_attach`) that adds Servo library inclusion and variable declaration to the generated Arduino code using `generator.addLibrary` and `generator.addVariable`. ```javascript Arduino.forBlock['servo_attach'] = function(block, generator) { generator.addLibrary('#include ','#include '); generator.addVariable('Servo myservo','Servo myservo'); // 实现代码参考blockly官方文档 return '' }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.