Category Archives: Programming

FTDI fights for copyright, but has little improvement on its own

FTDI, large semiconductor manufacturer, recently released an update for its popular device drivers, that was bringing counterfeit chips into not usable state. However this made users uproar, because no end-user has an option to find out whether he obtains device with legal or counterfeit ICs. Luckily, FTDI has removed that update at the moment and there are ways to bring the devices back to life, but they still threat users with other options to annoy counterfeit users. The full story you can read, for example here: FTDI on counterfeit chip bricking: “Our intentions were honorable”

But let’s look closer at the chip, all the talks are about.
FT232RL is an extremely convenient chip. It serves as a bridge between USB virtual serial port on PC and UART on the device. Besides that it supports bit-bang mode, which makes it to support multiple protocols (e.g. SPI, I2C, etc.) in a software way. And as a bonus it has an internal circuitry to interface either 5V or 3V3 devices.
From a programming point of view, you have just to plug the device into the USB port and automatic driver installation will give you an access to the virtual serial port. Wanna bit-bang, then just download the library and API from the FTDI website and use it.

Ideal hardware for makers even at the listed prices.

But makers usually do it as a one device at a time connectivity. Other devices communication is commonly routed through the main gateway connected to the PC.

What happens if you are developer and need multiple devices connected at once?

Plug your devices into USB ports and Bang! Nothing is working.
You scratch your head and dive into Device Manager just to find out the following picture:

Multiple FTDI adapters

All the devices try to use the same virtual serial port number and as a result no devices can communicate at all.
Again after scratching your head you can find out, that you can change the port numbers in the properties.

But look at the specs. Port numbers are assigned in sequence. Really, FTDI?
Haven’t you enough time to fix that bug, since first FT232R release?
Is fighting for copyright is more important than your reputation, FTDI?

Connecting TTP229 based 16 key touchpad to microcontroller

Recently I have received the TTP229 based 16 key touchpad, ordered from eBay.

Unfortunately no datasheet was provided with this module, nor could I find one from the internet.
Searching for TTP229 chip datasheet also brought unsatisfying results, since there is no datasheet available for exact chip revision used on module. However datasheets for alternative revisions were avaialble and I have found chip comparison table in some chinese blog.

With this information, I have traced, how chip is connected on the board and how it is configured. By default only 1 group of first 8 buttons works. However, if you solder pinheaders into the holes on the board, you can configure it differently with installing and removing jumpers.

With only 2 additional jumpers installed, all 16 buttons started to work and default active low mode changed into active high. I have written a simple Arduino sketch available at my GitHub repository https://github.com/dmmedia/TTP229B_16keypad to demonstrate how the thing is working. And if you connect via SerialMonitor, you can see the button press and release events sent by the touchpad module and received by Arduino.

My next plans are to rewrite Arduino sketch into a library and start using these modules in my different experiments and projects. Modules are slim and pinheaders can be replaced by solder joints and wires where needed. Keypads can be covered with own design films or laminated paper. And the price for these modules is very low, which makes it worth to just purchase them, than making yourself.

SQL parameter escaping once again

Infinite times it was spread over the internet, that every string parameter that enters SQL query, especially user entered, should be escaped.

And again, and again, you open some critical enterprise grade project and see something like this:
CString sql = "SELECT * FROM categories WHERE category = '" + category + "'";
where category is unchecked and unescaped string that comes directly from user input.

Almost every contemporary database API contain escaping either by special method or automated while using prepared statements. But nevertheless, some our colleagues still write unsafe code. I’d suggest that it should be taught at every programming course, just by showing proper database query assembling, not a quick and dirty examples as they are now.

Frequent error while using std::erase with std::remove_if in C++

To remove an element from container of type vector, string, list or deque we use erase() method with an iterator to unneeded element as a parameter.

After some time we do optimize the code or start to use STL algorithm remove_if() from the very beginning. Next we add some fancy boost::bind stuff for convenience and etc.

And once in a while we have to remove several elements from container instead of one. And it does not matter if it happens in new code, or while extending old. And here many C++ beginners can do the mistake, either but not knowing about it or just lazy enough not to check the erase() method documentation. They copy-paste the old code and change only remove_if() search condition. But they forget, that if only one argument is passed to erase(), it will delete that only element which is that iterator argument pointing to.

If several elements are needed to be removed from the container, there is an overloaded erase() method which accepts two iterators , to the start and to the end of the sequence to be removed. When remove_if() algorithm is used, it rearranges elements that should not be removed to the beginning of the container, leaving some trash in the end. For example, let’s have a vector , containing the following values:
1 2 3 1 2 3 1 2 3

Then if we call
bool IsTwo (int i) { return (i == 2); }

tstart = remove_if(v.begin(), v.end(), isTwo)
//remove elements with value 2 from the container v

we’ll get
1 3 1 3 1 3 ? ? ?
where ? – is some trash. tstart iterator, returned by remove_if(), will point to the first trash element, the third from the end in our case. Trash may contain the same values as before calling remove_if(), but that’s not guaranteed. Container size remains unchanged.

So if we write down the whole code which novice works with, we’ll get the following:
v.erase(remove_if(v.begin(), v.end(), isTwo));

And erase() will remove only the first trash element from the container and will change containers size by one. The contents will be the following:
1 3 1 3 1 3 ? ?
which is obviously wrong and causes unpredictable consequences if working further with this container.

How to avoid that? When developer is not sure in his skills, he is advised to check the documentation, and find out, that if we call erase() as follows:
v.erase(remove_if(v.begin(), v.end(), isTwo), v.end());
all the trash element will be removed from the container.

If the developer (sometimes overly) confident, the tests are always to the rescue. And don’t be lazy, write tests for border cases as well. In our case we should at least check removing not only single element, but several as well, and don’t forget to test none elements removal, and even test with empty container.

Danger of using default values in declarations

Working on a large project using C++, recently faced a dangerous things during
debugging. Let’s see in examples. Once there was a method:

Class1::SetEditBox(CString const& value, bool isUppercase = true);

Somewhere it was used differently:

obj1.SetEditBox(_T("Volume"), false);
 ...
 obj2.SetEditBox(_T("SerialNo"));

After a while this declaration was a bit extended:

Class1::SetEditBox(CString const& value, bool isEditable = false, bool isUppercase = true);

New places were using it properly, but not all that old places were changed, so

obj1.SetEditBox(_T("Volume"), false);

became to have isUppercase = true, however, since volume is usually numeric,
this was not detected during “happy way” testing.
Meanwhile system became more complex. Refactored and rewritten, developers were
also changing. Inheritance and other OOP stuff started to using this method:

Class2::SetListEdit(CString const& value, bool isEditable = false, bool isUppercase = true)
{
    // ...
    obj4.SetEditBox(value, isEditable, isUppercase);
    // ...
}

And again time passed, things changed, and so changed method declaration:

Class1::SetEditBox(CString const& value, bool isNumeric = true, bool isEditable = false, bool isUppercase = true);

Now there were even more things to recheck and update, but guess what?
Somebody, who did this change did not care about looking up every place to change
and now it became the bug, which I called “Shifting Boolean”:

Class3::AddListEdit(CString const& value, bool isUppercase = true)
{
    Class2 obj5;
    obj5.SetListEdit(value, isUppercase);
}

So the call to Class3::AddListEdit() and passing only CString value, made
isUppercase default value bacame isEditable in Class2::SetListEdit() and then
to became isNumeric in Class1::SetEditBox().

If there were no default values, all the differences would come out during
compilation, or even earlier, in IDE. However there are a trade-offs: either
you save typing and introduce default values, or make changes more transparent.

In any case, every change that can be interpreted by implicit type conversion
should be carefully examined and all the places should be changed as required.

It took me about 6 hours to refactor all the project code and get rid of this
bug. In my solution I have replaced booleans with a single enum argument, because
all the booleans were tightly coupled, and in the end switch()-ing between
enumerated values was easier than checking all the boolean combinations.

Omniva Rounding

Omniva (former Eesti Post) company sent me a message recently about the package which needed customs clearance.
However, it seems, that they have saved a penny on testing their software, that is generating such messages, especially the part about parcel weight.
I should feel lucky, that I haven’t received anything too large or too small to get an exponential formatting.
Omniva Rounding