Thu, 14 Aug 2014 22:43:41 +0200
Added thread locks for device processing
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /***************************************************************************** |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * Copyright (C) 2014 |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | * Michiel Broek <mbroek at mbse dot eu> |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | * |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | * This file is part of the mbsePi-apps |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | * |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | * This is free software; you can redistribute it and/or modify it |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | * under the terms of the GNU General Public License as published by the |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | * Free Software Foundation; either version 2, or (at your option) any |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | * later version. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | * |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | * mbsePi-apps is distributed in the hope that it will be useful, but |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | * General Public License for more details. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | * |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | * You should have received a copy of the GNU General Public License |
213
2317b8d644fa
Code cleanup, streamlined error messages.
Michiel Broek <mbroek@mbse.eu>
parents:
209
diff
changeset
|
19 | * along with ThermFerm; see the file COPYING. If not, write to the Free |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | *****************************************************************************/ |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | |
106 | 23 | #include "lock.h" |
24 | #include "logger.h" | |
25 | #include "rdconfig.h" | |
160 | 26 | #include "devices.h" |
106 | 27 | #include "server.h" |
41
f534ace74eea
Code preparation for client/server communication
Michiel Broek <mbroek@mbse.eu>
parents:
40
diff
changeset
|
28 | #include "thermferm.h" |
106 | 29 | #include "lcd-pcf8574.h" |
30 | #include "lcd-buffer.h" | |
198
20716bcff2b0
Added dependencies for panel
Michiel Broek <mbroek@mbse.eu>
parents:
195
diff
changeset
|
31 | #include "panel.h" |
106 | 32 | #include "futil.h" |
33 | #include "xutil.h" | |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | |
75
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
36 | int my_shutdown = FALSE; |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | static pid_t pgrp, mypid; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | |
75
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
39 | extern int debug; |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | extern sys_config Config; |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
41 | #ifdef HAVE_WIRINGPI_H |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | extern int lcdHandle; |
205 | 43 | int setupmenu = MENU_NONE; |
238 | 44 | units_list *current_unit = NULL; /* In panel editor this points to the current unit. */ |
205 | 45 | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
46 | #endif |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | int lcdupdate; |
74 | 48 | #ifndef HAVE_WIRINGPI_H |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
49 | pthread_t threads[4]; |
74 | 50 | #endif |
104 | 51 | extern const char UNITMODE[5][8]; |
195
b34a1b2421fb
Reset output when a different profile is selected. Better startup logging about the unit state. Added Beer, Fridge and partial Profile settings from the liveview weeb screen.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
52 | extern const char PROFSTATE[4][6]; |
104 | 53 | |
40
dafbbd5e9922
Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
54 | |
41
f534ace74eea
Code preparation for client/server communication
Michiel Broek <mbroek@mbse.eu>
parents:
40
diff
changeset
|
55 | int server(void); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | void help(void); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | void die(int); |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
58 | #ifdef HAVE_WIRINGPI_H |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
59 | void sendRCswitch(char *, int); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
60 | void stopLCD(void); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
61 | void stopRCswitch(void); |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
62 | #endif |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
63 | |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
65 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
66 | void help(void) |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
67 | { |
41
f534ace74eea
Code preparation for client/server communication
Michiel Broek <mbroek@mbse.eu>
parents:
40
diff
changeset
|
68 | fprintf(stdout, "mbsePi-apps thermferm v%s starting\n\n", VERSION); |
f534ace74eea
Code preparation for client/server communication
Michiel Broek <mbroek@mbse.eu>
parents:
40
diff
changeset
|
69 | fprintf(stdout, "Usage: thermferm [-d] [-h]\n"); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
70 | fprintf(stdout, " -d --debug Debug and run in foreground\n"); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
71 | fprintf(stdout, " -h --help Display this help\n"); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
73 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
75 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | void die(int onsig) |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
77 | { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
78 | switch (onsig) { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
79 | case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | break; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
81 | case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
82 | break; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
84 | break; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
85 | default: syslog(LOG_NOTICE, "die() on signal %d", onsig); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
86 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | |
75
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
88 | my_shutdown = TRUE; |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
89 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
90 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
91 | |
238 | 92 | |
93 | void show_mode(void) | |
94 | { | |
95 | char buf[21]; | |
96 | ||
97 | snprintf(buf, 20, "Old mode %s", UNITMODE[current_unit->mode]); | |
98 | lcdPuts(lcdHandle, buf); | |
99 | lcdPosition(lcdHandle, 0, 1); | |
100 | } | |
101 | ||
102 | ||
103 | ||
215
5ad534c79a22
Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents:
214
diff
changeset
|
104 | #ifdef HAVE_WIRINGPI_H |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
105 | void go_menu(int menu) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
106 | { |
238 | 107 | char buf[21]; |
108 | ||
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
109 | lcdClear(lcdHandle); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
110 | lcdPosition(lcdHandle, 0, 0); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
111 | setupmenu = menu; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
112 | |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
113 | switch (menu) { |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
114 | case MENU_NONE: lcd_buf_show(); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
115 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
116 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
117 | case MENU_TOP_DEFAULT: lcdPuts(lcdHandle, "Setup mode."); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
118 | lcdPosition(lcdHandle, 0, 1); |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
119 | lcdPuts(lcdHandle, "Up&Down = Escape"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
120 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
121 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
122 | case MENU_TOP_UNITS: lcdPuts(lcdHandle, "Select units"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
123 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
124 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
125 | case MENU_UNITS: lcdPuts(lcdHandle, "Choose unit:"); |
238 | 126 | if ((current_unit == NULL) && Config.units) { |
127 | /* | |
128 | * First time in this menu, select the first unit. | |
129 | */ | |
130 | current_unit = Config.units; | |
131 | lcdPosition(lcdHandle, 0, 1); | |
132 | lcdPuts(lcdHandle, Config.units->name); | |
133 | } | |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
134 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
135 | |
238 | 136 | case MENU_MODE_OFF: show_mode(); |
137 | lcdPuts(lcdHandle, "New mode OFF"); | |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
138 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
139 | |
238 | 140 | case MENU_MODE_NONE: show_mode(); |
141 | lcdPuts(lcdHandle, "New mode NONE"); | |
142 | break; | |
143 | ||
144 | case MENU_NONE_HEAT: snprintf(buf, 20, "Set heater %s", current_unit->heater_state ? "OFF":"ON"); | |
145 | lcdPuts(lcdHandle, buf); | |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
146 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
147 | |
238 | 148 | case MENU_NONE_COOL: snprintf(buf, 20, "Set cooler %s", current_unit->cooler_state ? "OFF":"ON"); |
149 | lcdPuts(lcdHandle, buf); | |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
150 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
151 | |
238 | 152 | case MENU_NONE_FAN: snprintf(buf, 20, "Set fan %s", current_unit->fan_state ? "OFF":"ON"); |
153 | lcdPuts(lcdHandle, buf); | |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
154 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
155 | |
238 | 156 | case MENU_MODE_BEER: show_mode(); |
157 | lcdPuts(lcdHandle, "New mode BEER"); | |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
158 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
159 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
160 | case MENU_BEER_TEMP: lcdPuts(lcdHandle, "Set beer temp"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
161 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
162 | |
238 | 163 | case MENU_MODE_FRIDGE: show_mode(); |
164 | lcdPuts(lcdHandle, "New mode FRIDGE"); | |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
165 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
166 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
167 | case MENU_FRIDGE_TEMP: lcdPuts(lcdHandle, "Set fridge temp"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
168 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
169 | |
238 | 170 | case MENU_MODE_PROFILE: show_mode(); |
171 | lcdPuts(lcdHandle, "New mode PROFILE"); | |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
172 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
173 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
174 | case MENU_PROFILE_SELECT: lcdPuts(lcdHandle, "Select profile"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
175 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
176 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
177 | case MENU_PROFILE_START: lcdPuts(lcdHandle, "Start profile"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
178 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
179 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
180 | case MENU_PROFILE_PAUSE: lcdPuts(lcdHandle, "Pause profile"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
181 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
182 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
183 | case MENU_PROFILE_ABORT: lcdPuts(lcdHandle, "Abort profile"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
184 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
185 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
186 | case MENU_PROFILE_RESUME: lcdPuts(lcdHandle, "Resume profile"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
187 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
188 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
189 | case MENU_PROFILE_GOOFF: lcdPuts(lcdHandle, "Set profile OFF"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
190 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
191 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
192 | case MENU_TOP_SYS: lcdPuts(lcdHandle, "System menu"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
193 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
194 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
195 | case MENU_SYS_HALT: lcdPuts(lcdHandle, "Halt system"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
196 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
197 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
198 | case MENU_SYS_REBOOT: lcdPuts(lcdHandle, "Reboot system"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
199 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
200 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
201 | case MENU_SYS_MASH: lcdPuts(lcdHandle, "Start Mash program"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
202 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
203 | |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
204 | case MENU_SYS_THERMS: lcdPuts(lcdHandle, "Start Thermometers"); |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
205 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
206 | } |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
207 | } |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
208 | |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
209 | |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
210 | |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
211 | void stopLCD(void) |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
212 | { |
223
14700edd2a67
Removed LCD shadow copy buffer.
Michiel Broek <mbroek@mbse.eu>
parents:
221
diff
changeset
|
213 | lcdClear(lcdHandle); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
214 | setBacklight(0); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
215 | } |
238 | 216 | |
217 | ||
218 | ||
219 | /* | |
220 | * Change mode of current_unit | |
221 | */ | |
222 | void change_mode(int mode) | |
223 | { | |
224 | if ((current_unit->mode == UNITMODE_OFF) && (mode != UNITMODE_OFF)) | |
225 | initlog(current_unit->name); | |
226 | syslog(LOG_NOTICE, "Mode from %s to %s via panel interface", UNITMODE[current_unit->mode], UNITMODE[mode]); | |
227 | current_unit->mode = mode; | |
228 | /* Allways turn everything off after a mode change */ | |
229 | current_unit->PID_I_err = current_unit->PID_err_old = 0.0; | |
230 | current_unit->heater_state = current_unit->cooler_state = current_unit->fan_state = 0; | |
231 | device_out(current_unit->heater_address, current_unit->heater_state); | |
232 | device_out(current_unit->cooler_address, current_unit->cooler_state); | |
233 | device_out(current_unit->fan_address, current_unit->fan_state); | |
234 | if (current_unit->mode == UNITMODE_PROFILE) { | |
235 | /* | |
236 | * Set a sane default until it will be overruled by the | |
237 | * main processing loop. | |
238 | */ | |
239 | current_unit->prof_target = 20.0; | |
240 | } | |
241 | } | |
242 | ||
243 | ||
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
244 | #endif |
28
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
245 | |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
246 | |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
247 | |
207 | 248 | int read_sensor(char *address, int *val) |
182 | 249 | { |
250 | devices_list *device; | |
251 | int tmp; | |
252 | ||
253 | for (device = Config.devices; device; device = device->next) { | |
254 | if (strcmp(address, device->uuid) == 0) { | |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
238
diff
changeset
|
255 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
238
diff
changeset
|
256 | piLock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
238
diff
changeset
|
257 | #endif |
209
c5b1dfd83e81
Added offset value for sensor that need it
Michiel Broek <mbroek@mbse.eu>
parents:
207
diff
changeset
|
258 | tmp = device->value + device->offset; |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
238
diff
changeset
|
259 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
238
diff
changeset
|
260 | piUnlock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
238
diff
changeset
|
261 | #endif |
182 | 262 | *val = tmp; |
263 | return device->present; | |
264 | } | |
265 | } | |
266 | ||
267 | return DEVPRESENT_NO; | |
268 | } | |
269 | ||
270 | ||
271 | ||
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
272 | int main(int argc, char *argv[]) |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
273 | { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
274 | int rc, c, i; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
275 | pid_t frk; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
276 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
277 | while (1) { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
278 | int option_index = 0; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
279 | static struct option long_options[] = { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
280 | {"debug", 0, 0, 'c'}, |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
281 | {"help", 0, 0, 'h'}, |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
282 | {0, 0, 0, 0} |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
283 | }; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
284 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
285 | c = getopt_long(argc, argv, "dh", long_options, &option_index); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
286 | if (c == -1) |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
287 | break; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
288 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
289 | switch (c) { |
75
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
290 | case 'd': debug = TRUE; |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
291 | break; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
292 | case 'h': help(); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
293 | return 1; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
294 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
295 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
296 | |
41
f534ace74eea
Code preparation for client/server communication
Michiel Broek <mbroek@mbse.eu>
parents:
40
diff
changeset
|
297 | openlog("thermferm", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); |
f534ace74eea
Code preparation for client/server communication
Michiel Broek <mbroek@mbse.eu>
parents:
40
diff
changeset
|
298 | syslog(LOG_NOTICE, "mbsePi-apps thermferm v%s starting", VERSION); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
299 | if (debug) |
41
f534ace74eea
Code preparation for client/server communication
Michiel Broek <mbroek@mbse.eu>
parents:
40
diff
changeset
|
300 | fprintf(stdout, "mbsePi-apps thermferm v%s starting\n", VERSION); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
301 | |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
83
diff
changeset
|
302 | if (rdconfig()) { |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
303 | fprintf(stderr, "Error reading configuration\n"); |
235 | 304 | syslog(LOG_WARNING, "Error reading configuration: halted"); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
305 | return 1; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
306 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
307 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
308 | /* |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
309 | * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
310 | * but that's live. This daemon should only be stopped by SIGTERM. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
311 | * Don't catch SIGCHLD. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
312 | */ |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
313 | for (i = 0; i < NSIG; i++) { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
314 | if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
315 | signal(i, (void (*))die); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
316 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
317 | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
318 | #ifdef HAVE_WIRINGPI_H |
28
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
319 | if (wiringPiSetup () ) |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
320 | return 1; |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
321 | |
108 | 322 | if ((rc = initLCD (Config.lcd_cols, Config.lcd_rows))) { |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
323 | fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
324 | return 1; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
325 | } |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
326 | #endif |
28
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
327 | |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
328 | if (debug) { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
329 | /* |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
330 | * For debugging run in foreground. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
331 | */ |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
332 | rc = server(); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
333 | } else { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
334 | /* |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
335 | * Server initialization is complete. Now we can fork the |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
336 | * daemon and return to the user. We need to do a setpgrp |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
337 | * so that the daemon will no longer be assosiated with the |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
338 | * users control terminal. This is done before the fork, so |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
339 | * that the child will not be a process group leader. Otherwise, |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
340 | * if the child were to open a terminal, it would become |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
341 | * associated with that terminal as its control terminal. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
342 | */ |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
343 | if ((pgrp = setpgid(0, 0)) == -1) { |
235 | 344 | syslog(LOG_WARNING, "setpgpid failed"); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
345 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
346 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
347 | frk = fork(); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
348 | switch (frk) { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
349 | case -1: |
235 | 350 | syslog(LOG_WARNING, "Daemon fork failed: %s", strerror(errno)); |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
351 | #ifdef HAVE_WIRINGPI_H |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
352 | stopLCD(); |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
353 | #endif |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
354 | exit(1); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
355 | case 0: /* |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
356 | * Run the daemon |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
357 | */ |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
358 | fclose(stdin); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
359 | if (open("/dev/null", O_RDONLY) != 0) { |
235 | 360 | syslog(LOG_WARNING, "Reopen of stdin to /dev/null failed"); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
361 | _exit(2); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
362 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
363 | fclose(stdout); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
364 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { |
235 | 365 | syslog(LOG_WARNING, "Reopen of stdout to /dev/null failed"); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
366 | _exit(2); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
367 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
368 | fclose(stderr); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
369 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { |
235 | 370 | syslog(LOG_WARNING, "Reopen of stderr to /dev/null failed"); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
371 | _exit(2); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
372 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
373 | mypid = getpid(); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
374 | rc = server(); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
375 | break; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
376 | /* Not reached */ |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
377 | default: |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
378 | /* |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
379 | * Here we detach this process and let the child |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
380 | * run the deamon process. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
381 | */ |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
382 | syslog(LOG_NOTICE, "Starting daemon with pid %d", frk); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
383 | exit(0); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
384 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
385 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
386 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
387 | syslog(LOG_NOTICE, "Finished, rc=%d", rc); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
388 | return rc; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
389 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
390 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
391 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
392 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
393 | int server(void) |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
394 | { |
105
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
395 | char buf[1024], *filename, target[40], heater[40], cooler[40], fan[40], door[40]; |
64
2431965e8a8c
Write loglines only every 60 seconds
Michiel Broek <mbroek@mbse.eu>
parents:
63
diff
changeset
|
396 | time_t now, last = (time_t)0; |
104 | 397 | units_list *unit; |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
398 | profiles_list *profile; |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
399 | prof_step *step; |
182 | 400 | int rc, run = 1, seconds = 0, minutes = 0, piddelay = 0, temp, deviation; |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
401 | int run_seconds, run_minutes, run_hours; |
185 | 402 | float err = 0.0, sp, pv, P_err, D_err, Out; |
105
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
403 | #ifdef HAVE_WIRINGPI_H |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
404 | struct tm *tm; |
205 | 405 | int row, key; |
105
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
406 | #else |
74 | 407 | long t = 0; |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
408 | #endif |
221
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
409 | int current_step, valid_step, time_until_now; |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
410 | float previous_target; |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
411 | |
41
f534ace74eea
Code preparation for client/server communication
Michiel Broek <mbroek@mbse.eu>
parents:
40
diff
changeset
|
412 | if (lockprog((char *)"thermferm")) { |
235 | 413 | syslog(LOG_WARNING, "Can't lock"); |
34 | 414 | return 1; |
415 | } | |
416 | ||
158 | 417 | rc = devices_detect(); |
418 | if (rc) { | |
419 | syslog(LOG_NOTICE, "Detected %d new devices", rc); | |
420 | wrconfig(); | |
421 | } | |
422 | ||
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
423 | #ifdef HAVE_WIRINGPI_H |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
424 | rc = piThreadCreate(my_devices_loop); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
425 | #else |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
426 | rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t ); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
427 | #endif |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
428 | if (rc) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
429 | fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc); |
235 | 430 | syslog(LOG_WARNING, "my_devices_loop thread didn't start rc=%d", rc); |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
431 | #ifndef HAVE_WIRINGPI_H |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
432 | } else { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
433 | t++; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
434 | #endif |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
435 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
436 | |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
160
diff
changeset
|
437 | #ifdef HAVE_WIRINGPI_H |
43 | 438 | rc = piThreadCreate(my_server_loop); |
74 | 439 | #else |
75
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
440 | rc = pthread_create(&threads[t], NULL, my_server_loop, (void *)t ); |
74 | 441 | #endif |
43 | 442 | if (rc) { |
443 | fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); | |
235 | 444 | syslog(LOG_WARNING, "my_server_loop thread didn't start rc=%d", rc); |
75
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
445 | #ifndef HAVE_WIRINGPI_H |
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
446 | } else { |
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
447 | t++; |
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
448 | #endif |
43 | 449 | } |
450 | ||
199 | 451 | #ifdef HAVE_WIRINGPI_H |
452 | rc = piThreadCreate(my_panel_loop); | |
453 | if (rc) { | |
454 | fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); | |
235 | 455 | syslog(LOG_WARNING, "my_panel_loop thread didn't start rc=%d", rc); |
199 | 456 | } |
457 | #endif | |
458 | ||
182 | 459 | /* |
460 | * Initialize units for processing | |
461 | */ | |
462 | for (unit = Config.units; unit; unit = unit->next) { | |
463 | /* | |
464 | * Safety, turn everything off | |
465 | */ | |
466 | unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; | |
195
b34a1b2421fb
Reset output when a different profile is selected. Better startup logging about the unit state. Added Beer, Fridge and partial Profile settings from the liveview weeb screen.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
467 | if (unit->mode == UNITMODE_PROFILE) { |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
468 | if (!unit->profile) |
195
b34a1b2421fb
Reset output when a different profile is selected. Better startup logging about the unit state. Added Beer, Fridge and partial Profile settings from the liveview weeb screen.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
469 | syslog(LOG_NOTICE, "Starting unit %s in profile mode, no profile defined.", unit->name); |
b34a1b2421fb
Reset output when a different profile is selected. Better startup logging about the unit state. Added Beer, Fridge and partial Profile settings from the liveview weeb screen.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
470 | else |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
471 | syslog(LOG_NOTICE, "Starting unit %s in profile state %s.", unit->name, PROFSTATE[unit->prof_state]); |
182 | 472 | } else if (unit->mode == UNITMODE_BEER) { |
473 | syslog(LOG_NOTICE, "Starting unit %s beer cooler at %.1f degrees", unit->name, unit->beer_set); | |
474 | } else if (unit->mode == UNITMODE_FRIDGE) { | |
475 | syslog(LOG_NOTICE, "Starting unit %s as refridgerator at %.1f degrees", unit->name, unit->fridge_set); | |
476 | } else if (unit->mode == UNITMODE_NONE) { | |
477 | syslog(LOG_NOTICE, "Starting unit %s in inactive state", unit->name); | |
478 | } else { | |
479 | syslog(LOG_NOTICE, "Starting unit %s in off state", unit->name); | |
480 | } | |
481 | } | |
482 | ||
93
b759f814469d
Added units processing thread
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
483 | |
111 | 484 | #ifdef HAVE_WIRINGPI_H |
238 | 485 | lcd_buf_write(1, (char *)" ThermFerm "); |
108 | 486 | lcd_buf_write(2, (char *)" Version %s ", VERSION); |
111 | 487 | #endif |
108 | 488 | |
104 | 489 | for (unit = Config.units; unit; unit = unit->next) { |
490 | if (unit->mode != UNITMODE_OFF) { | |
155
0d86f3c0a07b
Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents:
150
diff
changeset
|
491 | initlog(unit->name); |
104 | 492 | } |
493 | } | |
33
2357e8636a60
Added user log with temperatures and cooler state
Michiel Broek <mbroek@mbse.eu>
parents:
29
diff
changeset
|
494 | |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
495 | do { |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
496 | if (my_shutdown) |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
497 | run = 0; |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
498 | |
64
2431965e8a8c
Write loglines only every 60 seconds
Michiel Broek <mbroek@mbse.eu>
parents:
63
diff
changeset
|
499 | now = time(NULL); |
104 | 500 | if (now != last) { |
189
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
501 | /* |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
502 | * Each second |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
503 | */ |
64
2431965e8a8c
Write loglines only every 60 seconds
Michiel Broek <mbroek@mbse.eu>
parents:
63
diff
changeset
|
504 | last = now; |
104 | 505 | seconds++; |
506 | ||
105
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
507 | #ifdef HAVE_WIRINGPI_H |
108 | 508 | row = 3; |
509 | tm = localtime(&now); | |
510 | lcd_buf_write(row++, " %02d-%02d-%04d ", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900); | |
511 | lcd_buf_write(row++, " %02d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec); | |
182 | 512 | #endif |
105
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
513 | |
207 | 514 | if (Config.temp_address) { |
515 | rc = read_sensor(Config.temp_address, &temp); | |
516 | if (rc == DEVPRESENT_YES) { | |
517 | Config.temp_value = temp; | |
518 | Config.temp_state = 0; | |
519 | #ifdef HAVE_WIRINGPI_H | |
520 | lcd_buf_write(row++, "Room temp %.1f %cC ", Config.temp_value / 1000.0, 0xdf); | |
521 | #endif | |
522 | } else if (rc == DEVPRESENT_ERROR) { | |
523 | Config.temp_state = 1; | |
524 | } else { | |
525 | Config.temp_state = 2; | |
526 | } | |
527 | } | |
528 | ||
529 | if (Config.hum_address) { | |
530 | rc = read_sensor(Config.hum_address, &temp); | |
531 | if (rc == DEVPRESENT_YES) { | |
532 | Config.hum_value = temp; | |
533 | Config.hum_state = 0; | |
534 | #ifdef HAVE_WIRINGPI_H | |
535 | lcd_buf_write(row++, " Humidity %.1f %% ", Config.hum_value / 1000.0, 0xdf); | |
536 | #endif | |
537 | } else if (rc == DEVPRESENT_ERROR) { | |
538 | Config.hum_state = 1; | |
539 | } else { | |
540 | Config.hum_state = 2; | |
541 | } | |
542 | } | |
543 | ||
108 | 544 | for (unit = Config.units; unit; unit = unit->next) { |
182 | 545 | #ifdef HAVE_WIRINGPI_H |
184 | 546 | lcd_buf_write(row++, "Unit %s ", unit->name); |
547 | lcd_buf_write(row++, "Mode %s ", UNITMODE[unit->mode]); | |
182 | 548 | #endif |
549 | ||
550 | if (unit->air_address) { | |
207 | 551 | rc = read_sensor(unit->air_address, &temp); |
182 | 552 | if (rc == DEVPRESENT_YES) { |
553 | /* | |
554 | * It is possible to have read errors or extreme values. | |
555 | * This can happen with bad connections so we compare the | |
556 | * value with the previous one. If the difference is too | |
557 | * much, we don't send that value. That also means that if | |
558 | * the next value is ok again, it will be marked invalid too. | |
559 | * Maximum error is 40 degrees for now. | |
560 | */ | |
561 | deviation = 40000; | |
562 | if ((unit->air_temperature == 0) || | |
563 | (unit->air_temperature && (temp > (int)unit->air_temperature - deviation) && (temp < ((int)unit->air_temperature + deviation)))) { | |
564 | unit->air_temperature = temp; | |
565 | unit->air_state = 0; | |
188
4fc17ef4593f
Added heat/cool state to the LCD display
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
566 | #ifdef HAVE_WIRINGPI_H |
4fc17ef4593f
Added heat/cool state to the LCD display
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
567 | lcd_buf_write(row++, " Air %.3f %cC ", unit->air_temperature / 1000.0, 0xdf); |
4fc17ef4593f
Added heat/cool state to the LCD display
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
568 | #endif |
182 | 569 | } else { |
570 | syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp); | |
571 | if (debug) { | |
572 | fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temperature, temp); | |
573 | } | |
574 | } | |
575 | } else if (rc == DEVPRESENT_ERROR) { | |
576 | unit->air_state = 1; | |
577 | } else { | |
578 | unit->air_state = 2; | |
579 | } | |
580 | } | |
581 | ||
582 | if (unit->beer_address) { | |
207 | 583 | rc = read_sensor(unit->beer_address, &temp); |
182 | 584 | if (rc == DEVPRESENT_YES) { |
585 | deviation = 40000; | |
586 | if ((unit->beer_temperature == 0) || | |
587 | (unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) { | |
588 | unit->beer_temperature = temp; | |
589 | unit->beer_state = 0; | |
188
4fc17ef4593f
Added heat/cool state to the LCD display
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
590 | #ifdef HAVE_WIRINGPI_H |
4fc17ef4593f
Added heat/cool state to the LCD display
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
591 | lcd_buf_write(row++, "Beer %.3f %cC ", unit->beer_temperature / 1000.0, 0xdf); |
4fc17ef4593f
Added heat/cool state to the LCD display
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
592 | #endif |
182 | 593 | } else { |
594 | syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp); | |
595 | if (debug) { | |
596 | fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temperature, temp); | |
597 | } | |
598 | } | |
599 | } else if (rc == DEVPRESENT_ERROR) { | |
600 | unit->beer_state = 1; | |
601 | } else { | |
602 | unit->beer_state = 2; | |
603 | } | |
604 | } | |
184 | 605 | |
606 | /* | |
189
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
607 | * Handle profile |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
608 | */ |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
609 | if ((unit->mode == UNITMODE_PROFILE) && (unit->profile)) { |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
610 | /* |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
611 | * unit->profile - uuid of the selected profile. |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
612 | * unit->prof_started - start time or 0 if not yet running. |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
613 | * unit->prof_state - PROFILE_OFF|PROFILE_PAUSE|PROFILE_RUN|PROFILE_DONE |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
614 | * unit->prof_target - Calculated target temperature. |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
615 | * unit->prof_paused - Internal pause counter. |
189
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
616 | */ |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
617 | for (profile = Config.profiles; profile; profile = profile->next) { |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
618 | if (strcmp(unit->profile, profile->uuid) == 0) { |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
619 | |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
620 | switch (unit->prof_state) { |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
621 | case PROFILE_OFF: |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
622 | unit->prof_target = profile->inittemp; |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
623 | break; |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
624 | case PROFILE_PAUSE: |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
625 | /* |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
626 | * Keep current temperature, measure pause time. For |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
627 | * temperature fall thru. |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
628 | */ |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
629 | unit->prof_paused++; |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
630 | case PROFILE_RUN: |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
631 | /* |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
632 | * Calculate current profile step en desired temperature. |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
633 | * When all done, set state to PROFILE_DONE. |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
634 | */ |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
635 | previous_target = profile->inittemp; |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
636 | time_until_now = 0; |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
637 | current_step = 0; |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
638 | run_seconds = (int)(now - unit->prof_started - unit->prof_paused); |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
639 | run_minutes = run_seconds / 60; |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
640 | run_hours = run_minutes / 60; |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
641 | if (debug) |
221
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
642 | fprintf(stdout, "run_HMS=%d,%d,%d ", run_hours, run_minutes, run_seconds); |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
643 | |
221
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
644 | valid_step = FALSE; |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
645 | for (step = profile->steps; step; step = step->next) { |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
646 | /* |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
647 | * step->steptime |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
648 | * step->resttime |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
649 | * step->target |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
650 | */ |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
651 | current_step++; |
221
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
652 | if ((run_hours >= time_until_now) && (run_hours < (time_until_now + step->steptime + step->resttime))) { |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
653 | /* |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
654 | * This is our current step |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
655 | */ |
221
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
656 | valid_step = TRUE; |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
657 | if (debug) |
221
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
658 | fprintf(stdout, "step=%d step_pos=%d step=%d/%d target=%.1f ", |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
659 | current_step, run_hours - time_until_now, |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
660 | step->steptime, step->resttime, step->target); |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
661 | if ((run_hours - time_until_now) < step->steptime) { |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
662 | unit->prof_target = previous_target + (((run_minutes - (time_until_now * 60.0)) / (step->steptime * 60.0)) * (step->target - previous_target)); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
663 | if (debug) |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
664 | fprintf(stdout, "tempshift=%.1f minutes=%d duration=%d temp_move=%.3f ", |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
665 | step->target - previous_target, run_minutes - (time_until_now * 60), |
221
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
666 | step->steptime * 60, unit->prof_target); |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
667 | } else { |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
668 | unit->prof_target = step->target; |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
669 | fprintf(stdout, "resting target=%.1f ", step->target); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
670 | } |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
671 | break; |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
672 | } |
221
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
673 | time_until_now += step->steptime + step->resttime; |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
674 | previous_target = step->target; |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
675 | } |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
676 | if (debug) |
221
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
677 | fprintf(stdout, " %s\n", valid_step ? "TRUE":"FALSE"); |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
678 | |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
679 | /* |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
680 | * No more steps to do |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
681 | */ |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
682 | if (valid_step == FALSE) { |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
683 | unit->prof_state = PROFILE_DONE; |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
684 | syslog(LOG_NOTICE, "Profile `%s' is done", profile->name); |
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
685 | } |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
686 | break; |
221
91a5e7281c35
Fixed end of profile steps, didn't go to the PROFILE_DONE state.
Michiel Broek <mbroek@mbse.eu>
parents:
219
diff
changeset
|
687 | |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
688 | case PROFILE_DONE: |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
689 | /* |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
690 | * Keep this state, set target temperature to the last step. |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
691 | */ |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
692 | previous_target = profile->inittemp; |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
693 | for (step = profile->steps; step; step = step->next) { |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
694 | if (step->steptime == 0) |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
695 | break; |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
696 | previous_target = step->target; |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
697 | } |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
698 | unit->prof_target = previous_target; |
194
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
699 | break; |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
700 | } /* switch */ |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
701 | } |
9eaaba49450f
Added some ideas about profiles.
Michiel Broek <mbroek@mbse.eu>
parents:
189
diff
changeset
|
702 | } |
189
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
703 | } |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
704 | |
cc2b04d4db99
Init logfile when a unit is turned on. Added comments for profile processing.
Michiel Broek <mbroek@mbse.eu>
parents:
188
diff
changeset
|
705 | /* |
184 | 706 | * Manual switching |
707 | */ | |
708 | if (unit->mode == UNITMODE_NONE) { | |
709 | device_out(unit->heater_address, unit->heater_state); | |
710 | device_out(unit->cooler_address, unit->cooler_state); | |
711 | device_out(unit->fan_address, unit->fan_state); | |
712 | } | |
188
4fc17ef4593f
Added heat/cool state to the LCD display
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
713 | #ifdef HAVE_WIRINGPI_H |
219
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
714 | if (unit->heater_address && unit->cooler_address) { |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
715 | if (unit->heater_state) |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
716 | lcd_buf_write(row++, "Heater On "); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
717 | else if (unit->cooler_state) |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
718 | lcd_buf_write(row++, "Cooler On "); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
719 | else |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
720 | lcd_buf_write(row++, "Standby "); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
721 | switch (unit->mode) { |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
722 | case UNITMODE_BEER: lcd_buf_write(row++, "Target %.1f %cC ", unit->beer_set, 0xdf); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
723 | break; |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
724 | case UNITMODE_FRIDGE: lcd_buf_write(row++, "Target %.1f %cC ", unit->fridge_set, 0xdf); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
725 | break; |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
726 | case UNITMODE_PROFILE: if (unit->prof_state != PROFILE_OFF) |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
727 | lcd_buf_write(row++, "Target %.1f %cC ", unit->prof_target, 0xdf); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
728 | else |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
729 | lcd_buf_write(row++, "Target not set "); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
730 | break; |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
731 | default: lcd_buf_write(row++, "Target not set "); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
732 | } |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
733 | } else { |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
734 | if (unit->heater_address) { |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
735 | lcd_buf_write(row++, "Heat %s ", unit->heater_state ? "On ":"Off"); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
736 | } |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
737 | if (unit->cooler_address) { |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
738 | lcd_buf_write(row++, "Cool %s ", unit->cooler_state ? "On ":"Off"); |
ae720212accc
Added profile running and steps handling.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
739 | } |
188
4fc17ef4593f
Added heat/cool state to the LCD display
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
740 | } |
4fc17ef4593f
Added heat/cool state to the LCD display
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
741 | #endif |
105
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
742 | } |
108 | 743 | |
182 | 744 | piddelay++; |
745 | if (piddelay == 15) { | |
746 | piddelay = 0; | |
104 | 747 | |
748 | for (unit = Config.units; unit; unit = unit->next) { | |
184 | 749 | if ((unit->mode == UNITMODE_FRIDGE) || (unit->mode == UNITMODE_BEER) || (unit->mode == UNITMODE_PROFILE)) { |
750 | /* | |
751 | * PID controller | |
752 | */ | |
753 | sp = unit->beer_set; | |
754 | pv = unit->beer_temperature / 1000.0; | |
755 | if (unit->mode == UNITMODE_FRIDGE) { | |
176
8c7d87a2c094
Initial code for a simple PID controller.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
756 | sp = unit->fridge_set; |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
177
diff
changeset
|
757 | pv = unit->air_temperature / 1000.0; |
184 | 758 | } else if (unit->mode == UNITMODE_PROFILE) { |
176
8c7d87a2c094
Initial code for a simple PID controller.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
759 | sp = unit->prof_target; |
184 | 760 | } |
176
8c7d87a2c094
Initial code for a simple PID controller.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
761 | |
184 | 762 | unit->PID_err_old = err; |
763 | err = sp - pv; | |
185 | 764 | if (err < unit->idle_rangeH && err > unit->idle_rangeL) { |
184 | 765 | err = 0; |
185 | 766 | unit->PID_I_err -= unit->PID_err_old; |
767 | } else { | |
768 | unit->PID_I_err += unit->PID_err_old; | |
769 | } | |
770 | /* Limit intergral error */ | |
771 | if (unit->PID_I_err < -10.0) | |
772 | unit->PID_I_err = -10.0; | |
773 | if (unit->PID_I_err > 10.0) | |
774 | unit->PID_I_err = 10.0; | |
184 | 775 | P_err = err; |
776 | D_err = err - unit->PID_err_old; | |
176
8c7d87a2c094
Initial code for a simple PID controller.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
777 | |
184 | 778 | /* |
779 | * A postive value means heating, a negative value cooling. | |
780 | */ | |
185 | 781 | Out = (10.0*P_err) + (0.1*unit->PID_I_err) + (5*D_err); |
782 | // Kp 0.1 Ki 0.3 Kd 0.02 | |
184 | 783 | if (debug) |
784 | fprintf(stdout, "sp=%.2f pv=%.2f err_old=%.2f err=%.2f P_err=%.2f I_err=%.2f D_err=%.2f Out=%.2f\n", | |
185 | 785 | sp, pv, unit->PID_err_old, err, P_err, unit->PID_I_err, D_err, Out); |
195
b34a1b2421fb
Reset output when a different profile is selected. Better startup logging about the unit state. Added Beer, Fridge and partial Profile settings from the liveview weeb screen.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
786 | syslog(LOG_NOTICE, "sp=%.2f pv=%.2f err_old=%.2f err=%.2f P_err=%.2f I_err=%.2f D_err=%.2f Out=%.2f", |
b34a1b2421fb
Reset output when a different profile is selected. Better startup logging about the unit state. Added Beer, Fridge and partial Profile settings from the liveview weeb screen.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
787 | sp, pv, unit->PID_err_old, err, P_err, unit->PID_I_err, D_err, Out); |
184 | 788 | if (unit->heater_address) { |
234
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
789 | if (Out >= 2) { |
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
790 | if (! unit->heater_state) |
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
791 | syslog(LOG_NOTICE, "Heater Off => On"); |
184 | 792 | unit->heater_state = 100; |
234
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
793 | } else { |
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
794 | if (unit->heater_state) |
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
795 | syslog(LOG_NOTICE, "Heater On => Off"); |
184 | 796 | unit->heater_state = 0; |
234
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
797 | } |
184 | 798 | device_out(unit->heater_address, unit->heater_state); |
176
8c7d87a2c094
Initial code for a simple PID controller.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
799 | } |
184 | 800 | if (unit->cooler_address) { |
234
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
801 | if (Out <= -2) { |
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
802 | if (! unit->cooler_state) |
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
803 | syslog(LOG_NOTICE, "Cooler Off => On"); |
184 | 804 | unit->cooler_state = 100; |
234
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
805 | } else { |
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
806 | if (unit->cooler_state) |
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
807 | syslog(LOG_NOTICE, "Cooler On => Off"); |
184 | 808 | unit->cooler_state = 0; |
234
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
809 | } |
184 | 810 | device_out(unit->cooler_address, unit->cooler_state); |
811 | } | |
233
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
812 | if (unit->heater_address && unit->cooler_address && unit->fan_address) { |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
813 | /* |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
814 | * If the temperature difference between air and beer is more then |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
815 | * xxx degrees, turn the fan on to make an airflow. |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
816 | * Maybe, run the fan too if the heater is on because the heater in |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
817 | * most cases will be some sort of radiating heat device. |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
818 | * For cooling ??? dunno yet. |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
819 | */ |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
820 | if (((unit->air_temperature - unit->beer_temperature) > 1000) || |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
821 | ((unit->air_temperature - unit->beer_temperature) < -1000)) { |
234
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
822 | if (! unit->fan_state) |
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
823 | syslog(LOG_NOTICE, "Fan Off => On"); |
233
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
824 | unit->fan_state = 100; |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
825 | } else { |
234
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
826 | if (unit->fan_state) |
b69438db19ec
Added loggin when a device is switched on or off in beer/fridge/profile mode.
Michiel Broek <mbroek@mbse.eu>
parents:
233
diff
changeset
|
827 | syslog(LOG_NOTICE, "Fan On => Off"); |
233
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
828 | unit->fan_state = 0; |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
829 | } |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
830 | device_out(unit->fan_address, unit->fan_state); |
5874a6480322
Added code to control the Fan.
Michiel Broek <mbroek@mbse.eu>
parents:
227
diff
changeset
|
831 | } |
184 | 832 | } else { |
833 | err = 0.0; | |
185 | 834 | unit->PID_I_err = 0.0; |
184 | 835 | unit->PID_err_old = 0.0; |
182 | 836 | } |
837 | } | |
838 | } | |
839 | ||
204
9a14d6b2de7f
Fixed LCD display updates. Better key routines.
Michiel Broek <mbroek@mbse.eu>
parents:
200
diff
changeset
|
840 | #ifdef HAVE_WIRINGPI_H |
205 | 841 | if (setupmenu == MENU_NONE) |
842 | lcd_buf_show(); | |
204
9a14d6b2de7f
Fixed LCD display updates. Better key routines.
Michiel Broek <mbroek@mbse.eu>
parents:
200
diff
changeset
|
843 | #endif |
205 | 844 | |
182 | 845 | if (seconds == 60) { |
846 | seconds = 0; | |
847 | ||
848 | /* | |
849 | * Log temperature and status every minute if unit is active. | |
850 | */ | |
851 | for (unit = Config.units; unit; unit = unit->next) { | |
852 | if (unit->mode != UNITMODE_OFF) { | |
176
8c7d87a2c094
Initial code for a simple PID controller.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
853 | |
105
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
854 | snprintf(target, 39, "NA"); |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
855 | snprintf(heater, 39, "NA"); |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
856 | snprintf(cooler, 39, "NA"); |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
857 | snprintf(fan, 39, "NA"); |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
858 | snprintf(door, 39, "NA"); |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
859 | |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
860 | if (unit->mode == UNITMODE_BEER) |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
861 | snprintf(target, 39, "%.1f", unit->beer_set); |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
862 | else if (unit->mode == UNITMODE_FRIDGE) |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
863 | snprintf(target, 39, "%.1f", unit->fridge_set); |
175
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
864 | else if (unit->mode == UNITMODE_PROFILE) |
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
865 | snprintf(target, 39, "%.1f", unit->prof_target); |
105
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
866 | |
175
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
867 | if (unit->heater_address) { |
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
868 | snprintf(heater, 39, "%d", unit->heater_state); |
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
869 | } |
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
870 | if (unit->cooler_address) { |
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
871 | snprintf(cooler, 39, "%d", unit->cooler_state); |
105
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
872 | } |
175
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
873 | if (unit->fan_address) { |
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
874 | snprintf(fan, 39, "%d", unit->fan_state); |
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
875 | } |
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
876 | if (unit->door_address) { |
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
174
diff
changeset
|
877 | snprintf(door, 39, "%d", unit->door_state); |
105
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
878 | } |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
879 | |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
880 | snprintf(buf, 1023, "%s,%.3f,%.3f,%s,%s,%s,%s,%s", |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
881 | UNITMODE[unit->mode], unit->air_temperature / 1000.0, |
a5a964148e34
Started code of roulating lcd display
Michiel Broek <mbroek@mbse.eu>
parents:
104
diff
changeset
|
882 | unit->beer_temperature / 1000.0, target, heater, cooler, fan, door); |
104 | 883 | filename = xstrcpy(unit->name); |
884 | filename = xstrcat(filename, (char *)".log"); | |
885 | logger(filename, buf); | |
886 | free(filename); | |
887 | filename = NULL; | |
888 | } | |
889 | } | |
167
56b008563db8
Fixed GET AIR|BEER commands. Save configuration every 30 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
165
diff
changeset
|
890 | |
56b008563db8
Fixed GET AIR|BEER commands. Save configuration every 30 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
165
diff
changeset
|
891 | minutes++; |
56b008563db8
Fixed GET AIR|BEER commands. Save configuration every 30 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
165
diff
changeset
|
892 | if (minutes == 60) |
56b008563db8
Fixed GET AIR|BEER commands. Save configuration every 30 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
165
diff
changeset
|
893 | minutes = 0; |
56b008563db8
Fixed GET AIR|BEER commands. Save configuration every 30 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
165
diff
changeset
|
894 | |
227
1cb55ea51f76
Remove debug log information about regular configuration saving.
Michiel Broek <mbroek@mbse.eu>
parents:
223
diff
changeset
|
895 | /* |
1cb55ea51f76
Remove debug log information about regular configuration saving.
Michiel Broek <mbroek@mbse.eu>
parents:
223
diff
changeset
|
896 | * Save the configuration each half hour. |
1cb55ea51f76
Remove debug log information about regular configuration saving.
Michiel Broek <mbroek@mbse.eu>
parents:
223
diff
changeset
|
897 | */ |
1cb55ea51f76
Remove debug log information about regular configuration saving.
Michiel Broek <mbroek@mbse.eu>
parents:
223
diff
changeset
|
898 | if ((minutes == 15) || (minutes == 45)) |
167
56b008563db8
Fixed GET AIR|BEER commands. Save configuration every 30 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
165
diff
changeset
|
899 | wrconfig(); |
64
2431965e8a8c
Write loglines only every 60 seconds
Michiel Broek <mbroek@mbse.eu>
parents:
63
diff
changeset
|
900 | } |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
901 | } |
200
a215ddaabbe2
Added first panel key routines. The LCD display steps manual only and has a 2 minutes backlight timeout.
Michiel Broek <mbroek@mbse.eu>
parents:
199
diff
changeset
|
902 | |
a215ddaabbe2
Added first panel key routines. The LCD display steps manual only and has a 2 minutes backlight timeout.
Michiel Broek <mbroek@mbse.eu>
parents:
199
diff
changeset
|
903 | #ifdef HAVE_WIRINGPI_H |
205 | 904 | /* |
905 | * Handle panel key events. | |
906 | */ | |
907 | key = keycheck(); | |
908 | ||
909 | switch (setupmenu) { | |
910 | case MENU_NONE: | |
911 | if ((key == KEY_DOWN) || (key == KEY_UP)) | |
912 | lcd_buf_step(key); | |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
913 | if ((key == KEY_CONFIRM) && (setupmenu == MENU_NONE)) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
914 | go_menu(MENU_TOP_DEFAULT); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
915 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
916 | |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
917 | case MENU_TOP_DEFAULT: |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
918 | if (key == KEY_ESCAPE) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
919 | go_menu(MENU_NONE); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
920 | if (key == KEY_DOWN) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
921 | go_menu(MENU_TOP_UNITS); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
922 | if (key == KEY_UP) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
923 | go_menu(MENU_TOP_SYS); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
924 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
925 | |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
926 | case MENU_TOP_UNITS: |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
927 | if (key == KEY_ESCAPE) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
928 | go_menu(MENU_NONE); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
929 | if (key == KEY_DOWN) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
930 | go_menu(MENU_TOP_SYS); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
931 | if (key == KEY_UP) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
932 | go_menu(MENU_TOP_DEFAULT); |
238 | 933 | if (key == KEY_ENTER) |
934 | go_menu(MENU_UNITS); | |
935 | break; | |
936 | ||
937 | case MENU_UNITS: | |
938 | if (key == KEY_ESCAPE) | |
939 | go_menu(MENU_TOP_UNITS); | |
940 | if (key == KEY_DOWN) { | |
941 | for (unit = Config.units; unit; unit = unit->next) { | |
942 | if (strcmp(unit->uuid, current_unit->uuid) == 0) { /* Current unit */ | |
943 | if (unit->next) { /* Is there a next */ | |
944 | unit = unit->next; /* Select that */ | |
945 | lcdPosition(lcdHandle, 0, 1); | |
946 | lcdPuts(lcdHandle, Config.units->name); | |
947 | } | |
948 | } | |
949 | } | |
950 | } | |
951 | if (key == KEY_ENTER) { | |
952 | for (unit = Config.units; unit; unit = unit->next) { | |
953 | if (strcmp(unit->uuid, current_unit->uuid) == 0) { | |
954 | if (unit->mode == UNITMODE_OFF) | |
955 | go_menu(MENU_MODE_NONE); | |
956 | else | |
957 | go_menu(MENU_MODE_OFF); | |
958 | } | |
959 | } | |
960 | } | |
961 | break; | |
962 | ||
963 | case MENU_MODE_OFF: | |
964 | if (key == KEY_ESCAPE) | |
965 | go_menu(MENU_UNITS); | |
966 | if (key == KEY_DOWN) | |
967 | go_menu(MENU_MODE_NONE); | |
968 | if (key == KEY_UP) | |
969 | go_menu(MENU_MODE_PROFILE); | |
970 | if (key == KEY_ENTER) { | |
971 | change_mode(UNITMODE_OFF); | |
972 | go_menu(MENU_MODE_OFF); | |
973 | } | |
974 | break; | |
975 | ||
976 | case MENU_MODE_NONE: | |
977 | if (key == KEY_ESCAPE) | |
978 | go_menu(MENU_UNITS); | |
979 | if (key == KEY_DOWN) | |
980 | go_menu(MENU_MODE_FRIDGE); | |
981 | if (key == KEY_UP) | |
982 | go_menu(MENU_MODE_OFF); | |
983 | if (key == KEY_ENTER) { | |
984 | if (current_unit->mode == UNITMODE_NONE) | |
985 | go_menu(MENU_NONE_HEAT); | |
986 | else { | |
987 | change_mode(UNITMODE_NONE); | |
988 | go_menu(MENU_MODE_NONE); | |
989 | } | |
990 | } | |
991 | break; | |
992 | ||
993 | case MENU_NONE_HEAT: | |
994 | if (key == KEY_ESCAPE) | |
995 | go_menu(MENU_MODE_NONE); | |
996 | if (key == KEY_DOWN) | |
997 | go_menu(MENU_NONE_COOL); | |
998 | if (key == KEY_UP) | |
999 | go_menu(MENU_NONE_FAN); | |
1000 | if (key == KEY_ENTER) { | |
1001 | if (current_unit->heater_state) | |
1002 | current_unit->heater_state = 0; | |
1003 | else | |
1004 | current_unit->heater_state = 100; | |
1005 | go_menu(MENU_NONE_HEAT); | |
1006 | } | |
1007 | break; | |
1008 | ||
1009 | case MENU_NONE_COOL: | |
1010 | if (key == KEY_ESCAPE) | |
1011 | go_menu(MENU_MODE_NONE); | |
1012 | if (key == KEY_DOWN) | |
1013 | go_menu(MENU_NONE_FAN); | |
1014 | if (key == KEY_UP) | |
1015 | go_menu(MENU_NONE_HEAT); | |
1016 | if (key == KEY_ENTER) { | |
1017 | if (current_unit->cooler_state) | |
1018 | current_unit->cooler_state = 0; | |
1019 | else | |
1020 | current_unit->cooler_state = 100; | |
1021 | go_menu(MENU_NONE_COOL); | |
1022 | } | |
1023 | break; | |
1024 | ||
1025 | case MENU_NONE_FAN: | |
1026 | if (key == KEY_ESCAPE) | |
1027 | go_menu(MENU_MODE_NONE); | |
1028 | if (key == KEY_DOWN) | |
1029 | go_menu(MENU_NONE_HEAT); | |
1030 | if (key == KEY_UP) | |
1031 | go_menu(MENU_NONE_COOL); | |
1032 | if (key == KEY_ENTER) { | |
1033 | if (current_unit->fan_state) | |
1034 | current_unit->fan_state = 0; | |
1035 | else | |
1036 | current_unit->fan_state = 100; | |
1037 | go_menu(MENU_NONE_FAN); | |
1038 | } | |
1039 | break; | |
1040 | ||
1041 | case MENU_MODE_FRIDGE: | |
1042 | if (key == KEY_ESCAPE) | |
1043 | go_menu(MENU_UNITS); | |
1044 | if (key == KEY_DOWN) | |
1045 | go_menu(MENU_MODE_BEER); | |
1046 | if (key == KEY_UP) | |
1047 | go_menu(MENU_MODE_NONE); | |
1048 | if (key == KEY_ENTER) { | |
1049 | change_mode(UNITMODE_FRIDGE); | |
1050 | go_menu(MENU_MODE_FRIDGE); | |
1051 | } | |
1052 | break; | |
1053 | ||
1054 | case MENU_FRIDGE_TEMP: | |
1055 | break; | |
1056 | ||
1057 | case MENU_MODE_BEER: | |
1058 | if (key == KEY_ESCAPE) | |
1059 | go_menu(MENU_UNITS); | |
1060 | if (key == KEY_DOWN) | |
1061 | go_menu(MENU_MODE_PROFILE); | |
1062 | if (key == KEY_UP) | |
1063 | go_menu(MENU_MODE_FRIDGE); | |
1064 | if (key == KEY_ENTER) { | |
1065 | change_mode(UNITMODE_BEER); | |
1066 | go_menu(MENU_MODE_BEER); | |
1067 | } | |
1068 | break; | |
1069 | ||
1070 | case MENU_BEER_TEMP: | |
1071 | break; | |
1072 | ||
1073 | case MENU_MODE_PROFILE: | |
1074 | if (key == KEY_ESCAPE) | |
1075 | go_menu(MENU_UNITS); | |
1076 | if (key == KEY_DOWN) | |
1077 | go_menu(MENU_MODE_OFF); | |
1078 | if (key == KEY_UP) | |
1079 | go_menu(MENU_MODE_BEER); | |
1080 | if (key == KEY_ENTER) { | |
1081 | change_mode(UNITMODE_PROFILE); | |
1082 | go_menu(MENU_MODE_PROFILE); | |
1083 | } | |
1084 | break; | |
1085 | ||
1086 | case MENU_PROFILE_SELECT: | |
1087 | break; | |
1088 | ||
1089 | case MENU_PROFILE_START: | |
1090 | break; | |
1091 | ||
1092 | case MENU_PROFILE_PAUSE: | |
1093 | break; | |
1094 | ||
1095 | case MENU_PROFILE_ABORT: | |
1096 | break; | |
1097 | ||
1098 | case MENU_PROFILE_RESUME: | |
1099 | break; | |
1100 | ||
1101 | case MENU_PROFILE_GOOFF: | |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1102 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1103 | |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1104 | case MENU_TOP_SYS: |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1105 | if (key == KEY_ESCAPE) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1106 | go_menu(MENU_NONE); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1107 | if (key == KEY_DOWN) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1108 | go_menu(MENU_TOP_DEFAULT); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1109 | if (key == KEY_UP) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1110 | go_menu(MENU_TOP_UNITS); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1111 | if (key == KEY_ENTER) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1112 | go_menu(MENU_SYS_HALT); |
205 | 1113 | break; |
1114 | ||
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1115 | case MENU_SYS_HALT: |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1116 | if (key == KEY_ESCAPE) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1117 | go_menu(MENU_TOP_SYS); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1118 | if (key == KEY_DOWN) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1119 | go_menu(MENU_SYS_REBOOT); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1120 | if (key == KEY_UP) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1121 | go_menu(MENU_SYS_THERMS); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1122 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1123 | |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1124 | case MENU_SYS_REBOOT: |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1125 | if (key == KEY_ESCAPE) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1126 | go_menu(MENU_TOP_SYS); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1127 | if (key == KEY_DOWN) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1128 | go_menu(MENU_SYS_MASH); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1129 | if (key == KEY_UP) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1130 | go_menu(MENU_SYS_HALT); |
205 | 1131 | break; |
214
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1132 | |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1133 | case MENU_SYS_MASH: |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1134 | if (key == KEY_ESCAPE) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1135 | go_menu(MENU_TOP_SYS); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1136 | if (key == KEY_DOWN) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1137 | go_menu(MENU_SYS_THERMS); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1138 | if (key == KEY_UP) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1139 | go_menu(MENU_SYS_REBOOT); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1140 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1141 | |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1142 | case MENU_SYS_THERMS: |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1143 | if (key == KEY_ESCAPE) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1144 | go_menu(MENU_TOP_SYS); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1145 | if (key == KEY_DOWN) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1146 | go_menu(MENU_SYS_HALT); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1147 | if (key == KEY_UP) |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1148 | go_menu(MENU_SYS_MASH); |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1149 | break; |
37d1eb0e1d66
Added a bunch of panel menus
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
1150 | |
205 | 1151 | } |
1152 | ||
200
a215ddaabbe2
Added first panel key routines. The LCD display steps manual only and has a 2 minutes backlight timeout.
Michiel Broek <mbroek@mbse.eu>
parents:
199
diff
changeset
|
1153 | #endif |
a215ddaabbe2
Added first panel key routines. The LCD display steps manual only and has a 2 minutes backlight timeout.
Michiel Broek <mbroek@mbse.eu>
parents:
199
diff
changeset
|
1154 | |
28
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
1155 | usleep(100000); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1156 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1157 | } while (run); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1158 | |
182 | 1159 | /* |
1160 | * Stop units processing in a neat way | |
1161 | */ | |
1162 | for (unit = Config.units; unit; unit = unit->next) { | |
1163 | /* | |
1164 | * Turn everything off | |
1165 | */ | |
1166 | unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; | |
1167 | device_out(unit->heater_address, unit->heater_state); | |
1168 | device_out(unit->cooler_address, unit->cooler_state); | |
1169 | device_out(unit->fan_address, unit->fan_state); | |
1170 | syslog(LOG_NOTICE, "Stopped unit %s mode %s", unit->name, UNITMODE[unit->mode]); | |
1171 | } | |
1172 | ||
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1173 | if (debug) |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1174 | fprintf(stdout, (char *)"Out of loop\n"); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1175 | |
28
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
1176 | /* |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
1177 | * Give threads time to cleanup |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
1178 | */ |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
1179 | usleep(1500000); |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
1180 | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
1181 | #ifdef HAVE_WIRINGPI_H |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1182 | stopLCD(); |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
64
diff
changeset
|
1183 | #endif |
28
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
1184 | |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
83
diff
changeset
|
1185 | wrconfig(); |
53 | 1186 | |
41
f534ace74eea
Code preparation for client/server communication
Michiel Broek <mbroek@mbse.eu>
parents:
40
diff
changeset
|
1187 | ulockprog((char *)"thermferm"); |
34 | 1188 | |
28
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
1189 | if (debug) |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
1190 | fprintf(stdout, "Goodbye\n"); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1191 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1192 | return 0; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1193 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1194 |