From 4a29b3adc9ab0f0ad47a1f744e2d186027dad6e5 Mon Sep 17 00:00:00 2001 From: zxj <275873859@qq.com> Date: Thu, 7 Aug 2025 17:54:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=81=AF=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SecondaryScreenPresentation.kt | 8 +- .../java/com/sw/plate/utils/GpioUtils.java | 175 ++++++++++++++++++ .../java/com/sw/plate/utils/LightManager.java | 25 +++ 3 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 lib_face/src/main/java/com/sw/plate/utils/GpioUtils.java create mode 100644 lib_face/src/main/java/com/sw/plate/utils/LightManager.java diff --git a/app/src/main/java/com/sw/dualscreen/presentation/SecondaryScreenPresentation.kt b/app/src/main/java/com/sw/dualscreen/presentation/SecondaryScreenPresentation.kt index cfb6468..e4e7dca 100644 --- a/app/src/main/java/com/sw/dualscreen/presentation/SecondaryScreenPresentation.kt +++ b/app/src/main/java/com/sw/dualscreen/presentation/SecondaryScreenPresentation.kt @@ -1,7 +1,6 @@ package com.sw.dualscreen.presentation import android.Manifest -import android.R.attr.visibility import android.app.Presentation import android.content.Context import android.content.pm.PackageManager @@ -36,6 +35,7 @@ import com.sw.dualscreen.model.response.UserNutritionData import com.sw.dualscreen.utils.Debouncer import com.sw.dualscreen.utils.GlideUtils import com.sw.dualscreen.viewmodel.UserViewModel +import com.sw.plate.utils.LightManager import com.sw.plate.utils.ToastUtils import com.sw.plate.utils.arcface.ConfigUtil import com.sw.plate.utils.arcface.ErrorCodeUtil @@ -162,6 +162,8 @@ class SecondaryScreenPresentation( fun step1() { Timber.d("step1") currentStep = 1 + LightManager.closeGreenLight() + LightManager.openRedLight() //回复上一次的设置 // lastWeight = 0.0 dinnerTypeInfo = null @@ -186,7 +188,8 @@ class SecondaryScreenPresentation( fun step2(foodInfo: FoodInfo) { Timber.d("step2") currentStep = 2 - + LightManager.openRedLight() + LightManager.openGreenLight() dinnerTypeInfo = null userNutritionData = null recognitionWeight = 0.0 @@ -233,6 +236,7 @@ class SecondaryScreenPresentation( */ fun step3() { Timber.d("step3") + LightManager.closeRedLight() currentStep = 3 stepChangeCallback(currentStep) if (currentFood == null) { diff --git a/lib_face/src/main/java/com/sw/plate/utils/GpioUtils.java b/lib_face/src/main/java/com/sw/plate/utils/GpioUtils.java new file mode 100644 index 0000000..ed6e82f --- /dev/null +++ b/lib_face/src/main/java/com/sw/plate/utils/GpioUtils.java @@ -0,0 +1,175 @@ +package com.sw.plate.utils; + +import android.os.SystemClock; +import android.util.Log; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.DataOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +/** + * Created by Administrator on 2018/11/9. + */ +public class GpioUtils { + + private static final String TAG = "GpioUtils"; + + /* + 给export申请权限 + */ + public static void upgradeRootPermissionForExport() { + upgradeRootPermission("/sys/class/gpio/export"); + } + + /* + 配置一个gpio路径 + */ + public static boolean exportGpio(int gpio) { + return writeNode("/sys/class/gpio/export", "" + gpio); + } + + /* + 给获取io口的状态的路径申请权限,该方法需要在exportGpio后调用 + */ + public static void upgradeRootPermissionForGpio(int gpio) { + upgradeRootPermission("/sys/class/gpio/gpio" + gpio + "/direction"); + upgradeRootPermission("/sys/class/gpio/gpio" + gpio + "/value"); + } + + + /* + 设置io口为输入或输出 + */ + public static boolean setGpioDirection(int gpio, int arg) { + String gpioDirection = ""; + if (arg == 0) gpioDirection = "out"; + else if (arg == 1) gpioDirection = "in"; + else return false; + return writeNode("/sys/class/gpio/gpio" + gpio + "/direction", gpioDirection); + } + + /* + 获取io口的状态为输出还是输入 + */ + public static String getGpioDirection(int gpio) { + String gpioDirection = ""; + gpioDirection = readNode("/sys/class/gpio/gpio" + gpio + "/direction"); + return gpioDirection; + } + + /* + 给输出io口写值,高电平或低电平 + */ + public static boolean writeGpioValue(int gpio, String arg) { + return writeNode("/sys/class/gpio/gpio" + gpio + "/value", arg); + } + + //获取当前gpio是高还是低 + public static String getGpioValue(int gpio) { + return readNode("/sys/class/gpio/gpio" + gpio + "/value"); + } + + + private static boolean upgradeRootPermission(String path) { + Process process = null; + DataOutputStream os = null; + try { + String cmd = "chmod 777 " + path; + process = Runtime.getRuntime().exec("su"); //切换到root帐号 + os = new DataOutputStream(process.getOutputStream()); + os.writeBytes(cmd + "\n"); + os.writeBytes("exit\n"); + os.flush(); + process.waitFor(); + } catch (Exception e) { + } finally { + try { + if (os != null) { + os.close(); + } + process.destroy(); + } catch (Exception e) { + } + } + try { + return process.waitFor() == 0; + } catch (InterruptedException e) { + e.printStackTrace(); + } + return false; + } + + private static boolean writeNode(String path, String arg) { + Log.d(TAG, "Gpio_test set node path: " + path + " arg: " + arg); + if (path == null || arg == null) { + Log.e(TAG, "set node error"); + return false; + } + FileWriter fileWriter = null; + BufferedWriter bufferedWriter = null; + try { + fileWriter = new FileWriter(path); + fileWriter.write(arg); + } catch (Exception e) { + Log.e(TAG, "Gpio_test write node error!! path" + path + " arg: " + arg); + e.printStackTrace(); + return false; + } finally { + try { + if (fileWriter != null) { + fileWriter.close(); + } + if (bufferedWriter != null) { + bufferedWriter.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return true; + } + + private static long mTime = 0; + private static int mFailTimes = 0; + + private static String readNode(String path) { + String result = ""; + + FileReader fread = null; + BufferedReader buffer = null; + try { + fread = new FileReader(path); + buffer = new BufferedReader(fread); + String str = null; + while ((str = buffer.readLine()) != null) { + result = str; + break; + } + mFailTimes = 0; + } catch (IOException e) { + Log.e(TAG, "IO Exception"); + e.printStackTrace(); + if (mTime == 0 || SystemClock.uptimeMillis() - mTime < 1000) { + mFailTimes++; + } + if (mFailTimes >= 3) { + Log.d(TAG, "read format node continuous failed three times, exist thread"); + } + } finally { + try { + if (buffer != null) { + buffer.close(); + } + if (fread != null) { + fread.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return result; + } +} diff --git a/lib_face/src/main/java/com/sw/plate/utils/LightManager.java b/lib_face/src/main/java/com/sw/plate/utils/LightManager.java new file mode 100644 index 0000000..be8c13e --- /dev/null +++ b/lib_face/src/main/java/com/sw/plate/utils/LightManager.java @@ -0,0 +1,25 @@ +package com.sw.plate.utils; + +public class LightManager { + private static final String TAG = "LightManager"; + + public static void openGreenLight() { + L.e(TAG, "openGreenLight"); + GpioUtils.writeGpioValue(41, "1"); + } + + public static void closeGreenLight() { + L.e(TAG, "closeGreenLight"); + GpioUtils.writeGpioValue(41, "0"); + } + + public static void openRedLight() { + L.e(TAG, "openRedLight"); + GpioUtils.writeGpioValue(40, "1"); + } + + public static void closeRedLight() { + L.e(TAG, "closeRedLight"); + GpioUtils.writeGpioValue(40, "0"); + } +}