58e161ad2a19c71a03776b33984dde8826ee39bb
[jump.git] / dol / examples / example6 / 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   ; //nothing to be done here
19 }
20
21
22 int producer_fire(DOLProcess *p)
23 {
24   static int index;
25
26   srand(0); //initialize random number generator
27
28   //generate input samples and display them
29   printf("producer: samples = { ");
30
31   for (index = 0; index < 10; index++) {
32     p->local->sample[index] = (float) getRandomNumber(-9, 9);
33     if (index < 9) {
34       printf("%+3.1f, ", p->local->sample[index]);
35     }
36     else {
37       printf("%+3.1f }\n", p->local->sample[index]);
38     }
39   }
40
41   //write samples to output port
42   for (index = 0; index < 10; index++) {
43     printf("%8s: Write sample[%02d]: %+6.4f\n",
44            "producer", index, p->local->sample[index]);
45     DOL_write((void*)PORT_OUT, &(p->local->sample[index]),
46             sizeof(float), p);
47   }
48
49   DOL_detach(p);
50   return -1;
51 }
52