merged button into switch to generally reduce naming confusion and to allow greater...
authorLogxen <logxen@hotmail.com>
Sat, 1 Jun 2013 19:15:28 +0000 (12:15 -0700)
committerLogxen <logxen@hotmail.com>
Sat, 1 Jun 2013 19:15:28 +0000 (12:15 -0700)
src/modules/tools/switch/Switch.cpp
src/modules/tools/switch/Switch.h
src/modules/tools/switch/SwitchPool.h
src/modules/utils/button/Button.cpp [deleted file]
src/modules/utils/button/Button.h [deleted file]
src/modules/utils/button/ButtonPool.cpp [deleted file]
src/modules/utils/button/ButtonPool.h [deleted file]

index 443f180..81a61de 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "libs/Module.h"
 #include "libs/Kernel.h"
+#include "libs/SerialMessage.h"
 #include <math.h>
 #include "Switch.h"
 #include "libs/Pin.h"
@@ -18,9 +19,13 @@ Switch::Switch(){}
 
 Switch::Switch(uint16_t name){
     this->name_checksum = name;
+    //this->dummy_stream = &(StreamOutput::NullStream);
 }
 
 void Switch::on_module_loaded(){
+    this->input_pin_state = true;
+    this->switch_state = true;
+
     register_for_event(ON_CONFIG_RELOAD);
     this->register_for_event(ON_GCODE_RECEIVED);
     this->register_for_event(ON_GCODE_EXECUTE);
@@ -28,6 +33,9 @@ void Switch::on_module_loaded(){
     // Settings
     this->on_config_reload(this);
 
+    // input pin polling
+    this->kernel->slow_ticker->attach( 100, this, &Switch::pinpoll_tick);
+
     // PWM
     this->kernel->slow_ticker->attach(1000, &output_pin, &Pwm::on_tick);
 }
@@ -35,10 +43,15 @@ void Switch::on_module_loaded(){
 
 // Get config
 void Switch::on_config_reload(void* argument){
-    this->on_m_code     = this->kernel->config->value(switch_checksum, this->name_checksum, on_m_code_checksum     )->required()->as_number();
-    this->off_m_code    = this->kernel->config->value(switch_checksum, this->name_checksum, off_m_code_checksum    )->required()->as_number();
-    this->output_pin.from_string(this->kernel->config->value(switch_checksum, this->name_checksum, output_pin_checksum    )->required()->as_string())->as_output();
+    this->input_pin.from_string(this->kernel->config->value(switch_checksum, this->name_checksum, input_pin_checksum    )->by_default("nc")->as_string())->as_input();
+    this->input_pin_behavior     = this->kernel->config->value(switch_checksum, this->name_checksum, input_pin_behavior_checksum     )->by_default(momentary_checksum)->as_number();
+    this->input_on_command     = this->kernel->config->value(switch_checksum, this->name_checksum, input_on_command_checksum     )->by_default("")->as_string();
+    this->input_off_command    = this->kernel->config->value(switch_checksum, this->name_checksum, input_off_command_checksum    )->by_default("")->as_string();
+    this->switch_state         = this->kernel->config->value(switch_checksum, this->name_checksum, startup_state_checksum )->by_default(false)->as_bool();
+    this->output_pin.from_string(this->kernel->config->value(switch_checksum, this->name_checksum, output_pin_checksum    )->by_default("nc")->as_string())->as_output();
     this->output_pin.set(this->kernel->config->value(switch_checksum, this->name_checksum, startup_state_checksum )->by_default(0)->as_number() );
+    this->output_on_command     = this->kernel->config->value(switch_checksum, this->name_checksum, output_on_command_checksum     )->by_default("")->as_string();
+    this->output_off_command    = this->kernel->config->value(switch_checksum, this->name_checksum, output_off_command_checksum    )->by_default("")->as_string();
 
     set_low_on_debug(output_pin.port_number, output_pin.pin);
 }
@@ -47,7 +60,8 @@ void Switch::on_config_reload(void* argument){
 void Switch::on_gcode_received(void* argument){
     Gcode* gcode = static_cast<Gcode*>(argument);
     // Add the gcode to the queue ourselves if we need it
-    if( gcode->has_m && ( gcode->m == this->on_m_code || gcode->m == this->off_m_code ) ){
+    if( ( input_on_command.length() > 0 && gcode->command.compare(0, input_on_command.length(), input_on_command) )
+        || ( input_off_command.length() > 0 && gcode->command.compare(0, input_off_command.length(), input_off_command) ) ){
         gcode->mark_as_taken();
         if( this->kernel->conveyor->queue.size() == 0 ){
             this->kernel->call_event(ON_GCODE_EXECUTE, gcode );
@@ -61,26 +75,67 @@ void Switch::on_gcode_received(void* argument){
 // Turn pin on and off
 void Switch::on_gcode_execute(void* argument){
     Gcode* gcode = static_cast<Gcode*>(argument);
-    if( gcode->has_m){
-        int code = gcode->m;
-        if( code == this->on_m_code ){
-            if (gcode->has_letter('S'))
-            {
-                int v = gcode->get_value('S') * output_pin.max_pwm() / 256.0;
-                if (v)
-                    this->output_pin.pwm(v);
-                else
-                    this->output_pin.set(0);
-            }
+    if(gcode->command.compare(0, input_on_command.length(), input_on_command)){
+        if (gcode->has_letter('S'))
+        {
+            int v = gcode->get_value('S') * output_pin.max_pwm() / 256.0;
+            if (v)
+                this->output_pin.pwm(v);
             else
-            {
-                // Turn pin on
-                this->output_pin.set(1);
-            }
+                this->output_pin.set(0);
         }
-        if( code == this->off_m_code ){
-            // Turn pin off
-            this->output_pin.set(0);
+        else
+        {
+            // Turn pin on
+            this->output_pin.set(1);
         }
     }
+    else if(gcode->command.compare(0, input_off_command.length(), input_off_command)){
+        // Turn pin off
+        this->output_pin.set(0);
+    }
 }
+
+//TODO: Make this use InterruptIn
+//Check the state of the button and act accordingly
+uint32_t Switch::pinpoll_tick(uint32_t dummy){
+    // If pin changed
+    if(this->input_pin_state != this->input_pin.get()){
+        this->input_pin_state = this->input_pin.get();
+        // If pin high
+        if( this->input_pin_state ){
+            // if switch is a toggle switch
+            if( this->input_pin_behavior == toggle_checksum ){
+                this->flip();
+            // else default is momentary
+            }
+            else{
+                this->flip();
+            }
+        // else if button released
+        }else{
+            // if switch is momentary
+            if( !this->input_pin_behavior == toggle_checksum ){
+                this->flip();
+            }
+        }
+    }
+    return 0;
+}
+
+void Switch::flip(){
+    this->switch_state = !this->switch_state;
+    if( this->switch_state ){
+        this->send_gcode( this->output_on_command, &(StreamOutput::NullStream) );
+    }else{
+        this->send_gcode( this->output_off_command, &(StreamOutput::NullStream) );
+    }
+}
+
+void Switch::send_gcode(std::string msg, StreamOutput* stream) {
+    struct SerialMessage message;
+    message.message = msg;
+    message.stream = stream;
+    this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
+}
+
index 68c0b84..587df93 100644 (file)
 #include "libs/Pin.h"
 #include <math.h>
 
-#define    switch_checksum            CHECKSUM("switch")
-#define    on_m_code_checksum         CHECKSUM("on_m_code")
-#define    off_m_code_checksum        CHECKSUM("off_m_code")
-#define    output_pin_checksum        CHECKSUM("output_pin")
-#define    startup_state_checksum     CHECKSUM("startup_state")
+#define    switch_checksum              CHECKSUM("switch")
+#define    startup_state_checksum       CHECKSUM("startup_state")
+#define    input_pin_checksum           CHECKSUM("input_pin")
+#define    input_pin_behavior_checksum  CHECKSUM("input_pin_behavior")
+#define    toggle_checksum              CHECKSUM("toggle")
+#define    momentary_checksum           CHECKSUM("momentary")
+#define    input_on_command_checksum    CHECKSUM("input_on_command")
+#define    input_off_command_checksum   CHECKSUM("input_off_command")
+#define    output_pin_checksum          CHECKSUM("output_pin")
+#define    output_on_command_checksum   CHECKSUM("output_on_command")
+#define    output_off_command_checksum  CHECKSUM("output_off_command")
 
 class Switch : public Module {
     public:
@@ -26,11 +32,21 @@ class Switch : public Module {
         void on_config_reload(void* argument);
         void on_gcode_received(void* argument);
         void on_gcode_execute(void* argument);
+        uint32_t pinpoll_tick(uint32_t dummy);
+
+        void flip();
+        void send_gcode(string msg, StreamOutput* stream);
 
         uint16_t name_checksum;
-        uint16_t on_m_code;
-        uint16_t off_m_code;
-        Pwm      output_pin;
+        Pin       input_pin;
+        uint16_t input_pin_behavior;
+        bool      input_pin_state;
+        string    input_on_command;
+        string    input_off_command;
+        bool      switch_state;
+        Pwm       output_pin;
+        string    output_on_command;
+        string    output_off_command;
 };
 
-#endif
+#endif // SWITCH_H
index 85f11f1..1f4aa54 100644 (file)
@@ -13,7 +13,6 @@
 using namespace std;
 #include <vector>
 
-#define switch_checksum            CHECKSUM("switch")
 #define enable_checksum            CHECKSUM("enable")
 
 class SwitchPool : public Module {
@@ -27,4 +26,4 @@ class SwitchPool : public Module {
 
 
 
-#endif
+#endif // SWITCHPOOL_H
diff --git a/src/modules/utils/button/Button.cpp b/src/modules/utils/button/Button.cpp
deleted file mode 100644 (file)
index ef8176d..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-#include "libs/Kernel.h"
-#include "libs/SerialMessage.h"
-#include "Button.h"
-#include "libs/nuts_bolts.h"
-#include "libs/utils.h"
-#include <string>
-using namespace std;
-
-//Button::Button(){}
-
-Button::Button(uint16_t name){
-    this->name_checksum = name;
-    this->dummy_stream = &(StreamOutput::NullStream);
-}
-
-void Button::on_module_loaded(){
-    this->button_state = true;
-    this->switch_state = true;
-
-    // Settings
-    this->on_config_reload(this);
-
-    this->kernel->slow_ticker->attach( 100, this, &Button::button_tick );
-}
-
-void Button::on_config_reload(void* argument){
-    this->toggle                      = this->kernel->config->value( button_checksum, this->name_checksum, toggle_checksum )->by_default(false)->as_bool();
-    this->switch_state                = this->kernel->config->value( button_checksum, this->name_checksum, normal_state_checksum )->by_default(true)->as_bool();
-    this->button.from_string          ( this->kernel->config->value( button_checksum, this->name_checksum, input_pin_checksum )->by_default("2.12")->as_string() )->as_input();
-    this->on_m_code                   = "M" + this->kernel->config->value( button_checksum, this->name_checksum, on_m_code_checksum          )->required()->as_string() + "\r\n";
-    this->off_m_code                  = "M" + this->kernel->config->value( button_checksum, this->name_checksum, off_m_code_checksum         )->required()->as_string() + "\r\n";
-}
-
-//TODO: Make this use InterruptIn
-//Check the state of the button and act accordingly
-uint32_t Button::button_tick(uint32_t dummy){
-    // If button changed
-    if(this->button_state != this->button.get()){
-        this->button_state = this->button.get();
-        // If button pressed
-        if( this->button_state ){
-            // if switch is a toggle switch
-            if( this->toggle ){
-                this->switch_state = !this->switch_state;
-                if( this->switch_state ){
-                    this->send_gcode( this->on_m_code, dummy_stream );
-                }else{
-                    this->send_gcode( this->off_m_code, dummy_stream );
-                }
-            // else if switch is momentary
-            }else{
-                this->send_gcode( this->on_m_code, dummy_stream );
-            }
-        // else if button released
-        }else{
-            // if switch is momentary
-            if( !this->toggle ){
-                this->send_gcode( this->off_m_code, dummy_stream );
-            }
-        }
-    }
-    return 0;
-}
-
-void Button::send_gcode(std::string msg, StreamOutput* stream) {
-    struct SerialMessage message;
-    message.message = msg;
-    message.stream = stream;
-    this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
-}
-
diff --git a/src/modules/utils/button/Button.h b/src/modules/utils/button/Button.h
deleted file mode 100644 (file)
index 7022e3d..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-#ifndef BUTTON_H
-#define BUTTON_H
-
-#include "libs/Kernel.h"
-#include "libs/nuts_bolts.h"
-#include "libs/utils.h"
-#include "libs/Pin.h"
-#include <string>
-
-#define button_checksum             CHECKSUM("button")
-#define on_m_code_checksum          CHECKSUM("on_m_code")
-#define off_m_code_checksum         CHECKSUM("off_m_code")
-#define input_pin_checksum          CHECKSUM("input_pin")
-#define toggle_checksum             CHECKSUM("toggle")
-#define normal_state_checksum       CHECKSUM("normal_state")
-
-class Button : public Module {
-    public:
-        Button(uint16_t name);
-
-        void on_module_loaded();
-        void on_config_reload(void* argument);
-        uint32_t button_tick(uint32_t dummy);
-
-        void send_gcode(string msg, StreamOutput* stream);
-
-        bool       toggle;
-        bool       button_state;
-        bool       switch_state;
-        uint16_t   name_checksum;
-        std::string   on_m_code;
-        std::string   off_m_code;
-        Pin       button;
-
-        StreamOutput* dummy_stream;
-};
-
-#endif // BUTTON_H
diff --git a/src/modules/utils/button/ButtonPool.cpp b/src/modules/utils/button/ButtonPool.cpp
deleted file mode 100644 (file)
index 1ac8edd..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
-      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/>.
-*/
-
-#include "libs/Module.h"
-#include "libs/Kernel.h"
-#include <math.h>
-using namespace std;
-#include <vector>
-#include "ButtonPool.h"
-#include "Button.h"
-
-ButtonPool::ButtonPool(){}
-
-void ButtonPool::on_module_loaded(){
-
-    vector<uint16_t> modules;
-    this->kernel->config->get_module_list( &modules, button_checksum );
-
-    for( unsigned int i = 0; i < modules.size(); i++ ){
-        // If module is enabled
-        if( this->kernel->config->value(button_checksum, modules[i], enable_checksum )->as_bool() == true ){
-            Button* controller = new Button(modules[i]);
-            this->kernel->add_module(controller);
-            this->controllers.push_back( controller );
-        }
-    }
-}
-
diff --git a/src/modules/utils/button/ButtonPool.h b/src/modules/utils/button/ButtonPool.h
deleted file mode 100644 (file)
index b4c560f..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
-      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 BUTTONPOOL_H
-#define BUTTONPOOL_H
-
-#include "Button.h"
-#include <math.h>
-using namespace std;
-#include <vector>
-
-// defined in Button.h
-// #define button_checksum              CHECKSUM("button")
-#define enable_checksum              CHECKSUM("enable")
-
-class ButtonPool : public Module {
-    public:
-        ButtonPool();
-
-        void on_module_loaded();
-
-        vector<Button*> controllers;
-};
-
-#endif // BUTTONPOOL_H