package com.henrypump.poc;

/**
 * Created by patrickjmcd on 2/1/17.
 */

import mraa.Dir;
import mraa.Aio;
import mraa.mraa;
import mraa.Platform;
import mraa.Result;


import static java.lang.Thread.sleep;


public class AnalogIn {
    private int channel;
    private Aio gpioPin;
    private double lastValue;
    private double rawMin;
    private double rawMax;
    private double euMin;
    private double euMax;
    private double[] history = new double[100];


    AnalogIn(int channel, double rawMin, double rawMax, double euMin, double euMax){
        this.channel = channel;
        if (channel != 99) {
            Platform platform = mraa.getPlatformType();
            if (platform != Platform.INTEL_EDISON_FAB_C) {
                System.err.println("Error: This program can only be run on edison");
                System.exit(Result.ERROR_INVALID_PLATFORM.swigValue());
            }
            this.gpioPin = new Aio(this.channel);
        }

        this.rawMax = rawMax;
        this.rawMin = rawMin;
        this.euMax = euMax;
        this.euMin = euMin;
    }

    double getHistory(int pointIndex) {
        return history[pointIndex];
    }

    private long readRaw(){
        return gpioPin.read();
    }

    double readScaled(){
        double pv  = ((euMax - euMin)/(rawMax - rawMin)) * readRaw() + (euMax - ((euMax - euMin)/(rawMax - rawMin)) * rawMax);
        lastValue = pv;
        System.arraycopy(history, 0, history,1, history.length - 1);
        history[0] = lastValue;
        return pv;
    }

    double readScaledSim(double simRaw){
        double pv  = ((euMax - euMin)/(rawMax - rawMin)) * simRaw + (euMax - ((euMax - euMin)/(rawMax - rawMin)) * rawMax);
        lastValue = pv;
        System.arraycopy(history, 0, history,1, history.length - 1);
        history[0] = lastValue;
        return pv;
    }

    public static void main(String[] args){
        System.out.println("Testing Analog Inputs...");
        AnalogIn testPosition = new AnalogIn(0, 0, 1020, 0, 145);
        AnalogIn testLoad = new AnalogIn(1, 0, 1020, 0, 50000);
        int loops = 0;
        double currentPosition, currentLoad;
        long currentRawPosition, currentRawLoad;
        long maxRawPosition = Long.MIN_VALUE;
        long maxRawLoad = Long.MIN_VALUE;

        while(loops < 1000) {
            try {
                currentRawPosition = testPosition.readRaw();
                currentRawLoad = testLoad.readRaw();

                if (currentRawPosition > maxRawPosition){
                    maxRawPosition = currentRawPosition;
                }

                if (currentRawLoad > maxRawLoad){
                    maxRawLoad = currentRawLoad;
                }

                currentPosition = testPosition.readScaled();
                currentLoad = testLoad.readScaled();
                System.out.println("Position = " + currentPosition + ", Load = " + currentLoad);

                sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            loops++;
        }
        System.out.println("Max Raw Position = " + maxRawPosition);
        System.out.println("Max Raw Load = " + maxRawLoad);
    }
}
