Skip to content

File ctrl.h

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

Go to the documentation of this file

Source Code

#pragma once
#ifndef DSY_KNOB_H
#define DSY_KNOB_H 
#include <stdint.h>

#ifdef __cplusplus
namespace daisy
{
class AnalogControl
{
  public:
    AnalogControl() {}
    ~AnalogControl() {}

    void Init(uint16_t *adcptr,
              float     sr,
              bool      flip         = false,
              bool      invert       = false,
              float     slew_seconds = 0.002f);

    void InitBipolarCv(uint16_t *adcptr, float sr);

    float Process();

    inline float Value() const { return val_; }

    // using conditionals since clamp() is unavailable
    inline void SetCoeff(float val)
    {
        val = val > 1.f ? 1.f : val;
        val = val < 0.f ? 0.f : val;

        coeff_ = val;
    }

    inline void SetScale(const float scale) { scale_ = scale; }

    inline void SetOffset(const float offset) { offset_ = offset; }

    inline uint16_t GetRawValue() { return *raw_; }

    inline float GetRawFloat() { return (float)(*raw_) / 65535.f; }

    void SetSampleRate(float sample_rate);

  private:
    uint16_t *raw_;
    float     coeff_, samplerate_, val_;
    float     scale_, offset_;
    bool      flip_;
    bool      invert_;
    bool      is_bipolar_;
    float     slew_seconds_;
};
} // namespace daisy
#endif
#endif