Rev Author Line No. Line
1085 kaklik 1 #include <termio.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <sys/time.h>
5 #include <time.h>
6  
7 // how many measurements to average?
8 #define FIFOLEN 120
9 #define EVERY 20 // issue a printout every N seconds
10  
11 int
12 main (int argc, char **argv)
13 {
14 struct termios tel_termios;
15 char rbuf[10], buf[256], schar, credo = 1;
16  
17 int i, number = 0;
18 int status;
19 int port;
20 FILE *portf, *crfile;
21  
22 int fifopos = 0, fifolenx = 0;
23 float fifo[FIFOLEN];
24 float fifo2[FIFOLEN];
25 float fifo3[FIFOLEN];
26  
27 for (i = 0; i < FIFOLEN; i++)
28 {
29 fifo[i] = 0.0;
30 fifo2[i] = 0.0;
31 fifo3[i] = 0.0;
32 }
33  
34 port = open (argv[1], O_RDWR);
35  
36 if (port < 0)
37 {
38 fprintf (stderr, "mrakomer: cant access the device\n");
39 return -1;
40 }
41  
42 if (tcgetattr (port, &tel_termios) < 0)
43 {
44 fprintf (stderr, "mrakomer: device not a port?\n");
45 return -1;
46 }
47  
48 if (cfsetospeed (&tel_termios, B2400) < 0 ||
49 cfsetispeed (&tel_termios, B2400) < 0)
50 {
51 fprintf (stderr, "mrakomer: speed setting problem?\n");
52 return -1;
53 }
54  
55 tel_termios.c_iflag = IGNBRK & ~(IXON | IXOFF | IXANY);
56 tel_termios.c_oflag = 0;
57 tel_termios.c_cflag =
58 ((tel_termios.c_cflag & ~(CSIZE)) | CS8) & ~(PARENB | PARODD);
59 tel_termios.c_lflag = 0;
60 tel_termios.c_cc[VMIN] = 0;
61 tel_termios.c_cc[VTIME] = 5;
62  
63 if (tcsetattr (port, TCSANOW, &tel_termios) < 0)
64 {
65 fprintf (stderr, "mrakomer: port init failed at tcsetattr\n");
66 return -1;
67 }
68  
69 // get current state of control signals
70 ioctl (port, TIOCMGET, &status);
71  
72 // Drop DTR
73 status &= ~TIOCM_DTR;
74 ioctl (port, TIOCMSET, &status);
75  
76 // fprintf(stderr, "mrakomer: Port init complete\n");
77  
78 portf = fdopen (port, "w+");
79  
80 schar = 'x';
81 for (;;)
82 {
83 int tstat, tno;
84 char *x;
85 float temp0, temp1, avg, avg2, avg3;
86 struct timeval tv;
87  
88 // send the measurement request and mark up the time when it was sent.
89 // x=no heating, h=heating
90 write (port, &schar, 1);
91 gettimeofday (&tv, NULL);
92  
93 // Wait a while for the reply and process it.
94 usleep (480000);
95 x = fgets (buf, 256, portf);
96 sscanf (buf, "%d %f %f %d", &tno, &temp0, &temp1, &tstat);
97  
98 // thermostat ;)
99 if (temp0 < 5.0)
100 schar = 'h';
101 if (temp0 > 5.1)
102 schar = 'x';
103  
104 // decide if we agree to open. Only valid in the night, but that's not a problem anyway.
105 fifo[fifopos] = temp0 - temp1;
106 fifo2[fifopos] = temp0;
107 fifo3[fifopos] = temp1;
108  
109 if (++fifopos > FIFOLEN)
110 fifopos = 0;
111 if (++fifolenx > FIFOLEN)
112 fifolenx = FIFOLEN;
113  
114 for (avg2 = avg3 = avg = 0, i = 0; i < fifolenx; i++)
115 {
116 avg += fifo[i];
117 avg2 += fifo2[i];
118 avg3 += fifo3[i];
119 }
120 avg /= fifolenx;
121 avg2 /= fifolenx;
122 avg3 /= fifolenx;
123 // decide whether to open or not ;)
124 if (avg < 6.25)
125 credo = 1; // blocked
126 if (avg > 6.75)
127 credo = 0; // unblocked
128  
129  
130 crfile = fopen ("/home/standa/dcm/mrakomer.credo", "w+");
131 if (crfile)
132 fprintf (crfile, "%d\n", (int) credo);
133 if (crfile)
134 fclose (crfile);
135  
136 // send to the stdout the result.
137 if (!(++number % EVERY))
138 {
139 fprintf (stdout, "%.2f %d %.2f %.2f %d %c %.2f %d\n",
140 (double) tv.tv_sec + (double) tv.tv_usec / 1000000, tno,
141 avg2, avg3, tstat, schar, avg, (int) credo);
142  
143 fflush (stdout);
144 }
145 // sleep (DELAY-1);
146 }
147 }