moved from FunctionPointer to FPointer. hopefully, the last bit of mbed
authorArthur Wolf <wolf.arthur@gmail.com>
Mon, 26 Mar 2012 20:43:03 +0000 (22:43 +0200)
committerArthur Wolf <wolf.arthur@gmail.com>
Mon, 26 Mar 2012 20:43:03 +0000 (22:43 +0200)
attachement.

14 files changed:
src/libs/FPointer.h [new file with mode: 0644]
src/libs/Hook.cpp
src/libs/Hook.h
src/libs/SlowTicker.h
src/libs/StepTicker.h
src/modules/communication/utils/Gcode.cpp
src/modules/robot/Stepper.cpp
src/modules/robot/Stepper.h
src/modules/tools/extruder/Extruder.cpp
src/modules/tools/extruder/Extruder.h
src/modules/tools/temperaturecontrol/TemperatureControl.cpp
src/modules/tools/temperaturecontrol/TemperatureControl.h
src/modules/utils/pausebutton/PauseButton.cpp
src/modules/utils/pausebutton/PauseButton.h

diff --git a/src/libs/FPointer.h b/src/libs/FPointer.h
new file mode 100644 (file)
index 0000000..bec4243
--- /dev/null
@@ -0,0 +1,162 @@
+/*
+Copyright (c) 2011 Andy Kirkham
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef AJK_FPOINTER_H
+#define AJK_FPOINTER_H
+#ifndef NULL
+#define NULL 0
+#endif
+namespace AjK {
+
+class FPointerDummy;
+
+/** FPointer - Adds callbacks that take and return a 32bit uint32_t data type.
+*
+* The Mbed library supplies a callback using the FunctionPointer object as
+* defined in FunctionPointer.h However, this callback system does not allow
+* the caller to pass a value to the callback. Likewise, the callback itself
+* cannot return a value.
+*
+* FPointer operates in the same way but allows the callback function to be
+* passed one arg, a uint32_t value. Additionally, the callback can return
+* a single uint32_t value. The reason for using uint32_t is that the Mbed
+* and the microcontroller (LPC1768) have a natural data size of 32bits and
+* this means we can use the uint32_t as a pointer. See example1.h for more
+* information. This example passes an "int" by passing a pointer to that
+* int as a 32bit value. Using this technique you can pass any value you like.
+* All you have to do is pass a pointer to your value cast to (uint32_t). Your
+* callback can the deference it to get the original value.
+*
+* example2.h shows how to do the same thing but demostrates how to specify
+* the callback into a class object/method.
+*
+* Finally, example3.h shows how to pass multiple values. In this example we
+* define a data structure and in the callback we pass a pointer to that
+* data structure thus allowing the callback to again get the values.
+*
+* Note, when passing pointers to variables to the callback, if the callback
+* function/method changes that variable's value then it will also change the
+* value the caller sees. If C pointers are new to you, you are strongly
+* advised to read up on the subject. It's pointers that often get beginners
+* into trouble when mis-used.
+*
+* @see example1.h
+* @see example2.h
+* @see example3.h
+* @see http://mbed.org/handbook/C-Data-Types
+* @see http://mbed.org/projects/libraries/svn/mbed/trunk/FunctionPointer.h
+*/
+class FPointer {
+
+protected:
+
+    //! C callback function pointer.
+    uint32_t (*c_callback)(uint32_t);
+    
+    //! C++ callback object/method pointer (the object part).
+    FPointerDummy *obj_callback;
+    
+    //! C++ callback object/method pointer (the method part).
+    uint32_t (FPointerDummy::*method_callback)(uint32_t);
+
+public:
+    
+    /** Constructor
+*/
+    FPointer() {
+        c_callback = NULL;
+        obj_callback = NULL;
+        method_callback = NULL;
+    }
+    
+    /** attach - Overloaded attachment function.
+*
+* Attach a C type function pointer as the callback.
+*
+* Note, the callback function prototype must be:-
+* @code
+* uint32_t myCallbackFunction(uint32_t);
+* @endcode
+* @param A C function pointer to call.
+*/
+    void attach(uint32_t (*function)(uint32_t) = 0) { c_callback = function; }
+    
+    /** attach - Overloaded attachment function.
+*
+* Attach a C++ type object/method pointer as the callback.
+*
+* Note, the callback method prototype must be:-
+* @code
+* public:
+* uint32_t myCallbackFunction(uint32_t);
+* @endcode
+* @param A C++ object pointer.
+* @param A C++ method within the object to call.
+*/
+    template<class T>
+    void attach(T* item, uint32_t (T::*method)(uint32_t)) {
+        obj_callback = (FPointerDummy *)item;
+        method_callback = (uint32_t (FPointerDummy::*)(uint32_t))method;
+    }
+
+    /** call - Overloaded callback initiator.
+*
+* call the callback function.
+*
+* @param uint32_t The value to pass to the callback.
+* @return uint32_t The value the callback returns.
+*/
+    uint32_t call(uint32_t arg) {
+        if (c_callback != NULL) {
+            return (*c_callback)(arg);
+        }
+        else {
+            if (obj_callback != NULL && method_callback != NULL) {
+                return (obj_callback->*method_callback)(arg);
+            }
+        }
+        return (uint32_t)NULL;
+    }
+    
+    /** call - Overloaded callback initiator.
+*
+* Call the callback function without passing an argument.
+* The callback itself is passed NULL. Note, the callback
+* prototype should still be <b>uint32_t callback(uint32_t)</b>.
+*
+* @return uint32_t The value the callback returns.
+*/
+    uint32_t call(void) {
+        if (c_callback != NULL) {
+            return (*c_callback)((uint32_t)NULL);
+        }
+        else {
+            if (obj_callback != NULL && method_callback != NULL) {
+                return (obj_callback->*method_callback)((uint32_t)NULL);
+            }
+        }
+        return (uint32_t)NULL;
+    }
+};
+
+}; // namespace AjK ends
+
+using namespace AjK;
+
+#endif
index 4f61e99..6657a76 100644 (file)
@@ -1,3 +1,6 @@
+extern "C"{
+    #include <stdint.h>
+}
 #include "Hook.h"
 
 Hook::Hook(){}
index 810112c..f47051a 100644 (file)
@@ -1,9 +1,8 @@
 #ifndef HOOK_H
 #define HOOK_H
+#include "libs/FPointer.h"
 
-#include "mbed.h"
-// TODO : switch to Fpointer
-class Hook : public FunctionPointer {
+class Hook : public FPointer {
     public:
         Hook();
         double           frequency;
index 505b9df..96acead 100644 (file)
@@ -23,7 +23,7 @@ class SlowTicker : public Module{
         void set_frequency( int frequency );
         void tick();
         // For some reason this can't go in the .cpp, see :  http://mbed.org/forum/mbed/topic/2774/?page=1#comment-14221
-        template<typename T> void attach( double frequency, T *optr, void ( T::*fptr )( void ) ){
+        template<typename T> void attach( double frequency, T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
             Hook* hook = new Hook(); 
             hook->frequency = frequency;
             hook->attach(optr, fptr);
index 7946501..4170480 100644 (file)
@@ -26,21 +26,21 @@ class StepTicker{
         void reset_tick();
 
         // For some reason this can't go in the .cpp, see :  http://mbed.org/forum/mbed/topic/2774/?page=1#comment-14221
-        template<typename T> void attach( T *optr, void ( T::*fptr )( void ) ){
-            FunctionPointer* hook = new FunctionPointer(); 
+        template<typename T> void attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
+            FPointer* hook = new FPointer(); 
             hook->attach(optr, fptr);
             this->hooks.push_back(hook);
         }
 
-        template<typename T> void reset_attach( T *optr, void ( T::*fptr )( void ) ){
-            FunctionPointer* reset_hook = new FunctionPointer(); 
+        template<typename T> void reset_attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
+            FPointer* reset_hook = new FPointer(); 
             reset_hook->attach(optr, fptr);
             this->reset_hooks.push_back(reset_hook);
         }
 
 
-        vector<FunctionPointer*> hooks; 
-        vector<FunctionPointer*> reset_hooks; 
+        vector<FPointer*> hooks; 
+        vector<FPointer*> reset_hooks; 
         double frequency;
 
 };
index 64bfb3b..2e19821 100644 (file)
@@ -11,9 +11,9 @@ using std::string;
 #include "Gcode.h"
 #include "libs/StreamOutput.h"
 
-
 #include <stdlib.h>
 
+
 Gcode::Gcode(){}
 
 // Whether or not a Gcode has a letter
index e709c82..1004de7 100644 (file)
@@ -106,8 +106,8 @@ void Stepper::on_block_end(void* argument){
 
 // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse of Smoothie. It is executed at the rate set with
 // config_step_timer. It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
-inline void Stepper::main_interrupt(){
-    if( this->paused ){ return; } 
+inline uint32_t Stepper::main_interrupt(uint32_t dummy){
+    if( this->paused ){ return 0; } 
 
     // Step dir pins first, then step pinse, stepper drivers like to know the direction before the step signal comes in
     this->alpha_dir_pin->set(  ( this->out_bits >> 0  ) & 1 );
@@ -153,7 +153,7 @@ void Stepper::update_offsets(){
 // This is called ACCELERATION_TICKS_PER_SECOND times per second by the step_event
 // interrupt. It can be assumed that the trapezoid-generator-parameters and the
 // current_block stays untouched by outside handlers for the duration of this function call.
-void Stepper::trapezoid_generator_tick() {
+uint32_t Stepper::trapezoid_generator_tick( uint32_t dummy ) {
     if(this->current_block && !this->trapezoid_generator_busy && !this->paused ) {
           if(this->step_events_completed < this->current_block->accelerate_until<<16) {
               this->trapezoid_adjusted_rate += this->current_block->rate_delta;
@@ -211,7 +211,7 @@ void Stepper::set_step_events_per_minute( double steps_per_minute ){
 
 }
 
-void Stepper::reset_step_pins(){
+uint32_t Stepper::reset_step_pins(uint32_t dummy){
     this->alpha_step_pin->set(0);
     this->beta_step_pin->set(0); 
     this->gamma_step_pin->set(0);
index 05c8029..169208c 100644 (file)
@@ -32,11 +32,11 @@ class Stepper : public Module {
         void on_block_end(void* argument);
         void on_play(void* argument);
         void on_pause(void* argument);
-        void main_interrupt();
+        uint32_t main_interrupt(uint32_t dummy);
         void trapezoid_generator_reset();
         void set_step_events_per_minute(double steps_per_minute);
-        void trapezoid_generator_tick();
-        void reset_step_pins();
+        uint32_t trapezoid_generator_tick(uint32_t dummy);
+        uint32_t reset_step_pins(uint32_t dummy);
         void update_offsets();
         int config_step_timer( int cycles );
 
index 30226a2..3e8ad1d 100644 (file)
@@ -137,7 +137,7 @@ void Extruder::on_block_begin(void* argument){
         this->start_position = this->target_position;
         this->target_position =  this->start_position + ( this->current_block->millimeters * this->travel_ratio );
         if( this->target_position > this->current_position ){ this->direction = 1; }else if( this->target_position < this->current_position ){ this->direction = -1; }
-        this->acceleration_tick();
+        this->acceleration_tick(0);
     } 
 
 }
@@ -149,10 +149,10 @@ void Extruder::on_block_end(void* argument){
 }       
 
 // Called periodically to change the speed to match acceleration or to match the speed of the robot
-void Extruder::acceleration_tick(){
+uint32_t Extruder::acceleration_tick(uint32_t dummy){
 
     // Avoid trying to work when we really shouldn't ( between blocks or re-entry ) 
-    if( this->current_block == NULL || this->acceleration_lock || this->paused ){ return; }
+    if( this->current_block == NULL || this->acceleration_lock || this->paused ){ return 0; }
     this->acceleration_lock = true;
    
     // In solo mode, we mode independently from the robot
@@ -195,9 +195,9 @@ void Extruder::acceleration_tick(){
             next_relative_position += ( advance ); 
             
             // TODO : all of those "if->return" is very hacky, we should do the math in a way where most of those don't happen, but that requires doing tons of drawing ...
-            if( last_ratio == next_ratio ){ this->acceleration_lock = false; return; }else{ last_ratio = next_ratio; }           
-            if( next_ratio == 0 || next_ratio > 1 ){ this->acceleration_lock = false; return; }
-            if( ticks_forward > 1000 ){ this->acceleration_lock = false; return; } // This is very ugly
+            if( last_ratio == next_ratio ){ this->acceleration_lock = false; return 0; }else{ last_ratio = next_ratio; }           
+            if( next_ratio == 0 || next_ratio > 1 ){ this->acceleration_lock = false; return 0; }
+            if( ticks_forward > 1000 ){ this->acceleration_lock = false; return 0; } // This is very ugly
 
             // Hack : We have not looked far enough, we compute how far ahead we must look to get a relevant value
             if( position > next_relative_position ){ 
@@ -209,7 +209,7 @@ void Extruder::acceleration_tick(){
                 double ticks_to_equilibrium = ceil(far_back_ratio / ratio_per_tick) + 1;
                 ticks_forward += ticks_to_equilibrium;
                 // Because this is a loop, and we can be interrupted by the stepping interrupt, if that interrupt changes block, the new block may not be solo, and we may get trapped into an infinite loop
-                if( this->mode != FOLLOW ){ this->acceleration_lock = false; return; }
+                if( this->mode != FOLLOW ){ this->acceleration_lock = false; return 0; }
                 continue; 
             }
            
@@ -222,7 +222,7 @@ void Extruder::acceleration_tick(){
             this->set_speed( speed_to_next_tick );
 
             this->acceleration_lock = false;
-            return;
+            return 0;
         }
     }
 
@@ -243,8 +243,8 @@ void Extruder::set_speed( int steps_per_second ){
     
 }
 
-inline void Extruder::stepping_tick(){
-    if( this->paused ){ return; }
+inline uint32_t Extruder::stepping_tick(uint32_t dummy){
+    if( this->paused ){ return 0; }
 
     this->step_counter += this->counter_increment;
     if( this->step_counter > 1<<16 ){
@@ -266,6 +266,6 @@ inline void Extruder::stepping_tick(){
     }
 }
 
-void Extruder::reset_step_pin(){
+uint32_t Extruder::reset_step_pin(uint32_t dummy){
     this->step_pin = 0;
 }
index 15c6d09..daf4e25 100644 (file)
@@ -35,9 +35,9 @@ class Extruder : public Module{
         void on_play(void* argument);
         void on_pause(void* argument); 
         void set_speed(int steps_per_second);
-        void acceleration_tick();
-        void stepping_tick();
-        void reset_step_pin();
+        uint32_t acceleration_tick(uint32_t dummy);
+        uint32_t stepping_tick(uint32_t dummy);
+        uint32_t reset_step_pin(uint32_t dummy);
 
         DigitalOut      step_pin;                     // Step pin for the stepper driver
         DigitalOut      dir_pin;                      // Dir pin for the stepper driver
index 04a41e4..57d3f49 100644 (file)
@@ -130,7 +130,7 @@ double TemperatureControl::temperature_to_adc_value(double temperature){
     return v / this->vadc * 1.00000;                                               // The ADC reading
 }
 
-void TemperatureControl::thermistor_read_tick(){
+uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy){
     if( this->desired_adc_value != UNDEFINED ){
         if( this->new_thermistor_reading() > this->desired_adc_value ){
             this->heater_pin->set(1); 
index e871f3a..65375ea 100644 (file)
@@ -39,7 +39,7 @@ class TemperatureControl : public Module {
         double get_temperature();
         double adc_value_to_temperature(double adc_value);
         double temperature_to_adc_value(double temperature);
-        void thermistor_read_tick();
+        uint32_t thermistor_read_tick(uint32_t dummy);
         double new_thermistor_reading();
         double average_adc_reading();
         
index 1b86c7f..6da7b1a 100644 (file)
@@ -21,7 +21,7 @@ void PauseButton::on_module_loaded(){
 
 //TODO: Make this use InterruptIn
 //Check the state of the button and act accordingly
-void PauseButton::button_tick(){
+uint32_t PauseButton::button_tick(uint32_t dummy){
     // If button changed 
     if(this->button_state != this->button->get()){ 
         this->button_state = this->button->get();
index 2273899..f30eaf2 100644 (file)
@@ -14,7 +14,7 @@ class PauseButton : public Module {
         PauseButton();
        
         void on_module_loaded();
-        void button_tick();
+        uint32_t button_tick(uint32_t dummy);
         void on_play( void* argument );
         void on_pause( void* argument );