thermferm/one-wire.c

changeset 656
ca47c742a25d
parent 655
780cc08df263
child 657
38162f374842
equal deleted inserted replaced
655:780cc08df263 656:ca47c742a25d
21 extern int debug; 21 extern int debug;
22 22
23 #include "thermferm.h" 23 #include "thermferm.h"
24 #include "statetbl.h" 24 #include "statetbl.h"
25 #include "one-wire.h" 25 #include "one-wire.h"
26 #include "devices.h"
27 #include "delay.h"
26 #include "xutil.h" 28 #include "xutil.h"
27 29
28 30
29 extern sys_config Config; 31 extern sys_config Config;
30 extern int my_shutdown; 32 extern int my_shutdown;
59 61
60 62
61 SM_DECL(one_wire,(char *)"one-wire") 63 SM_DECL(one_wire,(char *)"one-wire")
62 SM_STATES 64 SM_STATES
63 Scan, 65 Scan,
66 Read2413,
64 Reading, 67 Reading,
65 Missing 68 Missing
66 SM_NAMES 69 SM_NAMES
67 (char *)"Scan", 70 (char *)"Scan",
71 (char *)"Read2413",
68 (char *)"Reading", 72 (char *)"Reading",
69 (char *)"Missing" 73 (char *)"Missing"
70 SM_EDECL 74 SM_EDECL
71 75
72 int found, i; 76 int found, i, rc;
73 FILE *fp; 77 FILE *fp;
74 // devices_list *device, *ndev; 78 devices_list *device;
75 w1_list *dev_w1, *n_w1, *cur_w1 = NULL; 79 w1_list *dev_w1, *n_w1, *cur_w1 = NULL;
76 char buffer[25], w1type[10]; 80 char buffer[25], w1type[10];
77 uuid_t uu; 81 uint8_t state, output;
78 82
79 SM_START(Scan) 83 SM_START(Scan)
80 84
81 SM_STATE(Scan) 85 SM_STATE(Scan)
82 86
92 syslog(LOG_NOTICE, "No w1_bus_master: %s", strerror(errno)); 96 syslog(LOG_NOTICE, "No w1_bus_master: %s", strerror(errno));
93 SM_ERROR; 97 SM_ERROR;
94 } 98 }
95 while ((fgets(buffer, 25, fp))) { 99 while ((fgets(buffer, 25, fp))) {
96 buffer[strlen(buffer)-1] = '\0'; 100 buffer[strlen(buffer)-1] = '\0';
97 // syslog(LOG_NOTICE, "device %d %s", strlen(buffer), buffer);
98
99 strncpy(w1type, buffer, 2); 101 strncpy(w1type, buffer, 2);
100 w1type[2] = '\0'; 102 w1type[2] = '\0';
101 103
102 /* 104 /*
103 * Check if device is known and already detected. 105 * Check if device is known and already detected.
147 } else { 149 } else {
148 syslog(LOG_NOTICE, "One-wire device %d %s unknown", strlen(buffer), buffer); 150 syslog(LOG_NOTICE, "One-wire device %d %s unknown", strlen(buffer), buffer);
149 } 151 }
150 } 152 }
151 fclose(fp); 153 fclose(fp);
154 SM_PROCEED(Read2413);
155
156 SM_STATE(Read2413)
157
158 if (my_shutdown) {
159 SM_SUCCESS;
160 }
161
162 for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
163 if (strcmp(dev_w1->family, "3a") == 0) {
164 syslog(LOG_NOTICE, "ds2413 %s", dev_w1->address);
165 for (i = 0; i < 2; i++) {
166 for (device = Config.devices; device; device = device->next) {
167 if ((strcmp(dev_w1->address, device->address) == 0) && (device->subdevice == i) && (device->direction == DEVDIR_IN_BIN)) {
168 /*
169 * First make sure that if this device is configured as input
170 * to drive the output high.
171 * TODO: This must be made different. Check state and fix only if wrong.
172 */
173 if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
174 state = (unsigned int)rc;
175 output = ((state & 0x02) >> 1) + ((state & 0x08) >> 2); /* Both latch states */
176 if (i == 0) {
177 output = (output & 0xfe);
178 output |= 0x01;
179 } else if (i == 1) {
180 output = (output & 0xfd);
181 output |= 0x02;
182 } else {
183 output = 0xff;
184 }
185 write_w1(device->address, (char *)"output", output);
186 }
187 if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
188 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
189 /*
190 * Read PIOA or PIOB pin state bits
191 */
192 if (device->subdevice == 0)
193 device->value = (rc & 0x01) ? 0 : 1;
194 else if (device->subdevice == 1)
195 device->value = (rc & 0x04) ? 0 : 1;
196 device->timestamp = time(NULL);
197 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
198 }
199 mDelay(20);
200 }
201 }
202 }
203 }
204 }
205
152 SM_PROCEED(Reading); 206 SM_PROCEED(Reading);
153 207
154 SM_STATE(Reading) 208 SM_STATE(Reading)
155 209
156 if (my_shutdown) { 210 if (my_shutdown) {

mercurial