From e7f62bb7c08361d370d8ab35a11243b70d4cd1bd Mon Sep 17 00:00:00 2001 From: Giuseppe Tufo Date: Tue, 3 Mar 2026 11:59:21 +0100 Subject: [PATCH] value based on gain --- cmd/simulator/main.go | 21 ++++++++++++++++++++- internal/device/state.go | 17 +++++++++++++++++ internal/tui/model.go | 9 +++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/cmd/simulator/main.go b/cmd/simulator/main.go index 53ffbfb..d545cb2 100644 --- a/cmd/simulator/main.go +++ b/cmd/simulator/main.go @@ -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 { diff --git a/internal/device/state.go b/internal/device/state.go index ad4b066..16cb555 100644 --- a/internal/device/state.go +++ b/internal/device/state.go @@ -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() diff --git a/internal/tui/model.go b/internal/tui/model.go index 11f6fa8..e7da766 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -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