locksss
This commit is contained in:
parent
bf3f376f5a
commit
adcc37bc81
2 changed files with 10 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue