/*
 * 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>


#ifndef ECXX_PLATFORM_AVR_TIMER_COUNTER_1_H
#define ECXX_PLATFORM_AVR_TIMER_COUNTER_1_H


namespace eCxx {


//
// A simple AVR timer/counter 1 wrapper
//
// WARNING : When the timer/counter is active, all PWM outputs that use this timer/counter
//           may not work as expected.
//
class TimerCounter1 : public Uninstantiable {
    public:
        // Clock prescaler
        enum class ClockPrescaler : uint8_t {
            // These values are derived from the datasheet
                _1 = 0b00000001, // Timer_Clock = CPU_Clock /    1 (default)
                _8 = 0b00000010, // Timer_Clock = CPU_Clock /    8
               _64 = 0b00000011, // Timer_Clock = CPU_Clock /   64
              _256 = 0b00000100, // Timer_Clock = CPU_Clock /  256
             _1024 = 0b00000101, // Timer_Clock = CPU_Clock / 1024
        };

    public:
        // Start and stop
        static void start(ClockPrescaler prescaler);
        static void stop();

        // Periodic interrupt
        static void setPeriod(uint16_t value);
        static uint16_t getPeriod();
        static void attachPeriodicInterrupt(eCxx::VoidCallback userFunc);
        static void detachPeriodicInterrupt();

        // Compare-match
        static void setCompareMatchValue(uint16_t value);
        static void attachCompareMatchInterrupt(eCxx::VoidCallback userFunc);
        static void detachCompareMatchInterrupt();
};


} // namespace eCxx


#endif
