• Just place all your processing into the anoymous funtion - either inline defined - or outside defined.

    For inline/anonymous, it would look - for channel 0 - like this:

    ads.getADC(0, function(val0) {
      // do calculations specific for channel 0 with read value val0
      // print the result 
    });
    

    A 'better' way would be to define upfront functions for the different - channel specific - haendling which include: receiving, processing, and printing. This allows to re-use the handlers for more than just one channel, if needed.

    var handle_the_A_way(val)  {
      // do calculations specific for A  with read value val
      // print the result
    };
    
    var handle_the_B_way(val)  {
      // do calculations specific for B  with read value val
      // print the result
    };
    

    The 'main' flow of your application will include something like that - logically:

    ads.getADC(0, handle_the_A_way);
    ads.getADC(1, handle_the_A_way);
    ads.getADC(2, handle_the_B_way);
    ads.getADC(3, handle_the_A_way);
    ...
    ...
    ...
    

    I said logically, because a next adsGetADC() can only be issued when the previous one has finished... In other words, the requests have to be enqueued, so that only one after the other is executed. Complexity depends on the reading schedule for each channel. What is for sure, you cannot read faster than a channel can deliver.

    Other postings cover the sequencing of (dependent) callbacks (see squence controller - lines 20..30).

    When all channels have to be read with the same frequency at about the same time, a sequence controller does just fine.

    If the channels have to be read whit a different frequency, it becomes a bit more complex: the sequenc controller has to be extended to handle time dependent evnt list, and the addition of an even has to insert the future event taking into acount when in the future it has to happen, how long it takes to complete. Complexity increases with handling efficiently dependent AND independent events, because independent events can overlap (something can be done between the initiation and the callback of the event in order to be efficient: either just some initiation(s) or complete independent event(s), all depending on the times involved.

About

Avatar for allObjects @allObjects started