move digipot instance to CurrentControl module
authorJim Morris <morris@wolfman.com>
Fri, 8 Feb 2013 22:04:47 +0000 (14:04 -0800)
committerJim Morris <morris@wolfman.com>
Fri, 8 Feb 2013 22:04:47 +0000 (14:04 -0800)
make CurrentControl delete itself if not enabled
make Laser Module delete itself if not loaded

src/libs/Kernel.cpp
src/libs/Kernel.h
src/modules/tools/laser/Laser.cpp
src/modules/utils/currentcontrol/CurrentControl.cpp
src/modules/utils/currentcontrol/CurrentControl.h

index 9250351..a1498d7 100644 (file)
@@ -11,7 +11,6 @@
 #include "libs/nuts_bolts.h"
 #include "libs/SlowTicker.h"
 #include "libs/Adc.h"
-#include "libs/Digipot.h"
 #include "libs/Pauser.h"
 #include "libs/StreamOutputPool.h"
 #include <mri.h>
@@ -63,7 +62,6 @@ Kernel::Kernel(){
     add_module( this->slow_ticker          = new SlowTicker());
     this->step_ticker          = new StepTicker();
     this->adc                  = new Adc();
-    this->digipot              = new Digipot();
 
     // LPC17xx-specific
     NVIC_SetPriorityGrouping(0);
index e861538..92d261c 100644 (file)
@@ -13,7 +13,6 @@
 #include "libs/StreamOutputPool.h"
 #include "libs/StepTicker.h"
 #include "libs/Adc.h"
-#include "libs/Digipot.h"
 #include "libs/Pauser.h"
 #include "modules/communication/SerialConsole.h"
 #include "modules/communication/GcodeDispatch.h"
@@ -50,7 +49,6 @@ class Kernel {
         SlowTicker*       slow_ticker;
         StepTicker*       step_ticker;
         Adc*              adc;
-        Digipot*          digipot;
 
     private:
         std::array<std::vector<Module*>, NUMBER_OF_DEFINED_EVENTS> hooks; // When a module asks to be called for a specific event ( a hook ), this is where that request is remembered
index 79019a2..00b6f44 100644 (file)
@@ -16,7 +16,11 @@ Laser::Laser(){
 }
 
 void Laser::on_module_loaded() {
-    if( !this->kernel->config->value( laser_module_enable_checksum )->by_default(false)->as_bool() ){ return; }
+       if( !this->kernel->config->value( laser_module_enable_checksum )->by_default(false)->as_bool() ){
+               // as not needed free up resource
+               delete this;
+               return;
+       }
 
     this->laser_pin = new mbed::PwmOut(p21);
     this->laser_pin->period_us(20);
index f4101d1..131efa2 100644 (file)
@@ -8,10 +8,20 @@
 #include <string>
 using namespace std;
 
-CurrentControl::CurrentControl(){}
+CurrentControl::CurrentControl(){
+       digipot= NULL;
+}
 
 void CurrentControl::on_module_loaded(){
-    if( !this->kernel->config->value( currentcontrol_module_enable_checksum )->by_default(false)->as_bool() ){ return; }
+       if( !this->kernel->config->value( currentcontrol_module_enable_checksum )->by_default(false)->as_bool() ){
+               // as this module is not needed free up the resource
+               delete this;
+               return;
+       }
+
+       // allocate digipot, if already allocated delete it first
+       delete digipot;
+       digipot = new Digipot();
 
     // Get configuration
     this->alpha_current =           this->kernel->config->value(alpha_current_checksum  )->by_default(0.8)->as_number();
@@ -19,10 +29,10 @@ void CurrentControl::on_module_loaded(){
     this->gamma_current =           this->kernel->config->value(gamma_current_checksum  )->by_default(0.8)->as_number();
     this->delta_current =           this->kernel->config->value(delta_current_checksum  )->by_default(0.8)->as_number();
 
-    this->kernel->digipot->set_current(0, this->alpha_current);
-    this->kernel->digipot->set_current(1, this->beta_current );
-    this->kernel->digipot->set_current(2, this->gamma_current);
-    this->kernel->digipot->set_current(3, this->delta_current);
+    this->digipot->set_current(0, this->alpha_current);
+    this->digipot->set_current(1, this->beta_current );
+    this->digipot->set_current(2, this->gamma_current);
+    this->digipot->set_current(3, this->delta_current);
 
     this->register_for_event(ON_GCODE_RECEIVED);
 }
@@ -40,8 +50,8 @@ void CurrentControl::on_gcode_received(void *argument)
             for (i = 0; i < 4; i++)
             {
                 if (gcode->has_letter(alpha[i]))
-                    this->kernel->digipot->set_current(i, gcode->get_value(alpha[i]));
-                gcode->stream->printf("%c:%3.1fA%c", alpha[i], this->kernel->digipot->get_current(i), (i == 3)?'\n':' ');
+                    this->digipot->set_current(i, gcode->get_value(alpha[i]));
+                gcode->stream->printf("%c:%3.1fA%c", alpha[i], this->digipot->get_current(i), (i == 3)?'\n':' ');
             }
         }
     }
index 0ed03b3..bdb04b9 100644 (file)
@@ -5,6 +5,7 @@
 #include "libs/nuts_bolts.h"
 #include "libs/utils.h"
 #include "libs/Pin.h"
+#include "libs/Digipot.h"
 
 #define alpha_current_checksum                  CHECKSUM("alpha_current")
 #define beta_current_checksum                   CHECKSUM("beta_current")
@@ -22,7 +23,9 @@ class CurrentControl : public Module {
         double alpha_current;
         double beta_current;
         double gamma_current;
-        double delta_current;
+               double delta_current;
+
+               Digipot* digipot;
 };