# HG changeset patch # User Michiel Broek # Date 1714916943 -7200 # Node ID da038d0bed045dd8400d2b5a49189d82b3b8dea2 # Parent 78744b7e602124a78ed76dee8fa2b36dde366bef Version 0.9.20a1. Analyse threads cpu usage. One-wire added a resolution flag to prevent continuous resolution checks, now only for new sensors and replugged known sensors. Added two 40 sSec delays in the state machine. In the panel thread moved a few locks so these expensive calls are called only when needed. diff -r 78744b7e6021 -r da038d0bed04 MIGRATION --- a/MIGRATION Sat May 04 19:10:03 2024 +0200 +++ b/MIGRATION Sun May 05 15:49:03 2024 +0200 @@ -26,6 +26,16 @@ 5. my_simulator_loop (if enabled). +After one day: + +root 9795 1 9795 3 7 May04 ? 00:29:27 /usr/bin/thermferm Total +root 9795 1 9796 0 7 May04 ? 00:00:09 /usr/bin/thermferm mqtt ? +root 9795 1 9797 0 7 May04 ? 00:00:01 /usr/bin/thermferm Websocket +root 9795 1 9798 2 7 May04 ? 00:19:09 /usr/bin/thermferm my_one_wire_loop +root 9795 1 9799 0 7 May04 ? 00:04:43 /usr/bin/thermferm my_devices_loop started +root 9795 1 9800 0 7 May04 ? 00:00:00 /usr/bin/thermferm my_server_loop +root 9795 1 9801 0 7 May04 ? 00:04:45 /usr/bin/thermferm my_panel_loop + Steps to do. ------------ diff -r 78744b7e6021 -r da038d0bed04 configure --- a/configure Sat May 04 19:10:03 2024 +0200 +++ b/configure Sun May 05 15:49:03 2024 +0200 @@ -2104,7 +2104,7 @@ PACKAGE="mbsePi-apps" -VERSION="0.9.19" +VERSION="0.9.20a1" COPYRIGHT="Copyright (C) 2014-2024 Michiel Broek, All Rights Reserved" CYEARS="2014-2024" diff -r 78744b7e6021 -r da038d0bed04 configure.ac --- a/configure.ac Sat May 04 19:10:03 2024 +0200 +++ b/configure.ac Sun May 05 15:49:03 2024 +0200 @@ -8,7 +8,7 @@ dnl General settings dnl After changeing the version number, run autoconf! PACKAGE="mbsePi-apps" -VERSION="0.9.19" +VERSION="0.9.20a1" COPYRIGHT="Copyright (C) 2014-2024 Michiel Broek, All Rights Reserved" CYEARS="2014-2024" AC_SUBST(PACKAGE) diff -r 78744b7e6021 -r da038d0bed04 thermferm/one-wire.c --- a/thermferm/one-wire.c Sat May 04 19:10:03 2024 +0200 +++ b/thermferm/one-wire.c Sun May 05 15:49:03 2024 +0200 @@ -62,6 +62,9 @@ payload = xstrcat(payload, (char *)"\",\"value\":"); snprintf(vbuf, 63, "%d", dev_w1->value); payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)",\"resolution\":"); + snprintf(vbuf, 63, "%d", dev_w1->resolution); + payload = xstrcat(payload, vbuf); payload = xstrcat(payload, (char *)",\"timestamp\":"); snprintf(vbuf, 63, "%ld", (long)dev_w1->timestamp); payload = xstrcat(payload, vbuf); @@ -180,6 +183,7 @@ syslog(LOG_NOTICE, "One-wire device %s is back", buffer); pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]); dev_w1->present = DEVPRESENT_YES; + dev_w1->resolution = 0; /* Might be wrong, so reset */ dev_w1->timestamp = time(NULL); pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]); changed = true; @@ -197,6 +201,7 @@ n_w1->family[2] = '\0'; n_w1->present = DEVPRESENT_YES; n_w1->value = (strcmp(w1type, (char *)"3a") == 0) ? 3:-1; + n_w1->resolution = 0; n_w1->timestamp = time(NULL); changed = true; @@ -250,6 +255,7 @@ devfile = NULL; } + mDelay(40); SM_PROCEED(Read2413); SM_STATE(Read2413) @@ -338,6 +344,7 @@ } } + mDelay(40); SM_PROCEED(ReadTemp); SM_STATE(ReadTemp) @@ -352,57 +359,73 @@ if (cur_w1 != NULL) { 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)) && - (cur_w1->present == DEVPRESENT_YES)) { + (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) && (cur_w1->present == DEVPRESENT_YES)) { + + /* + * Check and correct resolution. Only do this for new sensors + * or known sensors that are plugged in again. + */ + if (cur_w1->resolution != W1_TEMP_RESOLUTION) { + devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); + devfile = xstrcat(devfile, cur_w1->address); + devfile = xstrcat(devfile, (char *)"/resolution"); + if ((fp = fopen(devfile, "r+"))) { + if ((fgets(buffer, 25, fp))) { + sscanf(buffer, "%d", &value); + /* If device is removed, value is negative (errno?) */ + if ((value > 0) && (value != W1_TEMP_RESOLUTION)) { + syslog(LOG_NOTICE, "One-wire device %s set resolution from %d to %d", cur_w1->address, value, W1_TEMP_RESOLUTION); + fseek(fp, 0L, SEEK_SET); + sprintf(buffer, "%d", W1_TEMP_RESOLUTION); + fputs(buffer, fp); + } + cur_w1->resolution = W1_TEMP_RESOLUTION; + } + fclose(fp); + } else { + syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno)); + } + free(devfile); + } + + /* + * Set conversion wait time. Skip this?? + */ + conv_time = 760; devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); devfile = xstrcat(devfile, cur_w1->address); - devfile = xstrcat(devfile, (char *)"/resolution"); - if ((fp = fopen(devfile, "r+"))) { - if ((fgets(buffer, 25, fp))) { - sscanf(buffer, "%d", &value); - /* If device is removed, value is negative (errno?) */ - if ((value > 0) && (value != W1_TEMP_RESOLUTION)) { - syslog(LOG_NOTICE, "One-wire device %s set resolution from %d to %d", cur_w1->address, value, W1_TEMP_RESOLUTION); - fseek(fp, 0L, SEEK_SET); - sprintf(buffer, "%d", W1_TEMP_RESOLUTION); - fputs(buffer, fp); - } - } - fclose(fp); - free(devfile); + devfile = xstrcat(devfile, (char *)"/conv_time"); + if ((fp = fopen(devfile, "r"))) { + if ((fgets(buffer, 25, fp))) { + sscanf(buffer, "%d", &conv_time); + } + fclose(fp); + } + free(devfile); - conv_time = 760; - devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); - devfile = xstrcat(devfile, cur_w1->address); - devfile = xstrcat(devfile, (char *)"/conv_time"); - if ((fp = fopen(devfile, "r"))) { - if ((fgets(buffer, 25, fp))) { - sscanf(buffer, "%d", &conv_time); + /* + * Read temperature for one sensor. + * Bulk read does not work for unknown reason. + * It may be because there are also DS2413 devices. + */ + devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); + devfile = xstrcat(devfile, cur_w1->address); + devfile = xstrcat(devfile, (char *)"/temperature"); + if ((fp = fopen(devfile, "r"))) { + mDelay(conv_time); + if ((fgets(buffer, 25, fp))) { + sscanf(buffer, "%d", &value); + if (cur_w1->value != value) { + cur_w1->timestamp = time(NULL); + changed = true; + if (debug) + syslog(LOG_NOTICE, "One-wire device %s temperature read %d => %d", cur_w1->address, cur_w1->value, value); } - fclose(fp); - } - free(devfile); - - devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); - devfile = xstrcat(devfile, cur_w1->address); - devfile = xstrcat(devfile, (char *)"/temperature"); - if ((fp = fopen(devfile, "r"))) { - mDelay(conv_time); - if ((fgets(buffer, 25, fp))) { - sscanf(buffer, "%d", &value); - if (cur_w1->value != value) { - cur_w1->timestamp = time(NULL); - changed = true; - if (debug) - syslog(LOG_NOTICE, "One-wire device %s temperature read %d => %d", cur_w1->address, cur_w1->value, value); - } - cur_w1->value = value; /* devices.c will pick this up */ - } else { - syslog(LOG_NOTICE, "One-wire device %s temperature read error", cur_w1->address); - } - fclose(fp); - } - + cur_w1->value = value; /* devices.c will pick this up */ + } else { + syslog(LOG_NOTICE, "One-wire device %s temperature read error", cur_w1->address); + } + fclose(fp); } else { syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno)); } diff -r 78744b7e6021 -r da038d0bed04 thermferm/panel.c --- a/thermferm/panel.c Sat May 04 19:10:03 2024 +0200 +++ b/thermferm/panel.c Sun May 05 15:49:03 2024 +0200 @@ -188,9 +188,11 @@ /* * Any key is pressed. */ - pthread_mutex_lock(&mutexes[LOCK_LCD]); - setBacklight(1); - pthread_mutex_unlock(&mutexes[LOCK_LCD]); + if (Backlight == 0) { + pthread_mutex_lock(&mutexes[LOCK_LCD]); + setBacklight(1); + pthread_mutex_unlock(&mutexes[LOCK_LCD]); + } Backlight = LCD_SLEEP; menutimer = 0; } else { @@ -214,15 +216,15 @@ Backlight--; } - pthread_mutex_lock(&mutexes[LOCK_MENU]); if (setupmenu != MENU_NONE) { + pthread_mutex_lock(&mutexes[LOCK_MENU]); if (menutimer < MENU_TIMEOUT) menutimer++; else { setupmenu = MENU_NONE; } + pthread_mutex_unlock(&mutexes[LOCK_MENU]); } - pthread_mutex_unlock(&mutexes[LOCK_MENU]); } } diff -r 78744b7e6021 -r da038d0bed04 thermferm/thermferm.h --- a/thermferm/thermferm.h Sat May 04 19:10:03 2024 +0200 +++ b/thermferm/thermferm.h Sun May 05 15:49:03 2024 +0200 @@ -330,6 +330,7 @@ char family[3]; ///< Device family int present; ///< Present on bus int value; ///< Last value + int resolution; ///< Actual resolution time_t timestamp; ///< Last seen } w1_list;