Loading files
import { BeaconDataPacket } from 'beacon-packet'
import BLEClient from 'bleclient'
import { uuid } from 'btutils'
import { speeches } from 'speeches_greeting'
import { randomBetween } from 'stackchan-util'
import { TTS as LocalTTS } from 'tts-local'
const keys = Object.keys(speeches)
const hellos = keys.filter((k) => k.startsWith('hello_'))
const byes = keys.filter((k) => k.startsWith('bye_'))
const COMPANY_ID = 0x004c
const UUID = uuid`CFFD85BB-67E0-9CD4-B2D0-BE5A7ECAC915`
class Scanner extends BLEClient {
constructor() {
super()
this.count = undefined
}
onReady() {
this.startScanning({ duplicates: true })
}
onDiscovered(device) {
const manufacturerSpecific = device.scanResponse.manufacturerSpecific
if (!manufacturerSpecific || COMPANY_ID !== manufacturerSpecific.identifier) {
return
}
const { success, reason, value: dataPacket } = BeaconDataPacket.parse(manufacturerSpecific.data)
if (!success) {
trace(`${reason}\n`)
return
}
trace(`${dataPacket.uuid}, ${dataPacket.major}, ${dataPacket.minor}, ${dataPacket.txPower}`)
if (!UUID.equals(dataPacket.uuid)) {
return
}
const count = dataPacket.major
if (count !== this.count) {
this.count = count
this.handleData?.(dataPacket)
}
}
}
export function onContextCreated(robot) {
const scanner = new Scanner()
/**
* @note A workaround due to the sample rate of the mod resource being fixed at 11025.
* M5Stack CoreS3 cannot play at a sample rate of 11025, so we use a nearby valid common value.
**/
robot.audio.useTTS(new LocalTTS({ sampleRate: 11000 }))
scanner.handleData = (dataPacket) => {
const { major: count, minor: command } = dataPacket
trace(`got: ${count}, ${command}\n`)
if (command === 1) {
const hello = hellos[Math.floor(randomBetween(0, hellos.length))]
robot.audio.say(hello)
} else if (command === 2) {
const bye = byes[Math.floor(randomBetween(0, byes.length))]
robot.audio.say(bye)
}
}
}
126616f26This file is pinned to the selected release. Download the original to open it in your design tool.