-
Notifications
You must be signed in to change notification settings - Fork 4
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
Improve return values of LSM6DS33
class's methods by utilizing global error codes
#59
base: master
Are you sure you want to change the base?
Conversation
…s codes defined in `src/globals.h` 1. Changed function type of update() from bool to int 2. Added new error code `ERROR_NO_NEW_DATA` to `globals.h`
Software/src/sensors/lsm6ds33.cpp
Outdated
if (hasNewData() == RESULT_FALSE) | ||
//std::cerr << "No new data" << std::endl; | ||
return false; | ||
return ERROR_NO_NEW_DATA; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should also be returning RESULT_FALSE
; having no new data is not necessarily an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. I'll also get rid of the ERROR_NO_NEW_DATA
code since I made it for the sole purpose of reflecting that there was no new data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just need to also change any methods that call update() to check for the error codes, like powerOn() and pollData().
1. powerOn() calls update() 2. pollData() calls update()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good for now. We should also be implementing a way to handle proper error propagation, but I'll probably open that up in a separate issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was taking a bit of a deeper look, and realized there's a few other places we could utilize these error codes better:
- First of all, we seem to use the same error code
ERROR_ADDR
for two different error reasons. Can we separate the two? Perhaps another error code should be addedSPARTAN/Software/src/sensors/lsm6ds33.cpp
Lines 130 to 144 in 2286670
int spartan::LSM6DS33::powerOn() { if (m_status == STATUS_IDLE) return RESULT_SUCCESS; // set I2C address if (m_i2c.address(lsm6Address) != mraa::SUCCESS) { std::cerr << "Unable to set I2C address." << std::endl; return ERROR_ADDR; } // Update settings for accelerometer and gyroscope if (!updateSettings()) { std::cerr << "Update setting failed" << std::endl; return ERROR_ADDR; } - Second, since we're doing this for the
update
method, we should probably do the equivalent changes for thewriteReg
and bothupdateSettings
methods:
SPARTAN/Software/src/sensors/lsm6ds33.cpp
Lines 25 to 65 in 2286670
bool spartan::LSM6DS33::writeReg(uint8_t* buffer, unsigned short size) { mraa::Result msg = m_i2c.write(buffer, size); // Error handling switch (msg) { case mraa::SUCCESS: return true; case mraa::ERROR_INVALID_PARAMETER: std::cerr << "Invalid parameter." << std::endl; break; case mraa::ERROR_INVALID_HANDLE: std::cerr << "ERROR_INVALID_HANDLE" << std::endl; break; case mraa::ERROR_NO_RESOURCES: std::cerr << "ERROR_NO_RESOURCES" << std::endl; break; case mraa::ERROR_INVALID_RESOURCE: std::cerr << "ERROR_INVALID_RESOURCE" << std::endl; break; case mraa::ERROR_INVALID_QUEUE_TYPE: std::cerr << "ERROR_INVALID_QUEUE_TYPE" << std::endl; break; case mraa::ERROR_NO_DATA_AVAILABLE: std::cerr << "ERROR_NO_DATA_AVAILABLE" << std::endl; break; case mraa::ERROR_INVALID_PLATFORM: std::cerr << "ERROR_INVALID_PLATFORM" << std::endl; break; case mraa::ERROR_PLATFORM_NOT_INITIALISED: std::cerr << "ERROR_PLATFORM_NOT_INITIALISED" << std::endl; break; case mraa::ERROR_UNSPECIFIED: std::cerr << "ERROR_UNSPECIFIED" << std::endl; break; default: break; } return false; }
SPARTAN/Software/src/sensors/lsm6ds33.cpp
Lines 104 to 126 in 2286670
bool spartan::LSM6DS33::updateSettings() { // Update multiplier constant setMultipliers(); // Accelerometer settings m_buffer[0] = lsm6ds33::CTRL1_XL; m_buffer[1] = (m_settings.accel_odr << 4) | m_settings.accelRange | m_settings.accelAAFreq; if (!writeReg(m_buffer, 2)) return false; m_buffer[0] = lsm6ds33::CTRL2_G; m_buffer[1] = (m_settings.gyro_odr << 4) | m_settings.gyroRange; if (!writeReg(m_buffer, 2)) return false; return true; } bool spartan::LSM6DS33::updateSettings(spartan::LSM6DS33::lsm6Settings settings) { m_settings = settings; return updateSettings(); }
Also, just a few styling issues below... Otherwise, great work Grant!
LSM6DS33
class's methods by utilizing global error codes
equivalent to changes made in `update`
Closes #57
Changed update() method in
src/sensors/lsm6ds33.cpp
to return error codes defined insrc/globals.h
ERROR_NO_NEW_DATA
toglobals.h