Skip to content

Commit 448598c

Browse files
Oliver Stäblerdeadprogram
authored andcommitted
vl53l1x: Add functions for setting 'region of interest'
Implement functions to get and set 'region of interest' similar to its reference implementation in C: pololu/vl53l1x-arduino@a6de396 Signed-off-by: Oliver Stäbler <[email protected]>
1 parent ec98f2d commit 448598c

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

vl53l1x/registers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const (
4646
SD_CONFIG_INITIAL_PHASE_SD1 = 0x007B
4747
SYSTEM_GROUPED_PARAMETER_HOLD_1 = 0x007C
4848
SD_CONFIG_QUANTIFIER = 0x007E
49+
ROI_CONFIG_USER_ROI_CENTRE_SPAD = 0x007F
50+
ROI_CONFIG_USER_ROI_REQUESTED_GLOBAL_XY_SIZE = 0x0080
4951
SYSTEM_SEQUENCE_CONFIG = 0x0081
5052
SYSTEM_GROUPED_PARAMETER_HOLD = 0x0082
5153
SYSTEM_INTERRUPT_CLEAR = 0x0086

vl53l1x/vl53l1x.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package vl53l1x // import "tinygo.org/x/drivers/vl53l1x"
1111

1212
import (
13+
"errors"
1314
"time"
1415

1516
"tinygo.org/x/drivers"
@@ -417,6 +418,38 @@ func (d *Device) StopContinuous() {
417418
d.writeReg(PHASECAL_CONFIG_OVERRIDE, 0x00)
418419
}
419420

421+
// SetROI sets the 'region of interest' for x and y coordinates. Valid ranges are from 4/4 to 16/16.
422+
func (d *Device) SetROI(x, y uint8) error {
423+
if !validROIRange(x, y) {
424+
return errors.New("ROI value out of range")
425+
}
426+
427+
if x > 10 || y > 10 {
428+
d.writeReg(ROI_CONFIG_USER_ROI_CENTRE_SPAD, 199)
429+
}
430+
431+
d.writeReg(ROI_CONFIG_USER_ROI_REQUESTED_GLOBAL_XY_SIZE, (y-1)<<4|(x-1))
432+
return nil
433+
}
434+
435+
// GetROI returns the currently configured 'region of interest' for x and y coordinates.
436+
func (d *Device) GetROI() (x, y uint8, err error) {
437+
reg := d.readReg(ROI_CONFIG_USER_ROI_REQUESTED_GLOBAL_XY_SIZE)
438+
439+
x = (reg & 0x0f) + 1
440+
y = ((reg & 0xf0) >> 4) + 1
441+
442+
if !validROIRange(x, y) {
443+
err = errors.New("ROI value out of range")
444+
}
445+
446+
return
447+
}
448+
449+
func validROIRange(x, y uint8) bool {
450+
return x >= 4 && x <= 16 && y >= 4 && y <= 16
451+
}
452+
420453
// writeReg sends a single byte to the specified register address
421454
func (d *Device) writeReg(reg uint16, value uint8) {
422455
msb := byte((reg >> 8) & 0xFF)

0 commit comments

Comments
 (0)