// Std 8-bit unsigned integer: QI: Quarter Integer
typedef unsigned int __attribute__((__mode__(__QI__))) uint8;
// Std 16-bit unsigned integer: HI: Half Integer
typedef unsigned int __attribute__((__mode__(__HI__))) uint16;
// Std 32-bit single-precision floating-point: SF: Single Floating
typedef float __attribute__((__mode__(__SF__))) flt32;
// Std 8-bit unsigned integer literal
inline constexpr uint8 operator "" _ui8(unsigned long long int t_arg) noexcept{ return static_cast <uint8> ( t_arg ); }
// Std 16-bit unsigned integer literal
inline constexpr uint16 operator "" _ui16(unsigned long long int t_arg) noexcept{ return static_cast <uint16>( t_arg ); }
// Std 32-bit single-precision floating-point literal
inline constexpr flt32 operator ""_flt32(long double t_arg ) noexcept{ return static_cast <flt32>( t_arg ); }
typedef enum : uint8 {
P_BGRF_1 = 13_ui8,
P_BGRF_2 = 12_ui8,
P_BGRF_3 = 11_ui8,
P_BGRF_4 = 10_ui8,
P_BGRF_5 = 9_ui8,
P_BGRF_6 = 8_ui8,
P_BGRF_7 = 7_ui8,
P_BGRF_8 = 6_ui8,
P_BGRF_9 = 5_ui8,
P_BGRF_10 = 4_ui8
// Enum used to assign and group the pin values of the bargraph
}e_PIN_BARGRAPH;
typedef enum : uint8 {
P_BATT,
P_CURR
// Enum used to assign and group the pin value of the ADC
}e_PIN_ADC;
inline constexpr flt32 BOUNDARY_VOLTAGE_LEFT = 0.0_flt32;
inline constexpr flt32 BOUNDARY_VOLTAGE_RIGHT = 5.0_flt32;
inline constexpr uint8 BOUNDARY_PERCENT_LEFT = 0_ui8;
inline constexpr uint8 BOUNDARY_PERCENT_RIGHT = 100_ui8;
// Function prototypes
uint8 compBatteryLevel(flt32 t_batt_voltage);
void ctrlBarGraph(const e_PIN_BARGRAPH t_bargraph_arr[], uint8 t_batt_percentage);
inline flt32 mapFlt(flt32 t_val, flt32 t_inMin, flt32 t_inMax, flt32 t_outMin, flt32 t_outMax);
// Global variables
const e_PIN_BARGRAPH arr_bargraph[] = {P_BGRF_1, P_BGRF_2, P_BGRF_3, P_BGRF_4, P_BGRF_5,
P_BGRF_6, P_BGRF_7, P_BGRF_8, P_BGRF_9, P_BGRF_10};
flt32 batt_voltage = 0.0_flt32;
uint8 batt_percentage = 0_ui8;
void setup() {
// put your setup code here, to run once:
pinMode(P_BGRF_1, OUTPUT);
pinMode(P_BGRF_2, OUTPUT);
pinMode(P_BGRF_3, OUTPUT);
pinMode(P_BGRF_4, OUTPUT);
pinMode(P_BGRF_5, OUTPUT);
pinMode(P_BGRF_6, OUTPUT);
pinMode(P_BGRF_7, OUTPUT);
pinMode(P_BGRF_8, OUTPUT);
pinMode(P_BGRF_9, OUTPUT);
pinMode(P_BGRF_10, OUTPUT);
pinMode(P_BATT, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
batt_voltage = analogRead(P_BATT);
batt_voltage /= 1024;
batt_voltage *= 5;
batt_percentage = compBatteryLevel(batt_voltage);
Serial.println("VOLTAGE:");
Serial.println(batt_voltage);
Serial.println("___________");
Serial.println("PERCENTAGE:");
Serial.println(batt_percentage);
Serial.println("___________");
delay(1500);
ctrlBarGraph(arr_bargraph, batt_percentage);
}
void ctrlBarGraph(const e_PIN_BARGRAPH t_bargraph_arr[], uint8 t_batt_percentage)
{
}
uint8 compBatteryLevel(flt32 t_batt_voltage)
{
flt32 l_res = mapFlt(t_batt_voltage, BOUNDARY_VOLTAGE_LEFT, BOUNDARY_VOLTAGE_RIGHT,
BOUNDARY_PERCENT_LEFT, BOUNDARY_PERCENT_RIGHT);
return static_cast<uint8>(l_res);
}
inline flt32 mapFlt(flt32 t_val, flt32 t_inMin, flt32 t_inMax, flt32 t_outMin, flt32 t_outMax)
{
return (t_val - t_inMin) * (t_outMax - t_outMin) / (t_inMax - t_inMin) + t_outMin;
}