provide one method to add gcode to blocks, reduce code duplication
[clinton/Smoothieware.git] / src / modules / robot / Conveyor.cpp
index 0461bd2..f91709f 100644 (file)
@@ -18,66 +18,54 @@ using namespace std;
 #include "Conveyor.h"
 #include "Planner.h"
 
+// The conveyor holds the queue of blocks, takes care of creating them, and starting the executing chain of blocks
+
 Conveyor::Conveyor(){
     this->current_block = NULL;
     this->looking_for_new_block = false;
     flush_blocks = 0;
 }
 
-void Conveyor::on_module_loaded()
-{
+void Conveyor::on_module_loaded(){
     register_for_event(ON_IDLE);
 }
 
-void Conveyor::on_idle(void* argument)
-{
-    if (flush_blocks)
-    {
-        Block* block = queue.get_ref(0);
-//         printf("Block: clean %p\n", block);
-        while (block->gcodes.size())
-        {
-            Gcode* g = block->gcodes.back();
-            block->gcodes.pop_back();
-            delete g;
-        }
-        queue.delete_first();
-
+// Delete blocks here, because they can't be deleted in interrupt context ( see Block.cpp:release )
+void Conveyor::on_idle(void* argument){
+    while (flush_blocks > 0){
+        // Cleanly delete block
+        Block* block = queue.get_tail_ref();
+        block->gcodes.clear();
+        queue.delete_tail();
         __disable_irq();
         flush_blocks--;
         __enable_irq();
     }
 }
 
+void Conveyor::append_gcode(Gcode* gcode)
+{
+    gcode->mark_as_taken();
+    if (queue.size() == 0)
+        THEKERNEL->call_event(ON_GCODE_EXECUTE, gcode);
+    else
+        queue.get_ref(queue.size() - 1)->append_gcode(gcode);
+}
+
 // Append a block to the list
 Block* Conveyor::new_block(){
 
-    // Clean up the vector of commands in the block we are about to replace
-    // It is quite strange to do this here, we really should do it inside Block->pop_and_execute_gcode
-    // but that function is called inside an interrupt and thus can break everything if the interrupt was trigerred during a memory access
-
     // Take the next untaken block on the queue ( the one after the last one )
-    Block* block = this->queue.get_tail_ref();
+    Block* block = this->queue.get_head_ref();
     // Then clean it up
-    if( block->conveyor == this ){
-        for(; block->gcodes.size(); ){
-            Gcode* g = block->gcodes.back();
-            block->gcodes.pop_back();
-            delete g;
-        }
-    }
+    block->clear();
 
-    // Create a new virgin Block in the queue
-    this->queue.push_back(Block());
-    block = this->queue.get_ref( this->queue.size()-1 );
-    while( block == NULL ){
-        block = this->queue.get_ref( this->queue.size()-1 );
-    }
-    block->is_ready = false;
     block->initial_rate = -2;
     block->final_rate = -2;
-    block->conveyor = this;
-    
+
+    // Create a new virgin Block in the queue
+    this->queue.push_back(Block());
+
     return block;
 }
 
@@ -107,13 +95,13 @@ void Conveyor::pop_and_process_new_block(int debug){
     this->current_block = this->queue.get_ref(0);
 
     // Tell all modules about it
-    this->kernel->call_event(ON_BLOCK_BEGIN, this->current_block);
+    THEKERNEL->call_event(ON_BLOCK_BEGIN, this->current_block);
 
-    // In case the module was not taken
+       // In case the module was not taken
     if( this->current_block->times_taken < 1 ){
-        Block* temp = this->current_block; 
-        this->current_block = NULL; // It seems this was missing and adding it fixes things, if something breaks, this may be a suspect 
-        temp->take(); 
+        Block* temp = this->current_block;
+        this->current_block = NULL; // It seems this was missing and adding it fixes things, if something breaks, this may be a suspect
+        temp->take();
         temp->release();
     }
 
@@ -121,9 +109,22 @@ void Conveyor::pop_and_process_new_block(int debug){
 
 }
 
-void Conveyor::wait_for_queue(int free_blocks)
-{
+// Wait for the queue to have a given number of free blocks
+void Conveyor::wait_for_queue(int free_blocks){
     while( this->queue.size() >= this->queue.capacity()-free_blocks ){
-        this->kernel->call_event(ON_IDLE);
+        THEKERNEL->call_event(ON_IDLE);
     }
 }
+
+// Wait for the queue to be empty
+void Conveyor::wait_for_empty_queue(){
+    while( this->queue.size() > 0){
+        THEKERNEL->call_event(ON_IDLE);
+    }
+}
+
+// Return true if the queue is empty
+bool Conveyor::is_queue_empty(){
+    return (this->queue.size() == 0);
+}
+