B4R Question code module instance

Discussion in 'B4R Questions' started by Mostez, Sep 16, 2019.

  1. Mostez

    Mostez Active Member

    Hello,
    I have B4R code module called Glcd it's an lcd driver, I want to driver 2 lcds and instantiate this module with different properties(pin assignment, col, rows..etc). for example

    private LCD1 as Glcd
    private LCD2 as Glcd

    LCD1.begin(1,2,3)
    LCD2.begin(4,5,6)

    or should I create new module(a copy of original one) and name them LCD1, LCD2?

    Thanks
     
  2. emexes

    emexes Well-Known Member Licensed User

    You can reasonably successfully emulate objects / classes by morphing your example slightly:

    Code:
    Type Glcd( _
        InitializedFlag 
    As Boolean, _
        ClockPin 
    As Byte, _
        DataPin 
    As Byte, SelectPin As Byte, _
        NumCols 
    As Byte, _
        NumRows 
    As Byte, _
        Row 
    As Byte, _
        Col 
    As Byte, _
        other per-instance variables _
    )

    Private LCD1 As Glcd
    Private LCD2 As Glcd

    Glcd_Initialize(LCD1, 
    123)
    Glcd_Initialize(LCD2, 
    456)

    'poor man's object.method of LCD1.ClearScreen
    Glcd_ClearScreen(LCD1)

    'poor man's object.method of LCD1.Print("String")
    Glcd_Print(LCD1, "String")

    'poor man's object.method of LCD1.PrintAt(1, 10, "World")
    Glcd_PrintAt(LCD1, 110"World")

    'and then implement class methods as subs, passing specific instance as first parameter, eg:

    Sub Glcd_Print(LCD As Glcd, S As String)

        
    For I = 0 To S.Length - 1
            Glcd_PrintChar(LCD, S.CharAt(I))
        
    Next I

    End Sub

    Sub Glcd_PrintChar(LCD As Glcd, C As Char)

        Glcd_OutputByte(LCD, 
    Asc(C))

        LCD.Col = LCD.Col + 
    1
        
    If LCD.Col > Lcd.NumCols then
            LCD.Col = 
    1
            
    If LCD.Row < LCD.NumRows Then
                LCD.Row = LCD.Row + 
    1
            
    End If
        
    End If

    End Sub

    Sub Glcd_OutputByte(LCD as Glcd, B as Byte)

        
    'you'd probably need to insert some delays in here,
        'eg on a 160 MHz or whatever CPU, the signalling would likely be too fast for the LCD

        SetPin(LCD.ClockPin, 
    False)
        SetPin(LCD.SelectPin, 
    True)

        
    Dim BitMask As Byte = 1

        
    For BitNumber = 0 to 7
            SetPin(LCD.DataPin, 
    Bit.And(B, BitMask) <> 0)
            SetPin(LCD.ClockPin, 
    True)
            SetPin(LCD.ClockPin, 
    False)
            BitMask = BitMask + BitMask
        
    Next

        SetPin(LCD.SelectPin, 
    False)

    End Sub

    Sub Glcd_Clear(LCD As Glcd)

        Glcd_OutputByte(LCD, 
    12)    'ASCII Form Feed = Clear Screen
        LCD.Row = 1
        LCD.Col = 
    1

    End Sub
    You could generalize the whole thing so that your Print, PrintAt, Locate, ClearScreen etc routines can work with different display modules, but since one project tends to only have one type of display module, doing so usually adds extra complexity for no added benefit. You'd be better off having a code module for each type of display module, but with standardized Sub names and formats, and just include the code module applicable to the display module of the project.
     
    Last edited: Sep 17, 2019
    Mostez and Erel like this.
  3. emexes

    emexes Well-Known Member Licensed User

    Just to make sure you don't head off down this path to misery multiplication: if the LCD's are of the same type, then the answer is: NO NO NO

    :)
     
    Last edited: Sep 17, 2019
    Mostez likes this.
  4. Mostez

    Mostez Active Member

    oh, I'm using 'type' to create fonts, never thought to use same method with LCD. thanks so much @emexes

    Code:
    Type Fonts(FontID As Byte,FontCols As Byte,BytesPerCol As Byte,BytesOrder As Byte,ByteReflection As Byte,ASCIIstartChar As Byte,ROMstartPage As UInt,FontFileName As String)

        
    Public Font5x8 As Fonts
        
    Public Font8x8 As Fonts
    Code:
    Private Sub SetFontProperties()
        Font5x8.FontID = 
    1
        Font5x8.FontCols = 
    5
        Font5x8.BytesPerCol = 
    1
        Font5x8.BytesOrder = BYTES_ORDER_LSB
        Font5x8.ByteReflection = REFLECT_BYTE_NONE
        Font5x8.ASCIIstartChar = 
    32
        Font5x8.ROMstartPage = 
    0
     
        Font8x8.FontID = 
    2
        Font8x8.FontCols = 
    8
        Font8x8.BytesPerCol = 
    1
        Font8x8.BytesOrder = BYTES_ORDER_MSB
        Font8x8.ByteReflection = REFLECT_BYTE
        Font8x8.ASCIIstartChar = 
    0 'this char set starts from char 0 and it has some extra icons and arabic chars
        Font8x8.ROMstartPage = 455
    i use it like this:
    Code:
    Klcd.PutString(0,0,NumberFormat(hours, 20),Klcd.Font8x8,False)
     
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