desc:GPIO控制灯光报警

main
xiaowusky 1 year ago
parent 7643244a5a
commit 8d66a9b0cb

@ -196,16 +196,13 @@ abstract class BaseActivity : AppCompatActivity() {
return true return true
} }
} else if (keyCode == KeyEvent.KEYCODE_F1) { } else if (keyCode == KeyEvent.KEYCODE_F1) {
LogUtils.w("BaseActivity F1 pressed") LogUtils.w("BaseActivity F1 up")
playSound() SoundUtils.stopSoundIo()
return true
} }
return super.dispatchKeyEvent(event) return super.dispatchKeyEvent(event)
} }
private fun playSound() {
SoundUtils.playSound()
}
private fun dealActionDown(event: KeyEvent): Boolean { private fun dealActionDown(event: KeyEvent): Boolean {
val repeatCount = event.repeatCount val repeatCount = event.repeatCount
val keyCode = event.keyCode val keyCode = event.keyCode
@ -224,6 +221,9 @@ abstract class BaseActivity : AppCompatActivity() {
} }
} else if (keyCode == KeyEvent.KEYCODE_ENTER) { } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
isHomeLongPress = repeatCount != 0 isHomeLongPress = repeatCount != 0
} else if (keyCode == KeyEvent.KEYCODE_F1) {
LogUtils.w("BaseActivity F1 pressed")
SoundUtils.playSoundIo()
} }
return super.dispatchKeyEvent(event) return super.dispatchKeyEvent(event)
} }

@ -57,7 +57,6 @@ class HomeActivity : NoOptionsActivity() {
@RequiresApi(Build.VERSION_CODES.R) @RequiresApi(Build.VERSION_CODES.R)
override fun initView() { override fun initView() {
SoundUtils.init(this, R.raw.sound)
initTopbarHelper() initTopbarHelper()
HeartbeatService.actionStart(this@HomeActivity) HeartbeatService.actionStart(this@HomeActivity)
setForCamera() setForCamera()

@ -3,6 +3,10 @@ package com.yinuo.safetywatcher.watcher.utils
import android.content.Context import android.content.Context
import android.media.AudioAttributes import android.media.AudioAttributes
import android.media.SoundPool import android.media.SoundPool
import com.common.commonlib.utils.LogUtils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
object SoundUtils { object SoundUtils {
@ -20,7 +24,7 @@ object SoundUtils {
private var soundId = -1 private var soundId = -1
fun init(context: Context, resID: Int){ fun init(context: Context, resID: Int) {
soundId = soundPool.load(context, resID, 1); // 替换为你的音效资源文件 soundId = soundPool.load(context, resID, 1); // 替换为你的音效资源文件
} }
@ -28,4 +32,24 @@ object SoundUtils {
// 播放音效 // 播放音效
soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f); // 参数依次为音效ID、左声道音量、右声道音量、优先级、循环次数、播放速度 soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f); // 参数依次为音效ID、左声道音量、右声道音量、优先级、循环次数、播放速度
} }
var soundPlaying = false
fun playSoundIo() {
GPIOUtils.setGpioDirection(152, "out")
var gpioValue = 0
soundPlaying = true
GlobalScope.launch {
while (soundPlaying) {
gpioValue = if (gpioValue > 0) 0 else 1
LogUtils.w("cyy gpioValue ${gpioValue}")
GPIOUtils.setGpioValue(152, gpioValue)
delay(500)
}
}
}
fun stopSoundIo() {
soundPlaying = false
}
} }
Loading…
Cancel
Save