Quick GUI for MBED projects, part 2

The previous post I explained the code for the dear ImGui part of the GUI. In this part the code for the serial connection will be discussed.

This code is made up of 2 parts, the MBED code and the PC side code. The MBED code will be discussed first. The MBED code uses the excellent MODSERIAL library. The MBED code after stripping away the initialization and such looks like this:

The full code can be found on my Github.

The MBED sends a few strings to the PC, for an ADC value: “A1234\n”, in this case ADC1 has the value 12 and ADC2 has the value of 34. The ADC range is 00 to 99 and always is 2 digits. The IO pins are send as following: “I00110101\n”. Every 0 or 1 is the value of an IO pin. The PC side sends 2 strings back. The DAC value (“D12\n”, range of 00 to 99) and the selected LED’s (“L00001111\n”), works the same as the IO pins.)

The MBED code sets up MODSERIAL so it will make messageReceived high when an end of line “\n” character is received. In the main loop the MBED constantly checks if messageReceived is high and if so it decodes the message and sets the LEDs and DAC accordingly. 2 timer interrupts are initialized, one for the ADC and one for the IO pins. Every time these timer interrupts trigger the MBED sends the IO and ADC values to the PC side.

The PC code shown below are snippets from the code as all the ImGui related code has been explained in the previous blog post.

The serial port is opened with:

Open port ttyACM0 at 115200 BAUD with a timeout of 1000ms. The port has to be changed to the correct port when running this code obviously.

The code to read in lines and do something with them is as follows:

my_serial.readline(buffer, 100, “\n”); reads a line that ends with a “\n” character and places it in the buffer. It returns the amount of bytes read in. If there is 1 byte or more and the first byte is an A it’s ADC data, first byte an I means it’s IO pin data. The received data then gets decoded accordingly.

The LEDs and DAC values are send using the following code:

The actual part that sends data to the MBED is very simple, gather all the data in a string and send it using my_serial.write(); For the LEDs first all the separate LED values are combined in a single integer. If this integer is different from the last value it will send the new value, else it will do nothing. If a new value has to be send all the LED values are placed in an string and send to the MBED.

the DAC code works in a similar way. The integer value of the DAC gets converted a few times before it’s placed in a string and send to the MBED but the principle is the same as the LEDs.

The full code can be found on my Github page. It uses Dear ImGui and Serial, so the requirements for those two libraries should be installed, which I think is only OpenGL on a Linux system. But more info can be found on their github pages. In the end these two libraries make it fairly simple to create a fully C++ based GUI for any microcontroller with a serial port, be it an Arduino or a Raspberry pi. I hope these two posts can help others getting started with these two libraries and if there are any questions, feel free to contact me :)

So, what do you think ?