Gets potentiometer and button reading from a joystick (https://proto-pic.co.uk/thumb-joystick-analogue/)
Dependents: L2_SpaceInvaders 6-Joystick
Joystick.cpp@2:c9f2a9e2f304, 2016-05-05 (annotated)
- Committer:
- avi23
- Date:
- Thu May 05 11:00:46 2016 +0000
- Revision:
- 2:c9f2a9e2f304
- Parent:
- 1:78d3e8b50d19
Changed debounce timeout to attach on the falling edge of the joystick. Prevents debounce when button is held down
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
avi23 | 1:78d3e8b50d19 | 1 | /** |
avi23 | 1:78d3e8b50d19 | 2 | @file Joystick.cpp |
avi23 | 1:78d3e8b50d19 | 3 | |
avi23 | 1:78d3e8b50d19 | 4 | @brief Member functions implementations |
avi23 | 1:78d3e8b50d19 | 5 | |
avi23 | 1:78d3e8b50d19 | 6 | */ |
avi23 | 0:56a82b52e0d4 | 7 | #include "Joystick.h" |
avi23 | 0:56a82b52e0d4 | 8 | #include "mbed.h" |
avi23 | 0:56a82b52e0d4 | 9 | |
avi23 | 2:c9f2a9e2f304 | 10 | Joystick::Joystick(PinName x_axis_pin, PinName y_axis_pin, PinName button_pin) |
avi23 | 2:c9f2a9e2f304 | 11 | { |
avi23 | 2:c9f2a9e2f304 | 12 | x_axis_ = new AnalogIn(x_axis_pin); |
avi23 | 2:c9f2a9e2f304 | 13 | y_axis_ = new AnalogIn(y_axis_pin); |
avi23 | 2:c9f2a9e2f304 | 14 | button_ = new InterruptIn(button_pin); |
avi23 | 2:c9f2a9e2f304 | 15 | button_debounce_ = new Timeout(); |
avi23 | 0:56a82b52e0d4 | 16 | } |
avi23 | 0:56a82b52e0d4 | 17 | |
avi23 | 2:c9f2a9e2f304 | 18 | Joystick::~Joystick() |
avi23 | 2:c9f2a9e2f304 | 19 | { |
avi23 | 2:c9f2a9e2f304 | 20 | delete x_axis_; |
avi23 | 2:c9f2a9e2f304 | 21 | delete y_axis_; |
avi23 | 2:c9f2a9e2f304 | 22 | delete button_; |
avi23 | 2:c9f2a9e2f304 | 23 | delete button_debounce_; |
avi23 | 0:56a82b52e0d4 | 24 | } |
avi23 | 0:56a82b52e0d4 | 25 | |
avi23 | 2:c9f2a9e2f304 | 26 | void Joystick::init() |
avi23 | 2:c9f2a9e2f304 | 27 | { |
avi23 | 0:56a82b52e0d4 | 28 | //Sets up the button ISR |
avi23 | 2:c9f2a9e2f304 | 29 | button_->mode(PullDown); |
avi23 | 2:c9f2a9e2f304 | 30 | button_->rise(this, &Joystick::button_isr); |
avi23 | 2:c9f2a9e2f304 | 31 | button_->fall(this, &Joystick::button_fall_isr); |
avi23 | 0:56a82b52e0d4 | 32 | |
avi23 | 0:56a82b52e0d4 | 33 | //Initalises the vairables and flags |
avi23 | 2:c9f2a9e2f304 | 34 | x_offset_ = 0; |
avi23 | 2:c9f2a9e2f304 | 35 | y_offset_ = 0; |
avi23 | 2:c9f2a9e2f304 | 36 | g_button_flag_ = false; |
avi23 | 2:c9f2a9e2f304 | 37 | g_button_debounce_flag_ = false; |
avi23 | 0:56a82b52e0d4 | 38 | |
avi23 | 0:56a82b52e0d4 | 39 | //Samples the joystick 5 times and takes an average to get the offset |
avi23 | 2:c9f2a9e2f304 | 40 | float x_sum = 0; |
avi23 | 2:c9f2a9e2f304 | 41 | float y_sum = 0; |
avi23 | 0:56a82b52e0d4 | 42 | |
avi23 | 2:c9f2a9e2f304 | 43 | for (int i = 0; i < 5; ++i) { |
avi23 | 2:c9f2a9e2f304 | 44 | x_sum += x_axis_->read(); |
avi23 | 2:c9f2a9e2f304 | 45 | y_sum += y_axis_->read(); |
avi23 | 2:c9f2a9e2f304 | 46 | } |
avi23 | 0:56a82b52e0d4 | 47 | |
avi23 | 2:c9f2a9e2f304 | 48 | x_offset_ = 0.5f - x_sum/5.0f; |
avi23 | 2:c9f2a9e2f304 | 49 | y_offset_ = 0.5f - y_sum/5.0f; |
avi23 | 0:56a82b52e0d4 | 50 | } |
avi23 | 0:56a82b52e0d4 | 51 | |
avi23 | 2:c9f2a9e2f304 | 52 | float Joystick::GetXValue() |
avi23 | 2:c9f2a9e2f304 | 53 | { |
avi23 | 0:56a82b52e0d4 | 54 | |
avi23 | 2:c9f2a9e2f304 | 55 | float x_sum = 0; |
avi23 | 0:56a82b52e0d4 | 56 | //Iterates 5 times and calculates an average |
avi23 | 2:c9f2a9e2f304 | 57 | for (int i = 0; i < 5; ++i) { |
avi23 | 2:c9f2a9e2f304 | 58 | x_sum += x_axis_->read(); |
avi23 | 2:c9f2a9e2f304 | 59 | } |
avi23 | 0:56a82b52e0d4 | 60 | |
avi23 | 2:c9f2a9e2f304 | 61 | float x_value = x_sum/5.0f + x_offset_; |
avi23 | 0:56a82b52e0d4 | 62 | |
avi23 | 0:56a82b52e0d4 | 63 | //Caps the value for the POT between 0 and 1 |
avi23 | 2:c9f2a9e2f304 | 64 | if (x_value < 0.0f) { |
avi23 | 2:c9f2a9e2f304 | 65 | return 0; |
avi23 | 2:c9f2a9e2f304 | 66 | } else if (x_value > 1.0f) { |
avi23 | 2:c9f2a9e2f304 | 67 | return 1; |
avi23 | 2:c9f2a9e2f304 | 68 | } else { |
avi23 | 2:c9f2a9e2f304 | 69 | return x_value; |
avi23 | 2:c9f2a9e2f304 | 70 | } |
avi23 | 0:56a82b52e0d4 | 71 | } |
avi23 | 0:56a82b52e0d4 | 72 | |
avi23 | 2:c9f2a9e2f304 | 73 | float Joystick::GetYValue() |
avi23 | 2:c9f2a9e2f304 | 74 | { |
avi23 | 0:56a82b52e0d4 | 75 | |
avi23 | 2:c9f2a9e2f304 | 76 | float y_sum = 0; |
avi23 | 0:56a82b52e0d4 | 77 | //Iterates 5 times and calculates an average |
avi23 | 2:c9f2a9e2f304 | 78 | for (int i = 0; i < 5; ++i) { |
avi23 | 2:c9f2a9e2f304 | 79 | y_sum += y_axis_->read(); |
avi23 | 2:c9f2a9e2f304 | 80 | } |
avi23 | 0:56a82b52e0d4 | 81 | |
avi23 | 2:c9f2a9e2f304 | 82 | float y_value = y_sum/5.0f + y_offset_; |
avi23 | 0:56a82b52e0d4 | 83 | |
avi23 | 0:56a82b52e0d4 | 84 | //Caps the value for the POT between 0 and 1 |
avi23 | 2:c9f2a9e2f304 | 85 | if (y_value < 0.0f) { |
avi23 | 2:c9f2a9e2f304 | 86 | return 0; |
avi23 | 2:c9f2a9e2f304 | 87 | } else if (y_value > 1.0f) { |
avi23 | 2:c9f2a9e2f304 | 88 | return 1; |
avi23 | 2:c9f2a9e2f304 | 89 | } else { |
avi23 | 2:c9f2a9e2f304 | 90 | return y_value; |
avi23 | 2:c9f2a9e2f304 | 91 | } |
avi23 | 0:56a82b52e0d4 | 92 | } |
avi23 | 0:56a82b52e0d4 | 93 | |
avi23 | 2:c9f2a9e2f304 | 94 | int Joystick::get_button_flag() |
avi23 | 2:c9f2a9e2f304 | 95 | { |
avi23 | 2:c9f2a9e2f304 | 96 | return g_button_flag_; |
avi23 | 0:56a82b52e0d4 | 97 | } |
avi23 | 0:56a82b52e0d4 | 98 | |
avi23 | 2:c9f2a9e2f304 | 99 | void Joystick::set_button_flag(bool value) |
avi23 | 2:c9f2a9e2f304 | 100 | { |
avi23 | 2:c9f2a9e2f304 | 101 | g_button_flag_ = value; |
avi23 | 0:56a82b52e0d4 | 102 | } |
avi23 | 0:56a82b52e0d4 | 103 | |
avi23 | 2:c9f2a9e2f304 | 104 | void Joystick::button_isr() |
avi23 | 2:c9f2a9e2f304 | 105 | { |
avi23 | 2:c9f2a9e2f304 | 106 | if (!g_button_debounce_flag_) { |
avi23 | 2:c9f2a9e2f304 | 107 | g_button_flag_ = true; |
avi23 | 2:c9f2a9e2f304 | 108 | g_button_debounce_flag_ = true; |
avi23 | 2:c9f2a9e2f304 | 109 | } |
avi23 | 0:56a82b52e0d4 | 110 | } |
avi23 | 0:56a82b52e0d4 | 111 | |
avi23 | 2:c9f2a9e2f304 | 112 | void Joystick::button_fall_isr() |
avi23 | 2:c9f2a9e2f304 | 113 | { |
avi23 | 2:c9f2a9e2f304 | 114 | button_debounce_->attach(this, &Joystick::button_debounce_isr, 0.125); |
avi23 | 2:c9f2a9e2f304 | 115 | } |
avi23 | 2:c9f2a9e2f304 | 116 | |
avi23 | 2:c9f2a9e2f304 | 117 | void Joystick::button_debounce_isr() |
avi23 | 2:c9f2a9e2f304 | 118 | { |
avi23 | 2:c9f2a9e2f304 | 119 | g_button_debounce_flag_ = false; |
avi23 | 0:56a82b52e0d4 | 120 | } |