#include "libs/Module.h"
#include "libs/Kernel.h"
+#include "libs/SerialMessage.h"
#include <math.h>
#include "Switch.h"
#include "libs/Pin.h"
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);
// 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);
}
// 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);
}
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 );
// 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 );
+}
+
#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:
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
using namespace std;
#include <vector>
-#define switch_checksum CHECKSUM("switch")
#define enable_checksum CHECKSUM("enable")
class SwitchPool : public Module {
-#endif
+#endif // SWITCHPOOL_H
+++ /dev/null
-#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 );
-}
-
+++ /dev/null
-#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
+++ /dev/null
-/*
- 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 );
- }
- }
-}
-
+++ /dev/null
-/*
- 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