Blame view

hvc.c 2.67 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
16
17
18
  #include "ui.h"
  #include "hvc.h"
  #include "printx.h"
  #include "serial.h"
  
  #define	DEVICE1				"/dev/ttyACM1"
  #define	SPEED				B19200
ac0ae6b0   henyxia   HVC can now use t...
19
  #define HVC_POLLING_TIME	50000
9e9f365e   henyxia   Added HVC interface
20
21
22
23
  #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...
24
25
26
27
  #define	SET_HEAT_ON			'E'
  #define	SET_HEAT_OFF		'G'
  #define	SET_PUMP_ON			'B'
  #define	SET_PUMP_OFF		'D'
9e9f365e   henyxia   Added HVC interface
28
29
30
31
32
33
34
  
  // Globals
  int		hvc_fd = -1;
  bool	hvcStop = false;
  bool	sPump = false;
  bool	sHeat = false;
  struct	termios hvcSaveterm;
ac0ae6b0   henyxia   HVC can now use t...
35
36
  bool	wHeat = false;
  bool	wPump = false;
5dd66ab0   henyxia   Added timer optio...
37
38
39
40
41
42
  time_t	tHeatStart;
  time_t	tHeatStop;
  double	tHeatTimer;
  time_t	tPumpStart;
  double	tPumpTimer;
  time_t	tPumpStop;
ac0ae6b0   henyxia   HVC can now use t...
43
44
45
46
47
48
49
50
  
  void stopHVC()
  {
  	hvcStop = true;
  }
  
  void setPumpWantedState(bool s)
  {
5dd66ab0   henyxia   Added timer optio...
51
52
  	tPumpStart = clock();
  	tPumpStop = clock();
ac0ae6b0   henyxia   HVC can now use t...
53
54
55
56
57
  	wPump = s;
  }
  
  void setHeatWantedState(bool s)
  {
5dd66ab0   henyxia   Added timer optio...
58
59
  	tHeatStart = clock();
  	tHeatStop = clock();
ac0ae6b0   henyxia   HVC can now use t...
60
61
  	wHeat = s;
  }
9e9f365e   henyxia   Added HVC interface
62
  
5dd66ab0   henyxia   Added timer optio...
63
64
65
66
67
68
69
70
71
72
  void setPumpTimer(double t)
  {
  	tPumpTimer = t;
  }
  
  void setHeatTimer(double t)
  {
  	tHeatTimer = t;
  }
  
9e9f365e   henyxia   Added HVC interface
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  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;
  	}
  
  	return true;
  }
  
  void* processHVC(void* we)
  {
  	uint8_t data;
  
  	while(!hvcStop)
  	{
  		printx(DEBUG, HVC, "Querying data\n");
  		sendData(&hvc_fd, GET_TEMP);
  		data = getData(&hvc_fd);
  		setTemp(data);
9e9f365e   henyxia   Added HVC interface
107
  
5dd66ab0   henyxia   Added timer optio...
108
109
110
111
112
113
114
  		if(tHeatTimer > 0)
  		{
  			tHeatStop = clock();
  			if(((double)(tHeatStop - tHeatStart) / CLOCKS_PER_SEC) > tHeatTimer)
  			{
  				wHeat = false;
  				tHeatTimer = 0;
5dd66ab0   henyxia   Added timer optio...
115
116
117
118
119
120
121
122
123
124
125
  			}
  
  		}
  
  		if(tPumpTimer > 0)
  		{
  			tPumpStop = clock();
  			if(((double)(tPumpStop - tPumpStart) / CLOCKS_PER_SEC) > tPumpTimer)
  			{
  				wPump = false;
  				tPumpTimer = 0;
5dd66ab0   henyxia   Added timer optio...
126
127
128
  			}
  		}
  
ac0ae6b0   henyxia   HVC can now use t...
129
130
131
132
133
  		if(wHeat ^ sHeat)
  		{
  			sendData(&hvc_fd, wHeat ? SET_HEAT_ON : SET_HEAT_OFF);
  			sHeat = wHeat;
  			setHeat(sHeat);
5dd66ab0   henyxia   Added timer optio...
134
135
  			if(sHeat)
  				tHeatStart = clock();
ac0ae6b0   henyxia   HVC can now use t...
136
137
138
139
140
141
142
  		}
  
  		if(wPump ^ sPump)
  		{
  			sendData(&hvc_fd, wPump ? SET_PUMP_ON : SET_PUMP_OFF);
  			sPump = wPump;
  			setPump(sPump);
5dd66ab0   henyxia   Added timer optio...
143
144
  			if(sPump)
  				tPumpStart = clock();
ac0ae6b0   henyxia   HVC can now use t...
145
146
  		}
  
9e9f365e   henyxia   Added HVC interface
147
148
149
150
151
  		usleep(HVC_POLLING_TIME);
  	}
  
  	return NULL;
  }