Commit | Line | Data |
---|---|---|
423df6df | 1 | #include "Pauser.h" |
43b1a6e8 MM |
2 | |
3 | #include "libs/Kernel.h" | |
4 | #include "Block.h" | |
423df6df AW |
5 | #include "libs/nuts_bolts.h" |
6 | #include "libs/utils.h" | |
43b1a6e8 | 7 | |
423df6df AW |
8 | #include <string> |
9 | using namespace std; | |
10 | ||
6a240668 JM |
11 | // The Pauser module is the core of the pausing subsystem in smoothie. |
12 | // Basically we want several modules to be able to pause smoothie at the same | |
13 | // time ( think both the user with a button, and the temperature control | |
14 | // because a temperature is not reached ). To do that, modules call the take() | |
15 | // methode, a pause event is called, and the pause does not end before all | |
16 | // modules have called the release() method. | |
17 | // Please note : Modules should keep track of their pause status themselves... | |
18 | // or Not for instance it may be better for the play/pause button to be able | |
19 | // to unpause when something else causes a pause | |
43b1a6e8 MM |
20 | Pauser::Pauser(){ |
21 | paused_block = NULL; | |
22 | } | |
423df6df AW |
23 | |
24 | void Pauser::on_module_loaded(){ | |
25 | this->counter = 0; | |
43b1a6e8 MM |
26 | register_for_event(ON_BLOCK_BEGIN); |
27 | } | |
28 | ||
29 | void Pauser::on_block_begin(void* argument) | |
30 | { | |
31 | Block* block = static_cast<Block*>(argument); | |
32 | ||
33 | if (counter) | |
34 | { | |
35 | block->take(); | |
36 | paused_block = block; | |
37 | } | |
423df6df AW |
38 | } |
39 | ||
93694d6b | 40 | // Pause smoothie if nobody else is currently doing so |
423df6df AW |
41 | void Pauser::take(){ |
42 | this->counter++; | |
314ab8f7 | 43 | //THEKERNEL->streams->printf("take: %u \r\n", this->counter ); |
423df6df | 44 | if( this->counter == 1 ){ |
314ab8f7 | 45 | THEKERNEL->call_event(ON_PAUSE, &this->counter); |
423df6df AW |
46 | } |
47 | } | |
48 | ||
93694d6b | 49 | // Unpause smoothie unless something else is pausing it too |
423df6df | 50 | void Pauser::release(){ |
6a240668 JM |
51 | if( --this->counter <= 0 ){ |
52 | this->counter= 0; | |
314ab8f7 | 53 | THEKERNEL->call_event(ON_PLAY, &this->counter); |
43b1a6e8 MM |
54 | if (paused_block) |
55 | { | |
4e7a5763 | 56 | Block* tmp = paused_block; |
43b1a6e8 | 57 | paused_block = NULL; |
4e7a5763 | 58 | tmp->release(); |
43b1a6e8 | 59 | } |
423df6df AW |
60 | } |
61 | } | |
8e075270 | 62 | |
93694d6b AW |
63 | // Return wether smoothie is paused |
64 | bool Pauser::paused(){ | |
8e075270 MM |
65 | return (counter != 0); |
66 | } |