#pragma once
#include <Arduino.h>

u_int8_t get_charging()
{
  uint8_t charging_int_local = 0;
  // function_start = micros();
  /*
    - both GPIO must be PULLUP as LOW is active from TP4056
    - LEDs on TP4056 are NOT needed if PULL_UP both GPIO
    
    #define POWER_GPIO                38  // GREEN, STDB PIN6 on TP4056, LOW on CHARGED (LED ON),       comment out if not in use - don't use "0" here
    #define CHARGING_GPIO             39  // RED,   CHRG PIN7 on TP4056, LOW during CHARGING (LED ON),  comment out if not in use - don't use "0" here
    
    truth table:
    NC:               POWER_GPIO HIGH (PULLUP)    CHARGING_GPIO HIGH (PULLUP)
    CHARGING:         POWER_GPIO HIGH (PULLUP)    CHARGING_GPIO LOW     
    FULL (CHARGED):   POWER_GPIO LOW              CHARGING_GPIO HIGH (PULLUP)
    OFF (DISABLED):   POWER_GPIO HIGH (PULLUP)    CHARGING_GPIO HIGH (PULLUP) ???? - not checked yet

  */
    // moved to setup()
    // pinMode(CHARGING_GPIO,INPUT_PULLUP);
    // pinMode(POWER_GPIO,INPUT_PULLUP);
    
    uint8_t power_gpio_state    = digitalRead(POWER_GPIO);
    uint8_t charging_gpio_state = digitalRead(CHARGING_GPIO);
    
    if ((power_gpio_state)   and (charging_gpio_state))   {charging_int_local = 0;}
    if ((power_gpio_state)   and (!charging_gpio_state))  {charging_int_local = 1;}
    if ((!power_gpio_state)  and (charging_gpio_state))   {charging_int_local = 2;}
    // if ((digitalRead(POWER_GPIO) == 1) and (digitalRead(CHARGING_GPIO) == 1)) charging_int_local = 3; // OFF ??

    #ifdef DEBUG
        Serial.printf("[%s]: charging=%d\n",__func__,charging_int_local);
    #endif
  
  // Serial.printf("[%s]: took %uus\n",__func__,(micros()-function_start));
  return charging_int_local;
}