Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Functions | Variables
Units.c File Reference
#include "../../Inc/Utils/Units.h"
#include "../../Inc/Utils/MessageFormat.h"
#include <string.h>
#include <math.h>
Include dependency graph for Units.c:

Go to the source code of this file.

Functions

void initUnits (void)
 
const UnitDefinitiongetUnitDefinition (UnitId unit_id)
 
UnitId getUnitIdBySymbol (const char *symbol)
 
bool validateUnitValue (UnitId unit_id, float value)
 
float convertUnits (float value, UnitId from_unit, UnitId to_unit)
 

Variables

static const UnitDefinition unit_definitions [UNIT_COUNT]
 

Function Documentation

◆ convertUnits()

float convertUnits ( float  value,
UnitId  from_unit,
UnitId  to_unit 
)

Definition at line 99 of file Units.c.

99 {
100 const UnitDefinition* from_def = getUnitDefinition(from_unit);
101 const UnitDefinition* to_def = getUnitDefinition(to_unit);
102
103 // Only convert within same category
104 if (from_def->category != to_def->category) {
105 return value; // No conversion possible
106 }
107
108 // Convert to base unit, then to target unit
109 float base_value = value * from_def->scale_factor;
110 return base_value / to_def->scale_factor;
111}
const UnitDefinition * getUnitDefinition(UnitId unit_id)
Definition: Units.c:78
float scale_factor
Definition: Units.h:40
UnitCategory category
Definition: Units.h:34
Here is the call graph for this function:

◆ getUnitDefinition()

const UnitDefinition * getUnitDefinition ( UnitId  unit_id)

Definition at line 78 of file Units.c.

78 {
79 if (unit_id >= UNIT_COUNT) {
80 return &unit_definitions[UNIT_DIMENSIONLESS]; // Fallback
81 }
82 return &unit_definitions[unit_id];
83}
static const UnitDefinition unit_definitions[UNIT_COUNT]
Definition: Units.c:6
@ UNIT_COUNT
Definition: Units.h:70
@ UNIT_DIMENSIONLESS
Definition: Units.h:69
Here is the caller graph for this function:

◆ getUnitIdBySymbol()

UnitId getUnitIdBySymbol ( const char *  symbol)

Definition at line 85 of file Units.c.

85 {
86 for (UnitId i = 0; i < UNIT_COUNT; i++) {
87 if (strcmp(unit_definitions[i].symbol, symbol) == 0) {
88 return i;
89 }
90 }
91 return UNIT_DIMENSIONLESS; // Fallback
92}
UnitId
Definition: Units.h:44

◆ initUnits()

void initUnits ( void  )

Definition at line 74 of file Units.c.

74 {
75 sendMessage("Units", MSG_DEBUG, "Units system initialized with %d unit types", UNIT_COUNT);
76}
@ MSG_DEBUG
Definition: MessageFormat.h:14
void sendMessage(const char *sender, MessageType type, const char *format,...)
Definition: MessageFormat.c:5
Here is the call graph for this function:
Here is the caller graph for this function:

◆ validateUnitValue()

bool validateUnitValue ( UnitId  unit_id,
float  value 
)

Definition at line 94 of file Units.c.

94 {
95 const UnitDefinition* unit = getUnitDefinition(unit_id);
96 return (value >= unit->absolute_min && value <= unit->absolute_max);
97}
float absolute_min
Definition: Units.h:35
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ unit_definitions

const UnitDefinition unit_definitions[UNIT_COUNT]
static

Definition at line 6 of file Units.c.