Skip to content

File evaScrollButton.h

File List > src > evaScrollButton.h

Go to the documentation of this file

#pragma once

#include "evaButton.h"

namespace eva
{
    static const unsigned short REPEAT_DELAY = 300;

    static const unsigned char ON_REPEATKEY = 0x40;

    template <class TReader>
    class ScrollButton : public Button<TReader>
    {
    public:
        using Button<TReader>::Button;

    private:
        void handleLongPress(unsigned long now)
        {
            Button<TReader>::handleLongPress();
            this->notify(ON_REPEATKEY, this->levelCode);
            this->lastRepeatTime = max((unsigned long)1, now);
        }

        void handleDeactivating(unsigned char wasLevelCode, unsigned long now)
        {
            Button<TReader>::handleDeactivating(wasLevelCode, now);
            this->lastRepeatTime = 0;
        }

        bool checkRepeatTime(unsigned long now)
        {
            return (this->lastRepeatTime) && ((now - this->lastRepeatTime) > REPEAT_DELAY);
        }

        void handleRepeatTime(unsigned long now)
        {
            this->notify(ON_REPEATKEY, this->levelCode);
            this->lastRepeatTime = max((unsigned long)1, now);
        }

    private:
        void tick() override
        {
            if (this->levelCode < 0)
                return;

            unsigned long now = millis();
            unsigned char wasLevelCode = this->levelCode;
            if (!this->updateState())
                return;

            if (this->checkChanging(wasLevelCode))
                this->handleChanging();

            if (this->checkLongPress(now))
                handleLongPress(now);

            if (checkRepeatTime(now))
                handleRepeatTime(now);

            if (this->checkDeactivating(wasLevelCode))
                handleDeactivating(wasLevelCode, now);

            if (this->checkActivating(wasLevelCode))
                this->handleActivating(now);
        }

    private:
        unsigned long lastRepeatTime = 0;
    };

    template <int tPin, int tPinMode, int tActivatesOn>
    using PinScrollButton = Button<BinarizeEqDecor<DebounceDecor<DigitalPinReader<tPin, tPinMode>>, tActivatesOn>>;

    template <int tPin>
    using PullupScrollButton = ScrollButton<BinarizeEqDecor<DebounceDecor<DigitalPinReader<tPin, INPUT_PULLUP>>, LOW>>;

    template <int tPin, int tPinMode, signed short... tLevels>
    using PinScrollMultiButton = ScrollButton<QuantizeDecor<DebounceDecor<AnalogPinReader<tPin, tPinMode>>, tLevels...>>;
};