From b446522003aae446d05fcb8924a088c3c181c045 Mon Sep 17 00:00:00 2001 From: xiaowusky Date: Thu, 1 Feb 2024 15:51:09 +0800 Subject: [PATCH] =?UTF-8?q?desc:=E5=85=B3=E6=9C=BA=E6=97=B6=E4=B8=BB?= =?UTF-8?q?=E5=8A=A8=E5=85=B3=E9=97=AD=E6=B0=94=E6=B3=B5gpio=EF=BC=9B=20?= =?UTF-8?q?=E9=95=BF=E6=8C=89=E5=BC=B9=E5=87=BA=E5=85=B3=E6=9C=BA=E5=BC=B9?= =?UTF-8?q?=E6=A1=86=E4=B8=8D=E5=86=8D=E9=80=9A=E8=BF=87up=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E5=86=8Ddown=E4=B8=AD=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../watcher/base/BaseActivity.kt | 40 +++++++------------ .../safetywatcher/watcher/utils/GPIOUtils.kt | 40 ++++++++++++++++--- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/base/BaseActivity.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/base/BaseActivity.kt index 1143b19..b279ff7 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/base/BaseActivity.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/base/BaseActivity.kt @@ -17,7 +17,6 @@ import android.widget.SeekBar import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.AppCompatRadioButton -import androidx.core.view.postDelayed import androidx.lifecycle.lifecycleScope import com.common.commonlib.utils.LogUtils import com.common.commonlib.utils.MMKVUtils @@ -25,9 +24,9 @@ import com.loper7.date_time_picker.number_picker.NumberPicker import com.yinuo.safetywatcher.R import com.yinuo.safetywatcher.databinding.ActivityBaseBinding import com.yinuo.safetywatcher.watcher.constant.NAMESPACE_ANDROID -import com.yinuo.safetywatcher.watcher.port.GasPortUtils import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog import com.yinuo.safetywatcher.watcher.ui.view.CommonTopBar +import com.yinuo.safetywatcher.watcher.utils.GPIOUtils import com.yinuo.safetywatcher.watcher.utils.PlatformUtils import com.yinuo.safetywatcher.watcher.utils.RecordHelper import com.yinuo.safetywatcher.watcher.utils.SoundUtils @@ -72,22 +71,11 @@ abstract class BaseActivity : AppCompatActivity() { } } - private val shoutDownListener: (ByteArray) -> Unit = { - if (it[0] == 0x01.toByte() && it[1] == 0x06.toByte() && it[2] == 0x05.toByte() && it[5] == 0x00.toByte()) { - if (shoutDowning) { - shoutDowning = false - PlatformUtils.shutDown() - } - } - } - - var shoutDowning = false open fun onShutDown() { - GasPortUtils.addListener(shoutDownListener) - shoutDowning = true - GasPortUtils.setSpeed(0) // 暂停录制编码 RecordHelper.stopRecording() + GPIOUtils.shoutDownMotor() + PlatformUtils.shutDown() } private val setVoiceAreaRunnable = Runnable { @@ -237,13 +225,11 @@ abstract class BaseActivity : AppCompatActivity() { override fun onDestroy() { super.onDestroy() closeLoadingDialog() - GasPortUtils.removeListener(shoutDownListener) GlobalScope.launch(Dispatchers.IO) { MMKVUtils.sync() } } - var isHomeLongPress = false var isLeftRightLongPress = false override fun dispatchKeyEvent(event: KeyEvent): Boolean { LogUtils.w("cyy dispatchKeyEvent keyCode = ${event?.keyCode} count = ${event?.repeatCount} acton = ${event?.action}") @@ -264,13 +250,6 @@ abstract class BaseActivity : AppCompatActivity() { baseBinding.areaSound.postDelayed(hideVoiceAreaRunnable, 1500) return true } - } else if (keyCode == KeyEvent.KEYCODE_ENTER) { - if (isHomeLongPress) { - lastFocusView = currentFocus - baseBinding.layoutShutdown.visibility = View.VISIBLE - baseBinding.tvCancel.requestFocus() - return true - } } else if (keyCode == KeyEvent.KEYCODE_F1) { LogUtils.w("BaseActivity F1 up") SoundUtils.stopSoundIo() @@ -279,6 +258,8 @@ abstract class BaseActivity : AppCompatActivity() { return super.dispatchKeyEvent(event) } + private var centerDownTime = 0L + private fun dealActionDown(event: KeyEvent): Boolean { val repeatCount = event.repeatCount val keyCode = event.keyCode @@ -289,10 +270,17 @@ abstract class BaseActivity : AppCompatActivity() { var mediaVolume = baseBinding.sbSound.progress if (KeyEvent.KEYCODE_DPAD_LEFT == keyCode) mediaVolume-- else mediaVolume++ baseBinding.sbSound.progress = mediaVolume - return super.dispatchKeyEvent(event) } } else if (keyCode == KeyEvent.KEYCODE_ENTER) { - isHomeLongPress = repeatCount > 8 + if (repeatCount == 0) { + centerDownTime = System.currentTimeMillis() + } else { + if (System.currentTimeMillis() - centerDownTime > 2000 && baseBinding.layoutShutdown.visibility == View.GONE) { + lastFocusView = currentFocus + baseBinding.layoutShutdown.visibility = View.VISIBLE + baseBinding.tvCancel.requestFocus() + } + } } else if (keyCode == KeyEvent.KEYCODE_F1) { LogUtils.w("BaseActivity F1 pressed") SoundUtils.playSoundIo() diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/GPIOUtils.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/GPIOUtils.kt index 579f0c5..00fd3b7 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/GPIOUtils.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/GPIOUtils.kt @@ -10,7 +10,7 @@ import java.io.IOException object GPIOUtils { private const val TAG: String = "GPIOUtils" - fun setGpioDirection(gpio: Int, direction: String) { + fun setGpioDirection(gpio: Int, direction: String) { val path = "/sys/class/gpio/gpio$gpio/direction" if (File(path).exists()) { var writer: FileWriter? = null @@ -37,7 +37,7 @@ object GPIOUtils { * @param gpio 端口号 * @param value 1高电平 0低电平 */ - fun setGpioValue(gpio: Int, value: Int) { + fun setGpioValue(gpio: Int, value: Int) { val path = "/sys/class/gpio/gpio$gpio/value" if (File(path).exists()) { var writer: FileWriter? = null @@ -60,7 +60,7 @@ object GPIOUtils { } } - fun getGpioValue(gpio: Int): String? { + fun getGpioValue(gpio: Int): String? { val path = "/sys/class/gpio/gpio$gpio/value" if (File(path).exists()) { var reader: FileReader? = null @@ -79,12 +79,42 @@ object GPIOUtils { return null } - fun openCamera(){ + fun openCamera() { setGpioDirection(123, "out") setGpioValue(123, 1) } - fun closeCamera(){ + fun closeCamera() { setGpioValue(123, 0) } + + /** + * 关闭电机 + */ + fun shoutDownMotor() { + setAttributeValue(0) + } + + private fun setAttributeValue(value: Int) { + val path = "/sys/class/leds/moto_en/brightness" + if (File(path).exists()) { + var writer: FileWriter? = null + try { + writer = FileWriter(path) + writer.write(value.toString()) + writer.flush() + } catch (ex: IOException) { + ex.printStackTrace() + } catch (ex: NumberFormatException) { + ex.printStackTrace() + } finally { + if (writer != null) { + try { + writer.close() + } catch (ex: IOException) { + } + } + } + } + } } \ No newline at end of file