Author Topic: Built-in Arithmetic  (Read 2371 times)

0 Members and 1 Guest are viewing this topic.

LilFonky

  • Jr. Member
  • **
  • Posts: 20
  • Karma: 0
    • View Profile
Built-in Arithmetic
« on: September 02, 2014, 01:34:41 PM »
HI all-

First post! Not sure if it's bad form to start out with a feature request but here goes:

I'd like to see some basic arithmetic functions added to the syntax so that simple transforms like scaling, quantization, interpolation, as well as unit conversions can be done without the need for a python script or similar.  If I could do, for instance:

"on":true "Hue":5000 "bri":{1}*2

That would make it easy to scale the brightness of my hue lux to a percentage.  Right now I'm using a payload XML to adjust the level in steps of 10% which is fine but not optimal.  I'm a brand new user so if there's a way to do this already please feel free to set me straight.

Cheers,

Taylor

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: Built-in Arithmetic
« Reply #1 on: September 02, 2014, 01:48:45 PM »
Hi Taylor and welcome to the VC forum.

I've no hue, but you can use a payload range from 1 to 100 instead of a payload.xml? with the payloadRange you can set it to any value by voice from 1 to 100.

http://voxcommando.com/mediawiki/index.php?title=Customizing_Commands

I hope this make sense for you  ;)
***********  get excited and make things  **********

LilFonky

  • Jr. Member
  • **
  • Posts: 20
  • Karma: 0
    • View Profile
Re: Built-in Arithmetic
« Reply #2 on: September 02, 2014, 02:16:43 PM »
HI Kalle-

Yes that's exactly what I was doing, but the Hue is on a 0-200 brightness scale so that if I set 0,100 that only gets me 50% of the range.  If I want to be able to say "Set brightness to 100%" and adjust the brightness down in 1% steps, I have to set 100%=200, and then 99%=198, 97%=194, etc., in a payload XML all the way down to 0, or else write a python script to multiply all my inputs by 2.

It would make much better sense to set a payload range of 1,200 and then do "bri":{1}*2.

This would be even more useful if I were ramping out numbers to, say, a MIDI interface (0-127) if such a plugin existed.

Is there another way to accomplish this in Vox other than using payload XMLs or Python scripting?


nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 1999
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Built-in Arithmetic
« Reply #3 on: September 02, 2014, 02:26:10 PM »
While it doesn't fully address your request for "simple" arithmetic operations more broadly -- just to elaborate a bit on Kalle's suggestion, James has already integrated the ability to specify Hue light or group brightness or saturation as either a 0 to 255 integer value or a percentage value.

Looks like we still need to fully document the syntax (the Hue plugin is very new), but the standard preset Hue configuration comes with some examples (http://voxcommando.com/defaultconfigs/EnglishPhilipsHue.html). Here's one:

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.0.1.0-->
<command id="979" name="Set {1} brightness to {2} percent" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>Hue.Light</cmdType>
    <params>
      <param>{1}</param>
      <param>"on":true, "bri":{2}%</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>Set</phrase>
  <payloadFromXML phraseOnly="False" use2partPhrase="False" phraseConnector="by" Phrase2wildcard="anyone" optional="False">huePayloads\lights.xml</payloadFromXML>
  <phrase>brightness, level</phrase>
  <phrase optional="true">to</phrase>
  <payloadList>0,10,20,25,30,40,50,60,70,75,80,90,100</payloadList>
  <phrase optional="true">percent</phrase>
</command>

As you can see, if you include a % symbol, you can use percentage values ranging from 0 to 100 instead of 0-255.

This command uses a payload list, but as Kalle notes you could use a payload range instead if you want more precise control.
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Built-in Arithmetic
« Reply #4 on: September 02, 2014, 02:58:09 PM »
Making a feature request for your first post is never a good idea.   :biglaugh

If you want to do arithmetic, the python plugin is the perfect solution.  It is simple and easy to do and there is no practical advantage to creating a whole new math parser (by no means a simple task) within the program to accomplish the same thing.  There is no downside to using python for this other than a possible issue with two letter combinations that remind you of words that remind you of scaly animals that you don't like.  Try to think of it more as a badly spelled dessert.

Your example of "bri":{1}*2 would be virtually impossible to parse intelligently in terms of interpreting all the possible variations of action parameters and variables we are already using.

Kalle was suggesting that you could map integer phrases ranging from 1 to 100, to values from 1 to 255 in your payload xml.  The same way you are doing it now, but with more entries.  Or maybe that's not what he meant... but you could do that.

I see no point in such fine grained control of the hue brightness since there is not a huge difference between 10% and 20% brightness, but that's a personal thing.

In this particular instance using the % feature in the hue parameter makes the most sense, but if you needed to do the math for some reason it is only a single extra action

PY.ExecString       result={1}*2
and later "bri":{LastResult}
« Last Edit: September 02, 2014, 03:01:00 PM by jitterjames »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Built-in Arithmetic
« Reply #5 on: September 02, 2014, 02:59:09 PM »
FYI the hue scale is 0-255, not 0-200

LilFonky

  • Jr. Member
  • **
  • Posts: 20
  • Karma: 0
    • View Profile
Re: Built-in Arithmetic
« Reply #6 on: September 02, 2014, 04:54:35 PM »
Thanks guys, that pretty much has me squared away.  I've never bothered to learn how to program anything meaningful in Python (or any other language) because I never had a project I felt needed completing. I guess this gives me sufficient reason to learn Python now.

RE: 0-255 and the Hue: I don't know why that never occurred to me insofar as it's an IP-based doo-dad.

RE: percentages and documentation: Thanks for the link nime5ter. I began from the basic profile without the Hue presets because I wanted to force myself to learn how to set up everything from scratch without leaning too much on someone else's work. I'll go back and have a look at the examples and see what else I can learn.

RE: jitterjames's comment: "I see no point in such fine grained control of the hue brightness since there is not a huge difference between 10% and 20% brightness, but that's a personal thing."

I actually agree with you here but a) I'd like to be able to do it on principle, and b) during testing I found myself actually saying numbers like "12" and "77" sort of by reflex, and the system just sat there like a lump because I had everything setup for steps of 10%.  It's not so much that 70% is meaningfully different than 77% in terms of the brightness of the lamp, but rather that when I speak I want the system to do something, and I've been pleasantly surprised at how good the system is at hearing numbers when I speak them. And c) In my day job I'm an AV guy who deals with theatrical lighting systems on a regular basis so I guess I'm just predisposed to care about these things perhaps more than other folks. 

Thanks again guys I can't wait to get back home and start fiddling once more.

Cheers,

Taylor

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 1999
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Built-in Arithmetic
« Reply #7 on: September 02, 2014, 05:47:53 PM »
Thanks guys, that pretty much has me squared away.  I've never bothered to learn how to program anything meaningful in Python (or any other language) because I never had a project I felt needed completing. I guess this gives me sufficient reason to learn Python now.

As folks here know, I would never discourage anyone from learning some basic Python. :)  That said, in this instance I think it's worth clarifying that for basic arithmetic operations there's really no scripting involved (except maybe a bit of awareness of data types when doing things like dividing numbers).

Here is a command group with a few examples that you could copy into your command tree if you want to inspect more closely.

As you can see, there's really no scripting or anything more complicated than what you were originally requesting. The Python plugin does need to be enabled.

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.0.1.0-->
<commandGroup open="True" name="simple math operations - no complicated scripting" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="277" name="What is {1} times {2}" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>result={1}*{2}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>TTS.Speak</cmdType>
      <params>
        <param>{1} times {2} is equal to {LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{1} x {2} = {LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>What is</phrase>
    <payloadRange>1,10</payloadRange>
    <phrase>times</phrase>
    <payloadRange>1,10</payloadRange>
  </command>
  <command id="278" name="convert to a percentage" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>result =round({1}.0 / {2}.0*100)</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{1} is {LastResult}% of {2}.</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>What percent is</phrase>
    <payloadRange>1,20</payloadRange>
    <phrase>out of</phrase>
    <payloadRange>21,88</payloadRange>
  </command>
  <command id="279" name="convert Fahrenheit to Celsius" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>result=round(({1}-32) * 5/9)</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{1} °F is {LastResult} °C.</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>What is</phrase>
    <payloadRange>20,100</payloadRange>
    <phrase>Fahrenheit in Celsius</phrase>
  </command>
</commandGroup>

« Last Edit: September 02, 2014, 05:51:45 PM by nime5ter »
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)