update firmware.bin
[clinton/Smoothieware.git] / src / modules / robot / 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.
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
20 Pauser::Pauser(){
21 paused_block = NULL;
22 }
23
24 void Pauser::on_module_loaded(){
25 this->counter = 0;
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 }
38 }
39
40 // Pause smoothie if nobody else is currently doing so
41 void Pauser::take(){
42 this->counter++;
43 //THEKERNEL->streams->printf("take: %u \r\n", this->counter );
44 if( this->counter == 1 ){
45 THEKERNEL->call_event(ON_PAUSE, &this->counter);
46 }
47 }
48
49 // Unpause smoothie unless something else is pausing it too
50 void Pauser::release(){
51 if( --this->counter <= 0 ){
52 this->counter= 0;
53 THEKERNEL->call_event(ON_PLAY, &this->counter);
54 if (paused_block)
55 {
56 Block* tmp = paused_block;
57 paused_block = NULL;
58 tmp->release();
59 }
60 }
61 }
62
63 // Return wether smoothie is paused
64 bool Pauser::paused(){
65 return (counter != 0);
66 }