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:56:50 2016 +0000
Revision:
2:c871dc21467b
Parent:
1:7be9a82f3ab8
Child:
3:1ab88130bb9d
All sensors can now be disable

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