Skip to content

Commit e859e11

Browse files
committed
Added (Short/Long) range accuracy functionality
1 parent 6b65ecb commit e859e11

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed

src/inc/tmf882x_mode_app_ioctl.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ enum _tmf882x_mode_app_iocnr{
7272
APP_SET_CLKADJ,
7373
APP_SET_8X8MODE,
7474
APP_IS_8X8MODE,
75+
APP_SET_SHORTRANGE,
76+
APP_IS_SHORTRANGE,
7577
NUM_APP_IOCTL
7678
};
7779

@@ -336,6 +338,30 @@ struct tmf882x_mode_app_dev_UID {
336338
APP_IS_8X8MODE, \
337339
bool )
338340

341+
/**
342+
* @brief
343+
* IOCTL command code to Set the short-range mode
344+
* @param[in] input type: bool *
345+
* @param[out] output type: none
346+
* @return zero for success, fail otherwise
347+
* @warn Note that changing to/from short-range mode will clear the device
348+
* calibration
349+
*/
350+
#define IOCAPP_SET_SHORTRANGE _IOCTL_W( TMF882X_IOCTL_APP_MODE, \
351+
APP_SET_SHORTRANGE, \
352+
bool )
353+
354+
/**
355+
* @brief
356+
* IOCTL command code to Read the short-range operating mode state
357+
* @param[in] input type: none
358+
* @param[out] output type: bool *
359+
* @return zero for success, fail otherwise
360+
*/
361+
#define IOCAPP_IS_SHORTRANGE _IOCTL_R( TMF882X_IOCTL_APP_MODE, \
362+
APP_IS_SHORTRANGE, \
363+
bool )
364+
339365
#ifdef __cplusplus
340366
}
341367
#endif

src/qwiic_tmf882x.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,67 @@ bool QwDevTMF882X::setSPADConfig(struct tmf882x_mode_app_spad_config &spadConfig
823823
return true;
824824
}
825825

826+
//////////////////////////////////////////////////////////////////////////////////
827+
// getRange()
828+
//
829+
// Get the range accuracy mode on the connected TMF882X
830+
//
831+
// The range can either be long range (default setting, 5m distance), or short
832+
// range (higher accuracy, 1m distance)
833+
//
834+
// See the TMF882X datasheet for data on the accuracy of each mode
835+
//
836+
// Parameter Description
837+
// --------- -----------------------------
838+
// range enum to hold the range setting
839+
// retval True on success, false on error
840+
841+
bool QwDevTMF882X::getRange(TMF882XRange &range)
842+
{
843+
if (!_isInitialized)
844+
return false;
845+
846+
bool shortRange = false;
847+
if (tmf882x_ioctl(&_TOF, IOCAPP_IS_SHORTRANGE, NULL, &shortRange))
848+
return false;
849+
850+
range = shortRange ? SHORT_RANGE : LONG_RANGE;
851+
852+
return true;
853+
}
854+
855+
//////////////////////////////////////////////////////////////////////////////////
856+
// setRange()
857+
//
858+
// Set the range accuracy mode on the connected TMF882X
859+
//
860+
// Range accuracy can be either short range or long range. Long range is
861+
// the default setting which is able to detect objects up to 5 meters away.
862+
// The short range setting gives higher accuracy up to 1 meter. The range
863+
// setting will also take effect on the histogram output.
864+
//
865+
// See the TMF882X datasheet for data on the accuracy of each mode
866+
//
867+
// Parameter Description
868+
// --------- -----------------------------
869+
// range The range setting
870+
// retval True on success, false on error
871+
872+
bool QwDevTMF882X::setRange(TMF882XRange &range)
873+
{
874+
if (range == FAILED)
875+
return false;
876+
877+
if (!_isInitialized)
878+
return false;
879+
880+
bool shortRange = range == SHORT_RANGE;
881+
if (tmf882x_ioctl(&_TOF, IOCAPP_SET_SHORTRANGE, &shortRange, NULL))
882+
return false;
883+
884+
return true;
885+
}
886+
826887
////////////////////////////////////////////////////////////////////////////////////
827888
// setCommunicationBus()
828889
//

src/qwiic_tmf882x.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@
6565
#define TMF882X_MSG_ALL 0x07
6666
#define TMF882X_MSG_NONE 0x00
6767

68+
///////////////////////////////////////////////////////////////////////
69+
// TMF882XRange
70+
//
71+
// enum used to set and get the current range value.
72+
//
73+
// Property Description
74+
// -------- -----------------------------
75+
// FAILED Attempt to get the range accuracy value failed
76+
// SHORT_RANGE Short range accuracy. (<=1m range, higher accuracy)
77+
// LONG_RANGE Long range accuracy. (<=5m range, default accuracy)
78+
79+
enum TMF882XRange {
80+
FAILED = 0,
81+
SHORT_RANGE = 0x6E,
82+
LONG_RANGE = 0x6F
83+
};
84+
6885
//////////////////////////////////////////////////////////////////////////////
6986
// Messaage Callback Types
7087
//
@@ -499,6 +516,42 @@ class QwDevTMF882X
499516

500517
bool setSPADConfig(struct tmf882x_mode_app_spad_config &tofSpad);
501518

519+
//////////////////////////////////////////////////////////////////////////////////
520+
// getRange()
521+
//
522+
// Get the range accuracy mode on the connected TMF882X
523+
//
524+
// The range can either be long range (default setting, 5m distance), or short
525+
// range (higher accuracy, 1m distance)
526+
//
527+
// See the TMF882X datasheet for data on the accuracy of each mode
528+
//
529+
// Parameter Description
530+
// --------- -----------------------------
531+
// range enum to hold the range setting
532+
// retval True on success, false on error
533+
534+
bool getRange(TMF882XRange &range);
535+
536+
//////////////////////////////////////////////////////////////////////////////////
537+
// setRange()
538+
//
539+
// Set the range accuracy mode on the connected TMF882X
540+
//
541+
// Range accuracy can be either short range or long range. Long range is
542+
// the default setting which is able to detect objects up to 5 meters away.
543+
// The short range setting gives higher accuracy up to 1 meter. The range
544+
// setting will also have an effect on the histogram output.
545+
//
546+
// See the TMF882X datasheet for data on the accuracy of each mode
547+
//
548+
// Parameter Description
549+
// --------- -----------------------------
550+
// range The range setting
551+
// retval True on success, false on error
552+
553+
bool setRange(TMF882XRange &range);
554+
502555
//////////////////////////////////////////////////////////////////////////////////
503556
// getTMF882XContext()
504557
//

src/tmf882x_mode_app.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,72 @@ static int32_t tmf882x_mode_app_set_calib_data(struct tmf882x_mode_app *app,
18931893
return rc;
18941894
}
18951895

1896+
static bool tmf882x_mode_app_is_shortrange_mode(struct tmf882x_mode_app *app)
1897+
{
1898+
int32_t error = 0;
1899+
uint8_t reg = 0;
1900+
if (!app) return false;
1901+
1902+
// register 0x19 is short-range mode state
1903+
// 0x6e: short range
1904+
// 0x6f: long range
1905+
// 0: no short-range support
1906+
error = tof_get_register(priv(app), 0x19, &reg);
1907+
if (error) {
1908+
tof_err(priv(app), "Error reading shortrange mode register (%d)", error);
1909+
return false;
1910+
}
1911+
1912+
return (reg == 0x6E);
1913+
}
1914+
1915+
static int32_t tmf882x_mode_app_set_shortrange_mode(struct tmf882x_mode_app *app, bool is_shortrange)
1916+
{
1917+
int32_t rc = 0;
1918+
struct tmf882x_mode_app_i2c_msg *i2c_msg;
1919+
bool capture_state = false;
1920+
1921+
// issue cmd 0x6e -> short range mode
1922+
// issue cmd 0x6f -> long range mode (default)
1923+
1924+
if (is_shortrange == tmf882x_mode_app_is_shortrange_mode(app))
1925+
return 0; // nothing to do, already in correct mode
1926+
1927+
if ((capture_state = is_measuring(app))) {
1928+
rc = tmf882x_mode_app_stop_measurements(&app->mode);
1929+
if (rc) {
1930+
tof_err(priv(app), "Error (%d) stopping measurements "
1931+
"switching shortrange mode", rc);
1932+
return -1;
1933+
}
1934+
}
1935+
1936+
i2c_msg = to_i2cmsg(app);
1937+
i2c_msg->cmd = is_shortrange ? 0x6E : 0x6F;
1938+
i2c_msg->size = 0;
1939+
rc = tmf882x_mode_app_i2c_msg_send_timeout(app, i2c_msg, CMD_DEF_TIMEOUT_MS);
1940+
if (rc) {
1941+
tof_err(priv(app), "Error (%d) setting shortrange mode to %u", rc, is_shortrange);
1942+
return -1;
1943+
}
1944+
1945+
// check if the switch was successful
1946+
if (is_shortrange != tmf882x_mode_app_is_shortrange_mode(app)) {
1947+
tof_err(priv(app), "Error (%d) setting shortrange mode to '%u'", rc, is_shortrange);
1948+
return -1;
1949+
}
1950+
1951+
if (capture_state) {
1952+
rc = tmf882x_mode_app_start_measurements(&app->mode);
1953+
if (rc) {
1954+
tof_err(priv(app), "Error (%d) re-starting measurements", rc);
1955+
return -1;
1956+
}
1957+
}
1958+
1959+
return 0;
1960+
}
1961+
18961962
static int32_t tmf882x_mode_app_do_factory_calib(struct tmf882x_mode_app *app,
18971963
struct tmf882x_mode_app_calib *calib)
18981964
{
@@ -2128,6 +2194,13 @@ static int32_t tmf882x_mode_app_ioctl(struct tmf882x_mode *self, uint32_t cmd,
21282194
(*(bool *)output) = tmf882x_mode_app_is_8x8_mode(app);
21292195
rc = 0;
21302196
break;
2197+
case APP_SET_SHORTRANGE:
2198+
rc = tmf882x_mode_app_set_shortrange_mode(app, (*(bool *)input));
2199+
break;
2200+
case APP_IS_SHORTRANGE:
2201+
(*(bool *)output) = tmf882x_mode_app_is_shortrange_mode(app);
2202+
rc = 0;
2203+
break;
21312204
default:
21322205
tof_err(priv(app), "Error unhandled IOCTL cmd [%x]", cmd);
21332206
}

0 commit comments

Comments
 (0)