中文版本 | English Version
gpio_example is a demo program showing how to use AI Glass SDK's GPIO Hub event service to subscribe and handle GPIO button events.
- Subscribe to GPIO button events (Press/Release)
- Asynchronous event callback mechanism, real-time response
- Subscribes to all active GPIOs by default and prints the actual GPIO number
- Supports
-g <gpio_number>to subscribe to one GPIO only - Multi-process sharing same GPIO Hub event source
- No need to access GPIO hardware directly
- Complete event statistics and debug info
┌─────────────────┐
│ ai-core Service│ (Monitors GPIO HW)
│ GPIO Manager │
└────────┬────────┘
│ Broadcast Event
├─────────┐
│ │
┌────▼───┐ ┌─▼──────┐
│ Client 1│ │ Client 2│
└────────┘ └────────┘
- Server (ai-core): Monitors GPIO hardware state changes
- GPIO Hub: Broadcasts events to all subscribed clients via shared memory and Unix Domain Socket
- Client: Receives events and executes callback function in independent thread
cd ai_glass_sdk/examples/gpio_example
makeOutput: gpio_example
# In service directory
cd service
./build/ai-core --enable-gpioServer needs to correctly configure the GPIO pins to monitor. To expose multiple GPIOs, start ai-core with --gpio-numbers, for example:
./build/ai-core --enable-gpio --gpio-number 1 --gpio-numbers 0,1,75 --gpio-active-low 0,75# Subscribe to all active GPIOs. This is useful for finding which GPIO was pressed.
./gpio_example
# Subscribe to GPIO 1 only.
./gpio_example -g 1═══════════════════════════════════════════════════════════
GPIO Event Client - Hub Async Callback Mode
Target: all active GPIOs
═══════════════════════════════════════════════════════════
📝 [Step 1/3] Creating GPIO Hub event client...
✅ Client created
📝 [Step 2/3] Connecting to GPIO Hub event center...
✅ Connected to service
📌 Active GPIOs in Hub: GPIO0(raw=high,active-low,released) GPIO1(raw=low,active-high,released) GPIO75(raw=high,active-low,released)
📝 [Step 3/3] Subscribing to GPIO events...
✅ Subscribed to GPIO events
- Local Notify Socket: /tmp/ai_gpio_hub_client_12345_123456789
- Current Event Sequence: 0
═══════════════════════════════════════════════════════════
🎧 Listening... Please press any active GPIO button
💡 Hint: Press Ctrl+C to exit program
═══════════════════════════════════════════════════════════
═══════════════════════════════════════════
🔴 GPIO1 Button Press Event
───────────────────────────────────────────
Timestamp: 1234567890123 us
Press Count: 1
═══════════════════════════════════════════
═══════════════════════════════════════════
⚪ GPIO1 Button Release Event
───────────────────────────────────────────
Timestamp: 1234567891234 us
Release Count: 1
═══════════════════════════════════════════
Press Ctrl+C to exit, program will automatically clean up resources:
🛑 Received exit signal, preparing to close...
📝 Cleaning up resources...
- Unregistered notify socket
✅ Resources cleaned up
═══════════════════════════════════════════════════════════
Program Exited
───────────────────────────────────────────────────────────
Total Press Count: 5
Total Release Count: 5
═══════════════════════════════════════════════════════════
- GPIO_EVENT_PRESS - Button Pressed
- GPIO_EVENT_RELEASE - Button Released
- GPIO_EVENT_ERROR - Error Event
Program handles events via my_gpio_event_callback() function:
void my_gpio_event_callback(gpio_event_t event_type,
int gpio_number,
void *user_data) {
// Handle event
}- Real-time Response: Callback executes in independent thread, does not block main thread
- Event Statistics: Automatically counts press and release times
- Heartbeat Check: Checks service status every 10 seconds
- Graceful Exit: Ctrl+C triggers resource cleanup
void my_gpio_event_callback(gpio_event_t event_type, int gpio_number, void *user_data) {
if (event_type == GPIO_EVENT_PRESS) {
// Start recording
start_recording();
} else if (event_type == GPIO_EVENT_RELEASE) {
// Stop recording
stop_recording();
}
}static int button_press_count = 0;
void my_gpio_event_callback(gpio_event_t event_type, int gpio_number, void *user_data) {
if (event_type == GPIO_EVENT_PRESS) {
button_press_count++;
printf("Press Count: %d\n", button_press_count);
}
}static uint64_t press_timestamp = 0;
void my_gpio_event_callback(gpio_event_t event_type, int gpio_number, void *user_data) {
if (event_type == GPIO_EVENT_PRESS) {
press_timestamp = ai_gpio_get_timestamp_us();
} else if (event_type == GPIO_EVENT_RELEASE) {
uint64_t duration = ai_gpio_get_timestamp_us() - press_timestamp;
if (duration > 2000000) { // 2 seconds
printf("Long press detected!\n");
}
}
}❌ Connection failed, please ensure ai-core is started and GPIO enabled
Solution:
- Check if
ai-coreservice is running - Confirm started with
--enable-gpioparameter
❌ Subscription failed
Solution:
- Check if GPIO Hub is correctly initialized
- View server logs for detailed error info
⚠️ Service stopped, preparing to exit
Solution:
- Server exited unexpectedly, restart
ai-coreservice
| Value | Description |
|---|---|
0 |
Normal exit |
-1 |
Initialization failed or connection failed |
// 1. Create Hub client
int ai_gpio_hub_client_create(gpio_event_hub_client_t *client);
// 2. Connect to GPIO Hub
int ai_gpio_hub_client_connect(gpio_event_hub_client_t *client);
// 3. Subscribe to specific GPIOs
int ai_gpio_hub_client_subscribe_gpios(gpio_event_hub_client_t *client,
const int *gpio_list,
int gpio_count,
gpio_event_callback_t callback,
void *user_data);
// 4. Subscribe to all GPIOs
int ai_gpio_hub_client_subscribe_all(gpio_event_hub_client_t *client,
gpio_event_callback_t callback,
void *user_data);
// 5. Get active GPIO list
int ai_gpio_hub_client_get_active_gpios(gpio_event_hub_client_t *client,
int *gpio_list,
int max_count);
// 6. Check service status
int ai_gpio_hub_client_is_service_alive(gpio_event_hub_client_t *client);
// 7. Destroy client
void ai_gpio_hub_client_destroy(gpio_event_hub_client_t *client);
// Auxiliary function
uint64_t ai_gpio_get_timestamp_us(void); // Get microsecond timestampFor detailed programming interface documentation, please refer to: 📚 GPIO Client API Development Guide
Multiple clients can subscribe to the same GPIO event simultaneously:
# Terminal 1
./gpio_example
# Terminal 2
./gpio_example
# Both clients will receive the same button events- Low Latency: Event broadcast latency typically < 1ms
- High Concurrency: Supports multiple clients subscribing simultaneously
- Lightweight: Single client occupies minimal memory (< 100KB)
# Server will output GPIO state changes
# Check for event broadcast logs# View Client Socket
ls -la /tmp/ai_gpio_hub_client_*
# View Server Socket
ls -la /tmp/ai_gpio_event_hub_broadcast
# View Hub shared memory
ls -la /dev/shm/ai_gpio_event_hubModify log level in source code to see more debug info.
- This program needs to run on target ARM device, cannot execute directly on x86 host
- Must start
ai-coreservice first and enable GPIO function - Callback function executes in independent thread, pay attention to thread safety
- Ensure sufficient permission to access
/tmpdirectory
- View Help: Running program automatically shows usage instructions
- Check Service:
ps aux | grep ai-core - View Socket:
ls -la /tmp/ai_gpio_event_hub_broadcast /tmp/ai_gpio_hub_client_*
- Programming Interface: GPIO Client API Development Guide
- GPIO Architecture: GPIO Architecture (Note: File might be missing)
- SDK Documentation:
../../README.en.md - Header File:
../../include/ai_gpio.h