value based on gain
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"math/rand"
|
||||
"os"
|
||||
"sync"
|
||||
@@ -113,9 +114,20 @@ func main() {
|
||||
|
||||
func notificationLoop(char *bluetooth.Characteristic, state *device.DeviceState, logger *tui.LogBuffer, notifyCh chan string) {
|
||||
ticker := time.NewTicker(500 * time.Millisecond)
|
||||
wasCali := false
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
// Adjust ticker rate based on calibration mode
|
||||
cali := state.GetCaliMode()
|
||||
if cali != wasCali {
|
||||
wasCali = cali
|
||||
if cali {
|
||||
ticker.Reset(250 * time.Millisecond)
|
||||
} else {
|
||||
ticker.Reset(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
// Check for alarm state and send alarm message if active
|
||||
if alarmActive, alarmType := state.GetAlarmState(); alarmActive {
|
||||
msg := fmt.Sprintf("ALARM: %s", alarmType)
|
||||
@@ -133,7 +145,14 @@ func notificationLoop(char *bluetooth.Characteristic, state *device.DeviceState,
|
||||
if jitter > 0 {
|
||||
jitterVal = rand.Intn(jitter*2+1) - jitter
|
||||
}
|
||||
msg := fmt.Sprintf("SENSOR:%d", value+jitterVal)
|
||||
w20, _ := state.GetParam(20)
|
||||
gain := state.GetGain()
|
||||
nw := (float64(w20) - 1.0) / 127.0 - 1.0 // [1,255] → [-1, +1]
|
||||
ng := (float64(gain) - 1.0) / 49.5 - 1.0 // [1,100] → [-1, +1]
|
||||
jitterWeight := math.Exp2(nw + ng) // 2^(nw+ng): range [0.25, 4.0]
|
||||
valueWeight := 500.0 * (nw + ng + 2.0) // range [0, 2000], center 1000
|
||||
sensor := (float64(value) + valueWeight) + (float64(jitterVal) * jitterWeight)
|
||||
msg := fmt.Sprintf("SENSOR:%d", int(sensor))
|
||||
|
||||
_, err := char.Write([]byte(msg + "\r\n"))
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user