package com.phlx.anchorcollect.rfid;

import android.os.SystemClock;

import com.phlx.anchorcollect.Configs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 上电操作
 */
class DeviceControl {

    private void WriteFile(String path, String value) throws IOException {
        File DeviceName = new File(path);
        if (DeviceName.exists()) {
            BufferedWriter CtrlFile = new BufferedWriter(new FileWriter(DeviceName, false));
            CtrlFile.write(value);
            CtrlFile.flush();
            CtrlFile.close();
        }
    }


    void PowerOn_pad_RM_SD100T_MT1( ) {
        try {
            //pad RM_SD100T_MT1
            WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "mode75 0");
            WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "out75 1");
            WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "out55 1");
            Thread.sleep(3000);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    void PowerOff_pad_RM_SD100T_MT1() {
        try {
            //pad RM_SD100T_MT1
            WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "out75 0");
            WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "out55 0");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void PowerOn_pad_RM_SD1006762_S0( ) {
        try {
            //pad RM_SD1006762_S0
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 52 1");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "mode 52 0");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "dir 52 1");

            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 89 1");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "mode 89 0");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "dir 89 1");
            Thread.sleep( 3000);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    void PowerOff_pad_RM_SD1006762_S0() {
        try {
            //pad RM_SD1006762_S0
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 52 0");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 89 0");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void PowerOn_pad_RM_SD1006762_S1( ) {
        try {
            //pad RM_SD1006762_S1
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 24 1");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "mode 24 0");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "dir 24 1");

            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 55 1");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "mode 55 0");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "dir 55 1");

            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 173 1");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "mode 173 0");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "dir 173 1");
            Thread.sleep( 3000);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    void PowerOff_pad_RM_SD1006762_S1() {
        try {
            //pad RM_SD1006762_S1
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 24 0");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 55 0");
            WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 173 0");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}