Running some code every 30 milliseconds

This may not be possible, but I figure it's worth asking about.

I've got a Zooz ZEN34 remote switch that can send "held" and "released" events to an App I'm working on. What I want to do is use it as a dimmer, where I have a variable in the App that ranges from 0 to 100 and controls a light's brightness.

The problem is that to get a reasonable ramp up/down time, such as three seconds, I need to modify the value every 30 milliseconds as long as a button is held.

runIn() doesn't seem to be useful in this case, since it appears to only take an integer count of seconds as the first parameter. So what options do I have, if any, for doing this? In a C++ program I'd just start a worker std::thread and have the thread function loop till told to close, changing the variable and using a sleep of some sort to delay 30 ms between steps.

Is something like that even possible with Groovy, or is there a better / different way to do this?

runInMillis() is available, but I doubt you can or should try for 30 msecs per step.

Many dimmers support a ramp rate, which is certainly a better way to go for 3 seconds ramp time. For longer periods, you can do it with RM, but these are with intervals of perhaps 1 second most frequent.

3 Likes

I'm not sure if it's a standard command or not, but the startLevelChange and stopLevelChange might be the commands to look for I'd think. For example, here's how I have a hue bulb setup in button controller for one of my zen34's. On held you start raising (or lowering) and then on release you'd stop.

3 Likes

What device are you trying to control the dimming on? As @djw1191 suggested if it has a startLevelChange command you can use that. You just call that with HELD and then stopLevelChange with RELEASED. Not all drivers include it, I have just recently added it to a beta version for my Zooz switch drivers.

1 Like

Would I be correct in thinking that either the resolution isn't sufficient, or that the load on the VM would be excessive, or both?

Regretfully, the bulbs I'm using: TPLink Kasa KL125s don't support those commands, at least they're not exposed by the Kasa Driver, and directly banging the bulb via UDP from a C++ program doesn't include them in the command set. @jtp10181 This is probably the information you're looking for.

That said, the setLevel et al command do allow for a transition time, which may provide an alternative solution. A little research shows that event.date exists as a member of the Event passed to the handler, and about 10 minutes of digging on StackOverflow showed me how to convert that to a count of milliseconds from the epoch start.

With the millisecond count in hand for the press and release events, I can do the rest with relatively simple mathematics. I may get a slight brightness flicker at the release if it's only part way on, but I can probably live with that.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.