00001 #include "Controller.h" 00002 00003 Controller::Controller() 00004 { 00005 00006 oldJSValues.resize(8); 00007 oldButtonValues.resize(20); 00008 newJSValues.resize(8); 00009 newButtonValues.resize(20); 00010 00011 BUTTON_A = 0; 00012 BUTTON_B = 1; 00013 BUTTON_X = 2; 00014 BUTTON_Y = 3; 00015 RIGHT_TRIGGER = 4; 00016 RIGHT_BUMPER = 5; 00017 LEFT_TRIGGER = 6; 00018 LEFT_BUMPER = 7; 00019 RIGHT_STICK = 8; 00020 LEFT_STICK = 9; 00021 JS_X_AXIS = 10; 00022 JS_Y_AXIS = 11; 00023 00024 inputType = 0; 00025 shouldCalibrate = false; 00026 } 00027 00028 void Controller::update(bool isButton, int index, int value) 00029 { 00030 controllerLock.lock(); 00031 if (isButton) 00032 { 00033 oldButtonValues[index] = newButtonValues[index]; 00034 newButtonValues[index] = value; 00035 } 00036 else 00037 { 00038 oldJSValues[index] = newJSValues[index]; 00039 newJSValues[index] = value; 00040 } 00041 controllerLock.unlock(); 00042 } 00043 00044 void Controller::calibrate() 00045 { 00046 calibrationMonitor.lock(); 00047 cout << "Press button A: "; 00048 calibrationMonitor.wait(); 00049 BUTTON_A = inputType; 00050 00051 cout << "Press button B: "; 00052 calibrationMonitor.wait(); 00053 BUTTON_A = inputType; 00054 00055 cout << "Press button X: "; 00056 calibrationMonitor.wait(); 00057 00058 cout << "Press button Y: "; 00059 calibrationMonitor.wait(); 00060 00061 cout << "Press button A: "; 00062 calibrationMonitor.wait(); 00063 00064 cout << "Press button A: "; 00065 calibrationMonitor.wait(); 00066 00067 cout << "Press button A: "; 00068 calibrationMonitor.wait(); 00069 00070 cout << "Press button A: "; 00071 00072 shouldCalibrate = false; 00073 calibrationMonitor.unlock(); 00074 } 00075 00076 bool Controller::buttonPressed(int button) 00077 { 00078 bool pressed = false; 00079 controllerLock.lock(); 00080 if (oldButtonValues[button] == 1 && newButtonValues[button] == 0) 00081 pressed = true; 00082 else 00083 pressed = false; 00084 controllerLock.unlock(); 00085 return pressed; 00086 } 00087 00088 bool Controller::buttonHeld(int button) 00089 { 00090 bool held = false; 00091 controllerLock.lock(); 00092 if (oldButtonValues[button] == 1 && newButtonValues[button] == 1) 00093 held = true; 00094 else 00095 held = false; 00096 controllerLock.unlock(); 00097 return held; 00098 } 00099 00100 int Controller::getPreviousJSValue(int axis) 00101 { 00102 int value = 0; 00103 controllerLock.lock(); 00104 value = oldJSValues[axis]; 00105 controllerLock.unlock(); 00106 return value; 00107 } 00108 00109 int Controller::getCurrentJSValue(int axis) 00110 { 00111 int value = 0; 00112 controllerLock.lock(); 00113 value = newJSValues[axis]; 00114 controllerLock.unlock(); 00115 return value; 00116 } 00117 00118 bool Controller::needCalibration() 00119 { 00120 bool needed = false; 00121 calibrationMonitor.lock(); 00122 needed = shouldCalibrate; 00123 calibrationMonitor.unlock(); 00124 return needed; 00125 } 00126 00127 void Controller::assignInput(int index) 00128 { 00129 calibrationMonitor.lock(); 00130 inputType = index; 00131 calibrationMonitor.notify(); 00132 calibrationMonitor.unlock(); 00133 } 00134 00135 void Controller::setCalibration(bool needed) 00136 { 00137 calibrationMonitor.lock(); 00138 shouldCalibrate = needed; 00139 calibrationMonitor.unlock(); 00140 }