thermferm/one-wire.c

changeset 662
56c72393ca26
parent 661
8c1e7a52e24f
child 670
6c62e99ade00
equal deleted inserted replaced
661:8c1e7a52e24f 662:56c72393ca26
81 int found, i, rc, value, conv_time; 81 int found, i, rc, value, conv_time;
82 FILE *fp; 82 FILE *fp;
83 devices_list *device; 83 devices_list *device;
84 w1_list *dev_w1, *n_w1, *cur_w1 = NULL; 84 w1_list *dev_w1, *n_w1, *cur_w1 = NULL;
85 char buffer[25], w1type[10], *devfile = NULL; 85 char buffer[25], w1type[10], *devfile = NULL;
86 uint8_t state, output; 86 uint8_t state, output, newval;
87 87
88 SM_START(ScanNew) 88 SM_START(ScanNew)
89 89
90 SM_STATE(ScanNew) 90 SM_STATE(ScanNew)
91 91
235 mDelay(10); 235 mDelay(10);
236 if ((rc = read_w1(device->address, (char *)"state")) >= 0) /* Read PIO again */ 236 if ((rc = read_w1(device->address, (char *)"state")) >= 0) /* Read PIO again */
237 state = (unsigned int)rc; 237 state = (unsigned int)rc;
238 } 238 }
239 239
240 dev_w1->value = ((state & 0x04) >> 1) + (state & 0x01); 240 newval = ((state & 0x04) >> 1) + (state & 0x01);
241 syslog(LOG_NOTICE, "One-wire device %s-%d in %02x value %d", dev_w1->address, i, state, dev_w1->value); 241 if (newval != dev_w1->value) {
242 syslog(LOG_NOTICE, "One-wire device %s-%d in %02x value %d => %d", dev_w1->address, i, state, dev_w1->value, newval);
243 dev_w1->value = newval;
244 }
242 245
243 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 246 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
244 /* 247 /*
245 * Read PIOA or PIOB pin state bits 248 * Read PIOA or PIOB pin state bits
246 */ 249 */
272 if (cur_w1 != NULL) { 275 if (cur_w1 != NULL) {
273 276
274 if ((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) || 277 if ((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) ||
275 (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) { 278 (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) {
276 // syslog(LOG_NOTICE, "Reading %s", cur_w1->address); 279 // syslog(LOG_NOTICE, "Reading %s", cur_w1->address);
277 /*
278 * 1. Read sensor resolution. This is a present check too.
279 * 2. Fix resolution if wrong.
280 * 3. Read conversion time.
281 * 4. Open temperature for read.
282 * 5. Wait conversion time.
283 * 6. Read temperature.
284 */
285 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); 280 devfile = xstrcpy((char *)"/sys/bus/w1/devices/");
286 devfile = xstrcat(devfile, cur_w1->address); 281 devfile = xstrcat(devfile, cur_w1->address);
287 devfile = xstrcat(devfile, (char *)"/resolution"); 282 devfile = xstrcat(devfile, (char *)"/resolution");
288 if ((fp = fopen(devfile, "r+"))) { 283 if ((fp = fopen(devfile, "r+"))) {
289 if ((fgets(buffer, 25, fp))) { 284 if ((fgets(buffer, 25, fp))) {
312 307
313 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); 308 devfile = xstrcpy((char *)"/sys/bus/w1/devices/");
314 devfile = xstrcat(devfile, cur_w1->address); 309 devfile = xstrcat(devfile, cur_w1->address);
315 devfile = xstrcat(devfile, (char *)"/temperature"); 310 devfile = xstrcat(devfile, (char *)"/temperature");
316 if ((fp = fopen(devfile, "r"))) { 311 if ((fp = fopen(devfile, "r"))) {
317 syslog(LOG_NOTICE, "One-wire device %s temperature is open, delay %d", cur_w1->address, conv_time); 312 // syslog(LOG_NOTICE, "One-wire device %s temperature is open, delay %d", cur_w1->address, conv_time);
318 mDelay(conv_time); 313 mDelay(conv_time);
319 if ((fgets(buffer, 25, fp))) { 314 if ((fgets(buffer, 25, fp))) {
320 sscanf(buffer, "%d", &value); 315 sscanf(buffer, "%d", &value);
321 syslog(LOG_NOTICE, "One-wire device %s temperature read %d", cur_w1->address, value); 316 if (cur_w1->value != value)
317 syslog(LOG_NOTICE, "One-wire device %s temperature read %d => %d", cur_w1->address, cur_w1->value, value);
322 cur_w1->value = value; /* devices.c will pick this up */ 318 cur_w1->value = value; /* devices.c will pick this up */
323 } else { 319 } else {
324 syslog(LOG_NOTICE, "One-wire device %s temperature read error", cur_w1->address); 320 syslog(LOG_NOTICE, "One-wire device %s temperature read error", cur_w1->address);
325 } 321 }
326 fclose(fp); 322 fclose(fp);
341 } 337 }
342 if ((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) || 338 if ((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) ||
343 (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) 339 (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0))
344 break; 340 break;
345 } 341 }
346 syslog(LOG_NOTICE, "One-wire device %s next sensor %s", cur_w1->address, cur_w1->family); 342 // syslog(LOG_NOTICE, "One-wire device %s next sensor %s", cur_w1->address, cur_w1->family);
347 343
348 } else { 344 } else {
349 syslog(LOG_NOTICE, "cur_w1 == NULL"); 345 // syslog(LOG_NOTICE, "cur_w1 == NULL");
350 sleep(1); 346 mDelay(750);
351 } 347 }
352 348
353 SM_PROCEED(Missing); 349 SM_PROCEED(Missing);
354 350
355 SM_STATE(Missing) 351 SM_STATE(Missing)

mercurial