IO is an event based input manager which permit to select which composents are manged on your system
Dependencies: C12832 FXOS8700Q LM75B MMA7660
FrdmK64f_Input.hpp@4:50e2aefe516b, 2016-02-25 (annotated)
- Committer:
- co838_app56
- Date:
- Thu Feb 25 15:12:32 2016 +0000
- Revision:
- 4:50e2aefe516b
- Parent:
- 3:1ab88130bb9d
Fixing precision issue;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co838_app56 | 0:2ac59c564ab0 | 1 | #pragma once |
co838_app56 | 0:2ac59c564ab0 | 2 | |
co838_app56 | 0:2ac59c564ab0 | 3 | #include "mbed.h" |
co838_app56 | 3:1ab88130bb9d | 4 | #include "FXOS8700Q.h" |
co838_app56 | 0:2ac59c564ab0 | 5 | |
co838_app56 | 0:2ac59c564ab0 | 6 | template <class Parent> |
co838_app56 | 0:2ac59c564ab0 | 7 | class FrdmK64f_Input : public Parent |
co838_app56 | 0:2ac59c564ab0 | 8 | { |
co838_app56 | 1:7be9a82f3ab8 | 9 | public: // List of manged Inputs |
co838_app56 | 0:2ac59c564ab0 | 10 | |
co838_app56 | 0:2ac59c564ab0 | 11 | enum IDBinaryInput |
co838_app56 | 0:2ac59c564ab0 | 12 | { |
co838_app56 | 0:2ac59c564ab0 | 13 | ButtonLeft, |
co838_app56 | 1:7be9a82f3ab8 | 14 | ButtonRight, |
co838_app56 | 1:7be9a82f3ab8 | 15 | IDBinaryInput_MAX |
co838_app56 | 0:2ac59c564ab0 | 16 | }; |
co838_app56 | 1:7be9a82f3ab8 | 17 | |
co838_app56 | 3:1ab88130bb9d | 18 | enum IDVectorInput |
co838_app56 | 3:1ab88130bb9d | 19 | { |
co838_app56 | 3:1ab88130bb9d | 20 | Accel, |
co838_app56 | 3:1ab88130bb9d | 21 | Magnet, |
co838_app56 | 3:1ab88130bb9d | 22 | IDVectorInput_MAX |
co838_app56 | 3:1ab88130bb9d | 23 | }; |
co838_app56 | 3:1ab88130bb9d | 24 | |
co838_app56 | 1:7be9a82f3ab8 | 25 | private: |
co838_app56 | 0:2ac59c564ab0 | 26 | |
co838_app56 | 1:7be9a82f3ab8 | 27 | // Buttons |
co838_app56 | 1:7be9a82f3ab8 | 28 | InterruptIn _buttonLeft; |
co838_app56 | 1:7be9a82f3ab8 | 29 | InterruptIn _buttonRight; |
co838_app56 | 3:1ab88130bb9d | 30 | bool _binaryInputEnable[IDBinaryInput_MAX]; |
co838_app56 | 3:1ab88130bb9d | 31 | |
co838_app56 | 3:1ab88130bb9d | 32 | FXOS8700Q_acc _accel; |
co838_app56 | 3:1ab88130bb9d | 33 | FXOS8700Q_mag _magnet; |
co838_app56 | 3:1ab88130bb9d | 34 | Vector _accelValue; |
co838_app56 | 3:1ab88130bb9d | 35 | Vector _magnetValue; |
co838_app56 | 3:1ab88130bb9d | 36 | float _accelPrecision; |
co838_app56 | 3:1ab88130bb9d | 37 | float _magnetPrecision; |
co838_app56 | 3:1ab88130bb9d | 38 | bool _vectorInputEnable[IDVectorInput_MAX]; |
co838_app56 | 3:1ab88130bb9d | 39 | float _vectorInputPrec[IDVectorInput_MAX]; |
co838_app56 | 1:7be9a82f3ab8 | 40 | |
co838_app56 | 1:7be9a82f3ab8 | 41 | public: |
co838_app56 | 0:2ac59c564ab0 | 42 | FrdmK64f_Input(void) |
co838_app56 | 3:1ab88130bb9d | 43 | : _buttonLeft(SW3), _buttonRight(SW2), |
co838_app56 | 3:1ab88130bb9d | 44 | _accel( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1), _magnet( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1), |
co838_app56 | 3:1ab88130bb9d | 45 | _accelPrecision(100.0f), _magnetPrecision(100.0f) |
co838_app56 | 3:1ab88130bb9d | 46 | |
co838_app56 | 0:2ac59c564ab0 | 47 | { |
co838_app56 | 2:c871dc21467b | 48 | memset(_binaryInputEnable, 1, sizeof(_binaryInputEnable)); |
co838_app56 | 3:1ab88130bb9d | 49 | memset(_vectorInputEnable, 0, sizeof(_vectorInputEnable)); |
co838_app56 | 3:1ab88130bb9d | 50 | |
co838_app56 | 3:1ab88130bb9d | 51 | for (int it = 0; it < IDVectorInput_MAX; ++it) _vectorInputPrec[it] = 10.0f; |
co838_app56 | 1:7be9a82f3ab8 | 52 | |
co838_app56 | 0:2ac59c564ab0 | 53 | _buttonLeft.rise(this, &FrdmK64f_Input::onButtonLeftRise); |
co838_app56 | 0:2ac59c564ab0 | 54 | _buttonLeft.fall(this, &FrdmK64f_Input::onButtonLeftFall); |
co838_app56 | 0:2ac59c564ab0 | 55 | _buttonRight.rise(this, &FrdmK64f_Input::onButtonRightRise); |
co838_app56 | 0:2ac59c564ab0 | 56 | _buttonRight.fall(this, &FrdmK64f_Input::onButtonRightFall); |
co838_app56 | 3:1ab88130bb9d | 57 | |
co838_app56 | 3:1ab88130bb9d | 58 | _accel.enable(); |
co838_app56 | 3:1ab88130bb9d | 59 | _magnet.enable(); |
co838_app56 | 0:2ac59c564ab0 | 60 | } |
co838_app56 | 0:2ac59c564ab0 | 61 | |
co838_app56 | 1:7be9a82f3ab8 | 62 | using Parent::setEnable; |
co838_app56 | 2:c871dc21467b | 63 | void setEnable(FrdmK64f_Input::IDBinaryInput inp, bool act) { _binaryInputEnable[inp] = act; } |
co838_app56 | 3:1ab88130bb9d | 64 | void setEnable(FrdmK64f_Input::IDVectorInput inp, bool act) { _vectorInputEnable[inp] = act; } |
co838_app56 | 1:7be9a82f3ab8 | 65 | |
co838_app56 | 3:1ab88130bb9d | 66 | using Parent::setPrecision; |
co838_app56 | 4:50e2aefe516b | 67 | void setPrecision(FrdmK64f_Input::IDVectorInput inp, float prec) { _vectorInputPrec[inp] = pow(10.0f, prec); } |
co838_app56 | 3:1ab88130bb9d | 68 | |
co838_app56 | 1:7be9a82f3ab8 | 69 | // Not interresting section (do not use those methods) |
co838_app56 | 1:7be9a82f3ab8 | 70 | // Callbacks for joystick |
co838_app56 | 2:c871dc21467b | 71 | void onButtonLeftRise(void) { if (_binaryInputEnable[ButtonLeft]) Parent::_events.push(Event(Event::FrdmK64f, Event::BinaryInput, ButtonLeft, Event::Rise)); } |
co838_app56 | 2:c871dc21467b | 72 | void onButtonLeftFall(void) { if (_binaryInputEnable[ButtonLeft]) Parent::_events.push(Event(Event::FrdmK64f, Event::BinaryInput, ButtonLeft, Event::Fall)); } |
co838_app56 | 2:c871dc21467b | 73 | void onButtonRightRise(void) { if (_binaryInputEnable[ButtonRight]) Parent::_events.push(Event(Event::FrdmK64f, Event::BinaryInput, ButtonRight, Event::Rise)); } |
co838_app56 | 2:c871dc21467b | 74 | void onButtonRightFall(void) { if (_binaryInputEnable[ButtonRight]) Parent::_events.push(Event(Event::FrdmK64f, Event::BinaryInput, ButtonRight, Event::Fall)); } |
co838_app56 | 3:1ab88130bb9d | 75 | |
co838_app56 | 3:1ab88130bb9d | 76 | using Parent::checkAnalog; |
co838_app56 | 0:2ac59c564ab0 | 77 | // Callback for others sensors |
co838_app56 | 3:1ab88130bb9d | 78 | virtual void checkVector(void) |
co838_app56 | 3:1ab88130bb9d | 79 | { |
co838_app56 | 3:1ab88130bb9d | 80 | Parent::checkVector(); |
co838_app56 | 3:1ab88130bb9d | 81 | |
co838_app56 | 3:1ab88130bb9d | 82 | if(_vectorInputEnable[Accel]) |
co838_app56 | 3:1ab88130bb9d | 83 | { |
co838_app56 | 3:1ab88130bb9d | 84 | MotionSensorDataUnits acc_data; |
co838_app56 | 3:1ab88130bb9d | 85 | _accel.getAxis(acc_data); |
co838_app56 | 3:1ab88130bb9d | 86 | Vector tmp; |
co838_app56 | 3:1ab88130bb9d | 87 | tmp.x = prec(acc_data.x, _vectorInputPrec[Accel]); |
co838_app56 | 3:1ab88130bb9d | 88 | tmp.y = prec(acc_data.y, _vectorInputPrec[Accel]); |
co838_app56 | 3:1ab88130bb9d | 89 | tmp.z = prec(acc_data.z, _vectorInputPrec[Accel]); |
co838_app56 | 3:1ab88130bb9d | 90 | Event event(Event::FrdmK64f, Event::VectorInput, Accel, tmp); |
co838_app56 | 3:1ab88130bb9d | 91 | if (!(event.vector.eq(_accelValue, 1.0f / _vectorInputPrec[Accel]))) |
co838_app56 | 3:1ab88130bb9d | 92 | { |
co838_app56 | 3:1ab88130bb9d | 93 | _accelValue = event.vector; |
co838_app56 | 3:1ab88130bb9d | 94 | Parent::_events.push(event); |
co838_app56 | 3:1ab88130bb9d | 95 | } |
co838_app56 | 3:1ab88130bb9d | 96 | } |
co838_app56 | 3:1ab88130bb9d | 97 | |
co838_app56 | 3:1ab88130bb9d | 98 | if(_vectorInputEnable[Magnet]) |
co838_app56 | 3:1ab88130bb9d | 99 | { |
co838_app56 | 3:1ab88130bb9d | 100 | MotionSensorDataUnits mag_data; |
co838_app56 | 3:1ab88130bb9d | 101 | _magnet.getAxis(mag_data); |
co838_app56 | 3:1ab88130bb9d | 102 | Vector tmp; |
co838_app56 | 3:1ab88130bb9d | 103 | tmp.x = prec(mag_data.x, _vectorInputPrec[Magnet]); |
co838_app56 | 3:1ab88130bb9d | 104 | tmp.y = prec(mag_data.y, _vectorInputPrec[Magnet]); |
co838_app56 | 3:1ab88130bb9d | 105 | tmp.z = prec(mag_data.z, _vectorInputPrec[Magnet]); |
co838_app56 | 3:1ab88130bb9d | 106 | Event event(Event::FrdmK64f, Event::VectorInput, Magnet, tmp); |
co838_app56 | 3:1ab88130bb9d | 107 | if (!(event.vector.eq(_accelValue, (int)(1.0f / _vectorInputPrec[Magnet])))) |
co838_app56 | 3:1ab88130bb9d | 108 | { |
co838_app56 | 3:1ab88130bb9d | 109 | _accelValue = event.vector; |
co838_app56 | 3:1ab88130bb9d | 110 | Parent::_events.push(event); |
co838_app56 | 3:1ab88130bb9d | 111 | } |
co838_app56 | 3:1ab88130bb9d | 112 | } |
co838_app56 | 3:1ab88130bb9d | 113 | } |
co838_app56 | 0:2ac59c564ab0 | 114 | }; |