Blame view

hvc.c 3.7 KB
9e9f365e   henyxia   Added HVC interface
1
2
3
4
5
6
7
8
9
10
  #include <stdio.h>
  #include <stdlib.h>
  #include <termios.h>
  #include <unistd.h>
  #include <errno.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <strings.h>
  #include <inttypes.h>
5dd66ab0   henyxia   Added timer optio...
11
  #include <time.h>
9e9f365e   henyxia   Added HVC interface
12
13
14
15
  #include "ui.h"
  #include "hvc.h"
  #include "printx.h"
  #include "serial.h"
8dca7aeb   henyxia   Automatic Pumping...
16
  #include "heat.h"
11824cae   Pierre Letousey   Added automatic p...
17
  #include "pump.h"
9e9f365e   henyxia   Added HVC interface
18
19
20
  
  #define	DEVICE1				"/dev/ttyACM1"
  #define	SPEED				B19200
ac0ae6b0   henyxia   HVC can now use t...
21
  #define HVC_POLLING_TIME	50000
9e9f365e   henyxia   Added HVC interface
22
23
24
25
  #define	WAIT_BEFORE_RETRY	5000
  #define	GET_DEVICE_MODEL	'A'
  #define	DEVICE_MODEL_HVC	'C'
  #define	GET_TEMP			'H'
ac0ae6b0   henyxia   HVC can now use t...
26
27
28
29
  #define	SET_HEAT_ON			'E'
  #define	SET_HEAT_OFF		'G'
  #define	SET_PUMP_ON			'B'
  #define	SET_PUMP_OFF		'D'
b4a7f4ac   henyxia   Querying for pump...
30
31
  #define	PUMP_STATE_HIGH		'I'
  #define	GET_PUMP			'F'
9e9f365e   henyxia   Added HVC interface
32
33
34
35
  
  // Globals
  int		hvc_fd = -1;
  bool	hvcStop = false;
b4a7f4ac   henyxia   Querying for pump...
36
37
  int		pumped = 0;
  bool	previousPumpState;
9e9f365e   henyxia   Added HVC interface
38
39
  bool	sPump = false;
  bool	sHeat = false;
bb8c2799   henyxia   Fixed version
40
  bool	autoHeat = false;
9e9f365e   henyxia   Added HVC interface
41
  struct	termios hvcSaveterm;
ac0ae6b0   henyxia   HVC can now use t...
42
43
  bool	wHeat = false;
  bool	wPump = false;
5dd66ab0   henyxia   Added timer optio...
44
45
46
47
48
49
  time_t	tHeatStart;
  time_t	tHeatStop;
  double	tHeatTimer;
  time_t	tPumpStart;
  double	tPumpTimer;
  time_t	tPumpStop;
bb8c2799   henyxia   Fixed version
50
51
52
53
  int		tempCons;
  
  void setPumpOn()
  {
bb8c2799   henyxia   Fixed version
54
55
56
57
58
  	setPumpWantedState(true);
  }
  
  void setPumpOff()
  {
e912f423   henyxia   Urgent fix
59
  	pumped = 0;
bb8c2799   henyxia   Fixed version
60
61
62
63
64
65
66
67
68
69
70
71
  	setPumpWantedState(false);
  }
  
  void setHeatOn()
  {
  	setHeatWantedState(true);
  }
  
  void setHeatOff()
  {
  	setHeatWantedState(false);
  }
ac0ae6b0   henyxia   HVC can now use t...
72
73
74
75
76
77
78
79
  
  void stopHVC()
  {
  	hvcStop = true;
  }
  
  void setPumpWantedState(bool s)
  {
5dd66ab0   henyxia   Added timer optio...
80
81
  	tPumpStart = clock();
  	tPumpStop = clock();
ac0ae6b0   henyxia   HVC can now use t...
82
83
84
85
86
  	wPump = s;
  }
  
  void setHeatWantedState(bool s)
  {
5dd66ab0   henyxia   Added timer optio...
87
88
  	tHeatStart = clock();
  	tHeatStop = clock();
ac0ae6b0   henyxia   HVC can now use t...
89
90
  	wHeat = s;
  }
9e9f365e   henyxia   Added HVC interface
91
  
5dd66ab0   henyxia   Added timer optio...
92
93
94
95
96
97
98
99
100
101
  void setPumpTimer(double t)
  {
  	tPumpTimer = t;
  }
  
  void setHeatTimer(double t)
  {
  	tHeatTimer = t;
  }
  
8dca7aeb   henyxia   Automatic Pumping...
102
103
104
105
106
  void setAutoHeat(bool s)
  {
  	autoHeat = s;
  }
  
9e9f365e   henyxia   Added HVC interface
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  bool initHVC()
  {
  	unsigned char data;
  	printx(DEBUG, HVC, "Connecting to interface 1\n");
  	if(init_serial(DEVICE1, SPEED, &hvc_fd, &hvcSaveterm))
  	{
  		printx(DEBUG, HVC, "Connected to an interface\n");
  		sendData(&hvc_fd, GET_DEVICE_MODEL);
  		printx(DEBUG, HVC, "Sended identification request %02x\n", GET_DEVICE_MODEL);
  		data = getData(&hvc_fd);
  		if(data == DEVICE_MODEL_HVC)
  			printx(INFO, HVC, "HVC Arduino connected\n");
  		else
  			printx(DEBUG, HVC, "HVC Arduino not found, response was %02x\n", data);
  	}
  	else
  	{
  		printx(WARNING, HVC, "Unable to connect to such interface\n");
  		return false;
  	}
  
b4a7f4ac   henyxia   Querying for pump...
128
129
130
  	sendData(&hvc_fd, GET_PUMP);
  	data = getData(&hvc_fd);
  
717e403d   henyxia   Sperated output
131
132
  	previousPumpState = (data == PUMP_STATE_HIGH);
  	printf("Data received from HVC for PUMP %02x\n", data);
b4a7f4ac   henyxia   Querying for pump...
133
  
9e9f365e   henyxia   Added HVC interface
134
135
136
137
138
  	return true;
  }
  
  void* processHVC(void* we)
  {
b4a7f4ac   henyxia   Querying for pump...
139
140
141
  	uint8_t	data;
  	bool	pumpState;
  	float	temp;
9e9f365e   henyxia   Added HVC interface
142
143
144
  
  	while(!hvcStop)
  	{
e912f423   henyxia   Urgent fix
145
  		printx(DEBUG, HVC, "Querying data, with prev PUMP %d\n", pumped);
9e9f365e   henyxia   Added HVC interface
146
147
148
  		sendData(&hvc_fd, GET_TEMP);
  		data = getData(&hvc_fd);
  		setTemp(data);
8dca7aeb   henyxia   Automatic Pumping...
149
  		temp = 25 + (((float)(80*data)))/255;
bf0cbb04   Pierre Letousey   Automatic regulation
150
  		actTemp(temp);
5808c112   henyxia   Temperature now o...
151
  		printx(DEBUG, TEMP, "TEMP %f\n", temp);
5dd66ab0   henyxia   Added timer optio...
152
153
154
155
156
157
158
  		if(tHeatTimer > 0)
  		{
  			tHeatStop = clock();
  			if(((double)(tHeatStop - tHeatStart) / CLOCKS_PER_SEC) > tHeatTimer)
  			{
  				wHeat = false;
  				tHeatTimer = 0;
5dd66ab0   henyxia   Added timer optio...
159
  			}
5dd66ab0   henyxia   Added timer optio...
160
161
162
163
164
165
166
167
168
  		}
  
  		if(tPumpTimer > 0)
  		{
  			tPumpStop = clock();
  			if(((double)(tPumpStop - tPumpStart) / CLOCKS_PER_SEC) > tPumpTimer)
  			{
  				wPump = false;
  				tPumpTimer = 0;
5dd66ab0   henyxia   Added timer optio...
169
170
171
  			}
  		}
  
ac0ae6b0   henyxia   HVC can now use t...
172
173
174
175
176
  		if(wHeat ^ sHeat)
  		{
  			sendData(&hvc_fd, wHeat ? SET_HEAT_ON : SET_HEAT_OFF);
  			sHeat = wHeat;
  			setHeat(sHeat);
5dd66ab0   henyxia   Added timer optio...
177
178
  			if(sHeat)
  				tHeatStart = clock();
ac0ae6b0   henyxia   HVC can now use t...
179
180
181
182
183
184
185
  		}
  
  		if(wPump ^ sPump)
  		{
  			sendData(&hvc_fd, wPump ? SET_PUMP_ON : SET_PUMP_OFF);
  			sPump = wPump;
  			setPump(sPump);
5dd66ab0   henyxia   Added timer optio...
186
187
  			if(sPump)
  				tPumpStart = clock();
ac0ae6b0   henyxia   HVC can now use t...
188
189
  		}
  
b4a7f4ac   henyxia   Querying for pump...
190
  		// Querying pump state
717e403d   henyxia   Sperated output
191
  		sendData(&hvc_fd, GET_PUMP);
b4a7f4ac   henyxia   Querying for pump...
192
  		data = getData(&hvc_fd);
717e403d   henyxia   Sperated output
193
194
  		pumpState = (data == PUMP_STATE_HIGH);
  		printx(DEBUG, HVC, "PUMP %d %d Flow : %d\n", previousPumpState, pumpState, pumped);
b4a7f4ac   henyxia   Querying for pump...
195
196
197
  		if(pumpState ^ previousPumpState)
  			pumped++;
  		previousPumpState = pumpState;
717e403d   henyxia   Sperated output
198
  		actDeb(pumped);
b4a7f4ac   henyxia   Querying for pump...
199
  
9e9f365e   henyxia   Added HVC interface
200
201
202
203
204
  		usleep(HVC_POLLING_TIME);
  	}
  
  	return NULL;
  }