IO is an event based input manager which permit to select which composents are manged on your system

Dependencies:   C12832 FXOS8700Q LM75B MMA7660

Committer:
co838_app56
Date:
Tue Feb 23 18:38:27 2016 +0000
Revision:
1:7be9a82f3ab8
Parent:
0:2ac59c564ab0
Child:
2:c871dc21467b
Can now disable buttons

Who changed what in which revision?

UserRevisionLine numberNew 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 0:2ac59c564ab0 4 #include "LM75B.h"
co838_app56 0:2ac59c564ab0 5 #include "MMA7660.h"
co838_app56 0:2ac59c564ab0 6
co838_app56 0:2ac59c564ab0 7 #include "Vector.hpp"
co838_app56 0:2ac59c564ab0 8 #include "Utils.h"
co838_app56 0:2ac59c564ab0 9
co838_app56 0:2ac59c564ab0 10 template <class Parent>
co838_app56 0:2ac59c564ab0 11 class FrdmK64f_AppShield_Input : public Parent
co838_app56 0:2ac59c564ab0 12 {
co838_app56 1:7be9a82f3ab8 13 public: // List of manged Inputs
co838_app56 1:7be9a82f3ab8 14
co838_app56 1:7be9a82f3ab8 15 enum IDBinaryInput
co838_app56 1:7be9a82f3ab8 16 {
co838_app56 1:7be9a82f3ab8 17 JoystickUp,
co838_app56 1:7be9a82f3ab8 18 JoystickDown,
co838_app56 1:7be9a82f3ab8 19 JoystickLeft,
co838_app56 1:7be9a82f3ab8 20 JoystickRight,
co838_app56 1:7be9a82f3ab8 21 JoystickMiddle,
co838_app56 1:7be9a82f3ab8 22 IDBinaryInput_MAX
co838_app56 1:7be9a82f3ab8 23 };
co838_app56 1:7be9a82f3ab8 24
co838_app56 1:7be9a82f3ab8 25 enum IDAnalogInput
co838_app56 1:7be9a82f3ab8 26 {
co838_app56 1:7be9a82f3ab8 27 PotLeft,
co838_app56 1:7be9a82f3ab8 28 PotRight,
co838_app56 1:7be9a82f3ab8 29 Temp
co838_app56 1:7be9a82f3ab8 30 };
co838_app56 1:7be9a82f3ab8 31
co838_app56 1:7be9a82f3ab8 32 enum IDVectorInput
co838_app56 1:7be9a82f3ab8 33 {
co838_app56 1:7be9a82f3ab8 34 Accel
co838_app56 1:7be9a82f3ab8 35 };
co838_app56 1:7be9a82f3ab8 36
co838_app56 1:7be9a82f3ab8 37 private:
co838_app56 1:7be9a82f3ab8 38
co838_app56 0:2ac59c564ab0 39 // Joystick
co838_app56 0:2ac59c564ab0 40 InterruptIn _joystickUp;
co838_app56 0:2ac59c564ab0 41 InterruptIn _joystickDown;
co838_app56 0:2ac59c564ab0 42 InterruptIn _joystickLeft;
co838_app56 0:2ac59c564ab0 43 InterruptIn _joystickRight;
co838_app56 0:2ac59c564ab0 44 InterruptIn _joystickMiddle;
co838_app56 1:7be9a82f3ab8 45 bool _joystickEnable[IDBinaryInput_MAX];
co838_app56 0:2ac59c564ab0 46
co838_app56 0:2ac59c564ab0 47 // Potentiometers
co838_app56 0:2ac59c564ab0 48 AnalogIn _potLeft;
co838_app56 0:2ac59c564ab0 49 AnalogIn _potRight;
co838_app56 0:2ac59c564ab0 50 float _potLeftValue;
co838_app56 0:2ac59c564ab0 51 float _potRightValue;
co838_app56 0:2ac59c564ab0 52 float _potLeftPrecision;
co838_app56 0:2ac59c564ab0 53 float _potRightPrecision;
co838_app56 0:2ac59c564ab0 54
co838_app56 0:2ac59c564ab0 55 // Tempeture sensor
co838_app56 0:2ac59c564ab0 56 LM75B _temp;
co838_app56 0:2ac59c564ab0 57 float _tempValue;
co838_app56 0:2ac59c564ab0 58 float _tempPrecision;
co838_app56 0:2ac59c564ab0 59
co838_app56 0:2ac59c564ab0 60 // Accelerometer
co838_app56 0:2ac59c564ab0 61 MMA7660 _accel;
co838_app56 0:2ac59c564ab0 62 Vector<float> _accelValue;
co838_app56 0:2ac59c564ab0 63 float _accelPrecision;
co838_app56 0:2ac59c564ab0 64
co838_app56 0:2ac59c564ab0 65 public:
co838_app56 0:2ac59c564ab0 66
co838_app56 0:2ac59c564ab0 67 FrdmK64f_AppShield_Input(void)
co838_app56 0:2ac59c564ab0 68 : _joystickUp(A2), _joystickDown(A3), _joystickLeft(A4), _joystickRight(A5), _joystickMiddle(D4),
co838_app56 0:2ac59c564ab0 69 _potLeft(A0), _potRight(A1), _potLeftValue(0.0f), _potRightValue(0.0f), _potLeftPrecision(1000.0f), _potRightPrecision(1000.0f),
co838_app56 0:2ac59c564ab0 70 _temp(D14, D15), _tempValue(0.0f), _tempPrecision(10.0f),
co838_app56 0:2ac59c564ab0 71 _accel(I2C_SDA, I2C_SCL), _accelPrecision(10.0f)
co838_app56 0:2ac59c564ab0 72 {
co838_app56 1:7be9a82f3ab8 73 memset(_joystickEnable, 1, sizeof(_joystickEnable));
co838_app56 1:7be9a82f3ab8 74
co838_app56 0:2ac59c564ab0 75 _joystickUp.rise(this, &FrdmK64f_AppShield_Input::onJoystickUpRise);
co838_app56 0:2ac59c564ab0 76 _joystickUp.fall(this, &FrdmK64f_AppShield_Input::onJoystickUpFall);
co838_app56 0:2ac59c564ab0 77 _joystickDown.rise(this, &FrdmK64f_AppShield_Input::onJoystickDownRise);
co838_app56 0:2ac59c564ab0 78 _joystickDown.fall(this, &FrdmK64f_AppShield_Input::onJoystickDownFall);
co838_app56 0:2ac59c564ab0 79 _joystickLeft.rise(this, &FrdmK64f_AppShield_Input::onJoystickLeftRise);
co838_app56 0:2ac59c564ab0 80 _joystickLeft.fall(this, &FrdmK64f_AppShield_Input::onJoystickLeftFall);
co838_app56 0:2ac59c564ab0 81 _joystickRight.rise(this, &FrdmK64f_AppShield_Input::onJoystickRightRise);
co838_app56 0:2ac59c564ab0 82 _joystickRight.fall(this, &FrdmK64f_AppShield_Input::onJoystickRightFall);
co838_app56 0:2ac59c564ab0 83 _joystickMiddle.rise(this, &FrdmK64f_AppShield_Input::onJoystickMiddleRise);
co838_app56 0:2ac59c564ab0 84 _joystickMiddle.fall(this, &FrdmK64f_AppShield_Input::onJoystickMiddleFall);
co838_app56 0:2ac59c564ab0 85
co838_app56 0:2ac59c564ab0 86 _potLeftValue = prec(_potLeft, _potLeftPrecision);
co838_app56 0:2ac59c564ab0 87 _potRightValue = prec(_potRight, _potRightPrecision);
co838_app56 0:2ac59c564ab0 88 _tempValue = prec(_temp.read(), _tempPrecision);
co838_app56 0:2ac59c564ab0 89 }
co838_app56 0:2ac59c564ab0 90
co838_app56 1:7be9a82f3ab8 91 using Parent::setEnable;
co838_app56 1:7be9a82f3ab8 92 void setEnable(FrdmK64f_AppShield_Input::IDBinaryInput inp, bool act) { _joystickEnable[inp] = act; }
co838_app56 1:7be9a82f3ab8 93
co838_app56 0:2ac59c564ab0 94 void setPotLeftPrecision(short int pres) { _potLeftPrecision = pow(10.0f, pres); }
co838_app56 0:2ac59c564ab0 95 void setPotRightPrecision(short int pres) { _potRightPrecision = pow(10.0f, pres); }
co838_app56 0:2ac59c564ab0 96 void setTempPrecision(short int pres) { _tempPrecision = pow(10.0f, pres); }
co838_app56 0:2ac59c564ab0 97 void setAccelPrecision(short int pres) { _accelPrecision = pow(10.0f, pres); }
co838_app56 0:2ac59c564ab0 98
co838_app56 0:2ac59c564ab0 99 // Not interresting section (do not use those methods)
co838_app56 0:2ac59c564ab0 100 // Callbacks for joystick
co838_app56 1:7be9a82f3ab8 101 void onJoystickUpRise(void) { if (_joystickEnable[JoystickUp]) Parent::_events.push(Event(Event::AppShield, Event::BinaryInput, JoystickUp, Event::Rise)); }
co838_app56 1:7be9a82f3ab8 102 void onJoystickUpFall(void) { if (_joystickEnable[JoystickUp]) Parent::_events.push(Event(Event::AppShield, Event::BinaryInput, JoystickUp, Event::Fall)); }
co838_app56 1:7be9a82f3ab8 103 void onJoystickDownRise(void) { if (_joystickEnable[JoystickDown]) Parent::_events.push(Event(Event::AppShield, Event::BinaryInput, JoystickDown, Event::Rise)); }
co838_app56 1:7be9a82f3ab8 104 void onJoystickDownFall(void) { if (_joystickEnable[JoystickDown]) Parent::_events.push(Event(Event::AppShield, Event::BinaryInput, JoystickDown, Event::Fall)); }
co838_app56 1:7be9a82f3ab8 105 void onJoystickLeftRise(void) { if (_joystickEnable[JoystickLeft]) Parent::_events.push(Event(Event::AppShield, Event::BinaryInput, JoystickLeft, Event::Rise)); }
co838_app56 1:7be9a82f3ab8 106 void onJoystickLeftFall(void) { if (_joystickEnable[JoystickLeft]) Parent::_events.push(Event(Event::AppShield, Event::BinaryInput, JoystickLeft, Event::Fall)); }
co838_app56 1:7be9a82f3ab8 107 void onJoystickRightRise(void) { if (_joystickEnable[JoystickRight]) Parent::_events.push(Event(Event::AppShield, Event::BinaryInput, JoystickRight, Event::Rise)); }
co838_app56 1:7be9a82f3ab8 108 void onJoystickRightFall(void) { if (_joystickEnable[JoystickRight]) Parent::_events.push(Event(Event::AppShield, Event::BinaryInput, JoystickRight, Event::Fall)); }
co838_app56 1:7be9a82f3ab8 109 void onJoystickMiddleRise(void) { if (_joystickEnable[JoystickMiddle]) Parent::_events.push(Event(Event::AppShield, Event::BinaryInput, JoystickMiddle, Event::Rise)); }
co838_app56 1:7be9a82f3ab8 110 void onJoystickMiddleFall(void) { if (_joystickEnable[JoystickMiddle]) Parent::_events.push(Event(Event::AppShield, Event::BinaryInput, JoystickMiddle, Event::Fall)); }
co838_app56 0:2ac59c564ab0 111
co838_app56 0:2ac59c564ab0 112 // Callback for others sensors
co838_app56 0:2ac59c564ab0 113 virtual void chechAnalog(void)
co838_app56 0:2ac59c564ab0 114 {
co838_app56 0:2ac59c564ab0 115 Parent::chechAnalog();
co838_app56 0:2ac59c564ab0 116 {
co838_app56 0:2ac59c564ab0 117 Event event(Event::AppShield, Event::AnalogInput, PotLeft, prec(_potLeft, _potLeftPrecision));
co838_app56 0:2ac59c564ab0 118 if (event.analog != _potLeftValue)
co838_app56 0:2ac59c564ab0 119 {
co838_app56 0:2ac59c564ab0 120 _potLeftValue = event.analog;
co838_app56 0:2ac59c564ab0 121 Parent::_events.push(event);
co838_app56 0:2ac59c564ab0 122 }
co838_app56 0:2ac59c564ab0 123 }
co838_app56 0:2ac59c564ab0 124 {
co838_app56 0:2ac59c564ab0 125 Event event(Event::AppShield, Event::AnalogInput, PotRight, prec(_potRight, _potRightPrecision));
co838_app56 0:2ac59c564ab0 126 if (event.analog != _potRightValue)
co838_app56 0:2ac59c564ab0 127 {
co838_app56 0:2ac59c564ab0 128 _potRightValue = event.analog;
co838_app56 0:2ac59c564ab0 129 Parent::_events.push(event);
co838_app56 0:2ac59c564ab0 130 }
co838_app56 0:2ac59c564ab0 131 }
co838_app56 0:2ac59c564ab0 132 {
co838_app56 0:2ac59c564ab0 133 Event event(Event::AppShield, Event::AnalogInput, Temp, prec(_temp.read(), _tempPrecision));
co838_app56 0:2ac59c564ab0 134 if (event.analog != _tempValue)
co838_app56 0:2ac59c564ab0 135 {
co838_app56 0:2ac59c564ab0 136 _tempValue = event.analog;
co838_app56 0:2ac59c564ab0 137 Parent::_events.push(event);
co838_app56 0:2ac59c564ab0 138 }
co838_app56 0:2ac59c564ab0 139 }
co838_app56 0:2ac59c564ab0 140 {
co838_app56 0:2ac59c564ab0 141 Event event(Event::AppShield, Event::VectorInput, Accel, Vector<float>(prec(_accel.x(), _accelPrecision), prec(_accel.y(), _accelPrecision), prec(_accel.z(), _accelPrecision)));
co838_app56 0:2ac59c564ab0 142 if (!(event.vector.eq(_accelValue, 1.0f / _accelPrecision)))
co838_app56 0:2ac59c564ab0 143 {
co838_app56 0:2ac59c564ab0 144 _accelValue = event.vector;
co838_app56 0:2ac59c564ab0 145 Parent::_events.push(event);
co838_app56 0:2ac59c564ab0 146 }
co838_app56 0:2ac59c564ab0 147 }
co838_app56 0:2ac59c564ab0 148 }
co838_app56 0:2ac59c564ab0 149 };