thermferm/one-wire.c

changeset 731
8b7c63bddf75
parent 728
da038d0bed04
child 732
b0e99e30a008
equal deleted inserted replaced
730:6eba006ed8f5 731:8b7c63bddf75
60 payload = xstrcat(payload, (char *)"\",\"present\":\""); 60 payload = xstrcat(payload, (char *)"\",\"present\":\"");
61 payload = xstrcat(payload, (char *)DEVPRESENT[dev_w1->present]); 61 payload = xstrcat(payload, (char *)DEVPRESENT[dev_w1->present]);
62 payload = xstrcat(payload, (char *)"\",\"value\":"); 62 payload = xstrcat(payload, (char *)"\",\"value\":");
63 snprintf(vbuf, 63, "%d", dev_w1->value); 63 snprintf(vbuf, 63, "%d", dev_w1->value);
64 payload = xstrcat(payload, vbuf); 64 payload = xstrcat(payload, vbuf);
65 payload = xstrcat(payload, (char *)",\"resolution\":"); 65 payload = xstrcat(payload, (char *)",\"rd_cache\":");
66 snprintf(vbuf, 63, "%d", dev_w1->resolution); 66 snprintf(vbuf, 63, "%d", dev_w1->rd_cache);
67 payload = xstrcat(payload, vbuf);
68 payload = xstrcat(payload, (char *)",\"wr_cache\":");
69 snprintf(vbuf, 63, "%d", dev_w1->wr_cache);
67 payload = xstrcat(payload, vbuf); 70 payload = xstrcat(payload, vbuf);
68 payload = xstrcat(payload, (char *)",\"timestamp\":"); 71 payload = xstrcat(payload, (char *)",\"timestamp\":");
69 snprintf(vbuf, 63, "%ld", (long)dev_w1->timestamp); 72 snprintf(vbuf, 63, "%ld", (long)dev_w1->timestamp);
70 payload = xstrcat(payload, vbuf); 73 payload = xstrcat(payload, vbuf);
71 payload = xstrcat(payload, (char *)"}"); 74 payload = xstrcat(payload, (char *)"}");
181 found = TRUE; 184 found = TRUE;
182 if (dev_w1->present != DEVPRESENT_YES) { 185 if (dev_w1->present != DEVPRESENT_YES) {
183 syslog(LOG_NOTICE, "One-wire device %s is back", buffer); 186 syslog(LOG_NOTICE, "One-wire device %s is back", buffer);
184 pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]); 187 pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]);
185 dev_w1->present = DEVPRESENT_YES; 188 dev_w1->present = DEVPRESENT_YES;
186 dev_w1->resolution = 0; /* Might be wrong, so reset */ 189 dev_w1->rd_cache = 0; /* Might be wrong, so reset */
190 dev_w1->wr_cache = 0; /* Not used here */
187 dev_w1->timestamp = time(NULL); 191 dev_w1->timestamp = time(NULL);
188 pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]); 192 pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]);
189 changed = true; 193 changed = true;
190 device_present(dev_w1->address, DEVPRESENT_YES); 194 device_present(dev_w1->address, DEVPRESENT_YES);
191 } 195 }
199 n_w1->address = xstrcpy(buffer); 203 n_w1->address = xstrcpy(buffer);
200 strncpy(n_w1->family, buffer, 2); 204 strncpy(n_w1->family, buffer, 2);
201 n_w1->family[2] = '\0'; 205 n_w1->family[2] = '\0';
202 n_w1->present = DEVPRESENT_YES; 206 n_w1->present = DEVPRESENT_YES;
203 n_w1->value = (strcmp(w1type, (char *)"3a") == 0) ? 3:-1; 207 n_w1->value = (strcmp(w1type, (char *)"3a") == 0) ? 3:-1;
204 n_w1->resolution = 0; 208 n_w1->rd_cache = n_w1->wr_cache = 0;
205 n_w1->timestamp = time(NULL); 209 n_w1->timestamp = time(NULL);
206 changed = true; 210 changed = true;
207 211
208 pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]); 212 pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]);
209 if (w1_devices == NULL) { 213 if (w1_devices == NULL) {
290 write_w1(device->address, (char *)"output", output); 294 write_w1(device->address, (char *)"output", output);
291 mDelay(10); 295 mDelay(10);
292 if ((rc = read_w1(device->address, (char *)"state")) >= 0) /* Read PIO again */ 296 if ((rc = read_w1(device->address, (char *)"state")) >= 0) /* Read PIO again */
293 state = (unsigned int)rc; 297 state = (unsigned int)rc;
294 } 298 }
299 dev_w1->rd_cache = state;
295 300
296 newval = ((state & 0x04) >> 1) + (state & 0x01); 301 newval = ((state & 0x04) >> 1) + (state & 0x01);
302 dev_w1->wr_cache = newval;
297 if (newval != dev_w1->value) { 303 if (newval != dev_w1->value) {
298 syslog(LOG_NOTICE, "One-wire device %s-%d in %02x value %d => %d", dev_w1->address, i, state, dev_w1->value, newval); 304 syslog(LOG_NOTICE, "One-wire device %s-%d in %02x value %d => %d", dev_w1->address, i, state, dev_w1->value, newval);
299 dev_w1->value = newval; 305 dev_w1->value = newval;
300 dev_w1->timestamp = time(NULL); 306 dev_w1->timestamp = time(NULL);
301 changed = true; 307 changed = true;
327 } else if (device->subdevice == 1) { 333 } else if (device->subdevice == 1) {
328 newval = (newval & 0xfd); 334 newval = (newval & 0xfd);
329 newval |= (device->value) ? 0x00 : 0x02; 335 newval |= (device->value) ? 0x00 : 0x02;
330 } 336 }
331 337
338 dev_w1->wr_cache = newval;
332 if (output != newval) { 339 if (output != newval) {
333 if ((write_w1(dev_w1->address, (char *)"output", newval)) == 0) { 340 if ((write_w1(dev_w1->address, (char *)"output", newval)) == 0) {
334 syslog(LOG_NOTICE, "One-wire device %s-%d out %02x -> %02x", dev_w1->address, i, output, newval); 341 syslog(LOG_NOTICE, "One-wire device %s-%d out %02x -> %02x", dev_w1->address, i, output, newval);
335 dev_w1->value = newval; 342 dev_w1->value = newval;
336 dev_w1->timestamp = time(NULL); 343 dev_w1->timestamp = time(NULL);
363 370
364 /* 371 /*
365 * Check and correct resolution. Only do this for new sensors 372 * Check and correct resolution. Only do this for new sensors
366 * or known sensors that are plugged in again. 373 * or known sensors that are plugged in again.
367 */ 374 */
368 if (cur_w1->resolution != W1_TEMP_RESOLUTION) { 375 if (cur_w1->rd_cache != W1_TEMP_RESOLUTION) {
369 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); 376 devfile = xstrcpy((char *)"/sys/bus/w1/devices/");
370 devfile = xstrcat(devfile, cur_w1->address); 377 devfile = xstrcat(devfile, cur_w1->address);
371 devfile = xstrcat(devfile, (char *)"/resolution"); 378 devfile = xstrcat(devfile, (char *)"/resolution");
372 if ((fp = fopen(devfile, "r+"))) { 379 if ((fp = fopen(devfile, "r+"))) {
373 if ((fgets(buffer, 25, fp))) { 380 if ((fgets(buffer, 25, fp))) {
377 syslog(LOG_NOTICE, "One-wire device %s set resolution from %d to %d", cur_w1->address, value, W1_TEMP_RESOLUTION); 384 syslog(LOG_NOTICE, "One-wire device %s set resolution from %d to %d", cur_w1->address, value, W1_TEMP_RESOLUTION);
378 fseek(fp, 0L, SEEK_SET); 385 fseek(fp, 0L, SEEK_SET);
379 sprintf(buffer, "%d", W1_TEMP_RESOLUTION); 386 sprintf(buffer, "%d", W1_TEMP_RESOLUTION);
380 fputs(buffer, fp); 387 fputs(buffer, fp);
381 } 388 }
382 cur_w1->resolution = W1_TEMP_RESOLUTION; 389 cur_w1->rd_cache = W1_TEMP_RESOLUTION;
383 } 390 }
384 fclose(fp); 391 fclose(fp);
385 } else { 392 } else {
386 syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno)); 393 syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno));
387 } 394 }

mercurial