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/>.
10 #include "libs/nuts_bolts.h"
11 #include "libs/RingBuffer.h"
12 #include "../communication/utils/Gcode.h"
13 #include "libs/Module.h"
14 #include "libs/Kernel.h"
15 #include "Timer.h" // mbed.h lib
16 #include "wait_api.h" // mbed.h lib
22 this->current_block
= NULL
;
23 this->looking_for_new_block
= false;
26 // Append a block to the list
27 Block
* Player::new_block(){
29 // Clean up the vector of commands in the block we are about to replace
30 // It is quite strange to do this here, we really should do it inside Block->pop_and_execute_gcode
31 // but that function is called inside an interrupt and thus can break everything if the interrupt was trigerred during a memory access
32 //Block* block = this->queue.get_ref( this->queue.size()-1 );
33 Block
* block
= this->queue
.get_ref( this->queue
.size() );
34 if( block
->player
== this ){
35 for(short index
=0; index
<block
->gcodes
.size(); index
++){
36 block
->gcodes
.pop_back();
40 // Create a new virgin Block in the queue
41 this->queue
.push_back(Block());
42 block
= this->queue
.get_ref( this->queue
.size()-1 );
43 block
->is_ready
= false;
44 block
->initial_rate
= -2;
45 block
->final_rate
= -2;
51 // Used by blocks to signal when they are ready to be used by the system
52 void Player::new_block_added(){
53 if( this->current_block
== NULL
){
54 this->pop_and_process_new_block(33);
58 // Process a new block in the queue
59 void Player::pop_and_process_new_block(int debug
){
60 if( this->looking_for_new_block
){ return; }
61 this->looking_for_new_block
= true;
63 if( this->current_block
!= NULL
){ this->looking_for_new_block
= false; return; }
65 // Return if queue is empty
66 if( this->queue
.size() == 0 ){
67 this->current_block
= NULL
;
68 // TODO : ON_QUEUE_EMPTY event
69 this->looking_for_new_block
= false;
74 this->current_block
= this->queue
.get_ref(0);
76 // Tell all modules about it
77 this->kernel
->call_event(ON_BLOCK_BEGIN
, this->current_block
);
79 // In case the module was not taken
80 if( this->current_block
->times_taken
< 1 ){
81 this->looking_for_new_block
= false;
82 this->current_block
->release();
85 this->looking_for_new_block
= false;
89 void Player::wait_for_queue(int free_blocks
){
91 while( this->queue
.size() >= this->queue
.capacity()-free_blocks
){
94 this->kernel
->call_event(ON_IDLE
);
97 wait_us(500 - t
.read_us());