Documentation#
Welcome to the Bruce JavaScript API documentation. This section provides comprehensive documentation for all JavaScript modules available in the Bruce firmware interpreter.
Overview#
The Bruce firmware includes a powerful JavaScript interpreter built on MicroQuickJS that provides access to device hardware and functionality through a rich set of APIs. These APIs allow you to create custom scripts for device interaction, automation, and development.
It allows you to create apps or even games using JavaScript.
Available Modules#
The JavaScript API is organized into several modules, each focusing on specific device functionality:
Core Modules#
- Global - Global utility functions and timers
- Math - Extended mathematical functions
- Runtime - Script execution control
- Console - Sending messages to the console/serial output
- Device - Device information and system status
- Dialog - User interface dialogs and prompts
- Display - Graphics, text rendering, and display control
- Keyboard - On-screen keyboard input
- Notification - Visual notifications and indicators
- Storage - File system operations and data persistence
Hardware Modules#
- Audio - Audio playback and sound generation
- BadUSB - USB HID keyboard emulation
- GPIO - General purpose input/output pin control
- I2C - I2C bus communication
- Infrared - IR signal transmission and reception
- Microphone - Audio recording functionality
- RFID - RFID/NFC tag operations
- Serial - Serial communication and console output
- Sub-GHz - Sub-GHz radio frequency operations
- WiFi - WiFi connectivity and HTTP requests
Getting Started#
- Scripts are executed through the Bruce firmware's script runner
- Most modules are not loaded automatically and will need to be imported using
var moduleName = require("moduleName"); - Functions are called directly on module objects (e.g.,
display.drawText(...)) - Most functions return
undefinedunless explicitly documented otherwise
Example Usage#
// Display hello world
var display = require("display");
display.setTextSize(2);
display.setTextColor(display.color(255, 255, 255));
display.drawString("Hello World!", 10, 10);
// Blink LED
var notification = require("notification");
notification.blink(3000);
// Get device info
var device = require("device");
const deviceName = device.getName();
const batteryLevel = device.getBatteryCharge();
console.log(`Device: ${deviceName}, Battery: ${batteryLevel}%`);
Error Handling#
Most functions will throw JavaScript exceptions on error. Use try-catch blocks for error handling:
var storage = require("storage");
try {
const data = storage.read("/config.json");
const config = JSON.parse(data);
} catch (error) {
console.log("Failed to read config:", error.message);
}
Performance Considerations#
- The JavaScript interpreter runs on an embedded system with limited resources
- Avoid excessive memory allocation in loops
- Use
gc()to trigger garbage collection when needed - Prefer native functions over JavaScript implementations for performance-critical code