This commit is contained in:
henne 2025-08-09 21:06:50 +02:00
parent bf3f376f5a
commit adcc37bc81
No known key found for this signature in database
2 changed files with 10 additions and 0 deletions

View file

@ -99,6 +99,7 @@ type Radar struct {
baudrate int baudrate int
handler func(int) handler func(int)
speedHandler func(int) speedHandler func(int)
configLock sync.RWMutex
} }
func (r *Radar) write(data []byte) error { func (r *Radar) write(data []byte) error {
@ -112,6 +113,10 @@ func (r *Radar) write(data []byte) error {
return r.port.Drain() 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. // 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 // 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 { 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. // 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 { func (r *Radar) SetEventConfig(eventNumber, minDistance, maxDistance, minSpeed, maxSpeed, direction, state int) error {
r.lockConfig()
if eventNumber < 1 || eventNumber > 1 { if eventNumber < 1 || eventNumber > 1 {
return errors.New("eventNumber needs to be between 1 and 8") 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 { func (r *Radar) SetTargetSpeedConfig(direction Direction, minDistance, maxDistance, minSpeed, maxSpeed, speeding int, outputLogic OutputLogic) error {
r.lockConfig()
data := startSequence data := startSequence
data = append(data, 0x03, byte(direction), byte(minDistance), byte(maxDistance), byte(minSpeed), byte(maxSpeed), byte(speeding), byte(outputLogic)) data = append(data, 0x03, byte(direction), byte(minDistance), byte(maxDistance), byte(minSpeed), byte(maxSpeed), byte(speeding), byte(outputLogic))
data = append(data, endSequence...) 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 { func (r *Radar) SetCommunicationConfig(port Port, baud Baud, speedOutput, targetOutput, triggerOutput OutputType, communicationPeriod int) error {
r.lockConfig()
data := startSequence data := startSequence
speed := byte(speedOutput) speed := byte(speedOutput)
target := byte(targetOutput) target := byte(targetOutput)

View file

@ -44,6 +44,7 @@ func (r *Radar) decodeInput(buf []byte) {
s = "SUCCESS" s = "SUCCESS"
} }
log.Printf("%s response: Code: %d", s, buf[2]) log.Printf("%s response: Code: %d", s, buf[2])
r.configLock.Unlock()
return return
} }
// speed information // speed information