B4R Code Snippet ESP32 with built in OLED display progress bar - Inline C

SubName: An ESP32 with a built in OLED display screen updating a progress bar.
Description: With this example source code, you can place a progress bar on the screen of an ESP32 microcontroller with a built in OLED display. You can display and update the progress bar using Inline C extremely easily. It's a pretty simple looking progress bar but it does look good if you need one in your project. I've attached the library that I used for this project.

Note: Download and unzip the attached files directly into your Arduino 'Documents\Arduino\libraries' folder.

Below is just one way to use Inline C to utilise a small part of the SSD1306 library.
B4X:
'************************
'***    BOARD TYPE    ***
'*** ESP32 Dev Module ***
'************************

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial

    Private TmrPBar As Timer
    Private UpDown As Boolean
    Private Progress As Byte
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    RunNative("setup", Null)

    TmrPBar.Initialize("TmrPBar_Tick", 25)
    TmrPBar.Enabled = True
End Sub

Sub TmrPBar_Tick
    RunNative("cleardisplay", Null)

    If UpDown Then
        Progress = Progress - 1
        If Progress <= 0 Then UpDown = False
    Else
        Progress = Progress + 1
        If Progress >= 100 Then UpDown = True
    End If

    RunNative("drawbrogressbar", Null)
    RunNative("updatedisplay", Null)
End Sub

#if c

#include "SSD1306.h"

    // Initialize the OLED display
    SSD1306 display(0x3c, 5, 4);

void setup(B4R::Object* unused)
{
    display.init();
    display.flipScreenVertically();
}

void drawbrogressbar(B4R::Object* unused)
{
    // draw the progress bar
    display.drawProgressBar(0, 32, 120, 10, b4r_main::_progress);

    // draw the percentage as String
    display.setTextAlignment(TEXT_ALIGN_CENTER);
    display.drawString(64, 15, String(b4r_main::_progress) + "%");
}

void cleardisplay(B4R::Object* unused)
{
    display.clear();
}

void updatedisplay(B4R::Object* unused)
{
    display.display();
}

#End If
Tags: ESP32, OLED, Display, Progress, Bar, Inline, C, C++

Here it is in action.

Enjoy...
 

Attachments

  • esp8266-oled-ssd1306-master.zip
    53.9 KB · Views: 732
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User

Of course I've seen that library @Erel :p
I've previously posted code snippets on here using your SSD1306 library. For starters it's missing some methods like 'display.drawProgressBar', and in any case it does not connected to this particular ESP32 with built in OLED display.

Look at the difference yourself.
B4X:
Initialising using C:
     SSD1306 display(0x3c, 5, 4);    'Built-in OLED display

B4X:
Initialising using B4R:
     SSD.InitializeHSPI(9, 8, 10)    '7 pin OLED display
     or
     SSD.InitializeI2C(4, 0x3c)      '4 pin OLED display

The library in your link point blank refuses to connect to this particular ESP32 with built in OLED and throws up some error messages, I can't remember the error messages as that was weeks ago. Anyway, all the above does not really matter as there's no drawProgressBar in your library full stop, so it's a waste of time me even looking at it for this particular code snippet example that I wanted to post for others to use.

I do use your library if I'm coding with every other microcontroller that I own like an UNO, Mega or ESP8266 etc with either my 4 pin or 7 pin OLED displays ;)

I don't like asking you or anybody else for that matter to update or create libraries especially in B4R as it's a free product and you're a very busy man, so the fact that you cleverly added the ability to use Inline X in B4X products is how I get around waiting for libraries or library updates or just using extra code in B4A and B4R.

The most annoying thing is that it's taking me a lot longer than I expected to learn C++ and C# (plus I work full time for my clients), and with my currently limited C knowledge I've previously tried to wrap a library following your 'Building a library or wrap an existing library is not an easy task' txt file, man was it was not an easy task and in any case I failed to wrap the library I wanted, so I just used inline C instead :cool:
 
Last edited:

Hypnos

Active Member
Licensed User
Longtime User
SubName: An ESP32 with a built in OLED display screen updating a progress bar.
Description: With this example source code, you can place a progress bar on the screen of an ESP32 microcontroller with a built in OLED display. You can display and update the progress bar using Inline C extremely easily. It's a pretty simple looking progress bar but it does look good if you need one in your project.

Below is just one way to use Inline C to utilise a small part of the SSD1306 library.
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial

    Private TmrPBar As Timer
    Private UpDown As Boolean
    Private Progress As Byte
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    RunNative("setup", Null)

    TmrPBar.Initialize("TmrPBar_Tick", 25)
    TmrPBar.Enabled = True
End Sub

Sub TmrPBar_Tick
    RunNative("cleardisplay", Null)

    If UpDown Then
        Progress = Progress - 1
        If Progress <= 0 Then UpDown = False   
    Else
        Progress = Progress + 1
        If Progress >= 100 Then UpDown = True
    End If

    RunNative("drawbrogressbar", Null)
    RunNative("updatedisplay", Null)
End Sub

#if c

#include "SSD1306.h"

    // Initialize the OLED display
    SSD1306 display(0x3c, 5, 4);

void setup(B4R::Object* unused)
{
    display.init();
    display.flipScreenVertically();
}

void drawbrogressbar(B4R::Object* unused)
{
    // draw the progress bar
    display.drawProgressBar(0, 32, 120, 10, b4r_main::_progress);

    // draw the percentage as String
    display.setTextAlignment(TEXT_ALIGN_CENTER);
    display.drawString(64, 15, String(b4r_main::_progress) + "%");
}

void cleardisplay(B4R::Object* unused)
{
    display.clear();
}

void updatedisplay(B4R::Object* unused)
{
    display.display();
}

#End If
Tags: ESP32, OLED, Display, Progress, Bar, Inline, C, C++

Here it is in action.

Enjoy...

Hi Peter,

I got the following error, do you know how to fix?
I tried to rename my Adafruit_SSD1306.h to SSD1306.h but still have error

Thanks!

B4X:
C:\Users\simon\Desktop\WEMOSO~1\Objects\bin\sketch\b4r_main.cpp:12:21: fatal error: SSD1306.h: No such file or directory
compilation terminated.
 

Cableguy

Expert
Licensed User
Longtime User
Arduino IDE does not like spaces in folders not filenames!
 

Hypnos

Active Member
Licensed User
Longtime User
Arduino IDE does not like spaces in folders not filenames!
Seems not the folder spaces issue, there are many 1306 Library available (I'm using Adafruit's Library). Not sure which one Peter Simpson using?

upload_2018-5-11_19-14-47.png
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Guys

I am trying to use the example code above, but when I try to compile the code I get the following error

B4X:
C:\data\B4RPRO~1\SOURCE~1\ESP32\ESP32_~3\Objects\bin\sketch\b4r_main.cpp:9:22: fatal error: SSD1306.h: No such file or directory

I downloaded the zip file in the first post and unzipped it to
  • C:\Users\user\Documents\Arduino\libraries

Also I am wondering why the include statement says

B4X:
#include "SSD1306.h"

while the library doesn't have such a file

Can someone clarify please

Thanks
iCAB
 

Peter Simpson

Expert
Licensed User
Longtime User
@iCAB the actual library that I'm using is in the first post, you shouldn't be having any issue once you have copied the attached library to your Arduino IDE external library folder.
 

iCAB

Well-Known Member
Licensed User
Longtime User
@iCAB the actual library that I'm using is in the first post, you shouldn't be having any issue once you have copied the attached library to your Arduino IDE external library folder.

Thanks Peter for your reply.

I just re-downloaded and copied the contents of the zip file to:
  • C:\Users\user\Documents\Arduino\libraries (This is the folder under Preferences->Sketchbook Location )
I am getting the same results.

Just a quick question: the B4R code includes "SSD1306.h", where should I see this file?

Thanks
iCAB
 

Peter Simpson

Expert
Licensed User
Longtime User
Thanks Peter for your reply.

I just re-downloaded and copied the contents of the zip file to:
  • C:\Users\user\Documents\Arduino\libraries (This is the folder under Preferences->Sketchbook Location )
I am getting the same results.

Just a quick question: the B4R code includes "SSD1306.h", where should I see this file?

Thanks
iCAB

Let me load up my code again and look at it again, I'll get back to you shortly...
 

iCAB

Well-Known Member
Licensed User
Longtime User
@Peter Simpson thanks again for taking the time to help with this.

1. I just checked the Arduino IDE. The library you have mentioned is already installed ( version 1.1.2)
2. I copied your code as is from the first post and placed in the B4R project (just in case)
3. I opened Board Selector and made sure that "ESP32 Dev module" is selected

I am still getting the same error

Thanks
iCAB
 

iCAB

Well-Known Member
Licensed User
Longtime User
I have also searched my hard drive for SSD1306.h file, but I can't find one.
Where is your file located?
 

Peter Simpson

Expert
Licensed User
Longtime User
I have also searched my hard drive for SSD1306.h file, but I can't find one.
Where is your file located?

Okay, please download the attached file, unzip it and place it directly into your Arduino 'Documents\Arduino\libraries' folder, then try it again...
 

Attachments

  • esp8266-oled-ssd1306-master.zip
    53.9 KB · Views: 658
Last edited:

iCAB

Well-Known Member
Licensed User
Longtime User
It does work now, but I also had to edit rCore.h as follows:
B4X:
//extern void *operator new( size_t size, void *ptr );


However I am extremely confused about the different library folders as I posted in the link below:
https://www.b4x.com/android/forum/threads/understanding-libraries-folders.96276/#post-607629

This is a very good example that may answer my above question.
How did B4R/Arduino determine which folder/Library to use in this case?
Was it determined based on the location of the only SSD1306.h that was found in all of the above listed folders. So what happens if we have 2 folders that contain SSD1306.h under the external library folder ?


Peter, thanks for all your help.
Greatly appreciated!
 

Peter Simpson

Expert
Licensed User
Longtime User
B4R doesn't determine the folder, the inline C does.

I'm not sure exactly how B4R works, but I'm guessing that B4R recompiles the B4X code into something that Arduino understands and sends that code via some kind of command line tool to the original Arduino folder where the original Arduino tool does the rest of the work including programming the microcontroller. But please don't ask me as I could be and most probably am incorrect about a command line tool ;)

Anyway thank you for letting me know about the esp8266-oled-ssd1306-master.zip file, I'll add that to the the first post. Can you please remove the original file from your Arduino library folder and let me know it the code still works...
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Peter

At this point, the only folder I have under the external libraries folder is: esp8266-oled-ssd1306-master
The program works no problem.

Many thanks for your help!
 

Peter Simpson

Expert
Licensed User
Longtime User
Thank you @iCAB, I've updated the original post.

Please note that you can easily manipulate the other functions in the library using inline C and not just the progress bar...
 
Last edited:
Top