|
10 | 10 | package vl53l1x // import "tinygo.org/x/drivers/vl53l1x" |
11 | 11 |
|
12 | 12 | import ( |
| 13 | + "errors" |
13 | 14 | "time" |
14 | 15 |
|
15 | 16 | "tinygo.org/x/drivers" |
@@ -417,6 +418,38 @@ func (d *Device) StopContinuous() { |
417 | 418 | d.writeReg(PHASECAL_CONFIG_OVERRIDE, 0x00) |
418 | 419 | } |
419 | 420 |
|
| 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 | + |
420 | 453 | // writeReg sends a single byte to the specified register address |
421 | 454 | func (d *Device) writeReg(reg uint16, value uint8) { |
422 | 455 | msb := byte((reg >> 8) & 0xFF) |
|
0 commit comments