/*! \file RemoteControl.H c++/RemoteControl.H \brief C++ Remote Control Class Interface \author Zhengrong.Zang@gmail.com \date 2013.10.01 Defines interface to monitor RCX Remote Control */ // 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 _RemoteControl_H_ #define _RemoteControl_H_ #include #include extern volatile unsigned int remote_etype; extern volatile unsigned int remote_key; volatile unsigned int remote_etype; // pressed or released volatile unsigned int remote_key; // M1-M3, A1-C1, A2-C2, P1-P5, Stop, Beep // Called by system packet handler int remote_control_handler(unsigned int etype, unsigned int key) { remote_key = key; remote_etype = etype; return 1; } // key data pressed on RCX remote control wakeup_t remote_control_pressed_key(wakeup_t data) { return (remote_key & data) && (remote_etype & LREVT_KEYON); } // key data released on RCX remote control wakeup_t remote_control_released_key(wakeup_t data) { return (remote_key & data) && (remote_etype & LREVT_KEYOFF); } // any key pressed on RCX remote control wakeup_t remote_control_pressed(wakeup_t data) { return (remote_key) && (remote_etype & LREVT_KEYON); } // any key released on RCX remote control wakeup_t remote_control_released(wakeup_t data) { return (remote_key) && (remote_etype & LREVT_KEYOFF); } // any key pressed/released on RCX remote control wakeup_t remote_controlled(wakeup_t data) { return remote_key; } typedef void (*keyaction)(unsigned int etype, unsigned int key); class RemoteControl { private: keyaction rch; // Remote Control handler public: RemoteControl(keyaction controlled = (keyaction)NULL) { rch = controlled; lr_set_handler(remote_control_handler); } void run() { while (!shutdown_requested()) { remote_key = 0; remote_etype = 0; wait_event(&remote_control_pressed, 0); if (rch) { (*rch)(remote_etype, remote_key); } else if (remote_key & LRKEY_STOP) { cputs("Stop"); program_stop(1); } wait_event(&remote_control_released, 0); } } }; #endif // _RemoteControl_H_