package com.henrypump.poc;

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

import mraa.Dir;
import mraa.Gpio;
import mraa.mraa;
import mraa.Platform;
import mraa.Result;

public class DigitalIn {
    private int channel;
    private Gpio gpioPin;
    private int lastValue;

    DigitalIn(int channel){
        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 Gpio(this.channel);
            gpioPin.dir(Dir.DIR_IN);
        }


    }

    public int read(){
        if (channel != 99 ) {
            lastValue = gpioPin.read();
        }
        return lastValue;
    }

    public static void main(String[] args){
        System.out.println("Testing Digital Inputs. Script will loop until ch8 and ch9 both equal 1");
        boolean forceEnd = false;
        DigitalIn testIn8 = new DigitalIn(8);
        DigitalIn testIn9 = new DigitalIn(9);

        DigitalOut testOut2 = new DigitalOut(2,0);
        DigitalOut testOut3 = new DigitalOut(3,0);

        int in8val, in9val;
        do {
            in8val = testIn8.read();
            in9val = testIn9.read();

            testOut2.write(in8val);
            testOut3.write(in9val);

            if (in8val == 1 && in9val == 1) forceEnd = true;
            testOut2.write(0);
            testOut3.write(0);
        } while (!forceEnd);
    }

}

