⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drive.c

📁 circuit calculation program
💻 C
字号:

#include <stdlib.h>
#include "drive.h"

typedef struct event_def {
	/* scheduled time */
	float due_time;
	
	/* call back and arg */
	void (*fn) (void *arg);
	void *arg;
	
	/* link */
	struct event_def *next;
} event_t;

static float current_time;
static float last_time;
	
float drive_delta_time()
{
	return current_time - last_time;   /* 1 ns */
}



static void (*simu) ();
static float stop_time;

static void main_loop (void *v)
{
	if (simu && current_time < stop_time) {
		drive_add_event (main_loop, NULL, current_time + 1e-9);
		simu ();
	}
}	

void drive_stop () {simu = NULL;}
void drive_start (void (*f)(), float stop ) 
{
	simu = f;
	stop_time = stop;
}

static event_t loop = { 0, main_loop,0 };
static event_t *event_queue = &loop;

void drive_run () 
{
	event_t *p;
	
	while (p = event_queue){
		/* dispose the node at head */
		event_queue = p->next;	
		
		current_time = p->due_time;
		if (p->fn) p->fn (p->arg);
		
		if ( p!= &loop) free (p);
	}
}


int drive_add_event ( void (*f)(void *), void *arg, float due_time )
{
	event_t *p, *q, *t;
	
	p = (event_t *) malloc (sizeof (event_t));
	if (!p) {
		abort();
	}
	
	/* initialize */
	p->next = NULL;
	p->fn = f;
	p->arg = arg;
	p->due_time = due_time;
	
	/* find the place to insert */
	t = NULL;
	q = event_queue;
	while (q) {
		if (q->due_time>due_time ) break;
		t = q;
		q = q->next;
	}
	
	/* add to link */
	if (!t) {p->next = event_queue; event_queue = q;}
	else { p->next = q; t->next = p;}
	
	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -