MapComplete/src/Sensors/Motion.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

34 lines
886 B
TypeScript
Raw Normal View History

2023-12-19 22:21:34 +01:00
import { UIEventSource } from "../Logic/UIEventSource"
export default class Motion {
public static singleton = new Motion()
/**
* In m/s²
*/
public maxAcc = new UIEventSource<number>(0)
public lastShakeEvent = new UIEventSource<Date>(undefined)
private isListening = false
private constructor() {
this.startListening()
}
private onUpdate(eventData: DeviceMotionEvent) {
const acc = eventData.acceleration
this.maxAcc.setData(Math.max(acc.x, acc.y, acc.z))
if (this.maxAcc.data > 22) {
this.lastShakeEvent.setData(new Date())
}
}
startListening() {
if (this.isListening) {
return
}
this.isListening = true
console.log("Listening to motion events", this)
window.addEventListener("devicemotion", (e) => this.onUpdate(e))
}
}