/*! \file ButtonlMonitor.H c++/ButtonlMonitor.H \brief C++ Button Control Class Interface \author Zhengrong.Zang@gmail.com \date 2013.10.01 Defines interface to monitor RCX button */ // The contents of this file are subject to the Mozilla Public License // Version 1.0 (the "License"); you may not use this file except in // compliance with the License. You may obtain a copy of the License // at http://www.mozilla.org/MPL/ // // Software distributed under the License is distributed on an "AS IS" // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See // the License for the specific language governing rights and // limitations under the License. #ifndef _ButtonlMonitor_H_ #define _ButtonlMonitor_H_ #include #include #include //extern volatile unsigned int button_etype; //extern volatile unsigned int button_key; //volatile unsigned int button_etype; // pressed or released //volatile unsigned int button_key; // ONOFF, VIEW, PRGM, RUN // key data pressed on RCX button wakeup_t button_released_key(wakeup_t data) { return dbutton() & data; } // key data released on RCX button wakeup_t button_pressed_key(wakeup_t data) { return !button_released_key(data); } // any key released on RCX button wakeup_t button_released(wakeup_t data) { return (dbutton() & BUTTON_ONOFF) || (dbutton() & BUTTON_RUN) || (dbutton() & BUTTON_VIEW) || (dbutton() & BUTTON_PROGRAM); } // any key pressed on RCX button wakeup_t button_pressed(wakeup_t data) { return !button_released(data); } typedef void (*buttonaction)(int key); class ButtonMonitor { private: buttonaction bh; // Button handler public: ButtonMonitor(buttonaction controlled = (buttonaction)NULL) { bh = controlled; } void run() { while (!shutdown_requested()) { wait_event(&button_pressed, 0); Sound::beep(); if (bh) { (*bh)(dbutton()); } } } }; #endif // _ButtonlMonitor_H_