diff --git a/CMakeLists.txt b/CMakeLists.txt index 43e6993..603319a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,9 @@ endif() cs_add_executable(${PROJECT_NAME} src/rideheight_sensing_node.cpp src/rideheight_sensing.cpp + src/vl6180x_glue.cpp + src/vl6180x_api.c + src/vl6180x_i2c.c ) target_link_libraries(${PROJECT_NAME} diff --git a/include/vl6180x_i2c.h b/include/vl6180x_i2c.h index 080ed6e..f7da05f 100644 --- a/include/vl6180x_i2c.h +++ b/include/vl6180x_i2c.h @@ -2,7 +2,7 @@ * $Date: 2015-01-08 14:30:24 +0100 (Thu, 08 Jan 2015) $ * $Revision: 2039 $ */ - + /** * @file vl6180x_i2c.h * @@ -12,23 +12,39 @@ #ifndef VL6180_I2C_H_ #define VL6180_I2C_H_ +#ifdef __cplusplus +extern "C" { +#endif + #include "vl6180x_platform.h" /** * @defgroup cci_i2c CCI to RAW I2C translation layer * - * This optional tranlation layer is implemented in __platform/cci-i2c__ directory. If user uses this translation layer for his platform, only @a VL6180x_I2CRead() and - * @a VL6180x_I2CWrite() functions need to be implemented. Also, some code adaption (via macro) is required for multi-threading and for multiple device support. + * This optional tranlation layer is implemented in __platform/cci-i2c__ +directory. If user uses this translation layer for his platform, only @a +VL6180x_I2CRead() and + * @a VL6180x_I2CWrite() functions need to be implemented. Also, some code +adaption (via macro) is required for multi-threading and for multiple device +support. * - * File vl6180x_i2c.c implements device register access via raw i2c access. If the targeted application and platform has no multi-thread, no multi-cpu and uses single - * device, then nothing else is required than the 2 mandatory function : @a VL6180x_I2CRead() and @a VL6180x_I2CWrite().\n - * In other cases, review and customize @a VL6180x_GetI2CAccess() and @a VL6180x_DoneI2CAccess() functions as well as @a #VL6180x_I2C_USER_VAR macro. This should be enough + * File vl6180x_i2c.c implements device register access via raw i2c access. If +the targeted application and platform has no multi-thread, no multi-cpu and +uses single + * device, then nothing else is required than the 2 mandatory function : @a +VL6180x_I2CRead() and @a VL6180x_I2CWrite().\n + * In other cases, review and customize @a VL6180x_GetI2CAccess() and @a +VL6180x_DoneI2CAccess() functions as well as @a #VL6180x_I2C_USER_VAR macro. +This should be enough * to conform to a wide range of platform OS and application requirements .\n * - * If your configured i2c for per device buffer via @a #I2C_BUFFER_CONFIG == 2, you must implement @a VL6180x_GetI2cBuffer() + * If your configured i2c for per device buffer via @a #I2C_BUFFER_CONFIG == 2, +you must implement @a VL6180x_GetI2cBuffer() * * __I2C Port sample__ \n - * A __linux kernel__ port need a "long flags" var for its spin_lock in all functions. the following code example declares a spin lock "lock" in the custom device structure. \n + * A __linux kernel__ port need a "long flags" var for its spin_lock in all +functions. the following code example declares a spin lock "lock" in the custom +device structure. \n * @code struct MyVL6180Dev_t { struct VL6180xDevData_t StData; @@ -65,14 +81,17 @@ typedef struct MyVL6180Dev_t *VL6180xDev_t; * @li 0 : one GLOBAL buffer \n * Use one global buffer of MAX_I2C_XFER_SIZE byte in data space \n * This solution is not multi-device compliant nor multi-thread cpu safe \n - * It can be the best option for small 8/16 bit MCU without stack and limited ram (STM8s, 80C51 ...) + * It can be the best option for small 8/16 bit MCU without stack and limited + * ram (STM8s, 80C51 ...) * * @li 1 : ON_STACK/local \n * Use local variable (on stack) buffer \n - * This solution is multi-thread with use of i2c resource lock or mutex see @a VL6180x_GetI2CAccess() \n + * This solution is multi-thread with use of i2c resource lock or mutex see @a + * VL6180x_GetI2CAccess() \n * * @li 2 : User defined \n - * Per device potentially dynamic allocated. Requires @a VL6180x_GetI2cBuffer() to be implemented. + * Per device potentially dynamic allocated. Requires @a + * VL6180x_GetI2cBuffer() to be implemented. * @ingroup Configuration */ #define I2C_BUFFER_CONFIG 1 @@ -85,7 +104,7 @@ typedef struct MyVL6180Dev_t *VL6180xDev_t; * @return 0 on success * @ingroup cci_i2c */ -int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len); +int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len); /** * @@ -98,10 +117,10 @@ int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len); */ int VL6180x_I2CRead(VL6180xDev_t dev, uint8_t *buff, uint8_t len); - /** - * @brief Declare any required variables used by i2c lock (@a VL6180x_DoneI2CAccess() and @a VL6180x_GetI2CAccess()) - * and buffer access : @a VL6180x_GetI2cBuffer() + * @brief Declare any required variables used by i2c lock (@a + * VL6180x_DoneI2CAccess() and @a VL6180x_GetI2CAccess()) and buffer access : @a + * VL6180x_GetI2cBuffer() * * @ingroup cci_i2c */ @@ -109,11 +128,13 @@ int VL6180x_I2CRead(VL6180xDev_t dev, uint8_t *buff, uint8_t len); /** * @brief Acquire lock or mutex for access to i2c data buffer and bus.\n - * Delete the default VL6180x_GetI2CAccess 'do-nothing' macro below if you decide to implement this function. + * Delete the default VL6180x_GetI2CAccess 'do-nothing' macro below if you + * decide to implement this function. * - * This function is used to perform i2c bus level and multiple access locking required for multi thread/proccess system.\n - * Multiple access (read and update) will lock once and do multiple basic i2c rd/wr to complete the overall transfer.\n - * When no locking is needed this can be a void macro.\n + * This function is used to perform i2c bus level and multiple access locking + * required for multi thread/proccess system.\n Multiple access (read and + * update) will lock once and do multiple basic i2c rd/wr to complete the + * overall transfer.\n When no locking is needed this can be a void macro.\n * * @param dev the device * @ingroup cci_i2c @@ -122,14 +143,16 @@ void VL6180x_GetI2CAccess(VL6180xDev_t dev); /** * @def VL6180x_GetI2CAccess - * @brief Default 'do-nothing' macro for @a VL6180x_GetI2CAccess(). Delete if used. + * @brief Default 'do-nothing' macro for @a VL6180x_GetI2CAccess(). Delete if + * used. * @ingroup cci_i2c */ #define VL6180x_GetI2CAccess(dev) (void)0 /* TODO delete if function used */ /** * @brief Release acquired lock or mutex for i2c access.\n - * Delete default VL6180x_DoneI2CAccess 'do-nothing' macro below if implementing that function. + * Delete default VL6180x_DoneI2CAccess 'do-nothing' macro below if implementing + * that function. * * This function is used to release the acquired lock. * @param dev The device @@ -138,16 +161,17 @@ void VL6180x_GetI2CAccess(VL6180xDev_t dev); void VL6180x_DoneI2CAccess(VL6180xDev_t dev); /** @def VL6180x_DoneI2CAcces - * @brief Default 'do-nothing' macro for @a VL6180x_DoneI2CAcces(). Delete if used. + * @brief Default 'do-nothing' macro for @a VL6180x_DoneI2CAcces(). Delete if + * used. * @ingroup cci_i2c */ -#define VL6180x_DoneI2CAcces(dev) (void)0 /*TODO delete if function used */ +#define VL6180x_DoneI2CAcces(dev) (void)0 /*TODO delete if function used */ /** * @brief Provided data buffer for i2c access for at least n_byte. * - * You must implement it when i2c @a #I2C_BUFFER_CONFIG is set to 2 (User defined).\n - * This is used used in the context of #VL6180x_I2C_USER_VAR + * You must implement it when i2c @a #I2C_BUFFER_CONFIG is set to 2 (User + * defined).\n This is used used in the context of #VL6180x_I2C_USER_VAR * * @param dev The device * @param n_byte Minimal number of byte @@ -159,8 +183,8 @@ uint8_t *VL6180x_GetI2cBuffer(VL6180xDev_t dev, int n_byte); #error /* TODO add your macro of code here for VL6180x_GetI2cBuffer */ #endif - - - +#ifdef __cplusplus +} +#endif #endif /* VL6180_I2C_H_ */ diff --git a/src/rideheight_sensing.cpp b/src/rideheight_sensing.cpp index 103ea42..4d11113 100644 --- a/src/rideheight_sensing.cpp +++ b/src/rideheight_sensing.cpp @@ -11,7 +11,7 @@ RideheightSensing::RideheightSensing(ros::NodeHandle nh, : nh{nh}, nh_private{nh_private}, vl6180x_pub{nh.advertise("vl6180x", 10)}, vl53l0x_pub{nh.advertise("vl53l0x", 10)} { - VL6180x_glue_init("/dev/i2c-3"); + VL6180x_glue_init("/dev/i2c-8"); VL6180x_InitData(vl6180x_dev); VL6180x_Prepare(vl6180x_dev); } diff --git a/src/vl6180x_glue.c b/src/vl6180x_glue.cpp similarity index 91% rename from src/vl6180x_glue.c rename to src/vl6180x_glue.cpp index 83975bd..36dbd27 100644 --- a/src/vl6180x_glue.c +++ b/src/vl6180x_glue.cpp @@ -26,11 +26,16 @@ void VL6180x_glue_init(const char *i2c_dev_path) { ROS_FATAL("Couldn't set slave address: %s", strerror(errno)); return; } + + ROS_INFO("Successfully opened I2C device & set address"); } +extern "C" { + int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len) { int ret = write(i2c_dev_file, buff, len); if (ret == len) { + ROS_INFO("Successfully wrote to I2C device"); return 0; } else if (ret < 0) { ROS_ERROR("Error while writing to I2C device: %s", strerror(errno)); @@ -52,3 +57,4 @@ int VL6180x_I2CRead(VL6180xDev_t dev, uint8_t *buff, uint8_t len) { } return -1; } +}