Button #123

Merged
merged 13 commits into from Feb 17, 2013
@@ -35,7 +35,7 @@ class StreamOutput : public mbed::Stream {
}
virtual int _putc(int c) { return 0; }
virtual int _getc(void) { return 0; }
- virtual int puts(const char*) = 0;
+ virtual int puts(const char*) { return 0; };
};
#endif
View
@@ -11,6 +11,7 @@
#include "modules/tools/temperaturecontrol/TemperatureControlPool.h"
#include "modules/tools/endstops/Endstops.h"
#include "modules/tools/switch/SwitchPool.h"
+#include "modules/utils/button/ButtonPool.h"
#include "modules/robot/Player.h"
#include "modules/utils/simpleshell/SimpleShell.h"
#include "modules/utils/configurator/Configurator.h"
@@ -111,6 +112,7 @@ int main() {
kernel->add_module( new CurrentControl() );
kernel->add_module( new TemperatureControlPool() );
kernel->add_module( new SwitchPool() );
+ kernel->add_module( new ButtonPool() );
kernel->add_module( new PauseButton() );
kernel->add_module( new Endstops() );
@@ -0,0 +1,71 @@
+#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 = new StreamOutput();
+}
+
+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 );
+}
+
@@ -0,0 +1,38 @@
+#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
@@ -0,0 +1,32 @@
+/*
+ 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 );
+ }
+ }
+}
+
@@ -0,0 +1,28 @@
+/*
+ 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>
+
+#define button_checksum 5709
+#define enable_checksum 29545
+
+class ButtonPool : public Module {
+ public:
+ ButtonPool();
+
+ void on_module_loaded();
+
+ vector<Button*> controllers;
+};
+
+#endif // BUTTONPOOL_H