00001 // PTF - Psychological Test Framework 00002 // http://ptf.sourceforge.net 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Library General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 #include <ptf/input/inputdevice.hpp> 00019 #include <ptf/core/reactionrecord.hpp> 00020 #include <ptf/core/ptfcore.hpp> 00021 #include <string> 00022 00023 // bring things into scope 00024 using std::string; 00025 00029 InputDevice::InputDevice() { 00030 active = false; 00031 } 00032 00036 InputDevice::~InputDevice() { 00037 // nothing to do here 00038 } 00039 00048 void InputDevice::run() { 00049 // time 00050 TimeInterval* time = new TimeInterval(); 00051 time->measureA(); 00052 // poll loop 00053 while (active) { 00054 // poll input device 00055 bool eventsWaiting = poll(); 00056 // handle input if necessary 00057 if (eventsWaiting) { 00058 // measure time - the event must have occured in the 00059 // now completed time interval 00060 time->measureB(); 00061 // handle events 00062 vector<bool>* states = new vector<bool>; 00063 bool reactionsOccured = handle(*states); 00064 // look if reaction records have to be created 00065 if (reactionsOccured) { 00066 // create reaction record 00067 ReactionRecord* rec = new ReactionRecord(time, states); 00068 // add reaction record 00069 PtfCore::getSingleton().getReactionRecordsMutex().enterMutex(); 00070 PtfCore::getSingleton().getReactionRecords().push_back(rec); 00071 PtfCore::getSingleton().getReactionRecordsMutex().leaveMutex(); 00072 // convert button states to string 00073 string buttons; 00074 for (int i = 0; i < (int)states->size(); i++) { 00075 if ((*states)[i]) buttons += '1'; 00076 else buttons += '0'; 00077 } 00078 // display info 00079 PtfCore::getSingleton().getEventLog() << "Event caught:\n" 00080 << "\tValid reaction at " << time->toString() << " with [" << buttons << "]\n"; 00081 // create new time interval 00082 time = new TimeInterval(); 00083 } 00084 else { 00085 PtfCore::getSingleton().getEventLog() << "Event caught:\n" 00086 << "\tInvalid reaction at " << time->toString() << "\n"; 00087 } 00088 } 00089 // update time interval 00090 time->measureA(); 00091 } 00092 } 00093 00098 bool InputDevice::poll() { 00099 return false; 00100 } 00101 00105 bool InputDevice::handle(vector<bool>& states) { 00106 return false; 00107 } 00108 00112 void InputDevice::startPolling() { 00113 active = true; 00114 start(); 00115 } 00116 00120 void InputDevice::stopPolling() { 00121 active = false; 00122 }