dol: initial dol commit
[jump.git] / dol / examples / example7 / src / producer.c
1 #include <stdio.h>
2 #include <string.h>
3
4 #include "producer.h"
5
6 /**
7  * Returns a random integer in the range between lower_bound and
8  * upper_bound, where the bounding values are included in the interval.
9  */
10 int getRandomNumber(int lower_bound, int upper_bound)
11 {
12   return (rand() % (upper_bound - lower_bound + 1)) + lower_bound;
13 }
14
15
16 void producer_init(DOLProcess *p)
17 {
18   printf("init producer.\n");
19 }
20
21
22 int producer_fire(DOLProcess *p)
23 {
24   srand(0); //initialize random number generator
25
26   //generate input samples and display them
27   printf("producer: samples = { ");
28
29   for (p->local->index = 0; p->local->index < NUMBER_OF_SAMPLES; p->local->index++) {
30     p->local->sample[p->local->index] = (float) getRandomNumber(-9, 9);
31     if (p->local->index < NUMBER_OF_SAMPLES - 1) {
32       printf("%+3.1f, ", p->local->sample[p->local->index]);
33     }
34     else {
35       printf("%+3.1f }\n", p->local->sample[p->local->index]);
36     }
37   }
38
39   //write samples to output port
40   for (p->local->index = 0; p->local->index < NUMBER_OF_SAMPLES; p->local->index++) {
41     printf("%8s: Write sample[%02d]: %+6.4f\n",
42            "producer", p->local->index, p->local->sample[p->local->index]);
43     DOL_write((void*)PORT_OUT, &(p->local->sample[p->local->index]),
44             sizeof(float), p);
45   }
46
47   DOL_detach(p);
48   return -1;
49 }
50