Here's how:

struct t_myexternal {
    t_object x_obj;
    t_clock *x_clock;
    int x_running;
};
void tick(t_myexternal *x)
{
    // do some work
    // then reschedule (if not cancelled)
    if (x->x_running)
        clock_delay(x->x_clock, POLLINTERVAL);
}
void start(t_myexternal *x)
{
    clock_delay(x->x_clock, 0);
    x->x_running = true;
}

void stop(t_myexternal *x)
{
    x->x_running = false;
}

void* myexternal_new(void)
    t_myexternal *x = pd_new(myexternal_class);
    x->x_clock = clock_new(x, (t_method)tick);
    x->x_running = false;
}

Hope this helps!

Christof
On 29.04.2021 18:12, Kyriakos Charalampides via Pd-dev wrote:
I recently started learning how to write some custom pd externals. It has been quite fun until now, but its been a few days I am stuck in a loop. I want to read a buffer coming from an external device every 1 ms. I could do that with a metro but this can cause several issues with this buffer. 

My initial though was to use a for loop but this was obviously a horrible idea since pd will stuck in the for loop and hang. Then I though to use a clock to execute my reading function but all examples with clock and clock_delay seem to be based on for and while loops. What I would really like to achieve is to execute the reading function every 1ms and interrupt this process every time that  a new message is coming from pd. 

Any tips or ideas will be highly appreciated. 

All best,
Kyr


_______________________________________________
Pd-dev mailing list
Pd-dev@lists.iem.at
https://lists.puredata.info/listinfo/pd-dev