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

Discussion in 'B4R Code Snippets' started by Peter Simpson, Jul 7, 2017.

  1. Peter Simpson

    Peter Simpson Expert Licensed 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. 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.
    Code:
    '************************
    '***    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...
     

    Attached Files:

    Last edited: Aug 20, 2018
    Hypnos, Cableguy and Erel like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. Peter Simpson

    Peter Simpson Expert Licensed 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.
    Code:
    Initialising using C:
         SSD1306 display(
    0x3c54);    'Built-in OLED display
    Code:
    Initialising using B4R:
         SSD.InitializeHSPI(
    9810)    '7 pin OLED display
         or
         SSD.InitializeI2C(
    40x3c)      '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: Jul 9, 2017
    Johan Hormaza and Erel like this.
  4. Hypnos

    Hypnos Active Member Licensed User

    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!

    Code:
    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.
     
  5. Cableguy

    Cableguy Expert Licensed User

    Arduino IDE does not like spaces in folders not filenames!
     
  6. Hypnos

    Hypnos Active Member Licensed User

    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
     
  7. iCAB

    iCAB Well-Known Member Licensed 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

    Code:
    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

    Code:
    #include "SSD1306.h"
    while the library doesn't have such a file

    Can someone clarify please

    Thanks
    iCAB
     
  8. Peter Simpson

    Peter Simpson Expert Licensed 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.
     
  9. iCAB

    iCAB Well-Known Member Licensed 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
     
  10. Peter Simpson

    Peter Simpson Expert Licensed User

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

    iCAB Well-Known Member Licensed User

    Thank you
     
  12. iCAB

    iCAB Well-Known Member Licensed 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
     
  13. iCAB

    iCAB Well-Known Member Licensed User

    I have also searched my hard drive for SSD1306.h file, but I can't find one.
    Where is your file located?
     
  14. Peter Simpson

    Peter Simpson Expert Licensed User

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

    Attached Files:

    Last edited: Aug 20, 2018
  15. iCAB

    iCAB Well-Known Member Licensed User

    It does work now, but I also had to edit rCore.h as follows:
    Code:
    //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!
     
  16. Peter Simpson

    Peter Simpson Expert Licensed 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...
     
  17. iCAB

    iCAB Well-Known Member Licensed 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!
     
  18. Peter Simpson

    Peter Simpson Expert Licensed 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: Aug 20, 2018
  19. iCAB

    iCAB Well-Known Member Licensed User

    Peter Simpson likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice