Obniz Rust
🦀 A comprehensive Rust library for controlling obniz IoT devices via WebSocket communication.
Features
- Async/Await Support: Fully asynchronous API using tokio
- Type Safety: Strong typing with custom error handling
- Comprehensive Coverage: Supports all major obniz features
- Callback System: Real-time event handling with callbacks
- Memory Safe: Built with Rust's memory safety guarantees
Supported Modules
| Module | Status | Description |
|---|---|---|
| 🔧 IO | ✅ Complete | Digital pin control, stream mode, callbacks |
| 📺 Display | ✅ Complete | Text, graphics, QR codes, raw pixel data |
| ⚡ AD | ✅ Complete | Analog-to-digital conversion, voltage reading |
| 🌊 PWM | ✅ Complete | PWM generation, servo control, modulation |
| 📡 UART | ✅ Complete | Serial communication with flow control |
| 🔘 Switch | ✅ Complete | Built-in switch state monitoring |
| ⚙️ System | ✅ Complete | Device control, reset, configuration |
| 🔌 SPI | 🔄 Planned | SPI master/slave communication |
| 📶 I2C | 🔄 Planned | I2C bus communication |
| 📊 Logic Analyzer | 🔄 Planned | Digital signal analysis |
| 📏 Measurement | 🔄 Planned | Advanced measurement functions |
Quick Start
Add to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
Basic Usage
use *;
async
Advanced Example with Callbacks
use *;
async
Module Documentation
IO Control
let io = obniz.io;
// Basic pin operations
io.set_pin.await?; // Set pin high
io.set_pin_as_output.await?; // Configure as output
io.set_pin_as_input.await?; // Configure as input with stream
// Advanced configuration
let config = IoConfig ;
io.configure_pin.await?;
// Real-time monitoring
io.set_pin_callback.await?;
Display Control
let display = obniz.display;
// Text and basic operations
display.text.await?;
display.clear.await?;
display.brightness.await?;
// Graphics
display.rect.await?; // Filled rectangle
display.circle.await?; // Circle outline
display.line.await?; // Diagonal line
// QR codes
display.qr.await?;
// Raw pixel data
let config = RawDisplayConfig ;
display.raw.await?;
Analog Input (AD)
let ad = obniz.ad;
// Single reading
let voltage = ad.get_voltage.await?;
println!;
// Multiple channels
let readings = ad.get_voltages.await?;
for reading in readings
// Stream mode with callback
ad.set_channel_callback.await?;
// Utility functions
let percentage = voltage_to_percentage; // 66%
let is_safe = is_voltage_safe; // true
PWM Generation
let pwm = obniz.pwm;
// Basic PWM
pwm.configure_channel.await?;
// Servo control (0-180 degrees)
pwm.servo.await?;
// Square wave generation
pwm.square_wave.await?; // 2kHz on pin 7
// Duty cycle control
pwm.set_channel_duty_cycle.await?; // 25% duty
// Modulation for communication
let mod_config = ModulationConfig ;
pwm.channel?.modulate.await?;
UART Communication
let uart = obniz.uart;
// Simple configuration
let config = simple_config; // RX, TX, baud
uart.init_channel.await?;
// Send data
uart.send_string.await?;
uart.send_data.await?;
// Receive callback
uart.set_string_callback.await?;
// Advanced configuration with flow control
let advanced_config = flow_control_config;
uart.init_channel.await?;
Switch Monitoring
let switch = obniz.switch;
// Current state
let state = switch.get_state.await?;
println!;
// Event callbacks
switch.on_push.await?;
switch.on_left.await?;
switch.on_right.await?;
switch.on_release.await?;
// Blocking wait for events
let pressed_state = switch.wait_for_press.await?; // 5s timeout
switch.wait_for_release.await?; // No timeout
Error Handling
The library provides comprehensive error handling with the ObnizResult<T> type:
match obniz.io.get_pin.await
Examples
The repository includes comprehensive examples:
basic_example.rs- Simple IO and display operationsio_example.rs- Complete IO functionality demonstrationdisplay_example.rs- Display and graphics featurescomprehensive_example.rs- All modules working togetherdevice_test_template.rs- Template for testing with real devicesmock_integration_example.rs- Mock system demonstration
Run examples with:
Testing with Real Devices
For testing with actual obniz devices:
- Copy
device_test_template.rsto a new file - Replace
"YOUR-OBNIZ-ID"with your actual device ID - Important: Never commit files containing real device IDs to version control
# Copy template and edit device ID
# Edit the OBNIZ_ID constant in my_device_test.rs
Device Compatibility
Tested with:
- obniz Board
- obniz Board 1Y
- M5StickC (obniz firmware)
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- obniz for the excellent IoT platform
- The Rust community for amazing async ecosystem