Share My Creation OLED Animation

I've been banging my head all day to get it to work. The animation is from 3 bitmaps switched periodically. The code uses wire lib. Screen is 128x64, i2c, 1306 driver. The code is for Arduino Uno or Nano.
To convert image to code:
On a PC (MS Paint) save image as monochrom bmp.
Download LCD Assistant app from http://en.radzio.dxp.pl/bitmap_converter/
Set it to Byte Orientation - Vertical, Pixels/byte - 8. Save file as text file and copy the bitmap array to the code page.
B4X:
'I2C OLED connections
'SDA to A4
'SCL to A5

Sub Process_Globals
    Public Serial1 As Serial
    Private master As WireMaster
    Private const addr As Byte = 0x3C
    Private tmr As Timer
    Private in As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    tmr.Initialize("tmr_Tick", 1000)
    in.Initialize(in.A0, in.MODE_INPUT)
    tmr.Enabled = True
    master.Initialize
    RunNative("speed", Null)    'set I2C to 400KHz
    oled_init
    Delay(1000)
    clr_screen
 
    bmp1(2,40)
End Sub

Sub tmr_Tick
    bmp2(1,48)
    Delay(500)
    bmp3(1,48)
End Sub

Sub bmp1(y As Byte, x As Byte)
    Dim page, h, i As Byte
    Dim img1() As Byte = Array As Byte( _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, _
    0x00, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0xF0, 0xF0, 0xF0, 0xE0, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, _
    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, _
    0xC0, 0xDF, 0xDF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xEF, 0xCF, 0xC6, 0x00, 0x00, _
    0x00, 0x00, 0xFF, 0xFF, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, _
    0xFF, 0xFF, 0xF7, 0xF7, 0xF7, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, _
    0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0xFF, 0xFF, 0x1F, 0x00, 0x00, _
    0x00, 0x00, 0x0F, 0x0F, 0x18, 0xDF, 0x5F, 0x7F, 0x7F, 0x2F, 0xAF, 0xFF, 0xFF, 0x0F, 0x0F, 0x0F, _
    0x0F, 0x0F, 0x07, 0x03, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0xE0, 0xF0, 0x70, 0x30, 0x30, 0x30, 0xF1, 0xFF, 0xFF, 0x30, 0x30, 0x30, _
    0x70, 0xF0, 0xE0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00)    '209 w44 h38
 
    For page=0 To 4
        For h=0 To 2
            Dim c(17) As Byte
            c(0)=0x40
            For i=1 To 16
                c(i)=img1((i-1)+16*h+48*page)
            Next

            command(0x21)     'col addr
            command(h*16+x) 'col start
            command(h*16+x+16)  'col End
            command(0x22)    '0x22
            command(y+page) ' Page start
            command(y+1+page) ' Page End
            master.WriteTo(addr,c)

        Next
    Next
End Sub

Sub bmp2(y As Byte, x As Byte)
    Dim page, i As Byte
    Dim img1() As Byte = Array As Byte( _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xE0, 0x80, 0x80, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0x60, 0xF0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0x02, 0x07, 0x03, 0x06, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xC0, 0x80, 0x80, 0x00, _
    0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFE, 0x7E, 0x3E, _
    0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xBE)    '75 w25 h24
 
    For page=0 To 2
        'For h=0 To 2
            Dim c(26) As Byte
            c(0)=0x40
            For i=1 To 25
                c(i)=img1((i-1)+25*page)
            Next

            command(0x21)     'col addr
            command(x) 'col start
            command(x+25)  'col End
            command(0x22)    '0x22
            command(y+page) ' Page start
            command(y+1+page) ' Page End
            master.WriteTo(addr,c)

        'Next
    Next
End Sub

Sub bmp3(y As Byte, x As Byte)
    Dim page, i As Byte
    Dim img1() As Byte = Array As Byte( _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xE0, 0xE0, 0xF0, 0xF0, _
    0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, _
    0xE0, 0xF0, 0xF0, 0xF2, 0xFF, 0x3F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x3F, 0x1F, 0x00, 0x00, _
    0x00, 0x00, 0xC0, 0xF0, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)    '75 w25 h24
 
    For page=0 To 2
        'For h=0 To 2
            Dim c(26) As Byte
            c(0)=0x40
            For i=1 To 25
                c(i)=img1((i-1)+25*page)
            Next

            command(0x21)     'col addr
            command(x) 'col start
            command(x+25)  'col End
            command(0x22)    '0x22
            command(y+page) ' Page start
            command(y+1+page) ' Page End
            master.WriteTo(addr,c)

        'Next
    Next
End Sub

Sub command(cmnd As Byte)
    Dim cb(2) As Byte
    cb(0)=0
    cb(1)=cmnd
    master.WriteTo(addr,cb)
End Sub

Sub oled_init
    command(0xAE)   'DISPLAYOFF
    command(0x8D)   '       CHARGEPUMP *
    command(0x14)    ' 0x14-pump on
    command(0x20)    '      MEMORYMODE
    command(0x0)     ' 0x0=horizontal, 0x01=vertical, 0x02=page
    command(0xA1)    '    SEGREMAP * A0/A1=top/bottom
    command(0xC8)    ' COMSCANDEC * C0/C8=left/right
    command(0xDA)     '     SETCOMPINS *
    command(0x12)   '0x22=4rows, 0x12=8rows
    command(0x81)     '    SETCONTRAST
    command(0x9F)     '0x8F
 
    command(0xAF)       '   DISPLAYON
End Sub

Sub clr_screen
    Dim x, y As Byte
    Dim c() As Byte = Array As Byte(0x40,0,0,0,0,0,0,0,0)
 
    For  y = 0 To 7
        For x = 0 To 15
            command(0x21)     'col addr
            command(8 * x) 'col start
            command(8 * x + 7)  'col End
            command(0x22)    '0x22
            command(y) ' Page start
            command(y) ' Page End
            master.WriteTo(addr,c)

        Next
    Next
End Sub

#if C
void speed (B4R::Object* o) {
   Wire.setClock(400000);  //400khz
}
#End if
 

Attachments

  • oled_anim.zip
    1.8 KB · Views: 339
  • bmp.zip
    873 bytes · Views: 257
  • oled_ani360.jpg
    oled_ani360.jpg
    31.3 KB · Views: 1,578
Last edited:

amorosik

Expert
Licensed User
I've been banging my head all day to get it to work. The animation is from 3 bitmaps switched periodically. The code uses wire lib. Screen is 128x64, i2c, 1306 driver. The code is for Arduino Uno or Nano.
B4X:
'I2C OLED connections
'SDA to A4
'SCL to A5

Sub Process_Globals
    Public Serial1 As Serial
    Private master As WireMaster
    Private const addr As Byte = 0x3C
    Private tmr As Timer
    Private in As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    tmr.Initialize("tmr_Tick", 1000)
    in.Initialize(in.A0, in.MODE_INPUT)
    tmr.Enabled = True
    master.Initialize
    RunNative("speed", Null)    'set I2C to 400KHz
    oled_init
    Delay(1000)
    clr_screen
   
    bmp1(2,40)
End Sub

Sub tmr_Tick
    bmp2(1,48)
    Delay(500)
    bmp3(1,48)
End Sub

Sub bmp1(y As Byte, x As Byte)
    Dim page, h, i As Byte
    Dim img1() As Byte = Array As Byte( _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, _
    0x00, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0xF0, 0xF0, 0xF0, 0xE0, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, _
    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, _
    0xC0, 0xDF, 0xDF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xEF, 0xCF, 0xC6, 0x00, 0x00, _
    0x00, 0x00, 0xFF, 0xFF, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, _
    0xFF, 0xFF, 0xF7, 0xF7, 0xF7, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, _
    0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0xFF, 0xFF, 0x1F, 0x00, 0x00, _
    0x00, 0x00, 0x0F, 0x0F, 0x18, 0xDF, 0x5F, 0x7F, 0x7F, 0x2F, 0xAF, 0xFF, 0xFF, 0x0F, 0x0F, 0x0F, _
    0x0F, 0x0F, 0x07, 0x03, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0xE0, 0xF0, 0x70, 0x30, 0x30, 0x30, 0xF1, 0xFF, 0xFF, 0x30, 0x30, 0x30, _
    0x70, 0xF0, 0xE0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00)    '209 w44 h38
   
    For page=0 To 4
        For h=0 To 2
            Dim c(17) As Byte
            c(0)=0x40
            For i=1 To 16
                c(i)=img1((i-1)+16*h+48*page)
            Next

            command(0x21)     'col addr
            command(h*16+x) 'col start
            command(h*16+x+16)  'col End
            command(0x22)    '0x22
            command(y+page) ' Page start
            command(y+1+page) ' Page End
            master.WriteTo(addr,c)

        Next
    Next
End Sub

Sub bmp2(y As Byte, x As Byte)
    Dim page, i As Byte
    Dim img1() As Byte = Array As Byte( _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xE0, 0x80, 0x80, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0x60, 0xF0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, _
    0x00, 0x00, 0x00, 0x02, 0x07, 0x03, 0x06, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xC0, 0x80, 0x80, 0x00, _
    0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFE, 0x7E, 0x3E, _
    0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xBE)    '75 w25 h24
   
    For page=0 To 2
        'For h=0 To 2
            Dim c(26) As Byte
            c(0)=0x40
            For i=1 To 25
                c(i)=img1((i-1)+25*page)
            Next

            command(0x21)     'col addr
            command(x) 'col start
            command(x+25)  'col End
            command(0x22)    '0x22
            command(y+page) ' Page start
            command(y+1+page) ' Page End
            master.WriteTo(addr,c)

        'Next
    Next
End Sub

Sub bmp3(y As Byte, x As Byte)
    Dim page, i As Byte
    Dim img1() As Byte = Array As Byte( _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xE0, 0xE0, 0xF0, 0xF0, _
    0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, _
    0xE0, 0xF0, 0xF0, 0xF2, 0xFF, 0x3F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x3F, 0x1F, 0x00, 0x00, _
    0x00, 0x00, 0xC0, 0xF0, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, _
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)    '75 w25 h24
   
    For page=0 To 2
        'For h=0 To 2
            Dim c(26) As Byte
            c(0)=0x40
            For i=1 To 25
                c(i)=img1((i-1)+25*page)
            Next

            command(0x21)     'col addr
            command(x) 'col start
            command(x+25)  'col End
            command(0x22)    '0x22
            command(y+page) ' Page start
            command(y+1+page) ' Page End
            master.WriteTo(addr,c)

        'Next
    Next
End Sub

Sub command(cmnd As Byte)
    Dim cb(2) As Byte
    cb(0)=0
    cb(1)=cmnd
    master.WriteTo(addr,cb)
End Sub

Sub oled_init
    command(0xAE)   'DISPLAYOFF
    command(0x8D)   '       CHARGEPUMP *
    command(0x14)    ' 0x14-pump on
    command(0x20)    '      MEMORYMODE
    command(0x0)     ' 0x0=horizontal, 0x01=vertical, 0x02=page
    command(0xA1)    '    SEGREMAP * A0/A1=top/bottom
    command(0xC8)    ' COMSCANDEC * C0/C8=left/right
    command(0xDA)     '     SETCOMPINS *
    command(0x12)   '0x22=4rows, 0x12=8rows
    command(0x81)     '    SETCONTRAST
    command(0x9F)     '0x8F
   
    command(0xAF)       '   DISPLAYON
End Sub

Sub clr_screen
    Dim x, y As Byte
    Dim c() As Byte = Array As Byte(0x40,0,0,0,0,0,0,0,0)
   
    For  y = 0 To 7
        For x = 0 To 15
            command(0x21)     'col addr
            command(8 * x) 'col start
            command(8 * x + 7)  'col End
            command(0x22)    '0x22
            command(y) ' Page start
            command(y) ' Page End
            master.WriteTo(addr,c)

        Next
    Next
End Sub

#if C
void speed (B4R::Object* o) {
   Wire.setClock(400000);  //400khz
}
#End if


How has been created the Array of byte?
Have you a specific tool ?
 

moty22

Active Member
Licensed User
How has been created the Array of byte?
Have you a specific tool ?
I've added this:
On a PC (MS Paint) save image as monochrom bmp.
Download LCD Assistant app from http://en.radzio.dxp.pl/bitmap_converter/
Set it to Byte Orientation - Vertical, Pixels/byte - 8. Save file as text file and copy the bitmap array to the code page.
The array is for C so you need to make it B4R friendly. Each byte represent 8 pixels.
 
Top