/* University of California, Riverside - CS122B (Tony Givargis) */ /*--------------------------------------------------------------------------*/ #include #include #include #include #include #include #include /*--------------------------------------------------------------------------*/ #define DELAY_IN_SECONDS(x) taskDelay(x * 50); /*--------------------------------------------------------------------------*/ /* These functions mimick the actual sensor readers */ double read_sensor_a(void) { return 32 + rand() % 76; } double read_sensor_b(void) { return 16 + rand() % 50; } double read_sensor_c(void) { return 44 + rand() % 93; } /*--------------------------------------------------------------------------*/ typedef struct { /* these are sampled once per second */ double sample_a[10]; double sample_b[10]; double sample_c[10]; /* these are calculated from above once every 5 seconds */ double average_a[2]; double average_b[2]; double average_c[2]; /* these are calculated from the above once every 10 seconds */ double average_a2; double average_b2; double average_c2; /* this is calculated from the above once every 10 seconds */ double average; } temperature_t; /*--------------------------------------------------------------------------*/ int not_done; /* used to signal the end of the program */ temperature_t temperature; /* holds the sampled data */ SEM_ID mutex; /* used for mutual exclusion */ /*--------------------------------------------------------------------------*/ void task_a(void) { int buffer_index = 0; while( not_done ) { /* activated once every 1 second */ /* get sample and store in buffer (don't forget mutual exclusion) */ /* update buffer index (local variable, so no need for mutex) */ DELAY_IN_SECONDS(1); } } /*--------------------------------------------------------------------------*/ /* tasks b, c, d, e, f and g go here */ /*--------------------------------------------------------------------------*/ void prog_start(void) { /* called to start the system */ /* don't change these two lines */ srand(100); /* initialize shared variables */ not_done = 1; memset(&temperature, 0, sizeof(temperature)); mutex = semBCreate(SEM_Q_FIFO, SEM_FULL); /* start tasks */ taskSpawn("simulator", 200, 0, 2000, (FUNCPTR)task_a,0,0,0,0,0,0,0,0,0,0); /* and so on .... */ } /*--------------------------------------------------------------------------*/ void prog_stop(void) { /* called to stop the system */ not_done = 0; semDelete(mutex); }