Skip to content

File encoder.h

File List > external-docs > libDaisy > src > hid > encoder.h

Go to the documentation of this file

Source Code

#pragma once
#ifndef DSY_ENCODER_H
#define DSY_ENCODER_H
#include "daisy_core.h"
#include "per/gpio.h"
#include "hid/switch.h"

namespace daisy
{
class Encoder
{
  public:
    Encoder() {}
    ~Encoder() {}

    void Init(Pin a, Pin b, Pin click, float update_rate = 0.f);
    void Debounce();

    inline int32_t Increment() const { return updated_ ? inc_ : 0; }

    inline bool RisingEdge() const { return sw_.RisingEdge(); }

    inline bool FallingEdge() const { return sw_.FallingEdge(); }

    inline bool Pressed() const { return sw_.Pressed(); }

    inline float TimeHeldMs() const { return sw_.TimeHeldMs(); }

    inline void SetUpdateRate(float update_rate) {}

  private:
    uint32_t last_update_;
    bool     updated_;
    Switch   sw_;
    GPIO     hw_a_, hw_b_;
    uint8_t  a_, b_;
    int32_t  inc_;
};
} // namespace daisy
#endif