Using printf in C (PI5)
printf
So far, we have done a lot ourselves. We have found a way for the Raspberry Pi to display text on the screen. However, our DrawString function doesn't have many advantages. Many of you probably know the printf function from the C programming language. This function can produce formatted output, where variables can also be included. Since we are programming in a bare-metal environment (no operating system), we cannot directly use this pre-built function and have to implement it ourselves. printf is a very powerful function.
With a bit of research on the internet, I found that some people have already implemented this function for similar projects. So the idea quickly came up to integrate such a solution into our project.
I decided to use the printf version by Kustaa Nyholm because it is easy to integrate and supports the functions we need for further development.
The library can be downloaded here: Tinyprintf.
The ZIP archive contains only two files. We save the printf.c file in our main directory and the printf.h file in the include directory.
The printf.h file describes how to integrate the functions into your own project.
Changes in the Main Program
First, we change our main program:
// main.c
#include "led.h"
#include "screen.h"
#include "types.h"
#include "printf.h"
int main(void)
{
LED_off();
Init_Screen();
init_printf(0, putc);
SetFrontColor(0x80, 0xa0, 0xff);
printf("*****************************************************************************************\n");
printf("*** ***\n");
printf("*** (c) by Satyria ***\n");
printf("*** ***\n");
printf("*****************************************************************************************\n\n");
SetFrontColor(0xff, 0xff, 0xff);
LED_Error(2);
}
The instructions state that the printf system must be initialized. This is done with the command init_printf(0, putc);.
Here, I then use printf to display my title in the terminal.
Additionally, we need to create a putc function that simply refers to the DrawCharAtCursor function:
void putc(void* p, char c)
{
DrawCharAtCursor(c); // Using printf in the terminal
}
Why not program everything ourselves?
Basically, I want to describe here that it sometimes doesn't make much sense to program everything yourself. The code I use here for printf was written back in 2004. So it's a code that is now almost 20 years old. And it still works!
You can download the source code as a ZIP file from the following link: https://www.satyria.de/arm/sources/C/printf.zip.
Future
I will delete this part of the text once I have managed to get USB running on the Raspberry Pi 5. While writing this, I am already working on it. But unfortunately, without success so far. It was already problematic with the introduction of the Raspberry Pi 4, as there were no suitable descriptions for USB connectivity. So far, there is only one project (Circle) that supports it. However, it is quite difficult to integrate into your own project as there are many dependencies that cannot be easily resolved. Relatively quickly, René Stange from the Circle project released an implementation for the Raspberry Pi 5, but it has the same problem as the Pi 4.
Nevertheless, I have set myself the goal of finding a way to at least provide a keyboard and mouse via the USB interface. This should be as simple as possible so that many projects can follow suit.
| < Back (The Terminal in C (PI5)) | < Home > | Next (xxx) > |