Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short/Long range accuracy functionality and updated tof_bin_image #7

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/inc/tmf882x_mode_app_ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ enum _tmf882x_mode_app_iocnr{
APP_SET_CLKADJ,
APP_SET_8X8MODE,
APP_IS_8X8MODE,
APP_SET_SHORTRANGE,
APP_IS_SHORTRANGE,
NUM_APP_IOCTL
};

Expand Down Expand Up @@ -336,6 +338,30 @@ struct tmf882x_mode_app_dev_UID {
APP_IS_8X8MODE, \
bool )

/**
* @brief
* IOCTL command code to Set the short-range mode
* @param[in] input type: bool *
* @param[out] output type: none
* @return zero for success, fail otherwise
* @warn Note that changing to/from short-range mode will clear the device
* calibration
*/
#define IOCAPP_SET_SHORTRANGE _IOCTL_W( TMF882X_IOCTL_APP_MODE, \
APP_SET_SHORTRANGE, \
bool )

/**
* @brief
* IOCTL command code to Read the short-range operating mode state
* @param[in] input type: none
* @param[out] output type: bool *
* @return zero for success, fail otherwise
*/
#define IOCAPP_IS_SHORTRANGE _IOCTL_R( TMF882X_IOCTL_APP_MODE, \
APP_IS_SHORTRANGE, \
bool )

#ifdef __cplusplus
}
#endif
Expand Down
54 changes: 54 additions & 0 deletions src/qwiic_tmf882x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,60 @@ bool QwDevTMF882X::setSPADConfig(struct tmf882x_mode_app_spad_config &spadConfig
return true;
}

//////////////////////////////////////////////////////////////////////////////////
// isShortRange()
//
// Get the range accuracy mode on the connected TMF882X
//
// The range can either be long range (default setting, 5m distance), or short
// range (higher accuracy, 1m distance)
//
// See the TMF882X datasheet for data on the accuracy of each mode
//
// Parameter Description
// --------- -----------------------------
// shortRange bool to hold the range setting
// retval True on success, false on error

bool QwDevTMF882X::isShortRange(bool &shortRange)
{
if (!_isInitialized)
return false;

if (tmf882x_ioctl(&_TOF, IOCAPP_IS_SHORTRANGE, NULL, &shortRange))
return false;

return true;
}

//////////////////////////////////////////////////////////////////////////////////
// setShortRange()
//
// Set the range accuracy mode on the connected TMF882X
//
// Range accuracy can be either short range or long range. Long range is
// the default setting which is able to detect objects up to 5 meters away.
// The short range setting gives higher accuracy up to 1 meter. The range
// setting will also take effect on the histogram output.
//
// See the TMF882X datasheet for data on the accuracy of each mode
//
// Parameter Description
// --------- -----------------------------
// range The range setting
// retval True on success, false on error

bool QwDevTMF882X::setShortRange(bool shortRange)
{
if (!_isInitialized)
return false;

if (tmf882x_ioctl(&_TOF, IOCAPP_SET_SHORTRANGE, &shortRange, NULL))
return false;

return true;
}

////////////////////////////////////////////////////////////////////////////////////
// setCommunicationBus()
//
Expand Down
36 changes: 36 additions & 0 deletions src/qwiic_tmf882x.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,42 @@ class QwDevTMF882X

bool setSPADConfig(struct tmf882x_mode_app_spad_config &tofSpad);

//////////////////////////////////////////////////////////////////////////////////
// isShortRange()
//
// Get the range accuracy mode on the connected TMF882X
//
// The range can either be long range (default setting, 5m distance), or short
// range (higher accuracy, 1m distance)
//
// See the TMF882X datasheet for data on the accuracy of each mode
//
// Parameter Description
// --------- -----------------------------
// shortRange bool to hold the range setting
// retval True on success, false on error

bool isShortRange(bool &shortRange);

//////////////////////////////////////////////////////////////////////////////////
// setShortRange()
//
// Set the range accuracy mode on the connected TMF882X
//
// Range accuracy can be either short range or long range. Long range is
// the default setting which is able to detect objects up to 5 meters away.
// The short range setting gives higher accuracy up to 1 meter. The range
// setting will also have an effect on the histogram output.
//
// See the TMF882X datasheet for data on the accuracy of each mode
//
// Parameter Description
// --------- -----------------------------
// shortRange The range setting
// retval True on success, false on error

bool setShortRange(bool shortRange);

//////////////////////////////////////////////////////////////////////////////////
// getTMF882XContext()
//
Expand Down
73 changes: 73 additions & 0 deletions src/tmf882x_mode_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,72 @@ static int32_t tmf882x_mode_app_set_calib_data(struct tmf882x_mode_app *app,
return rc;
}

static bool tmf882x_mode_app_is_shortrange_mode(struct tmf882x_mode_app *app)
{
int32_t error = 0;
uint8_t reg = 0;
if (!app) return false;

// register 0x19 is short-range mode state
// 0x6e: short range
// 0x6f: long range
// 0: no short-range support
error = tof_get_register(priv(app), 0x19, &reg);
if (error) {
tof_err(priv(app), "Error reading shortrange mode register (%d)", error);
return false;
}

return (reg == 0x6E);
}

static int32_t tmf882x_mode_app_set_shortrange_mode(struct tmf882x_mode_app *app, bool is_shortrange)
{
int32_t rc = 0;
struct tmf882x_mode_app_i2c_msg *i2c_msg;
bool capture_state = false;

// issue cmd 0x6e -> short range mode
// issue cmd 0x6f -> long range mode (default)

if (is_shortrange == tmf882x_mode_app_is_shortrange_mode(app))
return 0; // nothing to do, already in correct mode

if ((capture_state = is_measuring(app))) {
rc = tmf882x_mode_app_stop_measurements(&app->mode);
if (rc) {
tof_err(priv(app), "Error (%d) stopping measurements "
"switching shortrange mode", rc);
return -1;
}
}

i2c_msg = to_i2cmsg(app);
i2c_msg->cmd = is_shortrange ? 0x6E : 0x6F;
i2c_msg->size = 0;
rc = tmf882x_mode_app_i2c_msg_send_timeout(app, i2c_msg, CMD_DEF_TIMEOUT_MS);
if (rc) {
tof_err(priv(app), "Error (%d) setting shortrange mode to %u", rc, is_shortrange);
return -1;
}

// check if the switch was successful
if (is_shortrange != tmf882x_mode_app_is_shortrange_mode(app)) {
tof_err(priv(app), "Error (%d) setting shortrange mode to '%u'", rc, is_shortrange);
return -1;
}

if (capture_state) {
rc = tmf882x_mode_app_start_measurements(&app->mode);
if (rc) {
tof_err(priv(app), "Error (%d) re-starting measurements", rc);
return -1;
}
}

return 0;
}

static int32_t tmf882x_mode_app_do_factory_calib(struct tmf882x_mode_app *app,
struct tmf882x_mode_app_calib *calib)
{
Expand Down Expand Up @@ -2128,6 +2194,13 @@ static int32_t tmf882x_mode_app_ioctl(struct tmf882x_mode *self, uint32_t cmd,
(*(bool *)output) = tmf882x_mode_app_is_8x8_mode(app);
rc = 0;
break;
case APP_SET_SHORTRANGE:
rc = tmf882x_mode_app_set_shortrange_mode(app, (*(bool *)input));
break;
case APP_IS_SHORTRANGE:
(*(bool *)output) = tmf882x_mode_app_is_shortrange_mode(app);
rc = 0;
break;
default:
tof_err(priv(app), "Error unhandled IOCTL cmd [%x]", cmd);
}
Expand Down