Skip to content

File MappedValue.h

File List > external-docs > libDaisy > src > util > MappedValue.h

Go to the documentation of this file

Source Code

#pragma once
#include <stdint.h>
#include "FixedCapStr.h"

namespace daisy
{
class MappedValue
{
  public:
    virtual ~MappedValue() {}

    virtual void AppentToString(FixedCapStrBase<char>& string) const = 0;

    virtual void ResetToDefault() = 0;

    virtual float GetAs0to1() const = 0;

    virtual void SetFrom0to1(float normalizedValue0to1) = 0;

    virtual void Step(int16_t numStepsUp, bool useCoarseStepSize) = 0;
};

class MappedFloatValue : public MappedValue
{
  public:
    enum class Mapping
    {
        lin,
        log,
        pow2
    };

    MappedFloatValue(float       min,
                     float       max,
                     float       defaultValue,
                     Mapping     mapping     = Mapping::lin,
                     const char* unitStr     = "",
                     uint8_t     numDecimals = 1,
                     bool        forceSign   = false);

    ~MappedFloatValue() override {}

    float Get() const { return value_; }

    const float* GetPtr() const { return &value_; }

    void Set(float newValue);

    operator float() const { return value_; }

    MappedFloatValue& operator=(float val)
    {
        Set(val);
        return *this;
    }

    // inherited form MappedValue
    void AppentToString(FixedCapStrBase<char>& string) const override;

    // inherited form MappedValue
    void ResetToDefault() override;

    // inherited form MappedValue
    float GetAs0to1() const override;

    // inherited form MappedValue
    void SetFrom0to1(float normalizedValue0to1) override;

    void Step(int16_t numStepsUp, bool useCoarseStepSize) override;

  private:
    float                  value_;
    const float            min_;
    const float            max_;
    const float            default_;
    Mapping                mapping_;
    const char*            unitStr_;
    const uint8_t          numDecimals_;
    const bool             forceSign_;
    static constexpr float coarseStepSize0to1_ = 0.05f;
    static constexpr float fineStepSize0to1_   = 0.01f;
};

class MappedIntValue : public MappedValue
{
  public:
    MappedIntValue(int         min,
                   int         max,
                   int         defaultValue,
                   int         stepSizeFine,
                   int         stepSizeCoarse,
                   const char* unitStr   = "",
                   bool        forceSign = false);

    ~MappedIntValue() override {}

    int Get() const { return value_; }

    const int* GetPtr() const { return &value_; }

    void Set(int newValue);

    operator int() const { return value_; }

    MappedIntValue& operator=(int val)
    {
        Set(val);
        return *this;
    }

    // inherited form MappedValue
    void AppentToString(FixedCapStrBase<char>& string) const override;

    // inherited form MappedValue
    void ResetToDefault() override;

    // inherited form MappedValue
    float GetAs0to1() const override;

    // inherited form MappedValue
    void SetFrom0to1(float normalizedValue0to1) override;

    void Step(int16_t numStepsUp, bool useCoarseStepSize) override;

  private:
    int         value_;
    const int   min_;
    const int   max_;
    const int   default_;
    const int   stepSizeFine_;
    const int   stepSizeCoarse_;
    const char* unitStr_;
    const bool  forceSign_;
};
class MappedStringListValue : public MappedValue
{
  public:
    MappedStringListValue(const char** itemStrings,
                          uint16_t     numItems,
                          uint32_t     defaultIndex);

    ~MappedStringListValue() override {}

    int GetIndex() const { return index_; }

    const char* GetString() const { return itemStrings_[index_]; }

    const uint32_t* GetIndexPtr() const { return &index_; }

    void SetIndex(uint32_t index);

    operator int() const { return index_; }

    operator const char*() const { return itemStrings_[index_]; }

    MappedStringListValue& operator=(int index)
    {
        SetIndex(index);
        return *this;
    }

    // inherited form MappedValue
    void AppentToString(FixedCapStrBase<char>& string) const override;

    // inherited form MappedValue
    void ResetToDefault() override;

    // inherited form MappedValue
    float GetAs0to1() const override;

    // inherited form MappedValue
    void SetFrom0to1(float normalizedValue0to1) override;

    void Step(int16_t numStepsUp, bool useCoarseStepSize) override;

  private:
    uint32_t     index_;
    const char** itemStrings_;
    const int    numItems_;
    uint32_t     defaultIndex_;
};


} // namespace daisy