Code for an autonomous plane I'm building. Includes process scheduling, process communication, and hardware sensor interfacing (via I2C). NOTE: currently in development, source code will be updated frequently.
Diff: control.cpp
- Revision:
- 4:44a5b1e8fd27
- Parent:
- 2:452dd766d212
--- a/control.cpp Fri Nov 01 01:23:04 2013 +0000 +++ b/control.cpp Sat Nov 02 08:47:14 2013 +0000 @@ -19,13 +19,18 @@ void init() { // initialize i2c sensors - init_sensors(); + schedule_proc("INITSENSE", &init_sensors); // set initial processes procs[0].status = READY; procs[0].start = &get_sensor_data; return; } +char* get_output(int pid) +{ + return procs[pid].output; +} + void schedule() { for (int i=0; i<MAXPROC; i++) @@ -33,9 +38,37 @@ process proc = procs[i]; if(proc.status == READY) { - proc.start(); + int ret = proc.start(i); + // if the process returns 0, it means don't run again + if (ret == 0) + { + // set proc.status to ZOMBIE + // ZOMBIE means process + // is no longer active, but + // it's output buffer is still + // needed. + proc.status = ZOMBIE; + } return; } } } - \ No newline at end of file + +int schedule_proc(char *sid, int (*funcptr)(int)) +{ + for (int i=0; i<MAXPROC; i++) + { + process proc = procs[i]; + if(proc.status == EMPTY) + { + proc.status = READY; + proc.start = funcptr; + strncpy(proc.sid, sid, MAX_SID_LEN-1); + // null terminate proc.sid + proc.sid[MAX_SID_LEN-1] = 0; + return i; + } + } + // if no open processes, return -1 + return -1; +} \ No newline at end of file