Wish [SOLVED] Module As Class for B4XLib

rwblinn

Well-Known Member
Licensed User
Not sure if this has been asked already = could not find in the threads.

When creating a B4XLib for B4R, the lib contains one or more modules.
These modules can be used directly without declaring an object but the module can only be used for a single instance and not for multiple objects.
Example using a module named LEDControl, to control a single LED, in Main, i.e. LEDControl.Initialize(Pin_Nr As Byte) , LED.SetState(State As Boolean).

Wish
Declare a module as a class, to be able to declare multiple objects. The class to be used in a B4XLib like a CPP library.
Basically although came across for a B4XLib, this is not bound to a B4XLib but in general.

Example using the module with class LEDControl
Declare in Main:
B4X:
Private LED_Red As LEDControl
Private LED_Green As LEDControl

LED_Red.Initialize(13)
LED_Green.Initialize(12)
The Class LEDControl to be defined like in B4J, i.e.
Sub Class_Globals
Public Sub Initialize
 

Erel

Administrator
Staff member
Licensed User
The request is clear, however it is actually a request to add support for classes in B4R. The source code in b4xlibs behaves exactly like the source code added directly to the project.

The reason that classes weren't implemented is related to the limited memory available and specifically to the static memory model.
 

rwblinn

Well-Known Member
Licensed User
Thanks for steer ... however still thinking/brainstorming/trying about how to control multiple LEDs using a single module or class.

The reason is seeking for a solution for my DCCEx project (see Post #5) to control N LEDs individually, like bufferstops, crossroads, lights etc.
With control, meaning dedicated LED functions like LEDBlinkForTime (let the LED blink for a certain preriod of time), LEDBlinkForCount (let the LED blink N times), LED switch On/Off etc.

Tried creating a
* B4R module which returns a pin to control but returning pin objects is not supported by B4R - a module controlling a single LED is no issue, but then need to create many modules,
* CPP library (testing on an UNO) using the B4R::Timer - BUT the example hangs (see attached) or loops = think messed up with pointers.

next is to try a CPP with timer class based on millis or use a module with fixed number of LED (i.e. 10) as array ... BUT any other ideas appreciated.
 

Attachments

rwblinn

Well-Known Member
Licensed User
Progressing based on steer.
Question: How to add a custom type to a timer tick (=let LED blink) to control each LED?

Module LEDControl
B4X:
Sub Process_Globals
    Type TLED (PinOut  As Pin, PinNr As Byte, BlinkTimer As Timer, BlinkState As Boolean, BlinkInterval As ULong)
    Private LED_Pin As Pin
    Private Blink_Timer As Timer
    Private Blink_Timer_Interval As ULong = 2000
End Sub

'Init the object
'Example usage in Main: <code>
'Private ledRED As TLED
'Private ledGREEN As TLED
'ledRED.PinNr = 0x0D
'ledGREEN.PinNr = 0x0B
'LEDControl.Initialize(ledRED)
'LEDControl.Set_Blink_State(ledRED, True)
'LEDControl.Initialize(ledGREEN)
'LEDControl.Set_Blink_State(ledGREEN, False)
'</code>
Public Sub Initialize(LED As TLED)
    Log("LEDControl Initialize:", LED.PinNr)
    LED.PinOut = LED_Pin
    LED.BlinkTimer = Blink_Timer
    LED.PinOut.Initialize(LED.PinNr, LED.PinOut.MODE_OUTPUT)
    LED.PinOut.DigitalWrite(False)
    '
    LED.BlinkTimer.Initialize("Blink_Timer_Tick", Blink_Timer_Interval)
    LED.BlinkTimer.Enabled = False
End Sub

Private Sub Blink_Timer_Tick(LED As TLED)    '<==How to set a custom type as parameter to a timer tick
    LED.PinOut.DigitalWrite(Not(LED.PinOut.DigitalRead))
End Sub

Public Sub Set_Blink_State(LED As TLED, State As Boolean)
    LED.BlinkState = State
    LED.BlinkTimer.Enabled = State
End Sub
 

Erel

Administrator
Staff member
Licensed User
Events will make things more complicated.

This example will help you:
B4X:
'Counter module:
Private Sub Process_Globals
    Type TCounter (Count As Int, Interval As Int, NextTick As ULong, Name As String)
    Private Counters(20) As TCounter 'maximum 20 counters
    Private CountersIndex As Int
    Private Timer1 As Timer
End Sub

Public Sub Initialize
    AddLooper("Looper1")
    Timer1.Initialize("Timer1_Tick", 1000)
    Timer1.Enabled = True
End Sub

Public Sub AddCounter(Cnt As TCounter)
    Counters(CountersIndex) = Cnt
    CountersIndex = CountersIndex + 1
    Cnt.NextTick = Millis + Cnt.Interval
End Sub

Private Sub Looper1
    For i = 0 To CountersIndex - 1
        Dim cnt As TCounter = Counters(i)
        If cnt.NextTick <= Millis Then
            cnt.NextTick = cnt.NextTick + cnt.Interval
            cnt.Count = cnt.Count + 1
        End If
    Next
End Sub

Private Sub Timer1_Tick
    For i = 0 To CountersIndex - 1
        Dim cnt As TCounter = Counters(i)
        Log(cnt.Name, ": ", cnt.Count)
    Next
End Sub
B4X:
'main
Sub Process_Globals
    Public Serial1 As Serial
    Private Counter1, Counter2, Counter3, Counter4 As TCounter
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Counter.Initialize
    Counter1.Name = "Counter1"
    Counter1.Interval = 100
    Counter2.Name = "Counter2"
    Counter2.Interval = 10
    Counter3.Name = "Counter3"
    Counter3.Interval = 35
    Counter4.Name = "Counter4"
    Counter4.Interval = 1000
    Counter.AddCounter(Counter1)
    Counter.AddCounter(Counter2)
    Counter.AddCounter(Counter3)
    Counter.AddCounter(Counter4)
End Sub
 

Attachments

Top