B4R Code Snippet Example of GFX.drawLine (Adafruit GFX library)

SubName: Example of drawing to a display with the Adafruit GFX library
Description: Use this code to draw and rotate lines onto an OLED display. You will need the rAdafruitSSD1306 and rAdafruitGFX libraries loaded into your libraries tab. The code is simple enough to follow once you get your head around it, this is good for learning GFX.drawLine.

I had to temporarily stopped learning C++ a weeks or so ago whilst working on a clients project, and also some small but quick B4R test projects. Today I got to thinking if I had reached the stage where I could maybe convert a small Arduino IDE C++ sketch into a B4R project.

'In the code below you will see that I have wrote and commented out some 'For' loops, the original code only had 'For' loops. You should try swapping the 'Do While' loops for the commented out 'For' loops just to see what happens on your display.
B4X:
'WIRE LEGEND for 12864 7 pin OLED display
'VCC (VDD)= 3.3V
'GND = GND
'D0 (SCK) = D13
'D1 (SDA) = D11
'RST (RES) = D8
'DC = D9
'CS (SS) = D10

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 nFrames As Int = 36
    Private SSD As AdafruitSSD1306
End Sub

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

    SSD.InitializeHSPI(9, 8, 10) 'Use InitializeHSPI for HSPI 7 pin display
    SSD.GFX.ConfigureText(1, SSD.WHITE, False)   
    SSD.ClearDisplay

    AddLooper("Loopy")
End Sub

'RIGHTLY OR WRONGLY, THIS IS MY WAY OF MIMICKING THE MAIN LOOP() IN THE ARDUINO IDE.
'FOR THIS PARTICULAR PROJECT, I THOUGHT I WOULD NOT USE A TIMER THIS TIME :-) 
Sub Loopy
'    'SAME ROUTINE AS BELOW
'    Dim Frame As Int = 0
'    For Frame = Frame To nFrames - 1
'        MainBuild(Frame)
'        Frame = Frame + 1
'    Next
'    Frame = nFrames - 1
'    For Frame = Frame To 0
'        MainBuild(Frame)
'        Frame = Frame - 1
'    Next   

    'SAME ROUTINE AS ABOVE
    Dim Frame As Int = 0
    Do While Frame < nFrames
        MainBuild(Frame)
        Frame = Frame + 1
    Loop
    Frame = nFrames - 1
    Do While Frame >= 0
        MainBuild(Frame)
        Frame = Frame - 1
    Loop
End Sub

Sub MainBuild(Frame As Int)
    SSD.clearDisplay()
   
    Dim i As Int = 0   
    Dim n As Int = 7
    Dim r As  Int = Frame * 64 / nFrames
    Dim rot As Float = Frame * 2 * cPI / nFrames

    Do While i < n - 1
        Dim a As Float = rot + i * 2 * cPI / n
        Dim x1 As Int = 64 + Cos(a) * r
        Dim y1 As Int = 32 + Sin(a) * r
        Dim j As Int = i + 1

'        'SAME ROUTINE AS BELOW
'        For j = j To n - 1
'            a = rot + j * 2 * Pi / n
'            Dim x2 As Int = 64 + Cos(a) * r
'            Dim y2 As Int = 32 + Sin(a) * r
'            SSD.GFX.drawLine(x1, y1, x2, y2, SSD.WHITE)
'            j = j + 1
'        Next

        'SAME ROUTINE AS ABOVE
        Do While j < n
            a = rot + j * 2 * cPI / n
            Dim x2 As Int = 64 + Cos(a) * r
            Dim y2 As Int = 32 + Sin(a) * r
            SSD.GFX.drawLine(x1, y1, x2, y2, SSD.WHITE)
            j = j + 1
        Loop
       
        i = i + 1
    Loop
   
    SSD.display()
End Sub

Tags: Draw, Adafruit, GFX, OLED, Display, Uno

ezgif-2-515a6bc99b.gif


Enjoy...
 
Last edited:
Top