Beispiel #1
0
int main()
{
    unsigned char c;
    uart0_init();  
	cur_timer_func = idle_func;
	init_timer_queue(&global_timer_queue);
	enqueue_timer(&global_timer_queue,1000,uart_message1);
	enqueue_timer(&global_timer_queue,300,uart_message2);
	enqueue_timer(&global_timer_queue,400,uart_message3);
  
	putc('T');
	putc('e');
	putc('s');
	putc('t');
	putc(':');
	putc('\n');
	putc('\r');
	putstr("str_test");
	putint(987649);
	
    while(1)
    {
        
        c = getc();
     		putc(c);
    }

    return 0;
}
Beispiel #2
0
/// Create a timer and enqueue it
unsigned tm_settimeout(unsigned time, bool repeat, int lua_cb) {
	tm_timer* t = calloc(sizeof(tm_timer), 1);
	t->repeat = repeat ? time : 0;
	t->lua_cb = lua_cb;
	t->next = 0;
	t->id = ++timer_id;

	if (!timers_head) {
		last_time = tm_uptime_micro();
	} else {
		// Adjust because the times on the queue are relative to last_time
		time += (tm_uptime_micro() - last_time);
	}

	if (enqueue_timer(time, t)) {
		configure_timer_interrupt();
	}

	return t->id;
}
Beispiel #3
0
/// Callback enqueued by the timer ISR. It is safe for this to be called more
/// often than necessary
void timer_cb(tm_event* event) {
	(void) event;
	
	unsigned prev_time = last_time;
	last_time = tm_uptime_micro();
	unsigned elapsed = last_time - prev_time;

	while (timers_head) {
		tm_timer* t = timers_head;

		if (t->time > elapsed) {
			t->time -= elapsed;
			break;
		}

		elapsed -= t->time;
		timers_head = t->next;

		lua_rawgeti(tm_lua_state, LUA_REGISTRYINDEX, t->lua_cb);

		if (t->repeat != 0) {
			// It's back in the queue, so clearInterval within the callback can cancel it
			enqueue_timer(t->repeat, t);
		} else {
			// Clean up before calling lua, as it can setjmp. The callback is safely rooted on the lua stack above.
			destroy_timer(t);
			t = NULL;
		}

		lua_getfield(tm_lua_state, LUA_GLOBALSINDEX, "global");
		tm_checked_call(tm_lua_state, 1);
	}

	// If lua setjmps, these will be cleaned up when the timer state is reset
	configure_timer_interrupt();
}