-
Notifications
You must be signed in to change notification settings - Fork 1
/
Air.cpp
110 lines (87 loc) · 2.32 KB
/
Air.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
#include "libs/Module.h"
#include "libs/Kernel.h"
#include "modules/communication/utils/Gcode.h"
#include "modules/robot/Stepper.h"
#include "Air.h"
#include "libs/nuts_bolts.h"
#include "Config.h"
#include "StreamOutputPool.h"
#include "Block.h"
#include "checksumm.h"
#include "ConfigValue.h"
#include "libs/Pin.h"
#include "Gcode.h"
#define air_module_enable_checksum CHECKSUM("air_module_enable")
#define air_module_pin_checksum CHECKSUM("air_module_pin")
Air::Air(){}
void Air::on_module_loaded() {
// Check if the module should be loaded
if( !THEKERNEL
->config
->value(air_module_enable_checksum)
->by_default(false)
->as_bool()
){
delete this;
return;
}
// Get the pin variable from config
this->valve_pin.from_string(
THEKERNEL
->config
->value(air_module_pin_checksum)
->by_default("nc")
->as_string()
)->as_output();
if (!this->valve_pin.connected()){
THEKERNEL->streams->printf("Error using this pin number. Air module disabled.\n");
delete this;
return;
}
// Register for kernel events
this->register_for_event(ON_GCODE_EXECUTE);
this->register_for_event(ON_PLAY);
this->register_for_event(ON_PAUSE);
this->register_for_event(ON_BLOCK_BEGIN);
this->register_for_event(ON_BLOCK_END);
}
// Turn of the valve at the end of blocks
void Air::on_block_end(void* argument){
this->valve_pin.set(false);
}
// Turn on the valve if needed at the beginning of a block
void Air::on_block_begin(void* argument){
if(this->valve_on && THEKERNEL->stepper->get_current_block()){
this->valve_pin.set(true);
} else {
this->valve_pin.set(false);
}
}
// Turn off the valve on pause
void Air::on_pause(void* argument){
this->valve_pin.set(false);
}
// Turn on the valve if needed on the play event
void Air::on_play(void* argument){
if(this->valve_on && THEKERNEL->stepper->get_current_block()){
this->valve_pin.set(true);
} else {
this->valve_pin.set(false);
}
}
// Turn valve on / off depending on the G Code supplied
void Air::on_gcode_execute(void* argument){
Gcode* gcode = static_cast<Gcode*>(argument);
this->valve_on = false;
if( gcode->has_g){
int code = gcode->g;
if( code == 0 ){
// No ink, since G0 code
this->valve_pin.set(false);
this->valve_on = false;
}else if( code >= 1 && code <= 3 ){
// Ink flow, since G1,G2 or G3 code
this->valve_on = true;
}
}
}