October 6, 2016

A Kyu tutorial - a better way to blink

Now we are back to blinking again. It is such a common thing to want to repeat something at a fixed time interval that Kyu provides a convenient way to do this. Once again we are using continuations, but here in conjunction with the thr_repeat_c() call.
#include "kyu.h"
#include "thread.h"

static int led = 0;

static void
blink ( int xx )
{
	gpio_led_set ( led );
	led = (led+1) % 2;
}

static void
blinker ( int xx )
{
	gpio_led_init ();
	thr_repeat_c ( 500, blink, 0 );
}

void
user_init ( int xx )
{

	thr_new ( "blinker", blinker, 0, 10, 0 );
}
We launch a thread, which runs the C function "blinker", which contains only initialization and setup code. It then specifies another function, namely "blink" that should be launched every 500 timer ticks. The blink function runs and then returns, i.e. "falls off the end". As long as it doesn't run for longer than the repeat interval, all is well.

This is a nice clean and simple way to blink an LED.


Have any comments? Questions? Drop me a line!

Kyu / tom@mmto.org