REBUILD_EVENT in modules
Aleksey
Posts: 22
I would like to use one module for several devices but unable to catch events
which compiler does not know during compiling process.
It works fine for constants like [dvTP,200] but not for variables and for some reason REBUILD_EVENT doesn't do what it should.
Here is a simple example.
What is wrong?
Thanks!
which compiler does not know during compiling process.
It works fine for constants like [dvTP,200] but not for variables and for some reason REBUILD_EVENT doesn't do what it should.
Here is a simple example.
PROGRAM_NAME='test' DEFINE_DEVICE dvTP1 = 10001:1:0 dvTP2 = 10002:1:0 DEFINE_MODULE 'Panel' TP1 (dvTP1) DEFINE_MODULE 'Panel' TP2 (dvTP2) DEFINE_PROGRAM DEFINE_START
MODULE_NAME='Panel' (DEV dvTP) DEFINE_VARIABLE DEVCHAN dcBtnDimmer[3]; DEVCHAN dcBtnRelay[2]; DEFINE_START dcBtnDimmer[1]={dvTP,1};dcBtnDimmer[2]={dvTP,2};dcBtnDimmer[3]={dvTP,3}; dcBtnRelay[1]={dvTP,101};dcBtnRelay[2]={dvTP,102}; REBUILD_EVENT(); DEFINE_EVENT BUTTON_EVENT [dcBtnDimmer] { PUSH: { SEND_STRING 0, "'Panel:',itoa(dvTP.Number),', Dimmer:',GET_LAST(dcBtnDimmer)" } } BUTTON_EVENT [dcBtnRelay] { PUSH: { SEND_STRING 0, "'Panel:',itoa(dvTP.Number),', Relay:',GET_LAST(dcBtnRelay)" } } BUTTON_EVENT [dvTP,200] { PUSH: { SEND_STRING 0, "'Panel:',itoa(dvTP.Number),', Button:',itoa(Button.Input.CHANNEL)" } }
What is wrong?
Thanks!
0
Comments
Hi
When I write modules I pass in a TP list(dev array) and have an integer array for my button list - then use GET_LAST to determine which button and which panel triggered the event. This always works - Never used a dev chan array passed in. I just got into a habit of doing it this way and it works.
There seems to be an issue though when you do this with levels - level.input.level seems to get a bit confused, but thats out of the scope of this.
I think if you use dev arrays and integer arrays instead, that'll fix your problem
Duncan
It is not possible to define those DEVCHAN arrays as a constant because it refers to a module's parameter which is a variable.
I agree with you and it is good way to use integer arrays for button list.
But how do you find index of pressed button in integer array if they have nonsequential numbers?
With DEVHAN and GET_LAST it is easy to get index of button in array.
And what could be wrong with levels?
You can pass a variable and constant array to a module as a parameter. You can't pass a constant but you can pass a constant array or even an element of a constant array. You can initialize your variable array where it is defined and then not need to initialize it in define start and then rebuild the event tables. The only time you really need to use rebuild event is when you can't initialize your vars used in event table prior to the event tables being built or if you're changing the event table values during runtime.
But I'll show you a way to fix this with your code just to give you some ideas.
I've split the lines to make them easier to read in the forum codeblock depending on your screen width.
Feel free to reply if you don't understand any part of the code.
For the purposes of touchpanel interaction and design, why would you need this? One typically cares about the button itself, not its index in an array. (Almost all of my button_events look something like above; you can see I don't even have an array from which to derive indices!)
You're free (within limits) to use the channels you want, just parse what you need in code depending on what you are doing.
In your example you have to subtract (number of first button minus 1) from button's channel. Just compare conditions
yours: active (button.input.channel >= 101 && button.input.channel <= 102):
mine: BUTTON_EVENT [dcBtnRelay]
and relay's number
yours: "'Panel:', itoa(button.input.device.number), ', Relay: ', itoa(button.input.channel - 100)"
mine: "'Panel:',itoa(dvTP.Number),', Relay:',GET_LAST(dcBtnRelay)"
But you and other advisers are definitely right, DEVCHAN isn't good for real code.
Thanks for help!
Typically I wouldn't handle it in this way; it was a literal transformation of your code. If you have a ton of stuff, making a stack_var called 'btn', do btn = button.input.channel, can shorten names.
What I might have for one-off buttons like these would be a switch instead. But it's still quite readable. This is just an example of a way to get this done.
Where this excels is in, say, handling IR commands for buttons, source or page selectors, etc. where there is a group of buttons that all perform the same/similar action but the relative button number decides what happens.
And keep in mind, you did miss out that there isn't any of this:
Lots of ways to get it done, but any way without devchans is best. Good luck
Interesting - whenever I did REBUILD_EVENT with an array I just used all of the elements in the event handler, didn't even think to try this. VERY helpful information, good work finding it.
Thanks for sharing, just lost lot of hours trying to fix this issue
Paul
Get_Last works with Integer arrays also: