Hardware Interface API

1. Led control

Example code for controlling Led:

//red light
HardwareCtrl.ctrlLedSwitch( HardwareCtrl.LED_RED, true);
or
HardwareCtrl.ctrlLedRed(true);

//green light
HardwareCtrl.ctrlLedSwitch( HardwareCtrl.LED_GREEN, true);
or
HardwareCtrl.ctrlLedGreen(true);

//white light
HardwareCtrl.ctrlLedSwitch( HardwareCtrl.LED_WHITE, true);
or
HardwareCtrl.ctrlLedWhite(true);

//Only turn on the white light and turn off the other Led lights.
HardwareCtrl.ctrlLedOnlyOpenWhite();//green light only:ctrlLedOnlyOpenGreen(), red light only:ctrlLedOnlyOpenRed();

//Hardware version 2.3 and above supports adjusting the brightness of white led light, while green light and red light do not support adjusting the brightness.
HardwareCtrl.ctrlWhiteLightness(0);//Control the brightness of white led light: value range: 0-8

2. Control screen brightness

public static void setBrightness(int value)

Function   Adjust screen brightness

Parameter   value : range: 0 to 255


Example :

HardwareCtrl.setBrightness(255);

3. Backlight control switch

public static void ctrlBlPower(boolean open)

Function   Backlight control switch

Parameter   open :true is on, false is off


Example :

HardwareCtrl.ctrlBlPower(true);

4. Screen touch switch

public static void ctrlTp(boolean open)

Function   Screen touch switch

Parameter   open : true is on, false is off


Example :

HardwareCtrl.ctrlTp(true);

5. RS485 / RS232 signal

Open RS485/RS232 serial port

public static SerialPort openSerialPortSignal(File device, int baudrate, SerialPort.Callback callback)

Function   Open RS485/RS232 serial port

Parameter   device : Serial port file
         baudrate : Baud rate
         callback : callback


Send signal

public static void sendSerialPortHexMsg(SerialPort mSerialPort, String msg)

Function   Send signal

Parameter   mSerialPort : Serial port object
         msg :Signal (hex string, such as “1E60010000002F”)


Close the RS485/RS232 serial port

public statis void closeSerialPortSignal(SerialPort mSerialPort)

Function   Close the RS485/RS232 serial port

Parameter   void


Example :

// Enter related content
/ **
such as:
1.A opens the gate to the credit card, the upper computer needs to send hexadecimal data:
Send: 0x1E 0x60 0x01 0x00 0x00 0x00 0x2F
The return code of the gate is divided into the following types:
a), the person has passed the gate
Returns: 0x1E 0x61 0x01 0x00 0x00 0x00 0x2F
b) The card does not pass the gate after a timeout after the card is swiped, the gate automatically closes the door and cancels the passage
Returns: 0x1E 0x44 0x01 0x00 0x00 0x00 0x2F
c) After the card is swiped, someone passes the gate in the reverse direction, and the gate is automatically closed to cancel the passage.
Returns: 0x1E 0x44 0x01 0x00 0x00 0x00 0x2F
*/
//Open RS485/RS232 serial port
SerialPort mSerialPort = HardwareCtrl.openSerialPortSignal(new File("dev/ttyS4"), 9600, new SerialPort.Callback() {
     @Override
     public void onDataReceived(byte[] buffer, int size) {
         String result = StringUtils.bytesToHexString(buffer, size);
         Log.e("lkdong","result = "+result);
     }
});

//Send signal
HardwareCtrl.sendSerialPortHexMsg(mSerialPort, "1E60010000002F");
//Close the RS485/RS232 serial port
HardwareCtrl.closeSerialPortSignal(mSerialPort);
Note:
1. The old interface of 485 signal control can be used continuously, such as:HardwareCtrl.openRs485Signal(File device, int baudrate, SerialPort.Callback callback)、 HardwareCtrl.sendRs485Signal(SerialPort mSerialPort, String msg) and HardwareCtrl.closeRs485Signal(SerialPort mSerialPort)

2. 485 serial port:/dev/ttyS4 ; 232 serial port:/dev/ttyS3


6. Wigan 26/34 signal

Wigan 26

public static void sendWiegandSignal(String msg)

Function   Wiegand Signal Control

Parameter   msg :


Wigan 34

public static void sendWiegand34Signal(String msg)

Function   Wiegand Signal Control

Parameter   msg :


Example :

//Enter related content, such as card number, etc.
HardwareCtrl.sendWiegandSignal("1233456789");
Note:
1. Wiegand 26 and Wiegand 34 share the same line, so only one of them can be used at a time

7.Wigan input

public static void recvWiegandSignal(RecvWiegandCallBack callBack)

Function   Wiegand signal input

Parameter   callBack : Wiegand input signal return value interface


Example :

HardwareCtrl.openRecvMiegandSignal("/dev/wiegand");//Open serial port
HardwareCtrl.recvWiegandSignal(new RecvWiegandCallBack() {
    @Override
    public void recvWiegandMsg(int i) {
       Log.e("lkdong","recvWiegandMsg = "+i);
    }
});
HardwareCtrl.closeRecvMiegandSignal();

8. RelaySignal

public static void sendRelaySignal(boolean up)

Function   Relay control

Parameter   up : The signal is pulled up to true and pulled down to false.


Example :

HardwareCtrl.sendRelaySignal(!HardwareCtrl.getRelayValue());

9. Radar

The working principle of the radar function: within the radar radiation range, with or without object activity, the key value is reported to the Android application layer, and developers can develop according to their own needs.

Example :

public class MainActivity extends AppCompatActivity {
    //Radar key value pair
    public static final int KEYCODE_RADAR_IN = 305;//There is object activity
    public static final int KEYCODE_RADAR_OUT = 306;//No object activity

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.v("lkdong", "dispatchKeyEvent keycode=" + event.getKeyCode());
        if (event.getKeyCode() == KEYCODE_RADAR_IN) {//Radar reports object activity
            if (event.getAction() == KeyEvent.ACTION_UP) {
		//TODO
            }
        } else if (event.getKeyCode() == KEYCODE_RADAR_OUT) {//Radar reports no object activity
            if (event.getAction() == KeyEvent.ACTION_UP) {
		//TODO
            }
        } 
        return super.dispatchKeyEvent(event);
    }

}

10.NFC

The principle of the nfc function: When the card is swiped, the system will report the key value to the Android application layer, and save the card information on the / dev / dl1825 node. When developing an NFC card reader, you need to read the “/dev/dl1825” node value after receiving the system report key value.

Example :

public class MainActivity extends AppCompatActivity {
    //NFC key pair
    public static final int KEYCODE_NFC=307;

     @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.v("lkdong", "dispatchKeyEvent keycode=" + event.getKeyCode());
        if (event.getKeyCode() == KEYCODE_NFC) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                //NFC card reading, please refer to the bottom gpiodemo for detailed implementation process.
		String mNfcCode = NfcUtil.getCode();

            }
        }
        return super.dispatchKeyEvent(event);
    }

}

11.Qrcode

The two-dimensional code reading is mainly performed through the serial port. The specific operation process is as follows:

Example :

public class QrCodeUtil {
    public static final String TAG = QrCodeUtil.class.getSimpleName();
    //The current command is based on module e3000h
    public static final byte[] ENABLE_QR_CONFIG = new byte[]{0x07, (byte) 0xC6, 0x04, 0x08, 0x00, (byte) 0xEC, 0x01, (byte) 0xFE, 0x3A};
    public static final byte[] DISABLE_QR_CONFIG = new byte[]{0x07, (byte) 0xC6, 0x04, 0x08, 0x00, (byte) 0xEC, 0x00, (byte) 0xFE, 0x3B};
    public static final byte[] LED_ON = new byte[]{0x08, (byte) 0xC6, 0x04, 0x08, 0x00, (byte) 0xF2, 0x02, 0x01, (byte) 0xFE, 0x31};
    public static final byte[] LED_OFF = new byte[]{0x08, (byte) 0xC6, 0x04, 0x08, 0x00, (byte) 0xF2, 0x02, 0x02, (byte) 0xFE, 0x30};
    public static final byte[] LED_AUTO = new byte[]{0x08, (byte) 0xC6, 0x04, 0x08, 0x00, (byte) 0xF2, 0x02, 0x00, (byte) 0xFE, 0x32};
    public static final byte[] COMMADN_RESULT = new byte[]{0x04, (byte) 0xd0, 0x00, 0x00, (byte) 0xff, 0x2c};
    private SerialPort mQrCodeSerialPort = null;
    private static QrCodeUtil sQrCodeUtil = null;
    private static boolean sQrCodeSupport = true;

    /**
     * Enable QR code recognition and open serial port
     */
    public static void open() {
        if (isQrCodeSupport() && sQrCodeUtil != null) {
            sQrCodeUtil.openQrCodeSerialPort(new SerialPort.Callback() {
                @Override
                public void onDataReceived(byte[] bytes, int i) {
                    if (QrCodeUtil.isCommandResult(bytes, i)) {
                    } else {
                        String s = new String(bytes, 0, i);
                        Log.v(TAG, "QR string " + s);
                        if (sQrCodeUtil.mQRCodeCallback != null) {
                            sQrCodeUtil.mQRCodeCallback.onQrCodeData(s);
                        }
                    }

                }
            });
        }
    }

    /**
     * Close the QR code identification serial port
     */
    public static void close() {
        if (isQrCodeSupport() && sQrCodeUtil != null) {
            sQrCodeUtil.closeQrCodeSerialPort();
        }
    }

    /**
     * Close the serial port and release
     */
    public static void destory() {
        if (isQrCodeSupport() && sQrCodeUtil != null) {
            sQrCodeUtil.closeQrCodeSerialPort();
            sQrCodeUtil = null;
        }
    }


    /**
     * Open serial port
     *
     * @param qrCodeDataCallBack
     */
    private void openQrCodeSerialPort(SerialPort.Callback qrCodeDataCallBack) {
        if (mQrCodeSerialPort == null) {
            Log.v(TAG, "openQrCodeSerialPort===");
            mQrCodeSerialPort = HardwareCtrl.openSerialPortSignal(new File("/dev/ttyS0"), 9600, qrCodeDataCallBack);
            disableQRCodeConfig();
        }
    }

    /**
     * Close serial port
     */
    private void closeQrCodeSerialPort() {
        Log.v(TAG, "closeQrCodeSerialPort()");
        if (mQrCodeSerialPort != null) {
            HardwareCtrl.closeQrCodeSerialPort(mQrCodeSerialPort);
            mQrCodeSerialPort = null;
        }
    }

    /**
     * Turn on identification configuration QR code function
     */
    private void enableQRCodeConfig() {
        if (mQrCodeSerialPort != null) {
            mQrCodeSerialPort.sendHexMsg(ENABLE_QR_CONFIG);
        }
    }

    /**
     * Turn off identification configuration QR code function
     */
    private void disableQRCodeConfig() {
        if (mQrCodeSerialPort != null) {
            mQrCodeSerialPort.sendHexMsg(DISABLE_QR_CONFIG);
        }
    }

    /**
     * Qrcode light automatically turns on during scanning
     */
    private void setLEDAuto() {
        if (mQrCodeSerialPort != null) {
            mQrCodeSerialPort.sendHexMsg(LED_AUTO);
        }
    }

    /**
     * Qrcode light is always off
     */
    private void setLEDOFF() {
        if (mQrCodeSerialPort != null) {
            mQrCodeSerialPort.sendHexMsg(LED_OFF);
        }
    }

    /**
     * Qrcode light on all the time
     */
    private void setLEDON() {
        if (mQrCodeSerialPort != null) {
            mQrCodeSerialPort.sendHexMsg(LED_ON);
        }
    }

    /**
     * Configure QR code scan qrcode light status.
     * At present, it is only opened and closed automatically during scanning
     *
     * @param isAuto
     */
    public static void setLedState(boolean isAuto) {
        if (isQrCodeSupport() && sQrCodeUtil != null) {
            if (isAuto)
                sQrCodeUtil.setLEDAuto();
            else
                sQrCodeUtil.setLEDOFF();
        }
    }

    public interface QRCodeCallback {
        public void onQrCodeData(String s);
    }

    private QRCodeCallback mQRCodeCallback;

    /**
     * Support successful monitoring of QR code scanning
     *
     * @param callback
     */
    public void setQRCodeCallback(QRCodeCallback callback) {
        mQRCodeCallback = callback;
    }
}

12. Normal GPIO control

D0 Signal

public static void sendSignalD0(boolean up)

Function    GPIO D0 signal control

Parameter   up : false is pulled low, true is pulled high


Example :

HardwareCtrl.sendSignalD0(true);

D1 Signal

public static void sendSignalD1(boolean up)

Function   GPIO D1 signal control

Parameter   up : false is pulled low, true is pulled high


Example :

HardwareCtrl.sendSignalD1(true);

13. Shutdown

public static void shutdown()

Function   Shutdown

Parameter   void


Example :

HardwareCtrl.shutdown();

14. reboot

public static void reboot()

Function   reboot

Parameter   void


Example :

HardwareCtrl.reboot();

15. Watchdog

public static void setWdt(int value)

Function   System freezes or does not respond for a long time, reboot the device

Parameter   value : Valid values are integer values between 0 (inclusive) and 3 (inclusive).
         0 means reboot after 0.46s of crash
         1 means reboot after 2.56s of crash
         2 means reboot after 10.24s of crash
         3 means reboot after 40.96s of crash


Example :

HardwareCtrl.ctrlWdt(1);

16. Get device unique ID

public static String getFireflyCid()

Function   device unique ID

Parameter   void


Example :

String cid = HardwareCtrl.getFireflyCid();

17. Other commands

public static void execSuCmd(String command)

Function   Run command through shell

Parameter   command:command


Example :

//Such as syncing files, etc.
HardwareCtrl.execSuCmd("sync");

18. Other GPIO use

public static int gpioParse(String gpioStr)

Function   Convert gpio name to corresponding gpio encoding

Parameter   gpioStr:gpio name, such as GPIO2_A2


Controlling GPIO

public static void ctrlGpio(int gpio, String direction, int value)

Function   Controlling GPIO

Parameter   gpio:gpio encoding, such as 152
         direction :
         value : Set GPIO value


Example :

HardwareCtrl.ctrlGpio(HardwareCtrl.gpioParse("GPIO2_A2"), "out", 1);