package com.example.piotrek.marsboard_rk3066_gpio_example;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void GPIO286init(View view) {

        String command = String.format("echo 286 > /sys/class/gpio/export");
        try {
            Runtime.getRuntime().exec(new String[]{"su", "-c", command});

        } catch (IOException e) {

        }
       command = String.format("echo \"out\" > /sys/class/gpio/gpio286/direction ");
        try {
            Runtime.getRuntime().exec(new String[]{"su", "-c", command});

        } catch (IOException e) {

        }
    }


    public void GPIO286setLow(View view) {
        String command = String.format("echo 0 > /sys/class/gpio/gpio286/value ");
        try {
            Runtime.getRuntime().exec(new String[]{"su", "-c", command});

        } catch (IOException e) {

        }
    }

    public void GPIO286setHigh(View view) {
        String command = String.format("echo 1 > /sys/class/gpio/gpio286/value ");
        try {
            Runtime.getRuntime().exec(new String[]{"su", "-c", command});

        } catch (IOException e) {

        }

    }


    public void GPIO286read(View view) {
        TextView GPIO286input = (TextView) findViewById(R.id.textView2);

        Process p;
        String command = String.format("cat /sys/class/gpio/gpio286/value");
        try {
            p = Runtime.getRuntime().exec(new String[] {"su", "-c", command});
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder text = new StringBuilder();
            String line;
            while((line = reader.readLine()) != null){
                text.append(line);
                text.append("\n");


            }
            GPIO286input.setText( text.toString());
        } catch (IOException e) {

        GPIO286input.setText("error");

    }
}

    public void GPIO283init(View view) {
        String command = String.format("echo 283 > /sys/class/gpio/export");
        try {
            Runtime.getRuntime().exec(new String[]{"su", "-c", command});

        } catch (IOException e) {

        }
        command = String.format("echo \"out\" > /sys/class/gpio/gpio283/direction ");
        try {
            Runtime.getRuntime().exec(new String[]{"su", "-c", command});

        } catch (IOException e) {

        }
    }

    public void GPIO283setLow(View view) {
        String command = String.format("echo 0 > /sys/class/gpio/gpio283/value ");
        try {
            Runtime.getRuntime().exec(new String[]{"su", "-c", command});

        } catch (IOException e) {

        }
    }

    public void GPIO283setHigh(View view) {
        String command = String.format("echo 1 > /sys/class/gpio/gpio283/value ");
        try {
            Runtime.getRuntime().exec(new String[]{"su", "-c", command});

        } catch (IOException e) {

        }
    }

    public void GPIO283read(View view) {
        TextView GPIO283input = (TextView) findViewById(R.id.textView3);

        Process p;
        String command = String.format("cat /sys/class/gpio/gpio283/value");
        try {
            p = Runtime.getRuntime().exec(new String[] {"su", "-c", command});
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder text = new StringBuilder();
            String line;
            while((line = reader.readLine()) != null){
                text.append(line);
                text.append("\n");

            }
            GPIO283input.setText( text.toString());
        } catch (IOException e) {
            Log.v("dd", "blad");
            GPIO283input.setText("error");

        }
    }
}
