V5 Callback Function, How to use???

  1. last week

    abennett5139

    Sep 11 Concord, NC 5139

    I noticed in VCS that there's a vex task call back function to bring the code back to an earlier line and i was wondering if anybody could explain to me how to use task::stop(callbackfuntion) ??

    int foo() {
      int count = 0;
    
      while(1) {
        Brain.Screen.printAt( 10, 50, "I am running %d", count++ );
        vex::task::sleep(10);
      }
      
      return(0);
    }
    
    int main() {
      // start another task
      vex::task newtask( foo );
      
      // wait
      vex::task::sleep(3000);
    
      // now stop task
      vex::task::stop( foo );  
    }
  2. jpearman

    Sep 11 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888

    That works just like stopTask in ROBOTC.
    If a task has been started, task::stop will stop it.
    (fyi, there will be minor updates to this API in next VCS release)

  3. abennett5139

    Sep 11 Concord, NC 5139

    @jpearman That works just like stopTask in ROBOTC.
    If a task has been started, task::stop will stop it.
    (fyi, there will be minor updates to this API in next VCS release)

    So I didn't use RobotC, but also I'm wondering is there a way to bring a program say it was on line 30 could I use it to say go back to line 10? This maybe completely wrong but i thought that was what the callback function was called?

  4. henryl

    Sep 11 353X
    Edited last week by henryl

    A callback function is when a function calls another. In this case, a callback is a function you define that you want the task to run. When you construct a task with a callback function, a task is created in parallel that runs along the main task, just like startTask() in robot c, executing the function. Except, in place of defining a task and using it as the argument, you define a callback function and use it as the argument for the task constructor.

  5. jpearman

    Sep 11 Moderator, ROBOTC Tech Support, V5 Beta Moderator Answer Los Angeles 8888
    Edited last week by jpearman
    int foo() {
      int count = 0;
    
      while(1) {
        Brain.Screen.printAt( 10, 50, "I am running %d", count++ );
        vex::task::sleep(10);
      }
      
      return(0);
    }
    
    int main() {
      // start another task
      vex::task newtask( foo );
      
      // wait
      vex::task::sleep(3000);
    
      // now stop task
      vex::task::stop( foo );  
    }
  6. Edited last week by John TYler

    @abennett5139 So I didn't use RobotC, but also I'm wondering is there a way to bring a program say it was on line 30 could I use it to say go back to line 10? This maybe completely wrong but i thought that was what the callback function was called?

    I think you're thinking of a GOTO type command, which are wildly frowned upon as they are very bad programming practice. If you're in C, doing something like

    do {
         line 10 stuff;
         line 11-29 stuff;
    } while (thing you were going to test for on line 30);

    would be the proper way. Loop commands like these were implemented to escape the tyranny of GOTO.

 

or Sign Up to reply!