desc:气体浓度达到高低报警值时需要和声光报警器联动报警;且高报和低报报警频率区分。
parent
a4b059762c
commit
ebda6d1a5b
@ -1,60 +1,94 @@
|
|||||||
package com.yinuo.safetywatcher.watcher.utils
|
package com.yinuo.safetywatcher.watcher.utils
|
||||||
|
|
||||||
import android.content.Context
|
import android.os.Handler
|
||||||
import android.media.AudioAttributes
|
import android.os.HandlerThread
|
||||||
import android.media.SoundPool
|
|
||||||
import com.common.commonlib.utils.LogUtils
|
import com.common.commonlib.utils.LogUtils
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.GlobalScope
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
|
|
||||||
object SoundUtils {
|
object SoundUtils {
|
||||||
|
|
||||||
private val soundPool by lazy {
|
private const val TYPE_SOS = 0
|
||||||
// 创建 SoundPool
|
|
||||||
val audioAttributes = AudioAttributes.Builder()
|
|
||||||
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
|
|
||||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
|
||||||
.build()
|
|
||||||
SoundPool.Builder()
|
|
||||||
.setAudioAttributes(audioAttributes)
|
|
||||||
.setMaxStreams(1) // 指定最大音效流的数量
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
private var soundId = -1
|
private const val TYPE_WARN_HIGHT = 1
|
||||||
|
private const val TYPE_WARN_LOW = 2
|
||||||
|
private val soundMap = hashMapOf<Int, Boolean>(
|
||||||
|
TYPE_SOS to false,
|
||||||
|
TYPE_WARN_LOW to false,
|
||||||
|
TYPE_WARN_HIGHT to false
|
||||||
|
)
|
||||||
|
private var soundPlaying = false
|
||||||
|
private var currentPlayingDelay = 500
|
||||||
|
private var soundThread: HandlerThread? = null
|
||||||
|
private var soundHandler: Handler? = null
|
||||||
|
var gpioValue = 0
|
||||||
|
|
||||||
fun init(context: Context, resID: Int) {
|
init {
|
||||||
soundId = soundPool.load(context, resID, 1); // 替换为你的音效资源文件
|
GPIOUtils.setGpioDirection(140, "out")
|
||||||
|
soundThread = HandlerThread("sound")
|
||||||
|
soundThread!!.start()
|
||||||
|
soundHandler = Handler(soundThread!!.looper)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun playSound() {
|
fun warnSound(isHigh: Boolean) {
|
||||||
// 播放音效
|
playSoundIo(if (isHigh) TYPE_WARN_HIGHT else TYPE_WARN_LOW)
|
||||||
soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f); // 参数依次为音效ID、左声道音量、右声道音量、优先级、循环次数、播放速度
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var soundPlaying = false
|
fun warnSoundClose() {
|
||||||
|
stopSoundIo(TYPE_WARN_HIGHT)
|
||||||
|
stopSoundIo(TYPE_WARN_LOW)
|
||||||
|
}
|
||||||
|
|
||||||
fun playSoundIo() {
|
fun playSoundIo(type: Int = TYPE_SOS) {
|
||||||
if (soundPlaying) {
|
if (soundMap[type] == true && soundPlaying) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
GPIOUtils.setGpioDirection(140, "out")
|
soundMap[type] = true
|
||||||
var gpioValue = 0
|
val delayTime = getDelayTime(soundMap)
|
||||||
|
// 不需要变化时,直接返回
|
||||||
|
if (soundPlaying && delayTime == currentPlayingDelay) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!soundPlaying) {
|
||||||
|
currentPlayingDelay = delayTime
|
||||||
|
soundHandler?.post {
|
||||||
|
setVoiceIo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getDelayTime(soundMap: HashMap<Int, Boolean>): Int {
|
||||||
|
if (soundMap[TYPE_SOS] == true || soundMap[TYPE_WARN_HIGHT] == true) {
|
||||||
|
return 500
|
||||||
|
} else if (soundMap[TYPE_WARN_LOW] == true) {
|
||||||
|
return 700
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setVoiceIo() {
|
||||||
|
// 在启动新的
|
||||||
soundPlaying = true
|
soundPlaying = true
|
||||||
GlobalScope.launch(Dispatchers.IO) {
|
|
||||||
while (soundPlaying) {
|
while (soundPlaying) {
|
||||||
gpioValue = if (gpioValue > 0) 0 else 1
|
gpioValue = if (gpioValue > 0) 0 else 1
|
||||||
LogUtils.w("cyy gpioValue ${gpioValue}")
|
LogUtils.w("cyy gpioValue ${gpioValue}, delay = $currentPlayingDelay")
|
||||||
GPIOUtils.setGpioValue(140, gpioValue)
|
GPIOUtils.setGpioValue(140, gpioValue)
|
||||||
delay(500)
|
Thread.sleep(currentPlayingDelay.toLong())
|
||||||
}
|
checkStatus()
|
||||||
}
|
}
|
||||||
|
GPIOUtils.setGpioValue(140, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopSoundIo() {
|
private fun checkStatus() {
|
||||||
|
val finalStatus =
|
||||||
|
(soundMap[TYPE_SOS] == true && soundMap[TYPE_WARN_LOW] == true && soundMap[TYPE_WARN_HIGHT] == true)
|
||||||
|
if (!finalStatus) {
|
||||||
soundPlaying = false
|
soundPlaying = false
|
||||||
GPIOUtils.setGpioValue(140, 0)
|
} else {
|
||||||
|
val delayTime = getDelayTime(soundMap)
|
||||||
|
currentPlayingDelay = delayTime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun stopSoundIo(type: Int = TYPE_SOS) {
|
||||||
|
soundMap[type] = false
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue