AirsoftTimer software based on mbed
Dependencies: mbed TextLCD keypad
Revision 18:abcebc4d0da0, committed 2015-05-23
- Comitter:
- sillevl
- Date:
- Sat May 23 20:57:45 2015 +0000
- Parent:
- 17:19dbb1dbb640
- Child:
- 19:2eba101d9c2c
- Commit message:
- work in progress;
Changed in this revision
--- a/board/Board.cpp Sat May 23 20:17:54 2015 +0000 +++ b/board/Board.cpp Sat May 23 20:57:45 2015 +0000 @@ -15,24 +15,25 @@ } uint32_t Board::keyboardButton(uint32_t index){ - //printf("button pressed \r\n"); + printf("button pressed \r\n"); //printf("button index: %d\r\n", index); //printf("button car: %c\r\n", Keyboard::KEYTABLE[index]); - buttonEvent(Keyboard::KEYTABLE[index]); + //----_callback.call(Keyboard::KEYTABLE[index]); + //buttonEvent(Keyboard::KEYTABLE[index]); leds->on(Leds::LEFT); return 0; } -void Board::attach(ButtonListener *bl) +/*void Board::attach(ButtonListener *bl) { //printf("ButtonListener attached\r\n"); listeners.push_back(bl); //printf("done\r\n"); -} -void Board::buttonEvent(char c) +}*/ +/*void Board::buttonEvent(char c) { //printf("ButtonListener executing\r\n"); for (int i = 0; i < listeners.size(); i++) listeners[i]->buttonEvent(c); //printf("done\r\n"); -} \ No newline at end of file +}*/
--- a/board/Board.h Sat May 23 20:17:54 2015 +0000 +++ b/board/Board.h Sat May 23 20:57:45 2015 +0000 @@ -8,6 +8,8 @@ #include "Button.h" #include "Keyboard.h" +#include "FPointer.h" + #include <vector> // class for debug leds @@ -23,13 +25,13 @@ LcdPins lcd; }; -class ButtonListener +/*class ButtonListener { public: - virtual void buttonEvent(char c){ - + uint32_t buttonEvent(uint32_t c){ + return 0; }; -}; +};*/ class Board{ public: @@ -42,13 +44,20 @@ Keyboard* keyboard; Buzzer* buzzer; - vector < ButtonListener * > listeners; + //vector < ButtonListener * > listeners; uint32_t keyboardButton(uint32_t index); - void attach(ButtonListener *bl); + //void attach(ButtonListener *bl); + //void attach(uint32_t (*fptr)(uint32_t)); +// template<class T> +/* void attach(T* item, uint32_t(T::*method)(uint32_t)){ + _callback.attach(item, method); + }*/ protected: - void buttonEvent(char c); + //void buttonEvent(char c); + + FPointer _callback; // Called after each input };
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/board/FPointer.h Sat May 23 20:57:45 2015 +0000 @@ -0,0 +1,163 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef AJK_FPOINTER_H +#define AJK_FPOINTER_H + +namespace AjK { + +class FPointerDummy; + +/** FPointer - Adds callbacks that take and return a 32bit uint32_t data type. + * + * The Mbed library supplies a callback using the FunctionPointer object as + * defined in FunctionPointer.h However, this callback system does not allow + * the caller to pass a value to the callback. Likewise, the callback itself + * cannot return a value. + * + * FPointer operates in the same way but allows the callback function to be + * passed one arg, a uint32_t value. Additionally, the callback can return + * a single uint32_t value. The reason for using uint32_t is that the Mbed + * and the microcontroller (LPC1768) have a natural data size of 32bits and + * this means we can use the uint32_t as a pointer. See example1.h for more + * information. This example passes an "int" by passing a pointer to that + * int as a 32bit value. Using this technique you can pass any value you like. + * All you have to do is pass a pointer to your value cast to (uint32_t). Your + * callback can the deference it to get the original value. + * + * example2.h shows how to do the same thing but demostrates how to specify + * the callback into a class object/method. + * + * Finally, example3.h shows how to pass multiple values. In this example we + * define a data structure and in the callback we pass a pointer to that + * data structure thus allowing the callback to again get the values. + * + * Note, when passing pointers to variables to the callback, if the callback + * function/method changes that variable's value then it will also change the + * value the caller sees. If C pointers are new to you, you are strongly + * advised to read up on the subject. It's pointers that often get beginners + * into trouble when mis-used. + * + * @see example1.h + * @see example2.h + * @see example3.h + * @see http://mbed.org/handbook/C-Data-Types + * @see http://mbed.org/projects/libraries/svn/mbed/trunk/FunctionPointer.h + */ +class FPointer { + +protected: + + //! C callback function pointer. + uint32_t (*c_callback)(uint32_t); + + //! C++ callback object/method pointer (the object part). + FPointerDummy *obj_callback; + + //! C++ callback object/method pointer (the method part). + uint32_t (FPointerDummy::*method_callback)(uint32_t); + +public: + + /** Constructor + */ + FPointer() { + c_callback = NULL; + obj_callback = NULL; + method_callback = NULL; + } + + /** attach - Overloaded attachment function. + * + * Attach a C type function pointer as the callback. + * + * Note, the callback function prototype must be:- + * @code + * uint32_t myCallbackFunction(uint32_t); + * @endcode + * @param A C function pointer to call. + */ + void attach(uint32_t (*function)(uint32_t) = 0) { c_callback = function; } + + /** attach - Overloaded attachment function. + * + * Attach a C++ type object/method pointer as the callback. + * + * Note, the callback method prototype must be:- + * @code + * public: + * uint32_t myCallbackFunction(uint32_t); + * @endcode + * @param A C++ object pointer. + * @param A C++ method within the object to call. + */ + template<class T> + void attach(T* item, uint32_t (T::*method)(uint32_t)) { + obj_callback = (FPointerDummy *)item; + method_callback = (uint32_t (FPointerDummy::*)(uint32_t))method; + } + + /** call - Overloaded callback initiator. + * + * call the callback function. + * + * @param uint32_t The value to pass to the callback. + * @return uint32_t The value the callback returns. + */ + uint32_t call(uint32_t arg) { + if (c_callback != NULL) { + return (*c_callback)(arg); + } + else { + if (obj_callback != NULL && method_callback != NULL) { + return (obj_callback->*method_callback)(arg); + } + } + return (uint32_t)NULL; + } + + /** call - Overloaded callback initiator. + * + * Call the callback function without passing an argument. + * The callback itself is passed NULL. Note, the callback + * prototype should still be <b>uint32_t callback(uint32_t)</b>. + * + * @return uint32_t The value the callback returns. + */ + uint32_t call(void) { + if (c_callback != NULL) { + return (*c_callback)((uint32_t)NULL); + } + else { + if (obj_callback != NULL && method_callback != NULL) { + return (obj_callback->*method_callback)((uint32_t)NULL); + } + } + return (uint32_t)NULL; + } +}; + +}; // namespace AjK ends + +using namespace AjK; + +#endif
--- a/board/Keyboard.h Sat May 23 20:17:54 2015 +0000 +++ b/board/Keyboard.h Sat May 23 20:57:45 2015 +0000 @@ -17,6 +17,7 @@ template<class T> void attach(T* item, uint32_t(T::*method)(uint32_t)){ + printf("Keyboard->attach\r\n"); _callback.attach(item, method); }
--- a/games/Game.h Sat May 23 20:17:54 2015 +0000 +++ b/games/Game.h Sat May 23 20:57:45 2015 +0000 @@ -3,7 +3,7 @@ #include "../board/Board.h" -class Game : public ButtonListener{ +class Game /*: public ButtonListener*/{ public: Game(Board* board); static const char* NAME;
--- a/games/GameSelector.cpp Sat May 23 20:17:54 2015 +0000 +++ b/games/GameSelector.cpp Sat May 23 20:57:45 2015 +0000 @@ -2,7 +2,7 @@ GameSelector::GameSelector(Board* board){ this->board = board; - this->board->attach(this); + //this->board->attach(this, &GameSelector::buttonEvent); titles[0] = "Hold it"; titles[1] = "Capture the bomb"; titles[2] = "Hurry up"; @@ -20,14 +20,14 @@ print_list(); printf("printed list\r\n"); while(selected_game == -1){ - board->leds->on(Leds::RIGHT); + //board->leds->on(Leds::RIGHT); } // wait until selection is done //printf("game selected: %d \r\n", selected_game); return selected_game; } -void GameSelector::buttonEvent(char c){ - //printf("Gameselector buttonEvent start\r\n"); +/*uint32_t GameSelector::buttonEvent(uint32_t c){ + printf("Gameselector buttonEvent start, char: %c\r\n", c); switch(c){ case '2': go_up(); @@ -42,7 +42,8 @@ break; } //printf("end\r\n"); -} + return 0; +}*/ void GameSelector::print_list(){ board->lcd->cls();
--- a/games/GameSelector.h Sat May 23 20:17:54 2015 +0000 +++ b/games/GameSelector.h Sat May 23 20:57:45 2015 +0000 @@ -3,7 +3,7 @@ #include "../board/Board.h" -class GameSelector : public ButtonListener{ +class GameSelector /*: public ButtonListener*/{ Board* board; void print_up_down_arrows(); void print_selection_arrow(); @@ -20,9 +20,10 @@ char* titles[7]; - virtual void buttonEvent(char c); + public: + //uint32_t buttonEvent(uint32_t c); GameSelector(Board* board); int select(); };
--- a/objectives/Objective.h Sat May 23 20:17:54 2015 +0000 +++ b/objectives/Objective.h Sat May 23 20:57:45 2015 +0000 @@ -3,7 +3,7 @@ #include "../games/Game.h" -class Objective : public ButtonListener{ +class Objective /*: public ButtonListener*/{ enum Status { WAITING, ACTIVE, COMPLETED };