From adcc37bc816623ce0d3eb00f2cff691a65262d3e Mon Sep 17 00:00:00 2001 From: henne Date: Sat, 9 Aug 2025 21:06:50 +0200 Subject: [PATCH] locksss --- radar_lib/main.go | 9 +++++++++ radar_lib/reader.go | 1 + 2 files changed, 10 insertions(+) diff --git a/radar_lib/main.go b/radar_lib/main.go index 75c56e7..cdda1ab 100644 --- a/radar_lib/main.go +++ b/radar_lib/main.go @@ -99,6 +99,7 @@ type Radar struct { baudrate int handler func(int) speedHandler func(int) + configLock sync.RWMutex } func (r *Radar) write(data []byte) error { @@ -112,6 +113,10 @@ func (r *Radar) write(data []byte) error { return r.port.Drain() } +func (r *Radar) lockConfig() { + r.configLock.Lock() +} + // SetPinTrigger configures a pin to trigger on the radar when an object is detected in a specific range. // controlPin defines the Pin to trigger, distance is the target distance in 0.5 meters, outputLevel is the voltage in 0.1V steps, direction defines weather to trigger on incoming / outgoing or both func (r *Radar) SetPinTrigger(controlPin int, distance int, outputLevel int, direction Direction) error { @@ -131,6 +136,8 @@ func (r *Radar) SetBaseConfig(height int, angle int, waveformConfig int) error { // SetEventConfig can configure up to 8 events that will trigger a response from the radar. distances are in 0.5m, speeds are in km/h. func (r *Radar) SetEventConfig(eventNumber, minDistance, maxDistance, minSpeed, maxSpeed, direction, state int) error { + r.lockConfig() + if eventNumber < 1 || eventNumber > 1 { return errors.New("eventNumber needs to be between 1 and 8") } @@ -141,6 +148,7 @@ func (r *Radar) SetEventConfig(eventNumber, minDistance, maxDistance, minSpeed, } func (r *Radar) SetTargetSpeedConfig(direction Direction, minDistance, maxDistance, minSpeed, maxSpeed, speeding int, outputLogic OutputLogic) error { + r.lockConfig() data := startSequence data = append(data, 0x03, byte(direction), byte(minDistance), byte(maxDistance), byte(minSpeed), byte(maxSpeed), byte(speeding), byte(outputLogic)) data = append(data, endSequence...) @@ -148,6 +156,7 @@ func (r *Radar) SetTargetSpeedConfig(direction Direction, minDistance, maxDistan } func (r *Radar) SetCommunicationConfig(port Port, baud Baud, speedOutput, targetOutput, triggerOutput OutputType, communicationPeriod int) error { + r.lockConfig() data := startSequence speed := byte(speedOutput) target := byte(targetOutput) diff --git a/radar_lib/reader.go b/radar_lib/reader.go index f6dc5c3..f6e5c28 100644 --- a/radar_lib/reader.go +++ b/radar_lib/reader.go @@ -44,6 +44,7 @@ func (r *Radar) decodeInput(buf []byte) { s = "SUCCESS" } log.Printf("%s response: Code: %d", s, buf[2]) + r.configLock.Unlock() return } // speed information