B4R Question .AddListener doesn't seem to be working

daveinhull

Active Member
Licensed User
Longtime User
Hi,

I've been trying to get .AddListener to work and I can't do it, probably something I'm doing or rather not doing.
I have a very simple bit of code.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private encSWPin As Pin
End Sub

Sub Changed ()
    Log ("Listener Switch Changed")       
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    encSWPin.Initialize (12,encSWPin.MODE_INPUT_PULLUP)
    encSWPin.AddListener("Changed")
end Sub

Any thoughts of what I might be doing wrong?

Many thanks
 

daveinhull

Active Member
Licensed User
Longtime User
Thank Erel,

Ok, I changed the code to what you suggested, but it still doesn't seem to work, and what I mean is that nothing gets logged in the Pin_StateChanged sub.
Still not sure what I'm doing wrong.

B4X:
B4X:

Sub Process_Globals
    Public Serial1 As Serial
    Private encSWPin As Pin
End Sub

Sub Pin_StateChanged (State as Boolean)
    Log ("Listener Switch Changed")       
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    encSWPin.Initialize (12,encSWPin.MODE_INPUT_PULLUP)
    encSWPin.AddListener("Pin_StateChanged")
end Sub
 
Upvote 0

daveinhull

Active Member
Licensed User
Longtime User
Hi Erel,

It's just connected to a push button which when I check the state, ie read the value, in a loop does work! The pin value changes. I could do it in a loop or use a timer, but wanted to get AddListener working.

I've also added a 4x4 keypad and used the keypad library to set it up, but that doesnt work either, I mean the key pressed routine never triggers.

[edit: pad.Initialize ("123A456B789C*0#D",Array As Byte(2,3,4,5), Array As Byte(6,7,8,9),"Pad_KeyPressed")]

Thanks
Dave
 
Last edited:
Upvote 0

daveinhull

Active Member
Licensed User
Longtime User
Hi Erel and many thanks

Of course, how stupid I feel. I'd been thinking it was some kind of interrupt, but you've talked about interrupts being problematic so kept telling myself its not an interrupt so if its not an interrupt how does it work if I'm in a tight loop scanning things, der! Change my code to get rid of loops and use Listeners and Timers.

Many thanks there were so many other things not quite working as you said!

Thanks again
Dave
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Never use Delay to control the program flow!

 
Upvote 0
Top