cleanup before merging
[clinton/Smoothieware.git] / src / modules / robot / Player.cpp
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl) with additions from Sungeun K. Jeon (https://github.com/chamnit/grbl)
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8 using namespace std;
9 #include <vector>
10 #include "mbed.h"
11 #include "libs/nuts_bolts.h"
12 #include "libs/RingBuffer.h"
13 #include "../communication/utils/Gcode.h"
14 #include "libs/Module.h"
15 #include "libs/Kernel.h"
16 #include "Block.h"
17 #include "Player.h"
18 #include "Planner.h"
19
20 Player::Player(){
21 this->current_block = NULL;
22 this->looking_for_new_block = false;
23 }
24
25 // Append a block to the list
26 Block* Player::new_block(){
27
28 // Clean up the vector of commands in the block we are about to replace
29 // It is quite strange to do this here, we really should do it inside Block->pop_and_execute_gcode
30 // but that function is called inside an interrupt and thus can break everything if the interrupt was trigerred during a memory access
31 //Block* block = this->queue.get_ref( this->queue.size()-1 );
32 Block* block = this->queue.get_ref( this->queue.size() );
33 if( block->player == this ){
34 for(short index=0; index<block->gcodes.size(); index++){
35 block->gcodes.pop_back();
36 }
37 }
38
39 // Create a new virgin Block in the queue
40 this->queue.push_back(Block());
41 block = this->queue.get_ref( this->queue.size()-1 );
42 block->is_ready = false;
43 block->initial_rate = -2;
44 block->final_rate = -2;
45 block->player = this;
46
47 return block;
48 }
49
50 // Used by blocks to signal when they are ready to be used by the system
51 void Player::new_block_added(){
52 if( this->current_block == NULL ){
53 this->pop_and_process_new_block(33);
54 }
55 }
56
57 // Process a new block in the queue
58 void Player::pop_and_process_new_block(int debug){
59 if( this->looking_for_new_block ){ return; }
60 this->looking_for_new_block = true;
61
62 if( this->current_block != NULL ){ this->looking_for_new_block = false; return; }
63
64 // Return if queue is empty
65 if( this->queue.size() == 0 ){
66 this->current_block = NULL;
67 // TODO : ON_QUEUE_EMPTY event
68 this->looking_for_new_block = false;
69 return;
70 }
71
72 // Get a new block
73 this->current_block = this->queue.get_ref(0);
74
75 // Tell all modules about it
76 this->kernel->call_event(ON_BLOCK_BEGIN, this->current_block);
77
78 // In case the module was not taken
79 if( this->current_block->times_taken < 1 ){
80 this->looking_for_new_block = false;
81 this->current_block->release();
82 }
83
84 this->looking_for_new_block = false;
85
86 }