User avatar
Grumpy Mike
Posts: 998
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District
Contact: Website

OLED driver in C?

Wed Apr 28, 2021 4:32 pm

I want to incorporate an OLED display in a C project. I have found one at:-
https://github.com/martinkooij/pi-pico-ss--oled
It uses the Pico's I2C drivers but it is for C++. Any Idea how to use this with C code, or how to remove the C++ wrapper?

Or indeed another OLED driver that will work with the Pico.
Thanks.

Reneg973
Posts: 26
Joined: Mon Apr 26, 2021 5:49 pm
Location: Germany

Re: OLED driver in C?

Wed Apr 28, 2021 6:21 pm

Your link doesn't work.

1. Rename class to struct.
2. move all member functions outside.
3. give all member functions as one parameter a pointer to the struct.
4. modify all member access with struct->member

User avatar
scruss
Posts: 4088
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: OLED driver in C?

Wed Apr 28, 2021 6:27 pm

this is the link: https://github.com/martinkooij/pi-pico-LCD

But it's not for OLEDs, but for HD44780 character LEDs (16x2, 20x4) etc.

Mike, are you looking for an SSD1306 driver? There are a few variants
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
Grumpy Mike
Posts: 998
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District
Contact: Website

Re: OLED driver in C?

Wed Apr 28, 2021 7:54 pm

Thanks Guys,
are you looking for an SSD1306 driver
Yes.
I did copy that link from a read me file, and on closer examination I got two - before the last word, sorry.
This is what I tried:-
https://github.com/martinkooij/pi-pico-ss-oled

User avatar
scruss
Posts: 4088
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: OLED driver in C?

Wed Apr 28, 2021 9:42 pm

aha! And I, in turn, copied the wrong link from the author's repo. We're both doing great today!

Looks like the library that it's built from is pure C: https://github.com/bitbank2/ss_oled
Now, it does assume 'soft' (bit-banged) I²C, which will still work on a Pico, just not be ideal. It looks like hardware I²C can work, just that it's not the default. The filenames look like they've been renamed to cpp and ino so they'll work with Arduino, but I don't see anything that looks like C++
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

ConfuSomu
Posts: 72
Joined: Tue Dec 04, 2018 8:47 am
Location: Canada

Re: OLED driver in C?

Wed Apr 28, 2021 11:11 pm

I currently use bitbank's library ported for the Pico: https://github.com/bitbank2/Pi_Pico_C_Projects (under the ss_oled directory).

scruss, I assume that you used that as a base for https://github.com/martinkooij/pi-pico-ss-oled/ ?

A while back, I created a topic about using OLED displays with Pico, it can have a little more information: viewtopic.php?f=144&t=299754&p=1802001
If you are low on RAM, make sure to use zram! ;)
I found & use this zram setup script: https://github.com/novaspirit/rpi_zram
A post about zram: t=207304
Make sure to disable dphys-swapfile and mount the tmp folders in RAM. Also learn about SysRq!

User avatar
Grumpy Mike
Posts: 998
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District
Contact: Website

Re: OLED driver in C?

Thu Apr 29, 2021 6:54 am

scruss wrote:
Wed Apr 28, 2021 9:42 pm
The filenames look like they've been renamed to cpp and ino so they'll work with Arduino, but I don't see anything that looks like C++
Well in the file ss_oled.hpp there is

Code: Select all

 class picoSSOLED {
Which is very C++ ;)

Thanks ConfuSomu for that other link, I will try and get this compiled.

On a related note my copy of LXTerminal has started to play up since I tried to compile the original C++ version and now it will only show a screen full of text and does not scroll any more. The scroll bar just shows as the full length of the window. Any ideas what this should be and how to fix it?
Cheers

martinkooij
Posts: 57
Joined: Sun Feb 28, 2021 5:22 pm
Location: The Netherlands

Re: OLED driver in C?

Thu Apr 29, 2021 10:51 am

Hi all,

Concerning the ss-oled library I created in https://github.com/martinkooij/pi-pico-ss-oled.

The code for driving the SS-OLED itself is very much C based, and can be used as such, it is indeed a variation on the bitbank library as can be seen from the attribution in the code and the README file: "The C++ is basically a simple wrapper around the C library of Larry Bank. Code concerning bit banging is stripped and replaced by the i2c facilities of pi Pico." I adapted the code to be focussed on the PICO, using the PICO I2C hardware for putting out the bits (not the soft bit-banged version that the Arduino uses).

Around this C-style driver software I used / copied / adapted the .ino work from Arduino to create some wrapper code that is very C++ based : creating an object class, where the object remember what the GPIO pins are, what the I2C parameters are and where the malloc'ed backbuffer memory can be found. This is all stored within the object at constructor time, and allows for an easy interface to the OLED screen by calling the various functions as methods on the constructed object.

So instead of the C pattern where you would create a struct holding some specific data to the oled and pass the address of that struct to a function (e.g. init(&myoled, x, y, z)), you can now use the C++ pattern of constructing an object (myOled(x,y,z,)) and calling the relevant method (myOled.init()) on that object. It is a matter of style, of course. (Under water the C++ compiler by the way does very much that, changing method calls to function calls with an invisible extra parameter pointing to the object ;)).

I personally like the C++ way of interfacing to tinker hardware where C++ objects are representations of "real world" objects (a screen, a sensor, a tone generator, ...) that can be handled via its methods. There is some loss of efficiency, but in the case of using just C++ as an abstraction wrapper, and writing most of the underlying code in C, using as much as possible the hardware of the PICO, not even too much. I do know and understand it is a matter of personal style. I just like to present it in that way. Feel free to use the C for your projects, of course!

Have fun tinkering and let's keep helping each other out!

User avatar
Grumpy Mike
Posts: 998
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District
Contact: Website

Re: OLED driver in C?

Thu Apr 29, 2021 2:14 pm

A great big thanks for your contribution.
I was able to run the demo code and confirm that my hardware was working. All I had to do was to change the I2C address I had wired it up to.
So instead of the C pattern where you would create a struct holding some specific data to the oled and pass the address of that struct to a function (e.g. init(&myoled, x, y, z)), you can now use the C++ pattern of constructing an object (myOled(x,y,z,)) and calling the relevant method (myOled.init()) on that object. It is a matter of style, of course.

Yes I like that structure too it is what I have been doing with the Arduino for the last 15 years or so. However, the Arduino hides all the conversion stuff with getting C code to run C++ classes. This has lead me to be totally unprepared for the Raspberry Pi way of doing things.

I did try and add your code to my C code but the compiler threw a wobbler at the first use of the keyword "class". I also tried changing my C code to CPP ( by simply changing the file extension of the file and in the make text) but that proved equally disastrous.
I then tried the code at https://github.com/bitbank2/Pi_Pico_C_Projects and add the C stuff along with your true I2C version of BitBang_i2c.h and .c. And eventually by extra #include statements I got it to compile. However, sadly this did not display anything on the display.

I am at a bit of a loss now as to what to try. Have you any suggestions?
Thanks
Mike

martinkooij
Posts: 57
Joined: Sun Feb 28, 2021 5:22 pm
Location: The Netherlands

Re: OLED driver in C?

Thu Apr 29, 2021 8:30 pm

No problem. Can you provide a pointer to yor code? Github, pastebin, zip attachment,... ? C files, header files, CMakeLists.txt...

I like to try and find what's wrong! I am also still learning and struggling with my own or other people's code is the best learning experience to dive further into the pico!

User avatar
Grumpy Mike
Posts: 998
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District
Contact: Website

Re: OLED driver in C?

Thu May 06, 2021 1:24 pm

martinkooij wrote:
Thu Apr 29, 2021 8:30 pm
No problem. Can you provide a pointer to yor code? Github, pastebin, zip attachment,... ? C files, header files, CMakeLists.txt..
Sure can you contact me with a PM and I will send you a link.

Return to “SDK”