/**
 * Author : Anatole SCHRAMM-HENRY
 * Created the : 30/05/2021
 * This classe exposes all the methods necessary to init the WSPeripherals (Weather Station Peripherals) and 
 * to retrieve various data and measurements like battery voltage, Temperatures etc...
 */
#ifndef WSPERIPHERALS_H
#define WSPERIPHERALS_H

#include <RF24.h>
#include <HTU21D.h>
#include <Adafruit_BMP280.h>
#include <LowPower.h>
 
#include "BoardConfig.h"

class WSPeripherals
{
  public:
    enum State {OFF, ON};
  
    WSPeripherals(const BoardConfig &boardConfig);
    /*
     * Returns 7 if all the external devices are working properly, or an other value if it is not the case.
     */
    uint8_t init();
    /*
     * After calling this methode , you need to execute initExternalPeripherals() to init the peripherals.
     */
    void externalPeripherals(State state){_3V3PowerRail(state);}

    /*
     * Used to init devices after each LDO powerup, external devices need to be turned one externalPeripherals(ON) before calling this function.
     */
    uint8_t initExternalPeripherals();

    float batteryVoltage();
    int sunlightMeasurement();
    void temperatureAndATMPressureFromBMP280(float *temperature = NULL, float *ATMPressure = NULL);
    float temperatureFromHTU21();
    float humidity();
    float compensatedHumidity();

    /*
     * Before calling this method, externalPeripherals(ON) and initExternalPeripherals() must be called respectively
     */
    void applyRadioConfig(uint8_t channel = RADIO_CHANNEL, uint8_t paLevel = RADIO_PA_LEVEL, rf24_datarate_e datarate = RADIO_DATARATE);
    const RF24 &getRadio();
    
  protected:
  private:
    void _3V3PowerRail(State state);
    const BoardConfig &_boardConfig;
    const Adafruit_BMP280 _BMP280;
    const HTU21D _HTU21;
    const RF24 _NRF;
  
};

#endif //WSPERIPHERALS_H
