HardwareTypes.h defines platform-agnostic hardware type definitions for the HardFOC system.
These types provide a consistent API across different hardware platforms without exposing
MCU-specific implementation details.
Design Philosophy
All base interface classes use these common types to ensure:
Platform Portability - Code works across different microcontrollers
Consistent Naming - Unified type names throughout the system
Future Extensibility - Easy to modify underlying types if needed
Type Safety - Strong typing to prevent common errors
Header File
1
#include"inc/base/HardwareTypes.h"
Core Integer Types
Unsigned Integer Types
1
2
3
4
usinghf_u8_t=uint8_t;// 8-bit unsigned (0 to 255)usinghf_u16_t=uint16_t;// 16-bit unsigned (0 to 65,535)usinghf_u32_t=uint32_t;// 32-bit unsigned (0 to 4,294,967,295)usinghf_u64_t=uint64_t;// 64-bit unsigned (0 to 18,446,744,073,709,551,615)
Signed Integer Types
1
2
3
4
usinghf_i8_t=int8_t;// 8-bit signed (-128 to 127)usinghf_i16_t=int16_t;// 16-bit signed (-32,768 to 32,767)usinghf_i32_t=int32_t;// 32-bit signed (-2,147,483,648 to 2,147,483,647)usinghf_i64_t=int64_t;// 64-bit signed (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
Boolean Type
1
usinghf_bool_t=bool;// Platform-agnostic boolean type
Hardware-Specific Types
GPIO Pin Types
1
usinghf_pin_num_t=hf_i32_t;// GPIO pin number type
Constants:
1
2
constexprhf_pin_num_tHF_INVALID_PIN=-1;// Invalid/unassigned pinconstexprhf_pin_num_tHF_MAX_PIN_NUMBER=255;// Maximum supported pin number
Validation:
1
constexprboolIsValidPin(hf_pin_num_tpin)noexcept;
Port and Controller Types
1
2
usinghf_port_num_t=hf_u32_t;// Communication port identifierusinghf_host_id_t=hf_u32_t;// Host/controller identifier
usinghf_frequency_hz_t=hf_u32_t;// Frequency in Hzusinghf_frequency_t=hf_frequency_hz_t;// Backward compatibility aliasusinghf_baud_rate_t=hf_u32_t;// UART baud rate
Timing Types
1
2
usinghf_time_t=hf_u32_t;// Time in millisecondsusinghf_timeout_ms_t=hf_time_t;// Timeout value in milliseconds
Timeout Constants:
1
2
3
constexprhf_time_tHF_TIMEOUT_DEFAULT_MS=1000;// Default 1 second timeoutconstexprhf_time_tHF_TIMEOUT_NONE=0;// No timeout (wait indefinitely)constexprhf_time_tHF_TIMEOUT_MAX=std::numeric_limits<hf_time_t>::max();// Maximum timeout
boolsetup_i2c_port(hf_port_num_tport,hf_frequency_hz_tfrequency){// Validate portif(!IsValidPort(port)){printf("Invalid I2C port: %u\n",port);returnfalse;}// Validate frequency range (typical I2C: 100kHz to 1MHz)if(frequency<100000||frequency>1000000){printf("Invalid I2C frequency: %u Hz\n",frequency);returnfalse;}printf("Setting up I2C port %u at %u Hz\n",port,frequency);returntrue;}voidtest_i2c_setup(){setup_i2c_port(0,400000);// Valid: I2C port 0 at 400kHzsetup_i2c_port(HF_INVALID_PORT,400000);// Invalid portsetup_i2c_port(1,50000);// Invalid frequency (too low)}
classSensorManager{private:staticconstexprhf_u8_tMAX_SENSORS=8;hf_channel_id_tsensor_channels*[MAX_SENSORS];public:SensorManager(){// Initialize all channels as invalidfor(hf_u8_ti=0;i<MAX_SENSORS;i++){sensor_channels*[i]=HF_INVALID_CHANNEL;}}booladd_sensor(hf_u8_tsensor_index,hf_channel_id_tchannel){if(sensor_index>=MAX_SENSORS){returnfalse;}if(!IsValidChannel(channel)){printf("Invalid ADC channel: %u\n",channel);returnfalse;}sensor_channels*[sensor_index]=channel;printf("Sensor %u assigned to ADC channel %u\n",sensor_index,channel);returntrue;}hf_channel_id_tget_sensor_channel(hf_u8_tsensor_index)const{if(sensor_index>=MAX_SENSORS){returnHF_INVALID_CHANNEL;}returnsensor_channels*[sensor_index];}boolis_sensor_configured(hf_u8_tsensor_index)const{hf_channel_id_tchannel=get_sensor_channel(sensor_index);returnIsValidChannel(channel);}};
enumclassOperationResult{SUCCESS,TIMEOUT,ERROR};OperationResultwait_for_data(hf_timeout_ms_ttimeout){hf_time_tstart_time=get_current_time_ms();while(true){if(data_available()){returnOperationResult::SUCCESS;}if(timeout!=HF_TIMEOUT_NONE){// Check for timeouthf_time_telapsed=get_current_time_ms()-start_time;if(elapsed>=timeout){printf("Operation timed out after %u ms\n",timeout);returnOperationResult::TIMEOUT;}}vTaskDelay(pdMS_TO_TICKS(1));// Small delay}}voidtest_timeout_handling(){// Wait with default timeoutOperationResultresult1=wait_for_data(HF_TIMEOUT_DEFAULT_MS);// Wait indefinitelyOperationResultresult2=wait_for_data(HF_TIMEOUT_NONE);// Wait with custom timeoutOperationResultresult3=wait_for_data(500);// 500ms timeout}
structGpioConfig{hf_pin_num_tpin;hf_gpio_direction_tdirection;hf_gpio_active_state_tactive_state;hf_gpio_pull_mode_tpull_mode;// Constructor with validationGpioConfig(hf_pin_num_tp,hf_gpio_direction_tdir,hf_gpio_active_state_tactive=hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH,hf_gpio_pull_mode_tpull=hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_FLOATING):pin(p),direction(dir),active_state(active),pull_mode(pull){if(!IsValidPin(pin)){throwstd::invalid_argument("Invalid GPIO pin number");}}boolis_valid()const{returnIsValidPin(pin);}};structI2cConfig{hf_port_num_tport;hf_frequency_hz_tfrequency;hf_timeout_ms_ttimeout;I2cConfig(hf_port_num_tp,hf_frequency_hz_tfreq,hf_timeout_ms_tto=HF_TIMEOUT_DEFAULT_MS):port(p),frequency(freq),timeout(to){if(!IsValidPort(port)){throwstd::invalid_argument("Invalid I2C port");}}};
Type Conversion Utilities
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Safe conversion with bounds checkingtemplate<typenameT,typenameU>constexprboolsafe_cast(Uvalue,T&result)noexcept{if(value<std::numeric_limits<T>::min()||value>std::numeric_limits<T>::max()){returnfalse;}result=static_cast<T>(value);returntrue;}// Example usageboolconvert_pin_number(intinput_pin,hf_pin_num_t&output_pin){returnsafe_cast(input_pin,output_pin)&&IsValidPin(output_pin);}
Best Practices
Type Usage Guidelines
Always use HardFOC types instead of raw integer types in public APIs
Validate inputs using the provided validation functions
Use constants instead of magic numbers (e.g., HF_INVALID_PIN vs -1)
Check for invalid values before performing operations
Use appropriate sized types for the data range (e.g., hf_u8_t for small counts)
Performance Considerations
All types are compile-time aliases with zero runtime overhead
Validation functions are constexpr and can be evaluated at compile time
Constants are compile-time evaluated and donβt consume memory
Platform Portability
Types automatically adapt to the underlying platformβs integer sizes
Code using these types will compile and run on any supported platform
No platform-specific #ifdef blocks needed in application code
Compilation Requirements
C++11 or later - Required for constexpr and type aliases