/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */


#include <eCxx/CTL/Types.h>

#include "PinName.h"


#ifndef ECXX_PLATFORM_AVR_ANALOG_COMPARATOR_LITE_H
#define ECXX_PLATFORM_AVR_ANALOG_COMPARATOR_LITE_H


namespace eCxx {


//
// A simple AVR analog comparator wrapper
//
// # The internal bandgap reference voltage at ~25 °C is around:
//       ~1.10 V for newer AVR MCUs
//       ~1.25 V for older AVR MCUs (mostly the original AVR cores)
// # If InputN::ADCMux is selected as the negative input, then Application::analogRead()
//   must not be used/called when the analog comparator is active.
//
class AnalogComparatorLite : public Uncopyable {
    public:
        inline ~AnalogComparatorLite() = default;

    private:
        bool _initialized;

        AnalogComparatorLite();

        friend class Application;

    public:
        // Positive input selection
        enum class InputP : uint8_t {
            // These values are derived from the datasheet
            AIN0    = 0x00, // AIN0 (default)
            Bandgap = 0x01  // Internal bandgap reference
        };

        // Negative input selection
        enum class InputN : uint8_t {
            // These values are derived from the datasheet
            AIN1     = 0x00, // AIN1                       (default for other series ; not available for U4    series)
            Bandgap  = 0x01, // Internal bandgap reference (default for U4    series ; not available for other series)
            ADCMux   = 0x02  // ADC multiplexer output
        };

    public:
        // Configurable public properties
        InputP  inputp;
        InputN  inputn;
        uint8_t adcMux; // 0-7 or 0-15

    public:
        bool begin();
        bool end();

        void reset();

        bool getOutStatus();

        void attachInterrupt(eCxx::VoidCallback userFunc, Application::InterruptMode mode);
        void detachInterrupt();
};


} // namespace eCxx


#endif
