Merge remote-tracking branch 'upstream/edge' into add/universal-panel-adapter
[clinton/Smoothieware.git] / src / libs / Pauser.cpp
1 #include "Pauser.h"
2
3 #include "libs/Kernel.h"
4 #include "Block.h"
5 #include "libs/nuts_bolts.h"
6 #include "libs/utils.h"
7
8 #include <string>
9 using namespace std;
10
11 // The Pauser module is the core of the pausing subsystem in smoothie. Basically we want several modules to be able to pause smoothie at the same time
12 // ( think both the user with a button, and the temperature control because a temperature is not reached ). To do that, modules call the take() methode,
13 // a pause event is called, and the pause does not end before all modules have called the release() method.
14 // Please note : Modules should keep track of their pause status themselves
15 Pauser::Pauser(){
16 paused_block = NULL;
17 }
18
19 void Pauser::on_module_loaded(){
20 this->counter = 0;
21 register_for_event(ON_BLOCK_BEGIN);
22 }
23
24 void Pauser::on_block_begin(void* argument)
25 {
26 Block* block = static_cast<Block*>(argument);
27
28 if (counter)
29 {
30 block->take();
31 paused_block = block;
32 }
33 }
34
35 // Pause smoothie if nobody else is currently doing so
36 void Pauser::take(){
37 this->counter++;
38 //THEKERNEL->streams->printf("take: %u \r\n", this->counter );
39 if( this->counter == 1 ){
40 THEKERNEL->call_event(ON_PAUSE, &this->counter);
41 }
42 }
43
44 // Unpause smoothie unless something else is pausing it too
45 void Pauser::release(){
46 this->counter--;
47 //THEKERNEL->streams->printf("release: %u \r\n", this->counter );
48 if( this->counter == 0 ){
49 THEKERNEL->call_event(ON_PLAY, &this->counter);
50 if (paused_block)
51 {
52 Block* tmp = paused_block;
53 paused_block = NULL;
54 tmp->release();
55 }
56 }
57 }
58
59 // Return wether smoothie is paused
60 bool Pauser::paused(){
61 return (counter != 0);
62 }