resolve merge conflict
[wakeup.git] / src / pwmread.cpp
1 #include <sys/stat.h>
2 #include <sys/types.h>
3 #include <unistd.h>
4
5 #include <sys/ipc.h>
6 #include <sys/shm.h>
7
8 #include "pwm.h"
9
10 /*
11  *   Read the PWM intervals from shared memory and calculate the light levels.
12  */
13
14 int pwmread()
15 {
16    int shmid;
17    key_t key;
18    char *shm;
19    struct pwm *signals;
20
21    int i;
22
23     /*
24      * We need to get the segment named
25      * SHM_KEY, created by the server.
26      */
27     key = SHM_KEY;
28
29     /*
30      * Locate the segment.
31      */
32     if ((shmid = shmget(key, sizeof(struct pwm) * 5, 0666)) < 0) {
33         perror("shmget");
34         exit(1);
35     }
36
37     /*
38      * Now we attach the segment to our data space.
39      */
40     if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
41         perror("shmat");
42         exit(1);
43     }
44
45     /*
46      * Now read what the server put in the memory.
47      */
48
49    signals = (struct pwm *)shm;
50
51    for (i=0; i < 5; i++)
52    {
53       printf("Interval = %d, output = %d\n", signals[i].interval, signals[i].output);
54    }
55
56    int repeat = 20000;
57
58    while (repeat--)
59    {
60       if (signals->interval != 0)
61       {
62          usleep(signals->interval);
63       }
64       if (signals->output == -1)
65       {
66          signals = (struct pwm *)shm;
67       }
68       else
69       {
70          signals++;
71       }
72    }
73
74    return 0;
75 }