B4R Code Snippet ESP32 with built in OLED display connected to WiFi - Inline C

Discussion in 'B4R Code Snippets' started by Peter Simpson, Jun 29, 2017.

  1. Peter Simpson

    Peter Simpson Expert Licensed User

    SubName: An ESP32 with a built in OLED display screen connected to WiFi.
    Description: With this example source code you can connect your ESP32 microcontroller with a built in OLED display to your local WiFi network (Access Point) and show whatever information you want to on the display.

    For this project I used Inline C and the SSD1306 library to power the built in OLED display. The current B4R library appears not to be fully compatible with this particular ESP8266 microcontroller, anyway that does not really matter as this was the perfect opportunity to use the B4R Inline C feature. This is where B4R Inline C comes into its own, using Inline C with the SSD1306 library allows you to fully utilise the methods listed below with no issues whatsoever.
    • display.drawRect
    • display.fillRect
    • display.drawHorizontalLine
    • display.drawVerticalLine
    • display.drawCircle
    • display.fillCircle
    • display.drawProgressBar :cool:
    • display.setTextAlignment
    • display.drawString
    • display.setFont
    Etc etc etc.

    I originally converted an Arduino IDE sketch to B4R that utilised the aforementioned library, I then decided to test the rESP8266WiFi library with this ESC32, it worked perfect. Once the WiFi was connecting I simply added the OLED display code below.

    Using Inline C made purchasing this particular ESC32 worth every single penny (about £8.50 with an 18650 battery shield with built in charger).

    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 WiFiConnect As ESP8266WiFi
    End Sub

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

        WiFiConnect.Connect2(
    "[YOUR ACCESS POINT]""[AP PASSWORD]")

        RunNative(
    "setup"Null)
        RunNative(
    "cleardisplay"Null)
        RunNative(
    "title""B4R")
        RunNative(
    "connected", JoinStrings(Array As String("Connected = ", WiFiConnect.IsConnected)))
        RunNative(
    "localip", JoinStrings(Array As String("IP ", WiFiConnect.LocalIp)))
        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 title(B4R::Object* String)
    {
        const char* Text = (const char*)String->data.PointerField;
        display.setTextAlignment(TEXT_ALIGN_CENTER);
        display.setFont(ArialMT_Plain_24);
        display.drawString(64, 0, Text); //64 is half the display width in pixels
    }

    void connected(B4R::Object* String)
    {
        const char* Text = (const char*)String->data.PointerField;
        display.setTextAlignment(TEXT_ALIGN_LEFT);
        display.setFont(ArialMT_Plain_16);
        display.drawString(0, 25, Text);
    }

    void localip(B4R::Object* String)
    {
        const char* Text = (const char*)String->data.PointerField;
        display.setTextAlignment(TEXT_ALIGN_LEFT);
        display.setFont(ArialMT_Plain_16);
        display.drawString(0, 45, Text);

        //display.drawHorizontalLine(0, 50, 128); //(Left, Down, Right)
    }

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

    void updatedisplay(B4R::Object* unused)
    {
        display.display();
    }
    #End If
    Tags: ESP32, WiFi, OLED, Display, Inline, C, C++

    IMG_20170629_223034.jpg

    Enjoy...
     

    Attached Files:

    Last edited: Aug 20, 2018
  2. max123

    max123 Active Member Licensed User

    Many thanks for this code!

    And how to pass more arguments to functions, eg. like this?
    • display.drawRect(x, y, width, height, color)
    Code:
    void drawRect(B4R::Object* x, B4R::Object* y, B4R::Object* width, B4R::Object* height, B4R::Object* color)
    Is this possible?
     
    Last edited: Jul 6, 2017
  3. Peter Simpson

    Peter Simpson Expert Licensed User

    Hello @max123,
    After seeing some of your coding, you were the last person that I would have expected to be asking that question, only joking ;)

    Anyway, here is the test square code that I used.

    IMG_20170706_230211.jpg
    Code:
    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 x, y, width, height As Byte 'Ignore - Can replace 'Private' with 'Public' and remove Ignore
    End Sub

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

        x = 
    12 : y = 12 : width = 20 : height = 20

        RunNative(
    "setup"Null)
        RunNative(
    "cleardisplay"Null)
        RunNative(
    "drawrect"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();
    }

    void drawrect(B4R::Object* unused)
    {
        display.drawRect(b4r_main::_x, b4r_main::_y, b4r_main::_width, b4r_main::_height);
    }

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

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

    #End If
    Enjoy...
     
    Last edited: Jul 6, 2017
    Johan Hormaza, max123 and inakigarm like this.
  4. max123

    max123 Active Member Licensed User

    Hi @Peter Simpson , many thanks for your fast reply, your test and code you posted.

    Yes, I'm not very expert but I"ve developed around 15 libraries for B4J one works on Raspberry PI from command line, same libraries for B4A, then 7 libraries for ESP8266, Arduino + WiFi and Arduino + Ethernet and now I'm here to know how to port some of these to B4R platform.

    Next days, this summer, I release my already working libraries on this forum.

    One library I've developed is a very fast SSD1331 (ESP8266_FastSSD1331) driver for 96x64 0.96 inch Oled with 16 bit colors, this very small color oled is on the market at very low price, I've purchased at 13.60€, but is a fantastic oled display over SPI bus, so very fast, nice contrast, I can see it as well with sun, very black not like TFT and very nice high contrast colors.

    Initially I've used SSD1331 Adafruit library, but the oled worked very slow, and no other orentation than Landscape, to draw a full screen image (read from ESP8266 4MB flash) required about 450-500 milliseconds, so Datasheet on the hand, I've decided to start my library for it.

    On my library I do not used digitalWrite that is very slow, but I write directly on internal ESP8266 GPIO Registers, now my color oled is capable to show small videos from flash @ 30 fps, so about 34 ms to read from flash and draw a full screen image. Video are formed to a subsequnce of uncompressed bitmap images.

    Adafruit library uses ESP CPU to calculate any sort of drawing and eg to draw a filled rettangle draw it pixel by pixel with a row-col array, so it is very slow, now I implemented about all hardware functions, so to draw a filled rectangle or other, now I just send only one command to oled.

    It now supports 4 orientations, LANDSCAPE, PORTRAIT, REVERSE_LANDSCAPE, REVERSE_PORTRAIT, contrast of 256 steps to separate R, G B channels, main contrast of 32 steps, flip pages and more.

    Now my library is really very fast, no flichers or small redrawing, same library implement a Meter class for orizzontally or vertically progress bars and a imageClip class capable to show clip images, I just set images then change frames, eg, to draw a rotary stepper motor, I initialize with 4 or 8 images of fases and then i can set frame I want anytime with just one line eg. ImageClip.setFrame(1) ecc... This way I can simulate any sort of animation, lamps, buttons, swithes, analog meters and more... it just draw images as bitmaps. To create realistic animations I use a free and very good KnobMan app, this tool is released to draw realistic knobs, but can be used to create any sort of controls, switch, leds, linear and round bars, then it export as stiched or single images with an index number, eg Image1, Image2, Image3 etc... then using a SPFIFS tool plugin from Arduino IDE I can upload about 260 fullscreen images. But my library works not only with flash, can use SD too to view long videos.

    I've already used your way to use or pass global variables to/from inline C, but my question is: it is possible to not use globals variables, but just pass more than one argument to functions?
     
    Last edited: Oct 30, 2018
    miker2069 and Peter Simpson like this.
  5. Peter Simpson

    Peter Simpson Expert Licensed User

    I wouldn't really know as I've not tried it any other way, I presume that it's not possible though without @Erel updating the core library (or had he put the library on Github).

    Anyway that was my original point. I know that you are really good at Java and C, so why don't you just wrap the library for yourself, it's just the latest Adafruit SSD1306 library on Github, somebody of your talents will wrap it in no time whatsoever, job done???

    I'm still in the process of learning C++ (and also C# just for the fun of it), but every single time I start to get seriously into learning it, paid work always takes over as paid work pays the bills, and living here in the UK is expensive :(
     
  6. max123

    max123 Active Member Licensed User

    Maybe I can do it in C++, I'm a beginner on B4R, so this not possible for now.

    I already have an SSD1306 oled, purchased in the same moment of SSD1331 color oled, but I just tried it for ten minutes, then I've used this one because it is 16 bit color, here you can see color images and videos, not only text and drawings.

    For now I've no time for this, SSD1331 library is about 1400 lines of code and optimizing it required 4 month of work.... maybe in future I can optimize SSD1306 using C++ and then B4R, but write directly ESP8266 GPIO register, require C++ language... or maybe it is possible on B4R using inline C++ , but maybe @Erel have a response to this point.

    As I says all Adafruit libraries are very slow, even last original you used, any library come as two parts, a generic library and a specific driver for specific screen controller, my library is a unique and hardware optimized library. If I touch Adafruit SSD1306 library I can increase speed from a factor of 1 to 15 times faster, depends on it's hardware, need to read Datasheet.
     
    Last edited: Jul 7, 2017
    Laurent95, spairo and Peter Simpson like this.
  7. highflyer

    highflyer Member Licensed User

    Code:
    B4R Version: 2.20
    Parsing code.    (
    0.00s)
    Compiling code.    (
    0.01s)
    Building project    (
    0.02s)
    Compiling & deploying Ino project (WEMOS LOLIN32 - COM13)    Error
    Loading configuration...
    Initializing packages...
    Preparing boards...
    Verifying...
    In file included from c:\users\paolo\documents\arduino\hardware\espressif\esp32\tools\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:59:0,
                     from c:\users\paolo\documents\arduino\hardware\espressif\esp32\tools\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\
    5.2.0\memory:62,
                     from C:\Users\paolo\Documents\Arduino\hardware\espressif\esp32\libraries\WiFi\src/WiFiClient.h:
    28,
                     from C:\Users\paolo\Documents\Arduino\hardware\espressif\esp32\libraries\WiFi\src/WiFi.h:
    37,
                     from C:\Users\paolo\Desktop\WIFI_C~
    1\Objects\bin\sketch\rESP8266WiFi.h:4,
                     from C:\Users\paolo\Desktop\WIFI_C~
    1\Objects\bin\sketch\B4RDefines.h:25,
                     from C:\Users\paolo\Desktop\WIFI_C~
    1\Objects\bin\sketch\B4RArduino.cpp:1:
    c:\users\paolo\documents\arduino\hardware\espressif\esp32\tools\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\
    5.2.0\new: In function 'void* operator new(std::size_t, void*)':
    c:\users\paolo\documents\arduino\hardware\espressif\esp32\tools\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0\new:129:51: error: declaration of 'void* operator new(std::size_t, void*) noexcept' has a different exception specifier
     inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
                                                       ^
    In file included from C:\Users\paolo\Desktop\WIFI_C~1\Objects\bin\sketch\B4RDefines.h:24:0,
                     from C:\Users\paolo\Desktop\WIFI_C~
    1\Objects\bin\sketch\B4RArduino.cpp:1:
    rCore.h:
    18: error: from previous declaration 'void* operator new(size_t, void*)'
     extern void *operator new( size_t size, void *ptr );
                  ^
    Multiple libraries were found 
    for "WiFi.h"
     Used: C:\Users\paolo\Documents\Arduino\hardware\espressif\esp32\libraries\WiFi
     
    Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
    exit status 1
    When I try to compile I get the above error..
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    As a workaround edit rCore.h and comment line number 18. You can find it in the internal libraries folder inside the rCore folder.

    Code:
    //extern void *operator new( size_t size, void *ptr );
     
    Blueforcer and Johan Hormaza like this.
  9. highflyer

    highflyer Member Licensed User

    The workaround worked perfectly.
    Thank you Erel.
     
    miker2069 and Erel like 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