Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Pauser.cpp Source File

Pauser.cpp

00001 #include "Pauser.h"
00002 
00003 #include "libs/Kernel.h"
00004 #include "Block.h"
00005 #include "libs/nuts_bolts.h"
00006 #include "libs/utils.h"
00007 
00008 #include <string>
00009 using namespace std;
00010 
00011 // 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 
00012 // ( 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, 
00013 // a pause event is called, and the pause does not end before all modules have called the release() method. 
00014 // Please note : Modules should keep track of their pause status themselves
00015 Pauser::Pauser(){
00016     paused_block = NULL;
00017 }
00018 
00019 void Pauser::on_module_loaded(){
00020     this->counter = 0;
00021     register_for_event(ON_BLOCK_BEGIN);
00022 }
00023 
00024 void Pauser::on_block_begin(void* argument)
00025 {
00026     Block* block = static_cast<Block*>(argument);
00027 
00028     if (counter)
00029     {
00030         block->take();
00031         paused_block = block;
00032     }
00033 }
00034 
00035 // Pause smoothie if nobody else is currently doing so
00036 void Pauser::take(){
00037     this->counter++;
00038     //THEKERNEL->streams->printf("take: %u \r\n", this->counter );
00039     if( this->counter == 1 ){
00040         THEKERNEL->call_event(ON_PAUSE, &this->counter);
00041     }
00042 }
00043 
00044 // Unpause smoothie unless something else is pausing it too
00045 void Pauser::release(){
00046     this->counter--;
00047     //THEKERNEL->streams->printf("release: %u \r\n", this->counter );
00048     if( this->counter == 0 ){
00049         THEKERNEL->call_event(ON_PLAY, &this->counter);
00050         if (paused_block)
00051         {
00052             Block* tmp = paused_block;
00053             paused_block = NULL;
00054             tmp->release();
00055         }
00056     }
00057 }
00058 
00059 // Return wether smoothie is paused
00060 bool Pauser::paused(){
00061     return (counter != 0);
00062 }