#ifndef POT_H
#define POT_H

#include <ArduinoLog.h>
#include "../config.h"
#include <osr-base.h>
#include <App.h>
#include <Addon.h>
#include <Component.h>
#include "types.h"

#include <Arduino_MachineControl.h>

using namespace machinecontrol;

float res_divider = 0.28057;
float reference = 24;

class POT : public Component
{

public:
    POT(Component *owner, short _pin) : Component("POT", 0, Component::COMPONENT_DEFAULT, owner),
                                        pin(_pin)
    {
        //setFlag(OBJECT_RUN_FLAGS::E_OF_DEBUG);
    }

    int runFoo()
    {
        Log.verboseln("POT Value %f", value);
        return E_OK;
    }

    short setup()
    {
        analogReadResolution(16);
        analog_in.set0_10V();

        return E_OK;
    }

    short debug()
    {
        // Log.verbose(F("POT:debug pin=%d value=%d" CR), pin, value);
        Log.verbose(F("POT:debug pin=%d value=%d | %d" CR), pin, value, analog_in.read(0) );
        return E_OK;
    }

    short loop()
    {
        float raw_voltage_ch0 = analog_in.read(pin);
        float voltage_ch0 = (raw_voltage_ch0 * reference) / 65535 / res_divider;
        value = voltage_ch0;

        return E_OK;
    }

    int pin;
    int value;
};

#endif
