taler-xotp_fw

xOTP generator firmware
Log | Files | Refs | Submodules | README

batMeasInterface.c (4586B)


      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 #include "batMeasInterface.h"
     25 
     26 #include <stdlib.h>
     27 #include <string.h>
     28 
     29 #include "logger.h"
     30 #include "xtotpUtil.h"
     31 
     32 uint8_t batMeas_initInterface(batMeasInterface *interfaceHandler,
     33                               batMeasPwrCB powerOnCB,
     34                               batMeasPwrCB powerOffCB,
     35                               getBatteryStateCB getStateCB)
     36 {
     37   if (NULL == interfaceHandler || NULL == powerOnCB || NULL == powerOffCB ||
     38       NULL == getStateCB) {
     39     return EXIT_FAILURE;
     40   }
     41   // Add callback functions
     42   interfaceHandler->powerOn = powerOnCB;
     43   interfaceHandler->powerOff = powerOffCB;
     44   interfaceHandler->getBatteryState = getStateCB;
     45 
     46   // Init values
     47   memset(interfaceHandler->averageValues, 0, BATTERY_PERCENT_AVERAGE_SIZE);
     48   interfaceHandler->lastUpdateTime = 0;
     49   interfaceHandler->storedValues = 0;
     50   interfaceHandler->voltageValue = 0;
     51   interfaceHandler->isEnabled = false;
     52   return EXIT_SUCCESS;
     53 }
     54 
     55 uint8_t batMeas_measure(batMeasInterface *interfaceHandler,
     56                         uint32_t currentTick,
     57                         uint32_t updateTime)
     58 {
     59   if (NULL == interfaceHandler) {
     60     return EXIT_FAILURE;
     61   }
     62   // Check battery voltage is needed
     63   if (interfaceHandler->storedValues < BATTERY_PERCENT_AVERAGE_SIZE ||
     64       getTickDiff(currentTick, interfaceHandler->lastUpdateTime) >=
     65           updateTime) {
     66     // Check measurement must be enabled
     67     if (!interfaceHandler->isEnabled) {
     68       interfaceHandler->powerOn();
     69       interfaceHandler->isEnabled = true;
     70       return EXIT_FAILURE;
     71     }
     72     // Get new value
     73     uint8_t newBatValue = 0;
     74     uint16_t newBatVoltageValue = 0;
     75 
     76     // Get new values, return in case of error
     77     if (interfaceHandler->getBatteryState(&newBatValue, &newBatVoltageValue,
     78                                           &interfaceHandler->chargingStatus)) {
     79       return EXIT_FAILURE;
     80     }
     81     // Correct values
     82     if (interfaceHandler->storedValues >= BATTERY_PERCENT_AVERAGE_SIZE) {
     83       // Update list
     84       for (uint8_t i = BATTERY_PERCENT_AVERAGE_SIZE - 1; i; i--) {
     85         interfaceHandler->averageValues[i] =
     86             interfaceHandler->averageValues[i - 1];
     87       }
     88       interfaceHandler->averageValues[0] = newBatValue;
     89 
     90     } else {
     91       // Increment new incremented value
     92       interfaceHandler->storedValues++;
     93       interfaceHandler->averageValues[BATTERY_PERCENT_AVERAGE_SIZE -
     94                                       interfaceHandler->storedValues] =
     95           newBatValue;
     96     }
     97     interfaceHandler->voltageValue = newBatVoltageValue;
     98     LOGGER_DEBUG("Current battery level: %u%%", newBatValue);
     99     interfaceHandler->lastUpdateTime = currentTick;
    100     interfaceHandler->powerOff();
    101     interfaceHandler->isEnabled = false;
    102     return EXIT_SUCCESS;
    103   } else {
    104     bat_status_type newState = interfaceHandler->chargingStatus;
    105     interfaceHandler->getBatteryState(NULL, NULL, &newState);
    106     if (newState != interfaceHandler->chargingStatus) {
    107       interfaceHandler->chargingStatus = newState;
    108       return EXIT_SUCCESS;
    109     } else {
    110       return EXIT_FAILURE;
    111     }
    112   }
    113 }
    114 
    115 uint8_t batMeas_getBatteryPercentState(const batMeasInterface *interfaceHandler)
    116 {
    117   if (NULL == interfaceHandler || 0 == interfaceHandler->storedValues) {
    118     return 0;
    119   }
    120   uint16_t sumState = 0;
    121   for (uint8_t i =
    122            BATTERY_PERCENT_AVERAGE_SIZE - interfaceHandler->storedValues;
    123        i < BATTERY_PERCENT_AVERAGE_SIZE; i++) {
    124     sumState += interfaceHandler->averageValues[i];
    125   }
    126   return (uint8_t)(sumState / interfaceHandler->storedValues);
    127 }
    128 
    129 uint16_t
    130 batMeas_getBatteryVoltageState(const batMeasInterface *interfaceHandler)
    131 {
    132   if (NULL == interfaceHandler) {
    133     return 0;
    134   }
    135   return interfaceHandler->voltageValue;
    136 }