batMeasInterface.h (4618B)
1 /** 2 * @file batMeasInterface.h 3 * @author Adrian STEINER (steia19@bfh.ch) 4 * @brief Battery measurement interface to get the current state in percent 5 * @version 0.1 6 * @date 17-02-2025 7 * 8 * @copyright (C) 2025 Adrian STEINER 9 * This program is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation, either version 3 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program. If not, see <https: //www.gnu.org/licenses/>. 21 * 22 */ 23 24 #ifndef BAT_MEAS_INTERFACE_H 25 #define BAT_MEAS_INTERFACE_H 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 #include <stdbool.h> 32 #include <stdint.h> 33 34 #define BATTERY_PERCENT_AVERAGE_SIZE \ 35 (5) ///< Size of stored battery percent states 36 37 /** 38 * @brief Battery status 39 * 40 */ 41 typedef enum { 42 BAT_DISCHARGE, 43 BAT_CHARGING, 44 BAT_FULL, 45 BAT_UNKNOWN 46 } bat_status_type; 47 48 /** 49 * @brief Power on and off callback prototype 50 * 51 */ 52 typedef void (*batMeasPwrCB)(void); 53 54 /** 55 * @brief Battery status callback prototype. 56 * 57 * 1. Param: Battery state in percent 58 * 2. Param: Battery voltage state 59 * 3. Param: Battery charging status 60 */ 61 typedef uint8_t (*getBatteryStateCB)(uint8_t *, uint16_t *, bat_status_type *); 62 63 /** 64 * @brief Battery measurement interface 65 * 66 */ 67 typedef struct { 68 /* User Callbacks */ 69 batMeasPwrCB powerOn; ///< Enables the measurement 70 batMeasPwrCB powerOff; ///< Disables the measurement 71 getBatteryStateCB 72 getBatteryState; ///< Get the current battery state in percent, the 73 ///< absolute value in mV and the charging state 74 /* Battery state values */ 75 uint8_t 76 averageValues[BATTERY_PERCENT_AVERAGE_SIZE]; ///< Last stored values used 77 ///< for get an average 78 79 uint8_t storedValues; ///< Number of stored values in average buffer 80 uint16_t voltageValue; ///< last measured voltage states 81 uint32_t lastUpdateTime; ///< Last update time in ticks 82 bat_status_type chargingStatus; ///< Charging status 83 bool isEnabled; 84 } batMeasInterface; 85 86 /** 87 * @brief Initialise the batMeas inteface handler with the callbacks 88 * 89 * @param interfaceHandler Pointer to the battery measurement interface handler. 90 * @param powerOnCB Callback to enable the battery measurement 91 * @param powerOffCB Callback to disable the battery measurement 92 * @param getStateCB Callback to get the current state of the battery 93 * @return uint8_t EXIT_SUCCESS, EXIT_FAILURE otherwise 94 */ 95 uint8_t batMeas_initInterface(batMeasInterface *interfaceHandler, 96 batMeasPwrCB powerOnCB, 97 batMeasPwrCB powerOffCB, 98 getBatteryStateCB getStateCB); 99 100 /** 101 * @brief Measures the current battery state. 102 * 103 * This function triggers a battery measurement if the specified update interval 104 * has elapsed since the last measurement. It updates the internal state of the 105 * battery interface. 106 * 107 * @param interfaceHandler Pointer to the battery measurement interface handler. 108 * @param currentTick Current system tick (e.g., from a timer or RTOS). 109 * @param updateTime Minimum time interval (in ticks) between measurements. 110 * @return uint8_t Returns 1 if a new measurement was performed, 0 otherwise. 111 */ 112 uint8_t batMeas_measure(batMeasInterface *interfaceHandler, 113 uint32_t currentTick, 114 uint32_t updateTime); 115 116 /** 117 * @brief Retrieves the current battery charge level as a percentage. 118 * 119 * Returns the last measured battery level in percent (0–100%). 120 * 121 * @param interfaceHandler Pointer to the battery measurement interface handler. 122 * @return uint8_t Battery charge level in percent. 123 */ 124 uint8_t 125 batMeas_getBatteryPercentState(const batMeasInterface *interfaceHandler); 126 127 /** 128 * @brief Retrieves the current battery voltage. 129 * 130 * Returns the last measured battery voltage in millivolts. 131 * 132 * @param interfaceHandler Pointer to the battery measurement interface handler. 133 * @return uint16_t Battery voltage in millivolts. 134 */ 135 uint16_t 136 batMeas_getBatteryVoltageState(const batMeasInterface *interfaceHandler); 137 138 #ifdef __cplusplus 139 } 140 #endif 141 142 #endif /* BAT_MEAS_INTERFACE_H*/