-
Notifications
You must be signed in to change notification settings - Fork 0
/
fan.cpp
130 lines (87 loc) · 2.83 KB
/
fan.cpp
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
////////////////////////////////////////////////////////////////////////////////////////
//
// humifand - the humidity fan deamon
//
// Please check www.way2.net for more information
//
// (c) Copyright 2014 by way2.net Services.
//
////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <bcm2835.h>
#include <time.h>
#include "fan.h"
#include "logger.h"
#include "conffile.h"
////////////////////////////////////////////////////////////////////////////////////////
CFan* CFan::m_pThis = NULL;
////////////////////////////////////////////////////////////////////////////////////////
CFan::CFan()
{
m_Fan_pin_sck = -1;
m_FanSpinning = false;
m_LastStartTime = 0;
m_RunTime = 0;
}
////////////////////////////////////////////////////////////////////////////////////////
CFan* CFan::getFan()
{
if (m_pThis == NULL)
{
m_pThis = new CFan();
}
return m_pThis;
}
////////////////////////////////////////////////////////////////////////////////////////
void CFan::InitPins(int f_Fan_pin_sck)
{
m_Fan_pin_sck = f_Fan_pin_sck;
// --- config as output, but set low first to avoid starting fans
bcm2835_gpio_write(m_Fan_pin_sck, LOW);
bcm2835_gpio_fsel(m_Fan_pin_sck, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(m_Fan_pin_sck, LOW);
}
////////////////////////////////////////////////////////////////////////////////////////
double CFan::GetRuntime(void)
{
if (m_FanSpinning)
{
// --- calculate the runtime of this "run"
double l_dt = difftime(time(0),m_LastStartTime);
// --- and return the total runtine
return m_RunTime+l_dt;
}
else
{
return m_RunTime;
}
}
////////////////////////////////////////////////////////////////////////////////////////
void CFan::Start(void)
{
// --- if already spinning - do nothing
if (m_FanSpinning) return;
if (CONF->coGetVerbose()) LOGGER->Log("Fan starting up");
// --- remember the time when we started the fan
m_LastStartTime = time(0);
// --- and switch it on
bcm2835_gpio_write(m_Fan_pin_sck, HIGH);
// -- remember the new state
m_FanSpinning = true;
}
////////////////////////////////////////////////////////////////////////////////////////
void CFan::Stop(void)
{
// --- if already stopped - do nothing
if (!m_FanSpinning) return;
if (CONF->coGetVerbose()) LOGGER->Log("Fan shutting down");
// --- and switch it off
bcm2835_gpio_write(m_Fan_pin_sck, LOW);
// --- set the total runtime to the old runtime + time of the current "run"
double l_oldRT = m_RunTime;
m_RunTime = GetRuntime();
double l_thisRT = m_RunTime-l_oldRT;
if (CONF->coGetVerbose()) LOGGER->Log("Fan was running for %f seconds, total runtime %f",l_thisRT,m_RunTime);
// -- remember the new state
m_FanSpinning = false;
}