-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirr_utils.c
156 lines (126 loc) · 3.6 KB
/
irr_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//---------------------------------------------------------------------------
// Copyright (C) 2009-2014 Robin Gilks
//
//
// irr_utils.c - This section looks after the various utility functions
//
// 0.70 - split into several files
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "irrigate.h"
/**
* Retrieve user input from the console.
*
* min minimum number to accept
* max maximum number to accept
*
* @return numeric value entered from the console.
*/
int
getNumber (int min, int max)
{
int value = min, cnt;
bool done = FALSE;
do
{
cnt = scanf ("%d", &value);
if (cnt > 0 && (value > max || value < min))
{
printf ("Value (%d) is outside of the limits (%d,%d)\n", value, min, max);
printf ("Try again:\n");
}
else
done = TRUE;
}
while (!done);
return value;
}
char *
getAddr (uint8_t * SNum)
{
static char printbuf[200];
sprintf (printbuf, "%02x%02x%02x%02x%02x%02x%02x%02x", SNum[0], SNum[1],
SNum[2], SNum[3], SNum[4], SNum[5], SNum[6], SNum[7]);
return printbuf;
}
// convert clock hours to time based on current seconds count
time_t
hours2time(uint16_t decahours)
{
time_t starttime;
struct tm tm;
localtime_r (&basictime, &tm);
tm.tm_hour = decahours / 100;
tm.tm_min = decahours - (tm.tm_hour * 100); // might allow mins later
tm.tm_sec = 0;
starttime = mktime (&tm);
return starttime;
}
// get expected solenoid current by adding up all the active valves
uint16_t get_expected_current(void)
{
uint16_t current = 0;
uint8_t zone;
for (zone = 1; zone < REALZONES; zone++)
{
// we're only interested in zones that are active
if ((chanmap[zone].output == ON) || (chanmap[zone].output == TEST))
current += chanmap[zone].current;
}
return current;
}
// get expected flow rate by adding up all the active zones
// skip pumps
uint16_t get_expected_flow(void)
{
uint16_t flow = 0;
uint8_t zone;
for (zone = 1; zone < REALZONES; zone++)
{
// we're only interested in zones that are active but not the well pump or any other feed
if ((chanmap[zone].output == ON) && ((chanmap[zone].type & ISPUMP) == 0))
flow += chanmap[zone].flow;
}
return flow;
}
// get maximum flow rate of the pumping system by finding the highest capacity pump value
uint16_t get_nominal_flow(void)
{
static uint16_t nomflow = 0;
uint8_t pump;
if (nomflow == 0)
{
for (pump = 0; pumpmap[pump].zone; pump++)
{
// we're only interested in pumps
if (pumpmap[pump].nomflow > nomflow)
{
nomflow = pumpmap[pump].nomflow;
}
}
}
return nomflow;
}
// get the pump number fro mthe zone number
uint8_t get_pump_by_zone(uint8_t zone)
{
uint8_t pump;
for (pump = 1; pump < MAXPUMPS; pump++)
{
// we're only interested in pumps
if (pumpmap[pump].zone == zone)
return pump;
}
return 0;
}