Author Topic: Raspberry Pi programming in C  (Read 9233 times)

0 Members and 5 Guests are viewing this topic.

Offline ricko_ukTopic starter

  • Super Contributor
  • ***
  • Posts: 1233
  • Country: fr
Raspberry Pi programming in C
« on: March 21, 2025, 11:40:19 am »
Hi,
I have programmed various micros quite a bit in C but never before used the Raspberry Pi. A client is asking to potentially implement a system using a Raspberry Pi (it doesn't matter which one) driving this touch display: https://www.waveshare.com/3.4inch-dsi-lcd-c.htm

I was wondering how easy and straight forward it is to develop simple application with multiple screens with only buttons (to turn on/off various devices) specifically using C ?

Assuming someone knows C quite well (C++ a bit rusty), but never used the Raspberry Pi platform or any of the development environments / libraries etc required to make such simple application, how long would it take them to get from 0 to up and running with a couple of screens and a couple of buttons? Just as a rough idea tog et a feel for it... A day? A week? A month?

Is the learning curve steep? Any things I should be aware of from the beginning before making such decision?

Thank you :)
« Last Edit: March 24, 2025, 03:11:03 am by ricko_uk »
 

Offline elektryk

  • Frequent Contributor
  • **
  • Posts: 284
  • Country: pl
Re: Raspberry Pi programming in C
« Reply #1 on: March 21, 2025, 11:54:20 am »
Doing it from scratch may be difficult, but from what I see they provide install script, so driver part should be already done.
Then you can do UI part in whatever you want, e.g. GTK (c) or QT (cpp).
 

Online squadchannel

  • Super Contributor
  • ***
  • Posts: 1273
  • Country: jp
  • deepl translate user
Re: Raspberry Pi programming in C
« Reply #2 on: March 21, 2025, 12:02:04 pm »
you know C, probably use GTK, but using a Linux-based system with an SD card, such as RaspberryPi, I think the firmware would be frequently corrupted.

If it were me, use STM32 and TouchGFX.(or LVGL? etc...)
« Last Edit: March 21, 2025, 12:07:39 pm by squadchannel »
 

Online pcprogrammer

  • Super Contributor
  • ***
  • Posts: 6130
  • Country: nl
Re: Raspberry Pi programming in C
« Reply #3 on: March 21, 2025, 12:09:08 pm »
Depends on how good your linux knowledge is.

The easiest way to use the Raspberry PI is to load a linux system onto it. Display and touch panel support are included and programming it in C is as simple as C.  :)

There are lots of examples on how to control IO with the Raspberry PI.

I used minibian, with a headless setup, to develop my central heating control system with all the extra code needed written in C. With the compiler on the Raspberry PI and connecting via ssh it was just as easy as writing C for desktop linux. Unfortunately minibian is no longer supported, but raspbian is still there and can be used for your purpose.

It did not take me that long to get it up and running, but already knew some ins and out on linux. No expert at the time though, and even now, after working with linux and solving issues for 10 years or so, still not a real linux expert.

The display you linked to will most likely work out of the box, by setting the correct device tree for it. The wiki page for it provides al the information you need.

The SD card corruption mentioned by squadchannel is only a problem when you store data on the card regularly and switch of the Pi during a write. With proper power down or a newer version with the usage of eMMC it should not be a problem.

My system used a harddisk because it was logging temperatures in a database and that is definitely sensitive when not properly powered down.

For the graphics LVGL is probably a better choice than the GTK or QT mentioned by others.

Offline elektryk

  • Frequent Contributor
  • **
  • Posts: 284
  • Country: pl
Re: Raspberry Pi programming in C
« Reply #4 on: March 21, 2025, 12:27:48 pm »
LVGL is also good idea, because its pretty portable, so you can use (almost) the same code with bare metal and OS.
 

Offline bostonman

  • Super Contributor
  • ***
  • Posts: 2845
  • Country: us
Re: Raspberry Pi programming in C
« Reply #5 on: March 21, 2025, 01:14:01 pm »
Does a difference exist between programming a Pi and Arduino in C and straight C?

I know some basic C and have been tinkering with a Arduino Nano, and the example programs look like generic C.
 

Offline elektryk

  • Frequent Contributor
  • **
  • Posts: 284
  • Country: pl
Re: Raspberry Pi programming in C
« Reply #6 on: March 21, 2025, 01:43:54 pm »
Yeah, Arduino users often doesn't use all of the cpp features, so sometimes code may look like plain c.

BTW some cpp features aren't advisable for embedded systems.
« Last Edit: March 21, 2025, 01:46:14 pm by elektryk »
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17814
  • Country: fr
Re: Raspberry Pi programming in C
« Reply #7 on: March 21, 2025, 01:57:28 pm »
Well unless you client is ok with the Pi being programmed "bare metal" (which is doable but not officially supported), you're going to be dealing with a Linux OS on the Pi, so after that, as long as the display is going to be a HDMI or MIPI DSI (like here) one, it's going to be seen as a regular display for the OS and you'll be able to use pretty much any GUI library available on Linux. If the language is C only, the obvious toolkit is GTK (3/4) which is one of the few that have a direct C API.

LVGL is an interesting suggestion as the time you'll invest in it will be reusable for other projects on more modest targets such as MCUs, so if you have potential projects on MCUs with a GUI, then I would recommend it over GTK, that'll be time better invested.

Just a note regarding this display: while it should be usable on a Pi via its MIPI DSI interface, you'll have some configuration work to do for that on the Pi side, and its odd resolution and shape may give you some headaches, but that's probably doable. I wanted to point this out, but as for help, I'll let others who have already done this chime in.
 

Online pcprogrammer

  • Super Contributor
  • ***
  • Posts: 6130
  • Country: nl
Re: Raspberry Pi programming in C
« Reply #8 on: March 21, 2025, 02:53:59 pm »
Well unless you client is ok with the Pi being programmed "bare metal" (which is doable but not officially supported), you're going to be dealing with a Linux OS on the Pi, so after that, as long as the display is going to be a HDMI or MIPI DSI (like here) one, it's going to be seen as a regular display for the OS and you'll be able to use pretty much any GUI library available on Linux. If the language is C only, the obvious toolkit is GTK (3/4) which is one of the few that have a direct C API.

Don't forget about X11. I use it to make my GUI's on the desktop. Rather straight forward and in plain C.

Offline Laval

  • Regular Contributor
  • *
  • Posts: 138
  • Country: ca
Re: Raspberry Pi programming in C
« Reply #9 on: March 21, 2025, 03:31:08 pm »
Does a difference exist between programming a Pi and Arduino in C and straight C?

I know some basic C and have been tinkering with a Arduino Nano, and the example programs look like generic C.

As far as the language is concerned, no. C is basically the same, it's a fairly simple and basic language and it doesn't do much in and by itself unless you want to go bare metal. The differences come with the frameworks and external (external to the language) libraries you will use. Such frameworks are used to deal with GUI (Gtk, Qt, wxWidget, LVGL, etc) as well as networking (http clients, usb communication, ...) and other higher level tasks encountered when developing applications. These are not part of the language. C is a bare bone language, it has basic types for variables, arrays and structs, it has flow control instructions like conditions and loops as well as basic io and maths libraries. Learning all these external frameworks can have a steep learning curve. A Pi is a computer that uses an OS and you are basically developing an application.

You mentioned C++. C++ is a superset of C. It is like C but with added features to support object oriented programming. It also has a more comprehensive standard library that comes with it. OOP is nothing more then a way to organized you code. It doesn't solve much problem at least at the algorithmic level, it's really just a different way to think and structure your code. (A lot will argue OPP creates its share of problems and there are valid arguments for this, this is a debate I'm not going to enter.)  There is also a learning curve associated to OOP. Like C though, C++ needs to use external libraries for GUI, network and many other application related things.

Programming a Pi means entering more into the realm of software and application development.
« Last Edit: March 21, 2025, 07:12:48 pm by Laval »
I would rather have questions that can't be answered than answers that can't be questioned.

- Richard Feynman
 

Offline ricko_ukTopic starter

  • Super Contributor
  • ***
  • Posts: 1233
  • Country: fr
Re: Raspberry Pi programming in C
« Reply #10 on: March 23, 2025, 10:04:06 am »
Thank you all for the information and the suggested options. Very useful. :)
 

Offline bostonman

  • Super Contributor
  • ***
  • Posts: 2845
  • Country: us
Re: Raspberry Pi programming in C
« Reply #11 on: March 23, 2025, 01:38:42 pm »
Due to a hardware project that involves a Nano, I've been using Arduino software that allows me to take the program developed by someone and program the Nano.

A few line edits need to be made, and, with my limited knowledge of C, I'm able to dive into the code to make the changes.

The reason I asked about the differences in C and a Nano is liking the Arduino software. I think the Arduino software is nice and user friendly. I can edit the C program and compile it all within the same window. In the past I've used Visual Studio along with other pieces of software. With VS, I've entered working C++ programs I've saved from when I took a course decades ago and it always gives compiling errors. Same with some basic C programs.

What I found (or believe was the case) was VS needed settings tinkered to properly compile and I found myself tinkering with VS more than I got to learn C. Other programs that were less involved like GTK (or something like that I think) result in too much window switching and manually typing the file name to run; or the program quickly opens a DOS window and then closes without me seeing the program run.

With Arduino, everything seems to be all in one program, but wondered whether it is ideal software for learning C (or expanding my C knowledge).
 

Offline Laval

  • Regular Contributor
  • *
  • Posts: 138
  • Country: ca
Re: Raspberry Pi programming in C
« Reply #12 on: March 23, 2025, 04:25:52 pm »
With VS, I've entered working C++ programs I've saved from when I took a course decades ago and it always gives compiling errors. Same with some basic C programs.

Visual Studio is an IDE (Integrated Development Environment) which integrates a compiler, a linker, a debugger and an enormous amount of libraries. Without going into the specifics of the problems you encountered, C and C++ programs will typically use libraries because those languages don't do a lot of things out of the box e.g. GUI, Network, etc. If you take a program that has been written in C that makes calls to some library functions, you need to have those libraries integrated in your project to be able to call them. They are not part of the language. You can't just copy an Arduino program, paste it in a Visual Studio command line program and expect it to work.

As for learning and expanding you knowledge of C itself, yes Arduino is as good a platform as any but all platforms have their specifics and they are different. Another thing that helps a lot with native programming is learning how toolchains work, what is a compiler, a linker and libraries and how they are used to generate executable code from text.


« Last Edit: March 23, 2025, 04:30:33 pm by Laval »
I would rather have questions that can't be answered than answers that can't be questioned.

- Richard Feynman
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6598
  • Country: 00
Re: Raspberry Pi programming in C
« Reply #13 on: March 23, 2025, 08:44:27 pm »
May be easier to write it in Python.

For example  https://projects.raspberrypi.org/en/projects/getting-started-with-guis/5

Use a good AI such as chatgpt or grok and they will help you.
 

Offline Laval

  • Regular Contributor
  • *
  • Posts: 138
  • Country: ca
Re: Raspberry Pi programming in C
« Reply #14 on: March 23, 2025, 11:27:22 pm »
May be easier to write it in Python.

For example  https://projects.raspberrypi.org/en/projects/getting-started-with-guis/5

Use a good AI such as chatgpt or grok and they will help you.

That's certainly an alternative. Keep in mind that using an interpreter like Python consumes more resources like memory which small MCUs have little of. This may or may not be a problem depending on what your program does.
« Last Edit: March 23, 2025, 11:29:12 pm by Laval »
I would rather have questions that can't be answered than answers that can't be questioned.

- Richard Feynman
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5745
  • Country: Earth
Re: Raspberry Pi programming in C
« Reply #15 on: March 23, 2025, 11:41:27 pm »
you can use ImGui framework with SDL2 OpenGL ES backend to implement GUI with hardware accelerated graphics, it can run on Wayland or just KMSDRM mode which can be used with light version of linux with no X11/Wayland desktop. Raspberry Pi Zero 2W is enough to run pretty complicated GUI with realtime rendering.

Here is example how ImGui looks:


It supports touch, but I didn't used it. There are different bindings for ImGui, so you can use it with c++, c#, python and other programming languages.

Regarding to the system, Wayland desktop is better, because it is more lightweight and has much more faster and smooth graphics than X11. The performance difference between X11 and Wayland is about 30%. Wayland is even a little bit better than KMSDRM backend for OpenGL ES apps - the difference is not significant, but running app under Wayland give you a little bit better frames per second.

Raspberry Pi 4 GPU is able to run Quake3 or Arx Fatalis 3D games with nice pretty playable fps, when running them on Wayland backend you will not notice lags, so it will be enough for pretty complex GUI.
« Last Edit: March 23, 2025, 11:59:39 pm by radiolistener »
 

Online ledtester

  • Super Contributor
  • ***
  • Posts: 4221
  • Country: us
Re: Raspberry Pi programming in C
« Reply #16 on: March 23, 2025, 11:53:14 pm »
May be easier to write it in Python.

For example  https://projects.raspberrypi.org/en/projects/getting-started-with-guis/5

Use a good AI such as chatgpt or grok and they will help you.

I think using Python is definitely a great way to learn a GUI library and it may also be suitable for your application since you are developing on a Raspberry Pi.

For lvgl there isn't a Python binding as far as I can tell but there is a MicroPython binding:

https://github.com/lvgl/lv_micropython

and MicroPython is usable on more capable microcontrollers if you are not on a full-fledged Linux system.
 

Online dobsonr741

  • Frequent Contributor
  • **
  • Posts: 978
  • Country: us
Re: Raspberry Pi programming in C
« Reply #17 on: March 24, 2025, 02:35:06 am »
Bare metal is simple. Just use Circle: https://github.com/rsta2/circle it’s awesome. Use ChatGPT if your C++ is rusty. Or Cursor, if you can afford subscribing. Best monthly subscription I ever had.

 

Offline ricko_ukTopic starter

  • Super Contributor
  • ***
  • Posts: 1233
  • Country: fr
Re: Raspberry Pi programming in C
« Reply #18 on: March 24, 2025, 03:14:24 am »
Thank you for the additional info. A lot to look into. :)
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5745
  • Country: Earth
Re: Raspberry Pi programming in C
« Reply #19 on: March 24, 2025, 11:53:40 am »
Bare metal is simple. Just use Circle: https://github.com/rsta2/circle it’s awesome. Use ChatGPT if your C++ is rusty. Or Cursor, if you can afford subscribing. Best monthly subscription I ever had.

Using Linux it is pretty simple to write OpenGL apps, for example, here is simple OpenGL code which renders triangle:
Code: [Select]
#include <SDL2/SDL.h>
#include <GL/gl.h>
#include <stdio.h>

float vertices[] = {
    // Positions       // Colors (RGB)
     0.0f,  0.5f, 0.0f,  1.0f, 0.0f, 0.0f,  // Top (Red)
    -0.5f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,  // Bottom Left (Green)
     0.5f, -0.5f, 0.0f,  0.0f, 0.0f, 1.0f   // Bottom Right (Blue)
};

void renderTriangle() {
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), vertices);
    glColorPointer(3, GL_FLOAT, 6 * sizeof(float), vertices + 3);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);
}

int main() {
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL_Init() failed: %s\n", SDL_GetError());
        return -1;
    }

    SDL_Window* window = SDL_CreateWindow("OpenGL Triangle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
    if (!window) {
        printf("SDL_CreateWindow() failed: %s\n", SDL_GetError());
        return -1;
    }

    SDL_GL_CreateContext(window);

    SDL_Event event;
    int running = 1;
    while (running) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) running = 0;
        }

        renderTriangle();
        SDL_GL_SwapWindow(window);
    }

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}


Then you can run it using X11, Wayland, or using lightweight Linux with no graphical desktop manager (using its KMSDRM mode). It will use full GPU acceleration.

Is it really more simple to use bare metal? Can you show more simple code to do the same?
« Last Edit: March 24, 2025, 11:58:16 am by radiolistener »
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17814
  • Country: fr
Re: Raspberry Pi programming in C
« Reply #20 on: March 24, 2025, 12:31:13 pm »
Bare metal is simple. Just use Circle: https://github.com/rsta2/circle it’s awesome. Use ChatGPT if your C++ is rusty. Or Cursor, if you can afford subscribing. Best monthly subscription I ever had.

Circle is fantastic work, but it's C++ and the OP said "specifically C". And, as I mentioned, while baremetal works and Circle is great, this is an approach that is based on a lot of reverse engineering and the client may not be comfortable with that.
Certainly something to consider otherwise and if you're not allergic to C++.

In a similar vein, but for those who aren't against trying something else and use Pascal, there is Ultibo: https://ultibo.org/
very interesting stuff. I don't know how active the project still is though, it doesn't seem to have RPi 5 support yet and may not have seen a lot of activity lately. Definitely outside of what the OP is looking for, but something that could pique some people's curiosity.
 

Offline DonKu

  • Regular Contributor
  • *
  • Posts: 54
  • Country: us
Re: Raspberry Pi programming in C
« Reply #21 on: March 24, 2025, 02:01:05 pm »
Depends on how good your linux knowledge is.

The easiest way to use the Raspberry PI is to load a linux system onto it. Display and touch panel support are included and programming it in C is as simple as C.  :)

...

The SD card corruption mentioned by squadchannel is only a problem when you store data on the card regularly and switch of the Pi during a write. With proper power down or a newer version with the usage of eMMC it should not be a problem.

You can also install FreeBSD on a RPi. My RPi with FreeBSD on a SD runs reliably regardless of the many times over the years its power plug was pulled without a graceful shutdown.
 
The following users thanked this post: pcprogrammer

Offline Kasper

  • Frequent Contributor
  • **
  • Posts: 966
  • Country: ca
Re: Raspberry Pi programming in C
« Reply #22 on: March 24, 2025, 04:21:56 pm »
In a similar vein, but for those who aren't against trying something else and use Pascal, there is Ultibo: https://ultibo.org/
very interesting stuff. I don't know how active the project still is though, it doesn't seem to have RPi 5 support yet and may not have seen a lot of activity lately. Definitely outside of what the OP is looking for, but something that could pique some people's curiosity.

Years ago I made a trail camera prototype with RPi.  Not knowing RPi, I started with Python following beginner guides.  Couldn't believe how easy it was to get a camera running and saving pictures.

Then to get it to boot faster, I tried Ultibo and Pascal.  That was at least 10x more work.  I think I got it down to 3 seconds from power on to picture taken.  Not fast enough for a trail camera but a good learning experience. 
 

Offline bostonman

  • Super Contributor
  • ***
  • Posts: 2845
  • Country: us
Re: Raspberry Pi programming in C
« Reply #23 on: March 24, 2025, 10:26:25 pm »
It seems the Arduino may be good for C programming, but I don't see the ability to create an EXE from it.

The thing I like about this: you have the C program, click compile, and it complies. The way I've done it which I think is a pain is run gnn or something like that, blah blah.

This isn't the first time I've mentioned searching for a simple GUI to do C, but it's often deviated towards suggestions about doing something in C++ or Python, etc...

Ideally I'd like a simple GUI based program on the idea of VS that allows me to compile with a few mouse clicks and run an EXE but without having to dive into endless options that cause me to wonder if my C program is wrong or I need to tweak an option.
 

Offline abeyer

  • Frequent Contributor
  • **
  • Posts: 955
  • Country: us
Re: Raspberry Pi programming in C
« Reply #24 on: March 25, 2025, 01:51:05 am »
Ideally I'd like a simple GUI based program on the idea of VS that allows me to compile with a few mouse clicks and run an EXE but without having to dive into endless options that cause me to wonder if my C program is wrong or I need to tweak an option.

If someone had a simple gui that let people know that their C programs were correct they'd be way too rich to be commenting here.  :-DD
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

-->