Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
joystick.hpp
00001 #ifndef JOYSTICK_H 00002 #define JOYSTICK_H 00003 00004 #include <mbed.h> 00005 00006 class Joystick{ 00007 private: 00008 class Button { 00009 protected: 00010 Callback<void()> _callback_rise; 00011 Callback<void()> _callback_fall; 00012 public: 00013 InterruptIn _irqpin; 00014 00015 enum Edge { RisingEdge = 0, FallingEdge }; 00016 00017 Button(PinName p, PinMode mode = PullUp) : _irqpin(p, mode) { 00018 _irqpin.rise(callback(this, &Button::call_rise)); 00019 _irqpin.fall(callback(this, &Button::call_fall)); 00020 } 00021 00022 void attach(Edge edge, Callback<void()> cb) { 00023 if (edge == RisingEdge) _callback_rise = cb ; 00024 else { _callback_fall = cb ; } 00025 } 00026 00027 void call_rise(void) { if (_callback_rise) _callback_rise.call(); } 00028 void call_fall(void) { if (_callback_fall) _callback_fall.call(); } 00029 }; 00030 00031 Button _key_up; 00032 Button _key_down; 00033 Button _key_left; 00034 Button _key_right; 00035 Button _key_select; 00036 public: 00037 enum Action { Press = 0, Release }; 00038 enum Direction { Up, Down, Left, Right, Select }; 00039 00040 Joystick(PinName key_up = PI_8, \ 00041 PinName key_down = PI_10, \ 00042 PinName key_left = PI_9, \ 00043 PinName key_right = PF_11, \ 00044 PinName key_select = PC_13) : \ 00045 _key_up(key_up, PullDown), \ 00046 _key_down(key_down, PullDown),\ 00047 _key_left(key_left, PullDown),\ 00048 _key_right(key_right, PullDown),\ 00049 _key_select(key_select, PullDown) 00050 { 00051 } 00052 00053 void attach(Direction direction, Action action, Callback<void()> cb) { 00054 Button * button; 00055 switch(direction){ 00056 case Up: 00057 button = &_key_up; 00058 break; 00059 case Down: 00060 button = &_key_down; 00061 break; 00062 case Left: 00063 button = &_key_left; 00064 break; 00065 case Right: 00066 button = &_key_right; 00067 break; 00068 case Select: 00069 button = &_key_select; 00070 break; 00071 } 00072 if (action == Press) button->attach( Button::RisingEdge, cb ); 00073 else button->attach( Button::FallingEdge, cb ); 00074 } 00075 }; 00076 00077 #endif
Generated on Sun Jul 24 2022 10:51:00 by
1.7.2