The HardFOC Interface Wrapper provides a unified,
platform-agnostic abstraction layer for embedded hardware peripherals.
It enables developers to write portable,
maintainable code that works across different microcontrollers and hardware platforms without
modification.
โจ Key Benefits
๐ Platform Portability - Write once, run anywhere
๐ก๏ธ Type Safety - Strongly typed interfaces with error handling
โก Performance Optimized - Minimal overhead with direct hardware access
๐ง Extensible - Easy to add new hardware platforms and peripherals
๐ Observable - Built-in statistics, diagnostics, and monitoring
๐งต Thread Safe - Designed for multi-threaded applications
๐ฏ Target Applications
Motor Control Systems - FOC, BLDC, stepper motor control
Sensor Networks - Multi-sensor data acquisition and processing
Communication Systems - CAN, UART, I2C, SPI protocols
Industrial Automation - PLC-like control systems
IoT Devices - Connected embedded systems
Robotics - Real-time control and sensing
๐๏ธ Architecture
Design Philosophy
The HardFOC Interface follows a layered abstraction pattern:
// Base classes (abstract interfaces)#include"inc/base/BaseAdc.h"
#include"inc/base/BaseGpio.h"// Platform implementations#include"inc/mcu/esp32/EspAdc.h"
#include"inc/mcu/esp32/EspGpio.h"
2. Create Hardware Instances
1
2
3
// Use platform-specific implementationsEspAdcadc(ADC_UNIT_1,ADC_ATTEN_DB_11);EspGpioled_pin(2,hf_gpio_direction_t::HF_GPIO_DIRECTION_OUTPUT);
3. Initialize and Use
1
2
3
4
5
6
7
8
9
10
11
12
13
// Lazy initialization (automatic on first use)adc.EnsureInitialized();led_pin.EnsureInitialized();// Use the hardwarefloatvoltage;if(adc.ReadChannelV(0,voltage)==hf_adc_err_t::ADC_SUCCESS){printf("Voltage: %.3f V\n",voltage);}if(voltage>3.0f){led_pin.SetActive();}
#include"inc/mcu/esp32/EspAdc.h"
#include"inc/mcu/esp32/EspPwm.h"
#include"inc/mcu/esp32/EspGpio.h"classMotorController{private:EspAdccurrent_sensor*;EspPwmmotor_driver*;EspGpioenable_pin*;public:MotorController():current_sensor*(ADC_UNIT_1,ADC_ATTEN_DB_11),motor_driver*(),enable_pin*(5,hf_gpio_direction_t::HF_GPIO_DIRECTION_OUTPUT){}boolInitialize(){current_sensor*.EnsureInitialized();motor_driver*.EnsureInitialized();enable_pin*.EnsureInitialized();// Configure motor drivermotor_driver*.EnableChannel(0);motor_driver*.SetFrequency(0,20000);// 20kHz PWMreturntrue;}voidSetSpeed(floatspeed_percent){motor_driver*.SetDutyCycle(0,speed_percent);}floatGetCurrent(){floatvoltage;current_sensor*.ReadChannelV(0,voltage);return(voltage-2.5f)/0.1f;// Convert to current (A)}};
#include"inc/mcu/esp32/EspI2c.h"
#include"inc/mcu/esp32/EspAdc.h"classSensorNetwork{private:EspI2ci2c_bus*;EspAdcanalog_sensors*;public:boolScanSensors(){hf_u8_taddresses[16];hf_u8_tcount=i2c_bus*.ScanBus(addresses,16);printf("Found %u I2C devices:\n",count);for(hf_u8_ti=0;i<count;i++){printf(" Address: 0x%02X\n",addresses[i]);}returncount>0;}floatReadTemperature(){// Read from I2C temperature sensorhf_u8_tdata[2];if(i2c_bus*.ReadRegisters(0x48,0x00,data,2)){hf_u16_traw=(data[0]<<8)|data[1];return(raw>>4)*0.0625f;// Convert to Celsius}return-999.0f;// Error value}floatReadPressure(){floatvoltage;analog_sensors*.ReadChannelV(1,voltage);returnvoltage*100.0f;// Convert to PSI}};
๐งช Testing
Comprehensive test suites for validating hardware interface implementations: