Wed, 01 Apr 2015 15:20:05 +0200
Completed DS2413 device code and driver.
158 | 1 | /***************************************************************************** |
325
35b6f6605ae3
More device locks in the network server
Michiel Broek <mbroek@mbse.eu>
parents:
324
diff
changeset
|
2 | * Copyright (C) 2014..2015 |
158 | 3 | * |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the mbsePi-apps | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * mbsePi-apps is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with thermferm; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
22 | ||
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
23 | #include "thermferm.h" |
158 | 24 | #include "devices.h" |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
25 | #include "rc-switch.h" |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
26 | #include "panel.h" |
158 | 27 | #include "xutil.h" |
28 | ||
29 | ||
30 | extern int debug; | |
31 | extern sys_config Config; | |
32 | extern int my_shutdown; | |
33 | ||
34 | ||
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
35 | #ifdef HAVE_WIRINGPI_H |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
36 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
37 | #define MAXTIMINGS 100 |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
38 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
39 | int dht11_pin = -1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
40 | int dht11_temperature = -1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
41 | int dht11_humidity = -1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
42 | int dht11_valid = FALSE; |
220
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
43 | time_t dht11_last = (time_t)0; |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
44 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
45 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
46 | static uint8_t sizecvt(const int read_value) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
47 | /* |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
48 | * digitalRead() and friends from wiringpi are defined as returning a value |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
49 | * < 256. However, they are returned as int() types. This is a safety function |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
50 | */ |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
51 | if (read_value > 255 || read_value < 0) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
52 | syslog(LOG_NOTICE, "invalid data from wiringPi library"); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
53 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
54 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
55 | return (uint8_t)read_value; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
56 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
57 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
58 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
59 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
60 | /* |
213
2317b8d644fa
Code cleanup, streamlined error messages.
Michiel Broek <mbroek@mbse.eu>
parents:
212
diff
changeset
|
61 | * DHT11 sensor read. |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
62 | */ |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
63 | void dht11Read(void) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
64 | int tries = 5; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
65 | unsigned short got_correct_data = 0; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
66 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
67 | if (dht11_pin == -1) |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
68 | return; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
69 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
70 | while (tries && !got_correct_data) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
71 | uint8_t laststate = HIGH; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
72 | uint8_t counter = 0; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
73 | uint8_t j = 0, i = 0; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
74 | int dht11_dat[5] = {0,0,0,0,0}; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
75 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
76 | /* |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
77 | * Select output mode to send the start signal. |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
78 | */ |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
79 | pinMode(dht11_pin, OUTPUT); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
80 | digitalWrite(dht11_pin, HIGH); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
81 | usleep(1000); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
82 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
83 | /* |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
84 | * Low for at least 18 milliseconds |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
85 | */ |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
86 | digitalWrite(dht11_pin, LOW); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
87 | usleep(20000); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
88 | digitalWrite(dht11_pin, HIGH); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
89 | pinMode(dht11_pin, INPUT); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
90 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
91 | /* |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
92 | * Detect change and read data |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
93 | */ |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
94 | for (i=0; i<MAXTIMINGS; i++) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
95 | counter = 0; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
96 | delayMicroseconds(10); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
97 | while (sizecvt(digitalRead(dht11_pin)) == laststate) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
98 | counter++; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
99 | delayMicroseconds(1); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
100 | if (counter == 255) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
101 | break; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
102 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
103 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
104 | laststate = sizecvt(digitalRead(dht11_pin)); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
105 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
106 | if (counter == 255) |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
107 | break; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
108 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
109 | /* |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
110 | * ignore first 3 transitions |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
111 | */ |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
112 | if ((i >= 4) && (i%2 == 0)) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
113 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
114 | // shove each bit into the storage bytes |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
115 | dht11_dat[(int)((double)j/8)] <<= 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
116 | if (counter > 16) |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
117 | dht11_dat[(int)((double)j/8)] |= 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
118 | j++; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
119 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
120 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
121 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
122 | /* |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
123 | * If there is no sensor, j = 0 |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
124 | */ |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
125 | if ((counter == 255) && (j == 0)) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
126 | if (dht11_temperature != -1) { |
268
dda91dfa4aa8
All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents:
262
diff
changeset
|
127 | syslog(LOG_NOTICE, "dht11 sensor disappeared"); |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
128 | } else { |
268
dda91dfa4aa8
All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents:
262
diff
changeset
|
129 | syslog(LOG_NOTICE, "dht11 sensor not present"); |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
130 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
131 | dht11_temperature = -1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
132 | dht11_humidity = -1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
133 | dht11_valid = FALSE; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
134 | return; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
135 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
136 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
137 | /* |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
138 | * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
139 | * print it out if data is good |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
140 | */ |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
141 | if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
142 | got_correct_data = 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
143 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
144 | int h = dht11_dat[0] + dht11_dat[1]; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
145 | int t = (dht11_dat[2] & 0x7F) + dht11_dat[3]; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
146 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
147 | if ((dht11_dat[2] & 0x80) != 0) |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
148 | t *= -1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
149 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
150 | dht11_temperature = t; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
151 | dht11_humidity = h; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
152 | dht11_valid = TRUE; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
153 | } else { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
154 | tries--; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
155 | if (tries == 0) |
268
dda91dfa4aa8
All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents:
262
diff
changeset
|
156 | syslog(LOG_NOTICE, "dht11 data checksum was wrong 5 times"); |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
157 | usleep(100000); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
158 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
159 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
160 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
161 | |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
162 | #endif |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
163 | |
158 | 164 | |
347
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
165 | int read_w1(char *address, char *file) |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
166 | { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
167 | char *addr = NULL; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
168 | int fn = -1, rc = -1, retries = 5; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
169 | uint8_t val; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
170 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
171 | addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
172 | addr = xstrcat(addr, address); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
173 | addr = xstrcat(addr, (char *)"/"); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
174 | addr = xstrcat(addr, file); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
175 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
176 | if ((fn = open(addr, O_RDONLY)) >= 0) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
177 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
178 | if ((lseek(fn, 0L, SEEK_SET)) == 0) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
179 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
180 | while (retries--) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
181 | if ((read(fn, &val, 1)) == 1) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
182 | rc = (int)val; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
183 | goto leave; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
184 | // } else { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
185 | // syslog(LOG_NOTICE, "read_w1() read %s try=%d: %s", addr, retries, strerror(errno)); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
186 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
187 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
188 | syslog(LOG_NOTICE, "read_w1() read %s fatal: %s", addr, strerror(errno)); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
189 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
190 | } else { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
191 | syslog(LOG_NOTICE, "read_w1() lseek %s: %s", addr, strerror(errno)); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
192 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
193 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
194 | } else { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
195 | syslog(LOG_NOTICE, "read_w1() open %s: %s", addr, strerror(errno)); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
196 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
197 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
198 | leave: |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
199 | if (fn != -1) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
200 | if ((close(fn)) == -1) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
201 | syslog(LOG_NOTICE, "read_w1() close %s: %s", addr, strerror(errno)); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
202 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
203 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
204 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
205 | free(addr); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
206 | return rc; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
207 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
208 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
209 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
210 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
211 | int write_w1(char *address, char *file, uint8_t val) |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
212 | { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
213 | char *addr = NULL; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
214 | int fn = -1, rc = -1, retries = 5; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
215 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
216 | addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
217 | addr = xstrcat(addr, address); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
218 | addr = xstrcat(addr, (char *)"/"); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
219 | addr = xstrcat(addr, file); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
220 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
221 | if ((fn = open(addr, O_WRONLY)) >= 0) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
222 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
223 | if ((lseek(fn, 0L, SEEK_SET)) == 0) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
224 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
225 | while (retries--) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
226 | if ((write(fn, &val, 1)) == 1) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
227 | rc = 0; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
228 | goto leave; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
229 | // } else { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
230 | // syslog(LOG_NOTICE, "write_w1() write %s try=%d: %s", addr, retries, strerror(errno)); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
231 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
232 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
233 | syslog(LOG_NOTICE, "write_w1() write %s fatal: %s", addr, strerror(errno)); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
234 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
235 | } else { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
236 | syslog(LOG_NOTICE, "write_w1() lseek %s: %s", addr, strerror(errno)); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
237 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
238 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
239 | } else { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
240 | syslog(LOG_NOTICE, "write_w1() open %s: %s", addr, strerror(errno)); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
241 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
242 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
243 | leave: |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
244 | if (fn != -1) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
245 | if ((close(fn)) == -1) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
246 | syslog(LOG_NOTICE, "write_w1() close %s: %s", addr, strerror(errno)); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
247 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
248 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
249 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
250 | free(addr); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
251 | return rc; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
252 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
253 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
254 | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
255 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
256 | int device_out(char *uuid, int value) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
257 | { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
258 | devices_list *device; |
323 | 259 | time_t now, my_timestamp; |
347
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
260 | int my_value, test_value; |
187
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
261 | #ifdef HAVE_WIRINGPI_H |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
262 | int i, rc; |
187
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
263 | char buf[40]; |
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
264 | #endif |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
265 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
266 | if (uuid == NULL) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
267 | return 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
268 | |
185 | 269 | now = time(NULL); |
181
4099412fca09
Fixed double commands to 433 MHz switches, they did not switch.
Michiel Broek <mbroek@mbse.eu>
parents:
179
diff
changeset
|
270 | |
337 | 271 | #ifdef HAVE_WIRINGPI_H |
272 | piLock(LOCK_DEVICES); | |
273 | #endif | |
274 | ||
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
275 | for (device = Config.devices; device; device = device->next) { |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
276 | if (! strcmp(uuid, device->uuid)) { |
185 | 277 | /* |
278 | * Execute command if different then the old value. But also | |
212 | 279 | * every 2 minutes because commands can have temporary |
280 | * disconnects, or have radio problems. | |
185 | 281 | */ |
323 | 282 | my_timestamp = device->timestamp; |
283 | my_value = device->value; | |
347
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
284 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
285 | if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN)) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
286 | test_value = (value == 0) ? 0 : 1; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
287 | } else { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
288 | test_value = value; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
289 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
290 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
291 | if ((test_value != my_value) || (((int)now - (int)my_timestamp) > 120)) { |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
292 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
293 | #ifdef HAVE_WIRINGPI_H |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
294 | rc = 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
295 | if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
296 | snprintf(buf, 39, "%s,%d", device->address, value ? 1:0); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
297 | for (i = 0; i < strlen(buf); i++) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
298 | if (buf[i] == '-') |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
299 | buf[i] = ','; |
337 | 300 | piUnlock(LOCK_DEVICES); |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
301 | enableTransmit(device->gpiopin); |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
302 | rc = toggleSwitch(buf); |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
303 | disableTransmit(); |
337 | 304 | piLock(LOCK_DEVICES); |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
305 | syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
306 | if (debug) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
307 | fprintf(stdout, "RC433 command %s rc=%d\n", buf, rc); |
212 | 308 | device->value = value; |
309 | device->timestamp = time(NULL); | |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
310 | piUnlock(LOCK_DEVICES); |
212 | 311 | return rc; |
312 | } | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
313 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
314 | if ((device->type == DEVTYPE_GPIO) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
315 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
316 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
317 | #endif |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
318 | if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) { |
347
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
319 | if (strncmp(device->address, (char *)"3a", 2) == 0) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
320 | /* |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
321 | * DS2413. First read state so that we can preserve the state of |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
322 | * the "other" PIO channel. To make things a bit more complicated |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
323 | * the bits in the state register differ from the output register. |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
324 | */ |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
325 | uint8_t state, output; |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
326 | |
347
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
327 | if ((rc = read_w1(device->address, (char *)"state")) >= 0) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
328 | state = (unsigned int)rc; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
329 | output = (state & 0x01) + ((state & 0x04) >> 1); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
330 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
331 | if (device->subdevice == 0) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
332 | output = (output & 0xfe); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
333 | output |= (value == 0) ? 0x01 : 0x00; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
334 | } else if (device->subdevice == 1) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
335 | output = (output & 0xfd); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
336 | output |= (value == 0) ? 0x02 : 0x00; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
337 | } else { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
338 | output = 0xff; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
339 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
340 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
341 | if ((write_w1(device->address, (char *)"output", output)) == 0) { |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
342 | syslog(LOG_NOTICE, "DS2413 PIO%c value=%d", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
343 | if (debug) |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
344 | fprintf(stdout, "DS2413 PIO%c value=%d\n", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
345 | device->value = (value == 0) ? 0 : 1; |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
346 | device->timestamp = time(NULL); |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
347 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
348 | } |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
349 | } |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
350 | } |
347
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
351 | |
259
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
352 | #ifdef USE_SIMULATOR |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
353 | if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) { |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
354 | |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
355 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
356 | #endif |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
357 | } else { |
337 | 358 | #ifdef HAVE_WIRINGPI_H |
359 | piUnlock(LOCK_DEVICES); | |
360 | #endif | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
361 | return 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
362 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
363 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
364 | } |
337 | 365 | #ifdef HAVE_WIRINGPI_H |
366 | piUnlock(LOCK_DEVICES); | |
367 | #endif | |
185 | 368 | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
369 | return 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
370 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
371 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
372 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
373 | |
158 | 374 | /* |
375 | * Auto detect hotplugged or known to be present devices | |
376 | */ | |
377 | int devices_detect(void) | |
378 | { | |
379 | struct dirent *de; | |
380 | DIR *fd; | |
381 | devices_list *device, *ndev; | |
340 | 382 | int found, subdevices, ival, i, rc = 0; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
383 | char buf[40]; |
158 | 384 | uuid_t uu; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
385 | #ifdef HAVE_WIRINGPI_H |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
386 | int pin; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
387 | #endif |
158 | 388 | |
389 | /* | |
390 | * Scan for 1-wire devices | |
391 | */ | |
392 | if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { | |
393 | while ((de = readdir(fd))) { | |
394 | if (de->d_name[0] != '.') { | |
395 | found = FALSE; | |
396 | for (device = Config.devices; device; device = device->next) { | |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
397 | if (strcmp(device->address,de->d_name) == 0) { |
158 | 398 | found = TRUE; |
399 | break; | |
400 | } | |
401 | } | |
402 | ||
403 | if (found == FALSE) { | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
404 | strncpy(buf, de->d_name, 2); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
405 | buf[2] = '\0'; |
340 | 406 | sscanf(buf, "%02x", &ival); |
407 | syslog(LOG_NOTICE, "Scan 1-wire %02x %d", ival, ival); | |
158 | 408 | subdevices = 1; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
409 | if (strcmp(buf, (char *)"29") == 0) |
158 | 410 | subdevices = 8; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
411 | if (strcmp(buf, (char *)"3a") == 0) |
158 | 412 | subdevices = 2; |
413 | for (i = 0; i < subdevices; i++) { | |
414 | ndev = (devices_list *)malloc(sizeof(devices_list)); | |
415 | ndev->next = NULL; | |
416 | ndev->version = 1; | |
417 | ndev->uuid = malloc(37); | |
418 | uuid_generate(uu); | |
419 | uuid_unparse(uu, ndev->uuid); | |
420 | ndev->type = DEVTYPE_W1; | |
421 | ndev->direction = DEVDIR_UNDEF; | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
422 | if (strcmp(buf, (char *)"10") == 0) { |
158 | 423 | ndev->direction = DEVDIR_IN_ANALOG; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
424 | ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer"); |
338
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
425 | } else if (strcmp(buf, (char *)"22") == 0) { |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
426 | ndev->direction = DEVDIR_IN_ANALOG; |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
427 | ndev->description = xstrcpy((char *)"DS1820 Digital thermometer"); |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
428 | } else if (strcmp(buf, (char *)"28") == 0) { |
158 | 429 | ndev->direction = DEVDIR_IN_ANALOG; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
430 | ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
431 | } else if (strcmp(buf, (char *)"29") == 0) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
432 | ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD"); |
344
acd840c9fcc0
Fixed Offset label in device editor. Added code to edit pin states for DS2408 and DS2413 1-wire devices.
Michiel Broek <mbroek@mbse.eu>
parents:
343
diff
changeset
|
433 | ndev->direction = DEVDIR_IN_BIN; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
434 | } else if (strcmp(buf, (char *)"3a") == 0) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
435 | ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch"); |
344
acd840c9fcc0
Fixed Offset label in device editor. Added code to edit pin states for DS2408 and DS2413 1-wire devices.
Michiel Broek <mbroek@mbse.eu>
parents:
343
diff
changeset
|
436 | ndev->direction = DEVDIR_IN_BIN; |
338
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
437 | } else if (strcmp(buf, (char *)"3b") == 0) { |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
438 | ndev->direction = DEVDIR_IN_ANALOG; |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
439 | ndev->description = xstrcpy((char *)"DS1825 Digital thermometer"); |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
440 | } else if (strcmp(buf, (char *)"42") == 0) { |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
441 | ndev->direction = DEVDIR_IN_ANALOG; |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
442 | ndev->description = xstrcpy((char *)"DS28EA00 Digital thermometer"); |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
443 | } else if (strcmp(buf, (char *)"w1") == 0) { |
158 | 444 | ndev->description = xstrcpy((char *)"Master System device"); |
445 | } else { | |
446 | ndev->description = xstrcpy((char *)"Unknown device family "); | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
447 | ndev->description = xstrcat(ndev->description, buf); |
158 | 448 | } |
343
ba3a2d27d59e
Initialize offset to zero with new detected 1-wire devices.
Michiel Broek <mbroek@mbse.eu>
parents:
342
diff
changeset
|
449 | ndev->value = ndev->offset = ndev->inuse = 0; |
158 | 450 | ndev->present = DEVPRESENT_YES; |
451 | ndev->address = xstrcpy(de->d_name); | |
452 | ndev->subdevice = i; | |
453 | ndev->gpiopin = -1; | |
454 | ndev->comment = xstrcpy((char *)"Auto detected device"); | |
455 | ndev->timestamp = time(NULL); | |
456 | ||
457 | if (Config.devices == NULL) { | |
458 | Config.devices = ndev; | |
459 | } else { | |
460 | for (device = Config.devices; device; device = device->next) { | |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
461 | if (device->next == NULL) { |
158 | 462 | device->next = ndev; |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
463 | break; |
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
464 | } |
158 | 465 | } |
466 | } | |
467 | rc++; | |
468 | } | |
469 | } | |
470 | } | |
471 | } | |
166
c31ea86fec43
Forgot some close functions so tha program ran out of file descriptors. Better loggin for writing the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
164
diff
changeset
|
472 | closedir(fd); |
158 | 473 | } |
474 | ||
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
475 | #ifdef HAVE_WIRINGPI_H |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
476 | if (piBoardRev() == 2) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
477 | /* |
324 | 478 | * Support rev B and newer boards only |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
479 | */ |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
480 | found = FALSE; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
481 | for (device = Config.devices; device; device = device->next) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
482 | if (device->type == DEVTYPE_GPIO) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
483 | found = TRUE; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
484 | break; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
485 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
486 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
487 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
488 | if (found == FALSE) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
489 | /* |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
490 | * There were no GPIO devices found. |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
491 | */ |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
492 | subdevices = 12; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
493 | pin = 0; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
494 | for (i = 0; i < subdevices; i++) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
495 | if (i == 8) |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
496 | pin = 17; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
497 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
498 | ndev = (devices_list *)malloc(sizeof(devices_list)); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
499 | ndev->next = NULL; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
500 | ndev->version = 1; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
501 | ndev->uuid = malloc(37); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
502 | uuid_generate(uu); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
503 | uuid_unparse(uu, ndev->uuid); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
504 | ndev->type = DEVTYPE_GPIO; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
505 | ndev->value = digitalRead(pin); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
506 | ndev->present = DEVPRESENT_YES; |
163
3d813570a5e3
LIST BUS now displays the new devices table. Adjusted the web screen
Michiel Broek <mbroek@mbse.eu>
parents:
162
diff
changeset
|
507 | ndev->address = xstrcpy((char *)"GPIO"); |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
508 | snprintf(buf, 39, "Raspberry GPIO %d", i); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
509 | ndev->description = xstrcpy(buf); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
510 | ndev->subdevice = i; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
511 | ndev->gpiopin = pin; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
512 | ndev->timestamp = time(NULL); |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
513 | if (i == PANEL_LED) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
514 | ndev->direction = DEVDIR_OUT_BIN; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
515 | ndev->inuse = 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
516 | ndev->comment = xstrcpy((char *)"Frontpanel LED"); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
517 | } else if (i == PANEL_ENTER) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
518 | ndev->direction = DEVDIR_IN_BIN; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
519 | ndev->inuse = 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
520 | ndev->comment = xstrcpy((char *)"Frontpanel Enter key"); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
521 | } else if (i == PANEL_DOWN) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
522 | ndev->direction = DEVDIR_IN_BIN; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
523 | ndev->inuse = 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
524 | ndev->comment = xstrcpy((char *)"Frontpanel Down key"); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
525 | } else if (i == PANEL_UP) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
526 | ndev->direction = DEVDIR_IN_BIN; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
527 | ndev->inuse = 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
528 | ndev->comment = xstrcpy((char *)"Frontpanel Up key"); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
529 | } else if (i == 7) { |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
530 | ndev->direction = DEVDIR_INTERN; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
531 | ndev->inuse = 1; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
532 | ndev->comment = xstrcpy((char *)"1-Wire bus"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
533 | } else { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
534 | ndev->direction = DEVDIR_IN_BIN; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
535 | ndev->inuse = 0; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
536 | ndev->comment = xstrcpy((char *)"Raspberry GPIO"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
537 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
538 | pin++; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
539 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
540 | if (Config.devices == NULL) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
541 | Config.devices = ndev; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
542 | } else { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
543 | for (device = Config.devices; device; device = device->next) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
544 | if (device->next == NULL) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
545 | device->next = ndev; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
546 | break; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
547 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
548 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
549 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
550 | rc++; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
551 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
552 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
553 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
554 | #endif |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
555 | |
259
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
556 | #ifdef USE_SIMULATOR |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
557 | found = FALSE; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
558 | for (device = Config.devices; device; device = device->next) { |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
559 | if (device->type == DEVTYPE_SIM) { |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
560 | found = TRUE; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
561 | break; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
562 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
563 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
564 | |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
565 | if (found == FALSE) { |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
566 | subdevices = 5; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
567 | for (i = 0; i < subdevices; i++) { |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
568 | ndev = (devices_list *)malloc(sizeof(devices_list)); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
569 | ndev->next = NULL; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
570 | ndev->version = 1; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
571 | ndev->uuid = malloc(37); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
572 | uuid_generate(uu); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
573 | uuid_unparse(uu, ndev->uuid); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
574 | ndev->type = DEVTYPE_SIM; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
575 | ndev->value = ndev->offset = 0; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
576 | ndev->present = DEVPRESENT_YES; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
577 | ndev->subdevice = i; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
578 | ndev->gpiopin = -1; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
579 | ndev->comment = xstrcpy((char *)"Auto detected device"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
580 | ndev->timestamp = time(NULL); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
581 | ndev->inuse = 0; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
582 | switch (i) { |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
583 | case 0: ndev->direction = DEVDIR_IN_ANALOG; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
584 | ndev->address = xstrcpy((char *)"SimRoomTemp"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
585 | ndev->description = xstrcpy((char *)"Simulated room temperature"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
586 | break; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
587 | case 1: ndev->direction = DEVDIR_IN_ANALOG; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
588 | ndev->address = xstrcpy((char *)"SimAirTemp"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
589 | ndev->description = xstrcpy((char *)"Simulated air temperature"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
590 | break; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
591 | case 2: ndev->direction = DEVDIR_IN_ANALOG; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
592 | ndev->address = xstrcpy((char *)"SimBeerTemp"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
593 | ndev->description = xstrcpy((char *)"Simulated beer temperature"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
594 | break; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
595 | case 3: ndev->direction = DEVDIR_OUT_ANALOG; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
596 | ndev->address = xstrcpy((char *)"SimHeater"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
597 | ndev->description = xstrcpy((char *)"Simulated heater"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
598 | break; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
599 | case 4: ndev->direction = DEVDIR_OUT_ANALOG; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
600 | ndev->address = xstrcpy((char *)"SimCooler"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
601 | ndev->description = xstrcpy((char *)"Simulated cooler"); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
602 | break; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
603 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
604 | |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
605 | if (Config.devices == NULL) { |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
606 | Config.devices = ndev; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
607 | } else { |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
608 | for (device = Config.devices; device; device = device->next) { |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
609 | if (device->next == NULL) { |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
610 | device->next = ndev; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
611 | break; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
612 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
613 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
614 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
615 | rc++; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
616 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
617 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
618 | #endif |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
619 | |
158 | 620 | return rc; |
621 | } | |
622 | ||
623 | ||
624 | ||
625 | #ifdef HAVE_WIRINGPI_H | |
626 | PI_THREAD (my_devices_loop) | |
627 | #else | |
628 | void *my_devices_loop(void *threadid) | |
629 | #endif | |
630 | { | |
631 | devices_list *device; | |
262
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
632 | #ifdef USE_SIMULATOR |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
633 | simulator_list *simulator; |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
634 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
635 | char *addr = NULL, line[60], *p = NULL; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
636 | FILE *fp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
637 | int temp, rc; |
242
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
638 | #ifdef HAVE_WIRINGPI_H |
220
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
639 | time_t now; |
242
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
640 | #endif |
158 | 641 | |
642 | syslog(LOG_NOTICE, "Thread my_devices_loop started"); | |
643 | ||
242
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
644 | #ifdef HAVE_WIRINGPI_H |
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
645 | if ((rc = piHiPri(10))) |
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
646 | syslog(LOG_NOTICE, "my_devices_loop: piHiPri(10) rc=%d", rc); |
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
647 | #endif |
239
793c09ece542
Increase priority for the devices thread
Michiel Broek <mbroek@mbse.eu>
parents:
235
diff
changeset
|
648 | |
158 | 649 | /* |
650 | * Loop forever until the external shutdown variable is set. | |
651 | */ | |
652 | for (;;) { | |
653 | ||
654 | /* | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
655 | * Process all devices. |
158 | 656 | */ |
657 | for (device = Config.devices; device; device = device->next) { | |
658 | ||
659 | if (my_shutdown) | |
660 | break; | |
661 | ||
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
662 | switch (device->type) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
663 | case DEVTYPE_W1: |
338
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
664 | /* |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
665 | * Only tested with DS18B20 but from the kernel source this |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
666 | * should work with all 1-wire thermometer sensors. |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
667 | */ |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
668 | if ((strncmp(device->address, (char *)"10", 2) == 0) || |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
669 | (strncmp(device->address, (char *)"22", 2) == 0) || |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
670 | (strncmp(device->address, (char *)"28", 2) == 0) || |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
671 | (strncmp(device->address, (char *)"3b", 2) == 0) || |
8e29d142d67d
Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents:
337
diff
changeset
|
672 | (strncmp(device->address, (char *)"42", 2) == 0)) { |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
673 | addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
674 | addr = xstrcat(addr, device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
675 | addr = xstrcat(addr, (char *)"/w1_slave"); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
676 | if ((fp = fopen(addr, "r"))) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
677 | if (device->present != DEVPRESENT_YES) { |
268
dda91dfa4aa8
All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents:
262
diff
changeset
|
678 | syslog(LOG_NOTICE, "sensor %s is back", device->address); |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
679 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
680 | piLock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
681 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
682 | device->present = DEVPRESENT_YES; |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
683 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
684 | piUnlock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
685 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
686 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
687 | /* |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
688 | * The output looks like: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
689 | * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
690 | * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
691 | */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
692 | fgets(line, 50, fp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
693 | line[strlen(line)-1] = '\0'; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
694 | if ((line[36] == 'Y') && (line[37] == 'E')) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
695 | /* CRC is Ok, continue */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
696 | fgets(line, 50, fp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
697 | line[strlen(line)-1] = '\0'; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
698 | strtok(line, (char *)"="); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
699 | p = strtok(NULL, (char *)"="); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
700 | rc = sscanf(p, "%d", &temp); |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
701 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
702 | piLock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
703 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
704 | if ((rc == 1) && (device->value != temp)) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
705 | device->value = temp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
706 | device->timestamp = time(NULL); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
707 | } |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
708 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
709 | piUnlock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
710 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
711 | } else { |
268
dda91dfa4aa8
All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents:
262
diff
changeset
|
712 | syslog(LOG_NOTICE, "sensor %s CRC error", device->address); |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
713 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
714 | piLock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
715 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
716 | device->present = DEVPRESENT_ERROR; |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
717 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
718 | piUnlock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
719 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
720 | } |
166
c31ea86fec43
Forgot some close functions so tha program ran out of file descriptors. Better loggin for writing the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
164
diff
changeset
|
721 | fclose(fp); |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
722 | } else { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
723 | if (device->present != DEVPRESENT_NO) { |
268
dda91dfa4aa8
All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents:
262
diff
changeset
|
724 | syslog(LOG_NOTICE, "sensor %s is missing", device->address); |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
725 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
726 | piLock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
727 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
728 | device->present = DEVPRESENT_NO; |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
729 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
730 | piUnlock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
731 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
732 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
733 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
734 | free(addr); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
735 | addr = NULL; |
341
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
736 | } /* if temperature sensor */ |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
737 | /* |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
738 | * DS2408 8 Channel addressable switch |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
739 | */ |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
740 | if (strncmp(device->address, (char *)"29", 2) == 0) { |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
741 | /* |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
742 | * Always read current state and set these bits |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
743 | * in the subdevices value. We do this even with |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
744 | * the output bits, they should match. |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
745 | */ |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
746 | addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
747 | addr = xstrcat(addr, device->address); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
748 | addr = xstrcat(addr, (char *)"/state"); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
749 | if ((fp = fopen(addr, "r"))) { |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
750 | if (device->present != DEVPRESENT_YES) { |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
751 | syslog(LOG_NOTICE, "DS2408 %s is back", device->address); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
752 | #ifdef HAVE_WIRINGPI_H |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
753 | piLock(LOCK_DEVICES); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
754 | #endif |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
755 | device->present = DEVPRESENT_YES; |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
756 | #ifdef HAVE_WIRINGPI_H |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
757 | piUnlock(LOCK_DEVICES); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
758 | #endif |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
759 | } |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
760 | |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
761 | fclose(fp); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
762 | } else { |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
763 | if (device->present != DEVPRESENT_NO) { |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
764 | syslog(LOG_NOTICE, "DS2408 %s is missing", device->address); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
765 | #ifdef HAVE_WIRINGPI_H |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
766 | piLock(LOCK_DEVICES); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
767 | #endif |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
768 | device->present = DEVPRESENT_NO; |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
769 | #ifdef HAVE_WIRINGPI_H |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
770 | piUnlock(LOCK_DEVICES); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
771 | #endif |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
772 | } |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
773 | } |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
774 | free(addr); |
cfc952a68d4a
Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents:
340
diff
changeset
|
775 | addr = NULL; |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
776 | } |
342
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
777 | /* |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
778 | * DS2413 Dual channel addressable switch |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
779 | */ |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
780 | if (strncmp(device->address, (char *)"3a", 2) == 0) { |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
781 | addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
782 | addr = xstrcat(addr, device->address); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
783 | addr = xstrcat(addr, (char *)"/state"); |
347
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
784 | |
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
785 | if ((access(addr, R_OK)) == 0) { |
342
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
786 | if (device->present != DEVPRESENT_YES) { |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
787 | syslog(LOG_NOTICE, "DS2413 %s is back", device->address); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
788 | #ifdef HAVE_WIRINGPI_H |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
789 | piLock(LOCK_DEVICES); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
790 | #endif |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
791 | device->present = DEVPRESENT_YES; |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
792 | #ifdef HAVE_WIRINGPI_H |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
793 | piUnlock(LOCK_DEVICES); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
794 | #endif |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
795 | } |
347
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
796 | if ((rc = read_w1(device->address, (char *)"state")) >= 0) { |
342
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
797 | #ifdef HAVE_WIRINGPI_H |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
798 | piLock(LOCK_DEVICES); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
799 | #endif |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
800 | /* |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
801 | * Read PIOA or PIOB pin state bits |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
802 | */ |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
803 | if (device->subdevice == 0) |
347
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
804 | device->value = (rc & 0x01) ? 0 : 1; |
342
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
805 | else if (device->subdevice == 1) |
347
0ab5c3fd7c77
Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents:
344
diff
changeset
|
806 | device->value = (rc & 0x04) ? 0 : 1; |
342
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
807 | device->timestamp = time(NULL); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
808 | #ifdef HAVE_WIRINGPI_H |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
809 | piUnlock(LOCK_DEVICES); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
810 | #endif |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
811 | } |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
812 | } else { |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
813 | if (device->present != DEVPRESENT_NO) { |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
814 | syslog(LOG_NOTICE, "DS2413 %s is missing", device->address); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
815 | #ifdef HAVE_WIRINGPI_H |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
816 | piLock(LOCK_DEVICES); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
817 | #endif |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
818 | device->present = DEVPRESENT_NO; |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
819 | #ifdef HAVE_WIRINGPI_H |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
820 | piUnlock(LOCK_DEVICES); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
821 | #endif |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
822 | } |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
823 | } |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
824 | free(addr); |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
825 | addr = NULL; |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
826 | } |
3bbc8f42adc0
Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents:
341
diff
changeset
|
827 | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
828 | break; |
203 | 829 | |
830 | #ifdef HAVE_WIRINGPI_H | |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
831 | case DEVTYPE_DHT: |
220
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
832 | /* |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
833 | * Make sure we don't read the sensor withing 2 seconds. |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
834 | */ |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
835 | now = time(NULL); |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
836 | if ((int)(now - dht11_last) > 2) { |
203 | 837 | dht11_pin = device->gpiopin; |
838 | dht11Read(); | |
220
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
839 | dht11_last = now; |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
840 | } |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
841 | if (device->subdevice == 0) { |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
842 | piLock(LOCK_DEVICES); |
203 | 843 | if (dht11_valid) { |
844 | device->value = dht11_temperature * 1000; | |
845 | device->timestamp = time(NULL); | |
846 | device->present = DEVPRESENT_YES; | |
847 | } else { | |
848 | device->present = DEVPRESENT_ERROR; | |
849 | } | |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
850 | piUnlock(LOCK_DEVICES); |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
851 | } else if (device->subdevice == 1) { |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
852 | piLock(LOCK_DEVICES); |
203 | 853 | if (dht11_valid) { |
854 | device->value = dht11_humidity * 1000; | |
855 | device->timestamp = time(NULL); | |
856 | device->present = DEVPRESENT_YES; | |
857 | } else { | |
858 | device->present = DEVPRESENT_ERROR; | |
859 | } | |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
860 | piUnlock(LOCK_DEVICES); |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
861 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
862 | break; |
203 | 863 | #endif |
259
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
864 | #ifdef USE_SIMULATOR |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
865 | case DEVTYPE_SIM: |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
866 | #ifdef HAVE_WIRINGPI_H |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
867 | piLock(LOCK_DEVICES); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
868 | #endif |
262
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
869 | if (Config.simulators) { |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
870 | simulator = Config.simulators; |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
871 | if (device->subdevice == 0) { |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
872 | device->value = (int)(simulator->room_temperature * 1000); |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
873 | device->timestamp = time(NULL); |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
874 | } else if (device->subdevice == 1) { |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
875 | device->value = (int)(simulator->air_temperature * 1000); |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
876 | device->timestamp = time(NULL); |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
877 | } else if (device->subdevice == 2) { |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
878 | device->value = (int)(simulator->beer_temperature * 1000); |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
879 | device->timestamp = time(NULL); |
d0014ccec615
Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents:
259
diff
changeset
|
880 | } |
259
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
881 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
882 | #ifdef HAVE_WIRINGPI_H |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
883 | piUnlock(LOCK_DEVICES); |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
884 | #endif |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
885 | break; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
886 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
887 | default: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
888 | break; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
889 | } |
212 | 890 | |
891 | /* | |
892 | * Delay a bit after procesing a device. | |
893 | */ | |
894 | usleep(10000); | |
895 | } | |
215
5ad534c79a22
Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
896 | /* |
5ad534c79a22
Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
897 | * Delay a bit after all devices |
5ad534c79a22
Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
898 | */ |
5ad534c79a22
Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
899 | usleep(100000); |
158 | 900 | } |
901 | ||
902 | syslog(LOG_NOTICE, "Thread my_devices_loop stopped"); | |
903 | return 0; | |
904 | } | |
905 | ||
906 | ||
907 |