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 17:25:07 2016 +0000
Revision:
0:2ac59c564ab0
Child:
1:7be9a82f3ab8
Managing k64f & app shield;

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