kernel->serial is dead
authorArthur Wolf <wolf.arthur@gmail.com>
Sun, 8 Jul 2012 11:01:16 +0000 (13:01 +0200)
committerArthur Wolf <wolf.arthur@gmail.com>
Sun, 8 Jul 2012 11:01:16 +0000 (13:01 +0200)
src/libs/ConfigSources/FileConfigSource.cpp
src/libs/Kernel.cpp
src/libs/Kernel.h
src/libs/StreamOutput.h
src/libs/StreamOutputPool.h [new file with mode: 0644]
src/libs/USBCDCMSC/USBCDCMSC.cpp
src/main.cpp
src/modules/communication/SerialConsole.cpp
src/modules/robot/Block.cpp
src/modules/robot/Planner.cpp
src/modules/robot/Stepper.cpp

index 8807a2d..c64c700 100644 (file)
@@ -85,7 +85,7 @@ void FileConfigSource::write( string setting, string value ){
             if( candidate.compare(setting) != 0 ){ buffer.clear(); continue; }
             int free_space = int(int(buffer.find_first_of("\r\n#", begin_value+1))-begin_value);
             if( int(value.length()) >= free_space ){
-                //this->kernel->serial->printf("ERROR: Not enough room for value\r\n");
+                //this->kernel->streams->printf("ERROR: Not enough room for value\r\n");
                 fclose(lp);
                 return;
             }
@@ -103,7 +103,7 @@ void FileConfigSource::write( string setting, string value ){
         }
     } while (c != EOF);
     fclose(lp);
-    //this->kernel->serial->printf("ERROR: configuration key not found\r\n");
+    //this->kernel->streams->printf("ERROR: configuration key not found\r\n");
 }
 
 // Return the value for a specific checksum
index 2655418..49570a9 100644 (file)
@@ -15,6 +15,7 @@ using namespace std;
 #include "libs/Adc.h"
 #include "libs/Digipot.h"
 #include "libs/Pauser.h"
+#include "libs/StreamOutputPool.h"
 
 #include "modules/communication/SerialConsole.h"
 #include "modules/communication/GcodeDispatch.h"
@@ -49,11 +50,13 @@ Kernel::Kernel(){
     // Config first, because we need the baud_rate setting before we start serial 
     this->config         = new Config();
     // Serial second, because the other modules might want to say something
-    this->serial         = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
+    this->streams        = new StreamOutputPool();
 
+    this->serial         = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(9600)->as_number());
+    
     this->add_module( this->config );
     this->add_module( this->serial );
-  
+
     // HAL stuff 
     this->slow_ticker          = new SlowTicker();
     this->step_ticker          = new StepTicker();
index 92d52f7..2b64fab 100644 (file)
@@ -10,6 +10,7 @@
 #include "libs/Module.h"
 #include "libs/Config.h"
 #include "libs/SlowTicker.h"
+#include "libs/StreamOutputPool.h"
 #include "libs/StepTicker.h"
 #include "libs/Adc.h"
 #include "libs/Digipot.h"
@@ -55,6 +56,8 @@ class Kernel {
 
         // These modules are aviable to all other modules
         SerialConsole*    serial;
+        StreamOutputPool* streams; 
+
         GcodeDispatch*    gcode_dispatch;
         Robot*            robot;
         Stepper*          stepper;
index ef17502..5760f3b 100644 (file)
@@ -1,3 +1,10 @@
+/*  
+      This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
+      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.
+      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.
+      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. 
+*/
+
 #ifndef STREAMOUTPUT_H
 #define STREAMOUTPUT_H
 
diff --git a/src/libs/StreamOutputPool.h b/src/libs/StreamOutputPool.h
new file mode 100644 (file)
index 0000000..4833a91
--- /dev/null
@@ -0,0 +1,61 @@
+/*  
+      This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
+      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.
+      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.
+      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. 
+*/
+
+#ifndef STREAMOUTPUTPOOL_H
+#define STREAMOUTPUTPOOL_H
+
+using namespace std;
+#include <vector>
+#include <string>
+#include <cstdio>
+#include <cstdarg>
+
+#include "libs/StreamOutput.h"
+
+
+
+class StreamOutputPool {
+    public:
+       StreamOutputPool(){}
+       int printf(const std::string format, ...){
+            // Make the message 
+            va_list args;
+            va_start(args, format);
+            int size = format.size() * 2;
+            char* buffer = new char[size];
+            while (vsprintf(buffer, format.c_str(), args) < 0){
+                delete[] buffer;
+                size *= 2;
+                buffer = new char[size];
+            }
+            string message = std::string(buffer); 
+            va_end(args);
+            
+            // Dispatch to all
+            for(unsigned int i=0; i < this->streams.size(); i++){
+                this->streams.at(i)->printf(message.c_str());
+            }
+       
+       }
+
+       void append_stream(StreamOutput* stream){
+           this->streams.push_back(stream);
+       }
+
+
+       vector<StreamOutput*> streams;
+};
+
+
+
+
+
+
+
+
+
+#endif
index e142471..04a0b13 100644 (file)
@@ -933,6 +933,10 @@ void USBCDCMSC::on_module_loaded(){
 \r
     // We only call the command dispatcher in the main loop, nowhere else\r
     this->register_for_event(ON_MAIN_LOOP);\r
+\r
+    // Add to the pack of streams kernel can call to, for example for broadcasting\r
+    this->kernel->streams->append_stream(this);\r
+\r
 }\r
 \r
 \r
@@ -941,9 +945,6 @@ void USBCDCMSC::on_module_loaded(){
 \r
 \r
 void USBCDCMSC::on_main_loop(void* argument){\r
-    //if( this->configured() ){\r
-    //    this->kernel->serial->printf("a:%d\r\n", this->buffer.size());\r
-    //}\r
     if( this->has_char('\n') ){\r
         int index = 0;\r
         string received;\r
@@ -955,8 +956,6 @@ void USBCDCMSC::on_main_loop(void* argument){
                 message.message = received;\r
                 message.stream = this;\r
                 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); \r
-               //this->kernel->serial->printf("received: %s \r\n", received.c_str() ); \r
-               //this->printf("received: %s\r\n", received.c_str() ); \r
                return;\r
             }else{\r
                 received += c;\r
@@ -972,9 +971,6 @@ void USBCDCMSC::on_serial_char_received(){
         char received = this->_getc();\r
         // convert CR to NL (for host OSs that don't send NL)\r
         if( received == '\r' ){ received = '\n'; }\r
-        //if( this->kernel != NULL ){ \r
-        //    this->kernel->serial->printf("received:%c\r\n", received); \r
-        //} \r
         this->buffer.push_back(received); \r
     }\r
 \r
index 3720cf8..fa3c318 100644 (file)
@@ -28,7 +28,7 @@ int main() {
 
     Kernel* kernel = new Kernel();
 
-    kernel->serial->printf("Smoothie ( grbl port ) version 0.6.1 \r\n");
+    kernel->streams->printf("Smoothie ( grbl port ) version 0.6.1 \r\n");
 
     kernel->add_module( new Laser(p21) );
     kernel->add_module( new Extruder() );
@@ -40,7 +40,7 @@ int main() {
 
     //kernel->add_module( &cdcmsc );
    
-    kernel->serial->printf("start\r\n");
+    kernel->streams->printf("start\r\n");
 
     while(1){
         kernel->call_event(ON_MAIN_LOOP);
index 1c4581d..7d75a28 100644 (file)
@@ -31,6 +31,9 @@ void SerialConsole::on_module_loaded() {
 
     // We only call the command dispatcher in the main loop, nowhere else
     this->register_for_event(ON_MAIN_LOOP);
+
+    // Add to the pack of streams kernel can call to, for example for broadcasting
+    this->kernel->streams->append_stream(this);
 }
         
 // Called on Serial::RxIrq interrupt, meaning we have received a char
index 4e3bf7d..97aeb10 100644 (file)
@@ -26,7 +26,7 @@ Block::Block(){
 }
 
 void Block::debug(Kernel* kernel){
-    kernel->serial->printf("%p: steps:%4d|%4d|%4d(max:%4d) nominal:r%10d/s%6.1f mm:%9.6f rdelta:%8d acc:%5d dec:%5d rates:%10d>%10d taken:%d ready:%d \r\n", this, this->steps[0], this->steps[1], this->steps[2], this->steps_event_count, this->nominal_rate, this->nominal_speed, this->millimeters, this->rate_delta, this->accelerate_until, this->decelerate_after, this->initial_rate, this->final_rate, this->times_taken, this->is_ready );
+    kernel->streams->printf("%p: steps:%4d|%4d|%4d(max:%4d) nominal:r%10d/s%6.1f mm:%9.6f rdelta:%8d acc:%5d dec:%5d rates:%10d>%10d taken:%d ready:%d \r\n", this, this->steps[0], this->steps[1], this->steps[2], this->steps_event_count, this->nominal_rate, this->nominal_speed, this->millimeters, this->rate_delta, this->accelerate_until, this->decelerate_after, this->initial_rate, this->final_rate, this->times_taken, this->is_ready );
 }
 
 
index c24182d..85632c7 100644 (file)
@@ -72,7 +72,7 @@ void Planner::append_block( int target[], double feed_rate, double distance, dou
         block->nominal_rate = 0;
     }
 
-    //this->kernel->serial->printf("nom_speed: %f nom_rate: %u step_event_count: %u block->steps_z: %u \r\n", block->nominal_speed, block->nominal_rate, block->steps_event_count, block->steps[2]  );
+    //this->kernel->streams->printf("nom_speed: %f nom_rate: %u step_event_count: %u block->steps_z: %u \r\n", block->nominal_speed, block->nominal_rate, block->steps_event_count, block->steps[2]  );
     
     // Compute the acceleration rate for the trapezoid generator. Depending on the slope of the line
     // average travel per step event changes. For a line along one axis the travel per step event
@@ -171,7 +171,7 @@ void Planner::append_block( int target[], double feed_rate, double distance, dou
 // 3. Recalculate trapezoids for all blocks.
 //
 void Planner::recalculate() {
-   //this->kernel->serial->printf("recalculate last: %p, queue size: %d \r\n", this->kernel->player->queue.get_ref( this->kernel->player->queue.size()-1  ), this->kernel->player->queue.size() );
+   //this->kernel->streams->printf("recalculate last: %p, queue size: %d \r\n", this->kernel->player->queue.get_ref( this->kernel->player->queue.size()-1  ), this->kernel->player->queue.size() );
    this->reverse_pass();
    this->forward_pass();
    this->recalculate_trapezoids();
@@ -227,7 +227,7 @@ void Planner::recalculate_trapezoids() {
     while(block_index != this->kernel->player->queue.tail){
         current = next;
         next = &this->kernel->player->queue.buffer[block_index];
-        //this->kernel->serial->printf("index:%d current:%p next:%p \r\n", block_index, current, next );
+        //this->kernel->streams->printf("index:%d current:%p next:%p \r\n", block_index, current, next );
         if( current ){
             // Recalculate if current block entry or exit junction speed has changed.
             if( current->recalculate_flag || next->recalculate_flag ){
@@ -248,7 +248,7 @@ void Planner::recalculate_trapezoids() {
 void Planner::dump_queue(){
     for( int index = 0; index <= this->kernel->player->queue.size()-1; index++ ){
        if( index > 10 && index < this->kernel->player->queue.size()-10 ){ continue; }
-       this->kernel->serial->printf("block %03d > ", index);
+       this->kernel->streams->printf("block %03d > ", index);
        this->kernel->player->queue.get_ref(index)->debug(this->kernel); 
     }
 }
index afb96b1..4346f93 100644 (file)
@@ -193,7 +193,6 @@ uint32_t Stepper::trapezoid_generator_tick( uint32_t dummy ) {
               }else{
                   this->trapezoid_adjusted_rate = double(this->current_block->rate_delta) * 1.5; 
                   //this->trapezoid_adjusted_rate = floor(double(this->trapezoid_adjusted_rate / 2 ));
-                  //this->kernel->serial->printf("over!\r\n");
               }
               if (this->trapezoid_adjusted_rate < this->current_block->final_rate ) {
                   this->trapezoid_adjusted_rate = this->current_block->final_rate;