-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValve.cpp
More file actions
136 lines (113 loc) · 2.47 KB
/
Valve.cpp
File metadata and controls
136 lines (113 loc) · 2.47 KB
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
//Valve.cpp
#include "Valve.h"
#include <string>
#include "/home/pi/bcm2835-1.50/src/bcm2835.h"
#include <iostream>
using namespace std;
Valve::Valve(string name_arg,
bool normal_open, int port_number_arg)
{
/*
if(!bcm2835_init())
{
printf("bcm2835_init failed. are you root?\n");
return;
}*/
is_normal_open = normal_open;
port_number = port_number_arg;
name = name_arg;
if(port_number < 0 || port_number > 7)
printf("Bad portnumber %d. Must be 1 to 7\n", port_number);
switch(port_number)
{
case(1):
bcm_pin_actuate = RPI_BPLUS_GPIO_J8_03;
break;
case(2):
bcm_pin_actuate = RPI_BPLUS_GPIO_J8_05;
break;
case(3):
bcm_pin_actuate = RPI_BPLUS_GPIO_J8_07;
break;
case(4):
bcm_pin_actuate = RPI_BPLUS_GPIO_J8_08;
break;
case(5):
bcm_pin_actuate = RPI_BPLUS_GPIO_J8_10;
break;
case(6):
bcm_pin_actuate = RPI_BPLUS_GPIO_J8_11;
break;
case(7):
bcm_pin_actuate = RPI_BPLUS_GPIO_J8_12;
break;
default:
bcm_pin_actuate = RPI_BPLUS_GPIO_J8_40; //an unused pin
}
bcm2835_gpio_fsel(bcm_pin_actuate, BCM2835_GPIO_FSEL_OUTP);
cout << "made a valve with name " << name;
if(normal_open)
cout << " normal open";
else
cout << " normal closed";
cout << " port num: " << port_number << endl;
}
string Valve::get_name()
{
return name;
}
bool Valve::get_normal_open()
{
return is_normal_open;
}
int Valve::get_port_number()
{
return port_number;
}
bool Valve::actuate()
{
bcm2835_gpio_write(bcm_pin_actuate, HIGH);
actuated = true;
return actuated;
}
bool Valve::deactuate()
{
bcm2835_gpio_write(bcm_pin_actuate, LOW);
actuated = false;
return actuated;
}
bool Valve::open_valve() {
if(is_normal_open)
deactuate();
else
actuate();
}
bool Valve::close_valve() {
if(is_normal_open)
actuate();
else
deactuate();
}
bool Valve::is_actuated()
{
return actuated;
}
//yes theres a shorter "better" way,
//but this is very readable
bool Valve::is_open()
{
if(actuated)
{
if(is_normal_open)
return false;
else
return true;
}
else
{
if(is_normal_open)
return true;
else
return false;
}
}