# HG changeset patch # User Michiel Broek # Date 1712843929 -7200 # Node ID 6cabc02f4c8db800df81552adae0df633bac7a92 # Parent 0c084b876a2c089ab4d6bf90389f3b17b6574357 Code cleanup diff -r 0c084b876a2c -r 6cabc02f4c8d thermferm/devices.c --- a/thermferm/devices.c Thu Apr 11 15:01:46 2024 +0200 +++ b/thermferm/devices.c Thu Apr 11 15:58:49 2024 +0200 @@ -65,12 +65,12 @@ void dht11Read(char *address) { int temp, hum; - int fd, rc, err; + int fd, rc, err, oldstate; char buffer[25], *dhtpath = NULL; dht11_temperature = -1; dht11_humidity = -1; - dht11_state = DEVPRESENT_UNDEF; + oldstate = dht11_state; dhtpath = xstrcpy((char *)"/sys/bus/iio/devices/"); dhtpath = xstrcat(dhtpath, address); dhtpath = xstrcat(dhtpath, (char *)"/in_temp_input"); @@ -95,7 +95,7 @@ } else { dht11_state = DEVPRESENT_ERROR; } - syslog(LOG_NOTICE, "DHT11 read temperature: %d %s", err, strerror(err)); + syslog(LOG_NOTICE, "DHT11 read temperature: %s", strerror(err)); } else { sscanf(buffer, "%d", &temp); dht11_temperature = temp; @@ -122,7 +122,7 @@ } else { dht11_state = DEVPRESENT_ERROR; } - syslog(LOG_NOTICE, "DHT11 read humidity: %d %s", err, strerror(err)); + syslog(LOG_NOTICE, "DHT11 read humidity: %s", strerror(err)); dht11_temperature = -1; /* Make invalid */ } else { sscanf(buffer, "%d", &hum); @@ -134,7 +134,8 @@ dhtpath = NULL; } - syslog(LOG_NOTICE, "dht11 t:%d h:%d state:%d", dht11_temperature, dht11_humidity, dht11_state); + if (oldstate != dht11_state) + syslog(LOG_NOTICE, "dht11 t:%d h:%d state:%d", dht11_temperature, dht11_humidity, dht11_state); } @@ -291,35 +292,11 @@ #endif if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) { if (strncmp(device->address, (char *)"3a", 2) == 0) { - /* - * DS2413. First read state so that we can preserve the state of - * the "other" PIO channel. To make things a bit more complicated - * the bits in the state register differ from the output register. - */ -// uint8_t state, output; - -// if ((rc = read_w1(device->address, (char *)"state")) >= 0) { -// state = (unsigned int)rc; -// output = (state & 0x01) + ((state & 0x04) >> 1); - -// if (device->subdevice == 0) { -// output = (output & 0xfe); -// output |= (value == 0) ? 0x01 : 0x00; -// } else if (device->subdevice == 1) { -// output = (output & 0xfd); -// output |= (value == 0) ? 0x02 : 0x00; -// } else { -// output = 0xff; -// } - -// if ((write_w1(device->address, (char *)"output", output)) == 0) { - syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment); -// pthread_mutex_lock(&mutexes[LOCK_DEVICES]); - device->value = (value == 0) ? 0 : 1; - device->timestamp = time(NULL); -// pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); -// } -// } + syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment); +// pthread_mutex_lock(&mutexes[LOCK_DEVICES]); + device->value = (value == 0) ? 0 : 1; + device->timestamp = time(NULL); +// pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); } } @@ -367,8 +344,6 @@ if (uuid == NULL) return 0; -// pthread_mutex_lock(&mutexes[LOCK_DEVICES]); - for (device = Config.devices; device; device = device->next) { if (! strcmp(uuid, device->uuid)) { present = device->present; @@ -377,13 +352,11 @@ } else { tmp = 0; } -// pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); *value = tmp; return present; } } -// pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); return DEVPRESENT_NO; } @@ -721,47 +694,14 @@ #ifdef USE_SIMULATOR simulator_list *simulator; #endif -// char *addr = NULL, line1[60], line2[60], *p = NULL; -// FILE *fp; int found; time_t now; my_devices_state = 1; syslog(LOG_NOTICE, "Thread my_devices_loop started"); - -#ifdef HAVE_WIRINGPI_H -// if ((rc = piHiPri(50))) -// syslog(LOG_NOTICE, "my_devices_loop: piHiPri(50) rc=%d", rc); -#endif - dht11_next = time(NULL); /* - * Set the temperature sensors to 12 bits resolution and write it in EEPROM - */ -// for (device = Config.devices; device; device = device->next) { -// if ((device->type == DEVTYPE_W1) && -// ((strncmp(device->address, (char *)"10", 2) == 0) || - // (strncmp(device->address, (char *)"22", 2) == 0) || -// (strncmp(device->address, (char *)"28", 2) == 0) || -// (strncmp(device->address, (char *)"3b", 2) == 0) || -// (strncmp(device->address, (char *)"42", 2) == 0))) { -// addr = xstrcpy((char *)"/sys/bus/w1/devices/"); -// addr = xstrcat(addr, device->address); -// addr = xstrcat(addr, (char *)"/w1_slave"); -// if ((fp = fopen(addr, "w"))) { -// rc = fprintf(fp, "12\n0\n"); // According to the kernel documentation. Seems to work. -// fclose(fp); -// if (rc != 5) { -// syslog(LOG_NOTICE, "Program 12 bits resolution error rc=%d for %s", rc, addr); -// } -// } -// free(addr); -// addr = NULL; -// } -// } - - /* * Loop forever until the external shutdown variable is set. */ for (;;) { @@ -773,7 +713,6 @@ if (my_devices_shutdown) break; -//syslog(LOG_NOTICE, "device type %d address %s-%d comment %s", device->type, device->address, device->subdevice, device->comment); switch (device->type) { case DEVTYPE_W1: @@ -789,7 +728,6 @@ found = FALSE; for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) { if (strcmp(device->address, dev_w1->address) == 0) { -// syslog(LOG_NOTICE, "sensor %s value %d", dev_w1->address, dev_w1->value); found = TRUE; if ((dev_w1->value == -1) || (dev_w1->value < -55000)) { if (device->present != DEVPRESENT_ERROR) diff -r 0c084b876a2c -r 6cabc02f4c8d thermferm/one-wire.c --- a/thermferm/one-wire.c Thu Apr 11 15:01:46 2024 +0200 +++ b/thermferm/one-wire.c Thu Apr 11 15:58:49 2024 +0200 @@ -142,7 +142,6 @@ n_w1->family[2] = '\0'; n_w1->present = DEVPRESENT_YES; n_w1->value = (strcmp(w1type, (char *)"3a") == 0) ? 3:-1; -// n_w1->subdevices = (strcmp(w1type, (char *)"3a") == 0) ? 2:1; n_w1->timestamp = time(NULL); pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]); @@ -209,7 +208,6 @@ for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) { if (strcmp(dev_w1->family, "3a") == 0) { -// syslog(LOG_NOTICE, "ds2413 %s", dev_w1->address); for (i = 0; i < 2; i++) { for (device = Config.devices; device; device = device->next) { if ((strcmp(dev_w1->address, device->address) == 0) && (device->subdevice == i) && (device->direction == DEVDIR_IN_BIN)) { @@ -272,15 +270,11 @@ } if (output != newval) { - if ((rc = write_w1(dev_w1->address, (char *)"output", newval)) == 0) { + if ((write_w1(dev_w1->address, (char *)"output", newval)) == 0) { syslog(LOG_NOTICE, "One-wire device %s-%d out %02x -> %02x", dev_w1->address, i, output, newval); dev_w1->value = newval; - } else { - syslog(LOG_NOTICE, "One-wire device %s-%d write output rc=%d", dev_w1->address, i, rc); } } - } else { - syslog(LOG_NOTICE, "One-wire device %s-%d read state rc=%d", dev_w1->address, i, rc); } } } /* for (device = Config.devices; ... */ @@ -303,7 +297,6 @@ if ((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) || (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) { -// syslog(LOG_NOTICE, "Reading %s", cur_w1->address); devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); devfile = xstrcat(devfile, cur_w1->address); devfile = xstrcat(devfile, (char *)"/resolution"); @@ -366,10 +359,8 @@ (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) break; } -// syslog(LOG_NOTICE, "One-wire device %s next sensor %s", cur_w1->address, cur_w1->family); } else { -// syslog(LOG_NOTICE, "cur_w1 == NULL"); mDelay(750); }