Author Topic: Trigger/timer executing a routine.  (Read 3935 times)

jane

  • Newbie
  • *
  • Posts: 49
Trigger/timer executing a routine.
« on: March 08, 2024, 09:23:32 AM »
hello. Just for thoughts of "how does it work"

I need to do this:
A  voice command VC1 that toggles a keyboard key,i.e. Num6, on/off

If Num6 on
{
start a trigger, executing a routine) every X millisecond
   { compare color at x,y position with a fixed value.
     if equal, click a keyboardkey
    if Num6 state is toggeled (off) by VC1, release trigger
   }
}

The color compare routine is allready taken care of and uses a inline function.

But howto implement a timer that fires off a routine every X millisecond, like above, is a mystery...to me  :)

Any suggestions?

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Trigger/timer executing a routine.
« Reply #1 on: March 08, 2024, 09:27:10 AM »
...use a while loop and a pause?

jane

  • Newbie
  • *
  • Posts: 49
Re: Trigger/timer executing a routine.
« Reply #2 on: March 08, 2024, 09:57:25 AM »
...use a while loop and a pause?
I would preferre the routine to be executed independant of what else going on in VA's profile with less than possible time variations.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Trigger/timer executing a routine.
« Reply #3 on: March 08, 2024, 10:02:45 AM »
If you somehow need timing to be more precise than a native loop action can facilitate, that'd be something you'll have to implement yourself.

Note that the "Resource balance offset" option can be used to reduce the minimum amount of time actions like loops will take.

jane

  • Newbie
  • *
  • Posts: 49
Re: Trigger/timer executing a routine.
« Reply #4 on: March 08, 2024, 10:07:49 AM »
If you somehow need timing to be more precise than a native loop action can facilitate, that'd be something you'll have to implement yourself.

Note that the "Resource balance offset" option can be used to reduce the minimum amount of time actions like loops will take.
Thank You, I will try the while suggestion first.

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 291
  • Upstanding Lunatic
    • My AVCS Homepage
Re: Trigger/timer executing a routine.
« Reply #5 on: March 08, 2024, 01:04:26 PM »
...use a while loop and a pause?
I would preferre the routine to be executed independant of what else going on in VA's profile with less than possible time variations.

Something along these lines would work independent of any other VA commands, can can operate asynchronously with the rest of your profile/command systems:


When called the first time, the Num6 key will become depressed, and your inline function will run in a loop.  When called a second time, the Num6 key (assuming it is depressed) will be released.  Any asynchronous command of this same command will then naturally end when it's loop check discovers Num6 is no longer depressed.

This structure assumes that your inline function does not loop, and will perform one single "compare color at x,y position with a fixed value => if equal, click a keyboard key" function each time it is called.

You could introduce the timing in MS before your inline function fires, or you could implement the actual pause from within your inline function.  Alternatively, you could use a System.Timer function inside your inline function which can fire off every X milliseconds on a trigger, and end when told to do so based on a trigger which could be watching a VoiceAttack Boolean variable for a change, on an event handler for example - apologies if I am preaching to the choir, no clue on your level of coding experience - here's a basic Timer example in C#:

Code: [Select]
using System;
using System.Threading;
 
 class Program
 {
     static void Main()
     {
         CancellationTokenSource cts = new CancellationTokenSource();
         Timer timer = new Timer(TimerCallback, cts.Token, 0, 1000);
 
         Console.WriteLine("Press any key to stop the timer...");
         Console.ReadKey();
 
         cts.Cancel();
         timer.Dispose();
 
         Console.WriteLine("Timer stopped.");
     }
 
     private static void TimerCallback(object state)
     {
         CancellationToken token = (CancellationToken)state;
 
         if (token.IsCancellationRequested)
         {
             return;
         }
 
         Console.WriteLine("Timer ticked at {0}", DateTime.Now);
     }
 }

jane

  • Newbie
  • *
  • Posts: 49
Re: Trigger/timer executing a routine.
« Reply #6 on: March 13, 2024, 09:55:40 AM »
Thank You for answer. I have changed pseudocode some like below. I noticed your answer at time writing this.
I use an inline function, getpixel function to check status of a level.
Im sure my logic can be done better off. Perhaps You might see any improvements for the pseudo code below?

I would really appreciate regarding this part of it:
during 20sek
                   {
                        set var vent1= false
                   }
I would apprciate beeing able to run other VA commands too. Perhaps I will find answer for that too after reading Your answer (?).

Code: [Select]
if spoken command is battlemode
{
         if var vent1 = true, exit command
else
{
  do this every X second
{
     check stam level
    if low {
click keyboardkey 4.
exit command for 20seconds (no need to check stam level during its refill time)
                         {
                              set var vent1= false
                                        }

          }  //end if low
         {  // end do this.......
} // end else...
}  // end of this (spoken) commdand in profile


...use a while loop and a pause?
I would preferre the routine to be executed independant of what else going on in VA's profile with less than possible time variations.

Something along these lines would work independent of any other VA commands, can can operate asynchronously with the rest of your profile/command systems:


When called the first time, the Num6 key will become depressed, and your inline function will run in a loop.  When called a second time, the Num6 key (assuming it is depressed) will be released.  Any asynchronous command of this same command will then naturally end when it's loop check discovers Num6 is no longer depressed.

This structure assumes that your inline function does not loop, and will perform one single "compare color at x,y position with a fixed value => if equal, click a keyboard key" function each time it is called.

You could introduce the timing in MS before your inline function fires, or you could implement the actual pause from within your inline function.  Alternatively, you could use a System.Timer function inside your inline function which can fire off every X milliseconds on a trigger, and end when told to do so based on a trigger which could be watching a VoiceAttack Boolean variable for a change, on an event handler for example - apologies if I am preaching to the choir, no clue on your level of coding experience - here's a basic Timer example in C#:

Code: [Select]
using System;
using System.Threading;
 
 class Program
 {
     static void Main()
     {
         CancellationTokenSource cts = new CancellationTokenSource();
         Timer timer = new Timer(TimerCallback, cts.Token, 0, 1000);
 
         Console.WriteLine("Press any key to stop the timer...");
         Console.ReadKey();
 
         cts.Cancel();
         timer.Dispose();
 
         Console.WriteLine("Timer stopped.");
     }
 
     private static void TimerCallback(object state)
     {
         CancellationToken token = (CancellationToken)state;
 
         if (token.IsCancellationRequested)
         {
             return;
         }
 
         Console.WriteLine("Timer ticked at {0}", DateTime.Now);
     }
 }

jane

  • Newbie
  • *
  • Posts: 49
Re: Trigger/timer executing a routine.
« Reply #7 on: March 13, 2024, 05:48:48 PM »
I think I will try this:

Code: [Select]
The below is content of spoken command battlemode

 var1 = true
         
while var1 = true do  //infinite loop only to be ended by another command
{
pause X milliseconds
get pixelcolor at a given mousepos
    compare with a fixed  rgb colorvalue
  if low {
click keyboardkey 4.  // fill up stam
          }  // end if
               }

allow other commands to execute while this one (battlemode)is running
this command to be killed by another command in profile

jane

  • Newbie
  • *
  • Posts: 49
Re: Trigger/timer executing a routine.
« Reply #8 on: March 13, 2024, 06:10:15 PM »
...use a while loop and a pause?
I would preferre the routine to be executed independant of what else going on in VA's profile with less than possible time variations.

Something along these lines would work independent of any other VA commands, can can operate asynchronously with the rest of your profile/command systems:


When called the first time, the Num6 key will become depressed, and your inline function will run in a loop.  When called a second time, the Num6 key (assuming it is depressed) will be released.  Any asynchronous command of this same command will then naturally end when it's loop check discovers Num6 is no longer depressed.

This structure assumes that your inline function does not loop, and will perform one single "compare color at x,y position with a fixed value => if equal, click a keyboard key" function each time it is called.

You could introduce the timing in MS before your inline function fires, or you could implement the actual pause from within your inline function.  Alternatively, you could use a System.Timer function inside your inline function which can fire off every X milliseconds on a trigger, and end when told to do so based on a trigger which could be watching a VoiceAttack Boolean variable for a change, on an event handler for example - apologies if I am preaching to the choir, no clue on your level of coding experience - here's a basic Timer example in C#:

Code: [Select]
using System;
using System.Threading;
 
 class Program
 {
     static void Main()
     {
         CancellationTokenSource cts = new CancellationTokenSource();
         Timer timer = new Timer(TimerCallback, cts.Token, 0, 1000);
 
         Console.WriteLine("Press any key to stop the timer...");
         Console.ReadKey();
 
         cts.Cancel();
         timer.Dispose();
 
         Console.WriteLine("Timer stopped.");
     }
 
     private static void TimerCallback(object state)
     {
         CancellationToken token = (CancellationToken)state;
 
         if (token.IsCancellationRequested)
         {
             return;
         }
 
         Console.WriteLine("Timer ticked at {0}", DateTime.Now);
     }
 }

Thank You very much for your example.

jane

  • Newbie
  • *
  • Posts: 49
Re: use of Loop
« Reply #9 on: March 14, 2024, 08:44:18 AM »
Dont need the variable var1 and while. A bit more risky to use a inf.loop instead

The below is content of spoken command battlemode
      Start  Indefinite Loop
      pause X milliseconds
      get pixelcolor at a given mousepos
         compare with a fixed  rgb colorvalue
        if true{
         click keyboardkey 4.  // fill up stam
         do some other instruction
                      }  // end if
        End Loop

allow other commands to execute while this one (battlemode)is running   
this command to be killed by another command (battleend),in profile to end the loop

jane

  • Newbie
  • *
  • Posts: 49
Re: Trigger/timer executing a routine.
« Reply #10 on: March 14, 2024, 09:53:32 AM »
Seemes like this loop doesnt loop. Only 1 ciffer 4 is written when testing

Set integer [~~mouseX] value to 962
Set integer [~~mouseY] value to 1010
Start Indefinite Loop
    Pause 1 second
    Inline C# Function: Function for retrieving pixel color data at (X, Y) screen coordinate, wait until execution finishes
    Begin Condition : ([~~RedValue] Is Greater Than 89 AND [~~RedValue] Is Less Than 112 AND [~~GreenValue] Is Greater Than 87 AND [~~GreenValue] Is Less Than 96)
        Press 4 key and hold for 0,06 seconds and release
    End Condition - Exit when condition met
End Loop

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Trigger/timer executing a routine.
« Reply #11 on: March 14, 2024, 09:55:29 AM »
...you have it configured to exit the command if the condition is met

jane

  • Newbie
  • *
  • Posts: 49
Re: Trigger/timer executing a routine.
« Reply #12 on: March 14, 2024, 10:03:35 AM »
...you have it configured to exit the command if the condition is met
oh my :) thanks.