Visual Studio Paho MQTT Error C2238 unexpected token(s) preceding ‘;’

If you’re receiving errors like the following when trying to build a project in Visual Studio 2017 using the Paho C client:

Error C2238 unexpected token(s) preceding ';'

Then there is a quick fix, the problem seems to revolve around the following for DLLImport & DLLExport in the Paho header files:

#if defined(WIN32) || defined(WIN64)
  #define DLLImport __declspec(dllimport)
  #define DLLExport __declspec(dllexport)
#else
  #define DLLImport extern
  #define DLLExport __attribute__ ((visibility ("default")))
#endif

The catch here is that neither WIN32 nor WIN64 will be defined as instead either _WIN32 or _WIN64 will be defined.

So a quick fix is to define either WIN32 or WIN64 in your project’s ‘Preprocessor Definitions’ depending on whether you are targeting x86 (32bit) or x64 (64bit).

Here are some of the other errors you might see when you have this problem:

error C3861: 'visibility': identifier not found
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2059: syntax error: 'const'
error C3861: 'visibility': identifier not found
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2374: '__attribute__': redefinition; multiple initialization
note: see declaration of '__attribute__'
error C2448: '__attribute__': function-style initializer appears to be a function definition
 error C3646: 'data': unknown override specifier
 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
 error C3646: 'value': unknown override specifier
 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
 error C2143: syntax error: missing ';' before '*'
 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
 error C2238: unexpected token(s) preceding ';'
 error C3861: 'visibility': identifier not found
 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
 error C2374: '__attribute__': redefinition; multiple initialization
note: see declaration of '__attribute__'
 error C2448: '__attribute__': function-style initializer appears to be a function definition
 error C2143: syntax error: missing ',' before '*'
 error C3861: 'visibility': identifier not found
 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

PCL Octree Cheat Sheet

The point cloud library (PCL) is a fantastic resource for working with point clouds, however it is a large library and it can take a while to effectively find your way around it. The octree construct is very useful for working with point clouds, but again it can take a while to learn how to interact with octrees. To help me to remember how to interact with them I will include some examples here:

To iterate across the nodes of an octree (depth first):

//
for (auto it = octree.begin(); it != octree.end(); ++it) {
	if (it.isBranchNode()) {
		cout << "branch" << std::endl;
	}
	if (it.isLeafNode()) {
		cout << "leaf" << std::endl;
	}
}
//

To iterate across the leaf nodes:

//
for (auto it = octree.leaf_begin(); it != octree.leaf_end(); ++it) {
}
//


Get a Voxel's Bounding Region given node iterator:

//
// Get iterator to first leaf
auto it = octree.leaf_begin();
Eigen::Vector3f voxel_min, voxel_max;
octree.getVoxelBounds(it, voxel_min, voxel_max);
//

Get point indices from leaf node iterator

//
auto it = octree.leaf_begin(); 
std::vector indices;
it.getLeafContainer().getPointIndices(indices);
//

Get point indices from leaf node pointer

//
std::vector indices;
if (node->getNodeType() == LEAF_NODE)
{
	// Nasty down-cast?
	auto* l = dynamic_cast ::LeafNode*>(node);
	(*l)->getPointIndices(indices);
}
//

PCL C2988 unrecognizable template declaration/definition Visual Studio 2017

If you get this compile error:

Error C2988 unrecognizable template declaration/definition

When you:

#include 

from the Point Cloud Library (PCL) in Visual Studio 2017, then either throw the following in before the #include, like this:

//
#define BOOST_TYPEOF_EMULATION
#include 

or upgrade your version of Visual Studio 2017 (I haven’t tested this yet myself!)

Not sure why, it’s something to do with workarounds for VS 2017 bugs or something…

An implementation of ntohf() with code

I am working on extracting data from some binary navigation messages today. Working with binary data can be difficult and quite confusing – especially when converting byte order from network (big endian) to host byte order (possibly little endian). The integer types are well covered by ntohs() and ntohl() et al. but when dealing with floats things can get a bit harder as ntohf() isn’t always available to the hard pressed programmer!

So I have included a simple implementation below, I have tested it during my own use, but it use at your own risk as this code may well be an example of a rather dubious use of unions!

//
#include 
#ifndef ntohf
// Converts a 32 bit IEEE float represented as
// 4 bytes in network byte order to a float in host byte order
// the 4 bytes are passed as an unsigned 32bit integer
inline float ntohf(uint32_t net32)
{
    union {
        float f;
        uint32_t u;
    } value;
    // Swap bytes if necessary and store to value
    value.u = ntohl(net32);
    // return value as float
    return value.f;
}
#endif
//

This version is slightly adapted from an original implementation by Kevin Bowling (pls. see copyright notice).

ASCII ‘art’ for Camera Calibration Python Script

Working on a python script for a focus calibration (software based) routine for a computer vision camera, in order to spruce it up a bit I decide to add a nice ASCII introduction! #Friday

camera_cal

ExifTool truncates ASCII MakerNote data

ExifTool can be used to output a specific Exif MakerNote from your images’ metadata by using the following command:

exiftool -u -Unknown_0x0013 image.jpg

This will output makernote Id 0x13, however if the makernote that you are interested is long, then ExifTool may truncate it and output only the start with […] at the end!

Unknown 0x0013                  : {"distortion":{"factory":{"version":"1.0","medium":"air[...]

To have ExifTool output the full makernote, use the -b (binary) option like this:

exiftool -u -b -Unknown_0x0013 image.jpg

Using a Virtual USB Serial Port from a Guest OS on VirtualBox

Here is a decent set of instructions on how to access normal COM ports as well as USB Virtual com ports from you Guest OS running on VirtualBox – Thankfully it worked for me but I had to install the FTDI drivers on the Guest for rs232, I got them from here.

Thanks jorgensen!
.

Have ExifTool Display Maker Notes

Phil Harvey’s ExifTool is a fantastic software tool for displaying and interacting with image Exif data, however by default it doesn’t display maker notes and I always forget the command line options to persuade it to list them, so here it is – the -u option gets exiftool to display ‘unknown’ tags and theses include maker notes!

exiftool -u image.jpg

The makernotes will be listed as unknown items by their hex ids.

Happy days – Thanks Phil!

exiftool display maker notes

.

I2C1_Wr / I2C1_Rd functions hang with MikroC for PIC – Need Timeout

In some cases both the MikroC i2c functions I2C1_Wr() and I2C1_Rd() can hang or lockup indefinitely until the PIC is reset. This can happen if the I2C bus isn’t properly terminated, for example if a connection breaks or similar (broken wire or solder joint etc). Having your PIC lockup during operation is generally not very desirable if your are trying to develop a stable software system – if for some reason an i2c device hasn’t been properly connected your system will just lockup for good!

Different posts discuss this problem and its various causes:

https://forum.mikroe.com/viewtopic.php?f=13&t=21270&sid=f45f0199695794c83516f89919183cbb&start=0

and

https://forum.mikroe.com/viewtopic.php?f=88&t=60224

MikroC provided the code to their functions so that software developers (us) could work around the problem, this library implements versions of I2C1_Wr() and I2C1_Rd() that timeout rather than hang, a timeout values is passed as a parameter:

https://libstock.mikroe.com/projects/view/1052/i2c-non-blocking

The library doesn’t implement a C version, so inspired by it (thanks Danny!) I have ported the functions into C, and added some comments – use at your own risk, it works well for me on PIC18F family chips but hasn’t been exhaustively tested!

I have declared the timeouts as constants rather than allowing the timeout to be passed in as a parameter – this is so that the functions can be used as drop-in replacements without having to modify client code.

#define I2C_WRITE_TIMEOUT_US 200
unsigned short tI2C1_Wr(unsigned short d) {
    const unsigned int delay = 2; // us
    unsigned int max_retry = I2C_WRITE_TIMEOUT_US / delay;
    unsigned int retry;
    
    if (max_retry == 0)
        max_retry = 1;
        
    // Interrupt Flag bit - Waiting to transmit/receive
    PIR1.SSP1IF = 0;
    
    // Set data for transmission
    SSP1BUF = (unsigned char)d;
    // Wait for transmission to complete
    // 1 = Transmit is in progress
    // 0 = Transmit is not in progress
    //
    retry = max_retry;
    while (SSP1STAT.r_not_w == 1 && --retry > 0)
        delay_us(delay);
    // Timed-out stop transfer and return error
    if (SSP1STAT.r_not_w == 1) {
        // Enable Stop Condition
        SSP1CON2.PEN = 1;
        return 1;
    }
    
    // Wait for completion
    // 1 = The transmission/reception is complete (must be cleared by software)
    // 0 = Waiting to transmit/receive
    //
    retry = max_retry;
    while (PIR1.SSP1IF == 0 && --retry > 0)
        delay_us(delay);
    // Timed-out stop transfer and return error
    if (PIR1.SSP1IF == 0) {
        // Enable Stop Condition
        SSP1CON2.PEN = 1;
        return 1;
    }
    // Check that we got an ACK
    // 1 = Acknowledge was not received
    // 0 = Acknowledge was received
    //
    if (SSP1CON2.ACKSTAT != 0) {
        // No ACK, abort
        // Enable Stop Condition
        SSP1CON2.PEN = 1;
        return 1;
    }
    // All good...
    return 0;
}

And to Read:

#define I2C_READ_TIMEOUT_US 200
unsigned short tI2C1_Rd(unsigned short ack) {
    unsigned short d = 0;
    const unsigned int delay = 2; // us
    unsigned int max_retry = I2C_READ_TIMEOUT_US / delay;
    unsigned int retry;
    if (max_retry == 0)
        max_retry = 1;
        
    // Interrupt Flag bit - Waiting to transmit/receive
    // 1 = The transmission/reception is complete (must be cleared by software)
    // 0 = Waiting to transmit/receive
    //
    PIR1.SSP1IF = 0;
    // Set receive mode
    // 1 = Enables Receive mode for I2C
    // 0 = Receive idle
    //
    SSP1CON2.RCEN = 1;
    // Wait for read completion
    // 1 = The transmission/reception is complete (must be cleared by software)
    // 0 = Waiting to transmit/receive
    //
    retry = max_retry;
    while (PIR1.SSP1IF == 0 && --retry > 0)
        delay_us(delay);
    // Still not complete, get out...
    if (PIR1.SSP1IF == 0)
        return 0;
    // grab the data
    d = (unsigned short)SSPBUF;
    // ACK required?
    if (ack == 0) {
        // No
        // 1 = Not Acknowledge
        // 0 = Acknowledge
        //
        SSP1CON2.ACKDT = 1;
    } else {
        // Yes
        SSP1CON2.ACKDT = 0;
    }
    // Interrupt Flag bit - Waiting to transmit/receive
    // 1 = The transmission/reception is complete (must be cleared by software)
    // 0 = Waiting to transmit/receive
    //
    PIR1.SSP1IF = 0;
    // Start Ack sequence
    // 1 = Initiate Acknowledge sequence on SDAx and SCLx pins, and transmit ACKDT data bit. Automatically cleared by hardware.
    // 0 = Acknowledge sequence idle
    //
    SSP1CON2.ACKEN = 1;
    // Wait for completion of ack sequence
    retry = max_retry;
    while (PIR1.SSP1IF == 0 && --retry > 0)
        delay_us(delay);
    return d;
}

This works for the first i2c device, it is left as an exercise for the reader to convert it for a second i2c device, i.e. I2C2_Wr() and I2C2_Rd()

libpng – Changing compression level when writing PNG Image

This took me a bit of time to find, but I have been experimenting with libpng for writing software to save 16bit grayscale images and wanted to switch off compression to see if libpng would save the images out any faster than it normally does. When you call png_set_IHDR() you have to set the compression type to PNG_COMPRESSION_TYPE_DEFAULT, there is no alternative (e.g. there is no value like PNG_COMPRESSION_TYPE_NONE), this threw me for a bit…

Anyway after a spell of RTFMing, I eventually found out that you can alter then compression level, and indeed switch it off via a call to:

//
png_set_compression_level()
//

Setting a value of 0 in your code switches off compression, while setting it to 9 causes the lib to use maximum compression. Quick testing revealed that the time taken to save a PNG does decrease as you reduce this value towards 0 (and the saved image size increases!).

PNG is a good file format option for saving 16bit images, it is lossless and faithfully represents grayscale rather than fudging it into an RGB structure, and as it turns out, supports adjustable compression levels. As more cameras natively support higher bit-depth images, formats and software tools that support the greater precision will become more important so that Computer Vision and Machine Vision software can take full advantage.

PS Viewing 16bit grayscale images is harder than you might think, most software tools seem to down-sample them to 8bit grayscale, So far, I have found that good options are a.) ImageJ b.) Gimp 2.9 (not yet released, have to use a development build). These tools allow you to view the images and properly sample the 16bit pixel values etc…