13. Interface and Application Programming

In this group assignment, we compare the different options we have for programming a general purpose application on a computer (Linux/Windows).

While there are plenty of new programming languages being developed, we focus on the most intuitive/famous examples.

C/C++

The C language was first invented to implement the Unix operating system. It is still one of the most popular languages to this day, and the base for a lot of open source projects. The underlying implementation of a variety of modern languages such as Python is written in C. This language is compiled, which means the code source gets translated to binary instruction (assembly code) specific to the target processor. This lets the programmer optimize the code to an advanced level, even allowing the use of inline assembly code. On the other hand, the code must be compiled after any change or new target architecture.

After the success of C, a modern alternative was proposed: C++. The main contribution is object-oriented programming: instead of storing variables in struct types, users can now define a class with public/private members, which can be functions or variables. Instances of this class are called objects, and they help structuring the code in a more intuitive way. Their members help to retain information and manipulate it internally: only a few public functions should be visible to the end user. Over the last few years, numerous updates to C++ have helped it in remaining relevant as a fast, object-oriented, compiled language. Advanced synthax has now become significantly different from C, so the two should not be confused.

Here is a simple hello world in C:

<include/stdio.h>

int main() {
  char* txt = "hello world\n";
  printf(txt);
  return 0;
}

Notice the type of the variable txt: it is a pointer type as indicated by the *. This powerful concept lets you manipulate any part of the memory that was allocated for your program. In this case, the text is stored as a character array (1 letter = 1 byte = 1 char), and the variable txt is nothing more than the address of the first character.

Quentin showed how to program a SAMD11C14 in pure C in a side project. Most other code shown in our group assignments are written in C or C++ through the Arduino library.

Python

Python is a versatile, multiplatform language. Unlike C/C++ which requires compilation, Python is interpreted. This mean each code line is translated to basic CPU operations in realtime by the Python interpreter, written in C. Moreover, Python is not typed: all variables are objects of the same nature, and they can be used interchangeably as long as their API allows it. Python is object-oriented at its very core: even functions are object instances.

One of the best aspects of programming in Python is the package manager, along with the Python package index, offering to download and install packages from the community. Any installed package can be imported and used in your own code.

Here is a hello world application in Python:

def main():
  txt = "hello world"
  print(txt)

main()

Notice how the syntax does not require semicolons (;) at the end of each line. Instead, line breaks and consistent tabs are mandatory. Also note that unlike C, the main() function does not have a special meaning, so it must be called from the outside.

In his week assignment, Quentin showed how to design a graphical interface with Python and the Qt module.

JavaScript

A possible option to build a web application is to use JavaScript. During his week assignment Robin used JavaScript to make a user interface for a HC-SR04 sonar. He took advantage of the files provided during the FABACAMEDY lectures, in particular this one: hello.mag.45.js. Here is a video presenting an example of GUI interface Robin made to interact with the HC-SR04 sonar.

The main asset of this solution is that it can work on any web browser and thanks to HTML and CSS, it is possible to improve the visual quality of the graphical user interface. JavaScript can also communicate with either a serial port, a USB port or through Bluetooth, or WiFi with the help of specific web APIs:

In fact, the Web Bluetooth API protocol was the solution chosen by Robin for its final project, the fountain of despair, in combination with JavaScript, HTML and CSS to give some style to the webpage. The following video provides a demonstration of it.

three.js

three.js is an outstanding JavaScript 3D library. It allows to create, integrate and display animated 3D computer graphics in a web browser using WebGL. During his week assignment Robin also worked on a three.js project with the aim of using it for its final project. As he demonstrated it, three.js allows to create a variety of geometric entities (such as cubes, spheres, etc.) and interact with them just like in a video game. In fact one can find examples in which three.js is used to create video games but we advice the reader to use other softwares such as Unreal or Unity which are more convenient for this purpose. three.js also allows to load 3D models on a scene and edit lights, cameras and even more.

Here is an example of an integrated 3D model that Robin made during his week assignment: