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 {
|
||||
|
||||
@@ -23,6 +23,7 @@ type DeviceState struct {
|
||||
|
||||
sensorValue int // Default 2067 (ideal rest value)
|
||||
jitterRange int // Default 10 (means ±10)
|
||||
gain int // Default 50 (TUI gain slider, 1-100)
|
||||
|
||||
connectedClients int // Track number of connected BLE clients
|
||||
}
|
||||
@@ -46,6 +47,7 @@ func NewDeviceState() *DeviceState {
|
||||
ncParam: true, // Default NO mode (1)
|
||||
sensorValue: 2067,
|
||||
jitterRange: 10,
|
||||
gain: 50,
|
||||
connectedClients: 0,
|
||||
}
|
||||
}
|
||||
@@ -135,6 +137,7 @@ func (s *DeviceState) ResetToDefaults() {
|
||||
s.ncParam = true // Default NO mode (1)
|
||||
s.sensorValue = 2067
|
||||
s.jitterRange = 10
|
||||
s.gain = 50
|
||||
}
|
||||
|
||||
// SetSensorValue sets the current sensor value
|
||||
@@ -158,6 +161,20 @@ func (s *DeviceState) SetJitterRange(jitter int) {
|
||||
s.jitterRange = jitter
|
||||
}
|
||||
|
||||
// GetGain returns the current gain value
|
||||
func (s *DeviceState) GetGain() int {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.gain
|
||||
}
|
||||
|
||||
// SetGain sets the gain value
|
||||
func (s *DeviceState) SetGain(gain int) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.gain = gain
|
||||
}
|
||||
|
||||
// TriggerAlarm triggers an alarm with the given type ("TRIGGERED" or "TAMPER")
|
||||
func (s *DeviceState) TriggerAlarm(alarmType string) {
|
||||
s.mu.Lock()
|
||||
|
||||
@@ -127,6 +127,15 @@ func NewModel(state *device.DeviceState, logBuffer *LogBuffer, notifyCh chan str
|
||||
Max: 100,
|
||||
Step: 5,
|
||||
},
|
||||
{
|
||||
Name: "Gain",
|
||||
Type: ControlSlider,
|
||||
GetValue: state.GetGain,
|
||||
SetValue: state.SetGain,
|
||||
Min: 1,
|
||||
Max: 100,
|
||||
Step: 1,
|
||||
},
|
||||
}
|
||||
|
||||
return m
|
||||
|
||||
Reference in New Issue
Block a user