ð Interfaces in libhal
What are Interfaces?
An interface is like a contract between different parts of your program. It defines what methods a class must implement, without specifying how they should work. This is powerful because it lets you:
- Write code that works with any hardware that follows the interface
- Switch hardware without changing your application code
- Test your code more easily
For example, if you write code using the hal::input_pin
interface, it will
work with any microcontroller that provides an implementation of that interface.
Available Interfaces
Digital I/O
See API:
hal::input_pin
Reads the state of a digital pin (HIGH or LOW). Used for:
- Reading button presses
- Detecting digital signals
- Reading logic levels
See API:
hal::output_pin
Controls digital outputs (HIGH or LOW). Used for:
- Controlling LEDs
- Sending digital signals
- Setting logic levels
See API:
hal::interrupt_pin
Calls a function when a pin's state changes. Used for:
- Detecting button presses
- Responding to external signals
- Event-driven programming
Analog Interfaces
See API:
hal::adc
Converts analog signals to digital values. Used for:
- Reading sensor values
- Measuring voltages
- Processing analog inputs
See API:
hal::dac
Converts digital values to analog signals. Used for:
- Generating analog voltages
- Controlling analog devices
See API:
hal::dac
Converts digital values to analog signals. Used for:
- Generating analog voltages based on a PCM waveform data
- Generating audio output
See API:
hal::pwm
Generates square waves with controllable duty cycle. Used for:
- Motor speed control
- LED brightness control
- Signal generation
Time Management
See API:
hal::timer
Schedules future events. Used for:
- Delayed operations
- Periodic tasks
- Timeout management
See API:
hal::steady_clock
Provides consistent time measurements. Used for:
- Measuring durations
- Timing operations
- Creating delays
Communication Protocols
See API:
hal::spi
Fast, synchronous communication protocol. Used for:
- Communicating with displays
- Reading memory chips
- High-speed sensor data
See API:
hal::i2c
Two-wire communication protocol. Used for:
- Connecting multiple sensors
- Reading small devices
- Low-speed communication
See API:
hal::serial
Basic serial communication. Used for:
- Bi-direction asynchronous communication with a single device
- Communication with computers
See API:
hal::can
Robust communication bus. Used for:
- Automotive systems
- Industrial networks
- Multi-device communication
Motion Control
See API:
hal::motor
Controls open-loop motors. Used for:
- Basic motor control
- Fan control
- Simple actuators
See API:
hal::servo
Controls position-based motors. Used for:
- Precise positioning
- Robotic arms
- Camera mounts
Sensors
See API:
hal::temperature_sensor
Measures temperature. Used for:
- Environmental monitoring
- System protection
- Process control
See API:
hal::accelerometer
Measures acceleration in X, Y, Z axes. Used for:
- Motion detection
- Orientation sensing
- Vibration monitoring
See API:
hal::magnetometer
Measures magnetic fields. Used for:
- Compass heading
- Position detection
- Metal detection
See API:
hal::distance_sensor
Measures linear distance. Used for:
- Object detection
- Range finding
- Proximity sensing
See API:
hal::rotation_sensor
Measures angular position. Used for:
- Motor position feedback
- Device orientation tracking
- Angle measurement
âģ Coming Soon
API not available yet
Measure electrical current flow in circuits. Used for:
- Calculating battery state of charge
- Measuring system power consumption
- Measure motor torque/force
API not available yet
Will measure voltage differences in circuits.
API not available yet
Will provide location, time, and velocity data from GPS signals.
Understanding Virtual Functions in C++
A quick note about virtual functions (which libhal uses extensively):
- They don't require heap memory: Virtual functions work fine with stack-allocated objects.
- Performance impact is minimal: The overhead is usually just one pointer lookup.
- Memory overhead is small: Each class with virtual functions needs only one vtable (shared between all instances).
Example of using virtual functions efficiently:
// This works fine - no heap allocation needed
hal::lpc4078::i2c i2c2(2); // Stack allocated
initialize_display(i2c2); // Uses virtual functions, but still efficient