This directory contains comprehensive examples demonstrating how to use the ApraLinuxUtils library for embedded Linux development.
Demonstrates digital I/O operations:
- Basic output (blinking LED)
- Basic input (reading button state)
- Edge interrupt detection
- Combined input/output (button-controlled LED)
Hardware: LED on GPIO 23, Button on GPIO 24, Sensor on GPIO 25
# Compile
g++ -std=c++14 gpio_example.cpp -I../includes -L../build -lApraLinuxUtils -lpthread -o gpio_example
# Run all examples
sudo ./gpio_example
# Run specific example
sudo ./gpio_example output
sudo ./gpio_example input
sudo ./gpio_example interrupt
sudo ./gpio_example combinedDemonstrates I2C sensor interfacing:
- Reading temperature from TMP102 sensor
- Writing configuration to I2C devices
- Asynchronous I2C transactions
- Error handling and callbacks
Hardware: TMP102 temperature sensor on I2C bus 1, address 0x48
# Compile
g++ -std=c++14 i2c_example.cpp -I../includes -L../build -lApraLinuxUtils -lpthread -o i2c_example
# Run
sudo ./i2c_exampleDemonstrates pulse width modulation:
- LED brightness control with fade effects
- Servo motor positioning (0-180 degrees)
- Different frequencies and duty cycles
- Smooth transitions
Hardware: LED on PWM chip 0 channel 0, Servo on PWM chip 0 channel 1
# Compile
g++ -std=c++14 pwm_example.cpp -I../includes -L../build -lApraLinuxUtils -o pwm_example
# Run all examples
sudo ./pwm_example
# Run specific example
sudo ./pwm_example led
sudo ./pwm_example servoDemonstrates USB device detection:
- Detecting USB flash drives
- Monitoring insertion/removal events
- Reading storage information
- Mount/unmount operations
Hardware: USB flash drive
# Compile
g++ -std=c++14 usb_storage_example.cpp -I../includes -L../build -lApraLinuxUtils -ludev -o usb_storage_example
# Run basic detection
sudo ./usb_storage_example
# Continuous monitoring
sudo ./usb_storage_example monitor
# Detection only (no mount)
sudo ./usb_storage_example detect-onlyDemonstrates thread management and message passing:
- Creating custom threads (extending ProcessThread)
- Message passing (REQUEST_ONLY and REQUEST_RESPONSE)
- Producer-consumer pattern
- Request-response pattern
- Thread lifecycle management
Hardware: None required
# Compile
g++ -std=c++14 threading_example.cpp -I../includes -L../build -lApraLinuxUtils -lpthread -o threading_example
# Run all examples
./threading_example
# Run specific example
./threading_example basic
./threading_example producer-consumer
./threading_example request-response
./threading_example lifecycleMost examples require elevated privileges to access hardware:
# Run with sudo
sudo ./example_name
# Or add user to appropriate groups
sudo usermod -a -G gpio,i2c,dialout $USER
# Logout and login for changes to take effectImportant: Be careful when connecting hardware to avoid damage:
- Use appropriate current-limiting resistors for LEDs (typically 220Ω - 1kΩ)
- Verify voltage levels match your board (3.3V or 5V)
- Check pin assignments for your specific hardware platform
- Never connect outputs directly together
- Use pull-up/pull-down resistors for inputs where appropriate
All examples can be compiled with:
g++ -std=c++14 example_name.cpp -I../includes -L../build -lApraLinuxUtils [additional libs] -o example_nameCommon additional libraries:
-lpthread- Required for threading examples and some hardware interfaces-ludev- Required for USB storage examples
These examples are designed for embedded Linux platforms such as:
- Raspberry Pi
- BeagleBone Black
- NVIDIA Jetson
- Other embedded Linux boards with GPIO, I2C, PWM support
GPIO/PWM pin numbers and I2C bus numbers may vary by platform. Check your board's documentation.
Each example follows this structure:
- Copyright Header: MIT License with Apra Labs copyright
- Documentation: Purpose, hardware setup, compilation, execution
- Includes: ApraLinuxUtils and standard library headers
- Helper Functions: Utility functions for the examples
- Example Functions: Individual demonstrations
- Main Function: Command-line argument parsing and execution
- Error Handling: Proper error checking and reporting
- Cleanup: Resource deallocation and GPIO/hardware cleanup
Recommended order for learning:
- GPIO Example - Start here to understand basic digital I/O
- PWM Example - Learn analog-like control with digital signals
- I2C Example - Understand sensor communication protocols
- Threading Example - Master concurrent programming patterns
- USB Storage Example - Learn system integration and device management
# Solution 1: Run with sudo
sudo ./example_name
# Solution 2: Add user to groups
sudo usermod -a -G gpio,i2c,dialout $USER# Check if devices exist
ls /sys/class/gpio/
ls /dev/i2c-*
# Enable I2C if needed (Raspberry Pi)
sudo raspi-config
# Navigate to: Interface Options -> I2C -> Enable# Check PWM availability
ls /sys/class/pwm/
# Some platforms require device tree overlays
# Raspberry Pi example:
# Add to /boot/config.txt:
# dtoverlay=pwm-2chan# Ensure ApraLinuxUtils is built
cd ../build
cmake ..
make
# Check library path
ls ../build/libApraLinuxUtils.a
# Verify include path
ls ../includes/ApraLinuxUtils.hHave a useful example? Consider contributing:
- Follow the existing example structure
- Include comprehensive comments
- Add hardware requirements
- Test on real hardware
- Submit a pull request
See CONTRIBUTING.md for details.
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Main README
All examples are licensed under the MIT License - see LICENSE for details.
Happy Coding! 🚀