Commit c52ba0d5 authored by Pila's avatar Pila

Can now abort on SIGINT signal

parent f7aeb399
Pipeline #1161170 skipped
......@@ -17,40 +17,69 @@
using namespace std;
libusb_device_handle* SteamController_OpenAndClaim(int *interface_num){
struct ParamsStruct{
const char* midiSong;
unsigned int intervalUSec;
int libusbDebugLevel;
bool repeatSong;
};
struct SteamControllerInfos{
libusb_device_handle* dev_handle;
int interfaceNum;
};
SteamControllerInfos steamController1;
bool SteamController_OpenAndClaim(SteamControllerInfos* controller){
if(!controller)
return false;
libusb_device_handle* dev_handle;
//Open Steam Controller device
if((dev_handle = libusb_open_device_with_vid_pid(NULL, 0x28DE, 0x1102)) != NULL){ // Wired Steam Controller
cout<<"Found wired Steam Controller"<<endl;
(*interface_num) = 2;
controller->dev_handle = dev_handle;
controller->interfaceNum = 2;
}
else if((dev_handle = libusb_open_device_with_vid_pid(NULL, 0x28DE, 0x1142)) != NULL){ // Steam Controller dongle
cout<<"Found Steam Dongle, will attempt to use the first Steam Controller"<<endl;
(*interface_num) = 1;
controller->dev_handle = dev_handle;
controller->interfaceNum = 1;
}
else{
cout<<"No device found"<<endl;
std::cin.ignore();
return NULL;
return false;
}
//On Linux, automatically detach and reattach kernel module
libusb_set_auto_detach_kernel_driver(dev_handle,1);
libusb_set_auto_detach_kernel_driver(controller->dev_handle,1);
//Claim the USB interface controlling the haptic actuators
int r = libusb_claim_interface(dev_handle,(*interface_num));
int r = libusb_claim_interface(controller->dev_handle,controller->interfaceNum);
if(r < 0) {
cout<<"Interface claim Error "<<r<<endl;
std::cin.ignore();
libusb_close(dev_handle);
return NULL;
libusb_close(controller->dev_handle);
return false;
}
return dev_handle;
return true;
}
void SteamController_Close(SteamControllerInfos* controller){
int r = libusb_release_interface(controller->dev_handle,controller->interfaceNum);
if(r < 0) {
cout<<"Interface release Error "<<r<<endl;
std::cin.ignore();
return;
}
libusb_close(controller->dev_handle);
}
int SteamController_PlayNote(libusb_device_handle *dev_handle, int haptic, unsigned int note,double duration = DURATION_MAX){
int SteamController_PlayNote(SteamControllerInfos* controller, int haptic, unsigned int note,double duration = DURATION_MAX){
double midiFrequency[128] = {8.1758, 8.66196, 9.17702, 9.72272, 10.3009, 10.9134, 11.5623, 12.2499, 12.9783, 13.75, 14.5676, 15.4339, 16.3516, 17.3239, 18.354, 19.4454, 20.6017, 21.8268, 23.1247, 24.4997, 25.9565, 27.5, 29.1352, 30.8677, 32.7032, 34.6478, 36.7081, 38.8909, 41.2034, 43.6535, 46.2493, 48.9994, 51.9131, 55, 58.2705, 61.7354, 65.4064, 69.2957, 73.4162, 77.7817, 82.4069, 87.3071, 92.4986, 97.9989, 103.826, 110, 116.541, 123.471, 130.813, 138.591, 146.832, 155.563, 164.814, 174.614, 184.997, 195.998, 207.652, 220, 233.082, 246.942, 261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440, 466.164, 493.883, 523.251, 554.365, 587.33, 622.254, 659.255, 698.456, 739.989, 783.991, 830.609, 880, 932.328, 987.767, 1046.5, 1108.73, 1174.66, 1244.51, 1318.51, 1396.91, 1479.98, 1567.98, 1661.22, 1760, 1864.66, 1975.53, 2093, 2217.46, 2349.32, 2489.02, 2637.02, 2793.83, 2959.96, 3135.96, 3322.44, 3520, 3729.31, 3951.07, 4186.01, 4434.92, 4698.64, 4978.03, 5274.04, 5587.65, 5919.91, 6271.93, 6644.88, 7040, 7458.62, 7902.13, 8372.02, 8869.84, 9397.27, 9956.06, 10548.1, 11175.3, 11839.8, 12543.9};
unsigned char dataBlob[64] = {0x8f,
......@@ -90,7 +119,7 @@ int SteamController_PlayNote(libusb_device_handle *dev_handle, int haptic, unsig
dataBlob[8] = repeatCount / 0xff;
int r;
r = libusb_control_transfer(dev_handle,0x21,9,0x0300,2,dataBlob,64,1000);
r = libusb_control_transfer(controller->dev_handle,0x21,9,0x0300,2,dataBlob,64,1000);
if(r < 0) {
cout<<"Command Error "<<r<< endl;
return 1;
......@@ -144,15 +173,15 @@ void displayCurrentNote(int channel, unsigned int note){
cout.flush();
}
void playSong(libusb_device_handle *steamcontroller_handle, const char* songfile, unsigned int sleepIntervalUsec){
void playSong(SteamControllerInfos* controller,const ParamsStruct params){
MidiFile_t midifile;
//Open Midi File
midifile = MidiFile_load(songfile);
midifile = MidiFile_load(params.midiSong);
if(midifile == NULL){
cout << "Unable to open song file " << songfile << endl;
cout << "Unable to open song file " << params.midiSong << endl;
return;
}
......@@ -163,7 +192,7 @@ void playSong(libusb_device_handle *steamcontroller_handle, const char* songfile
}
//Waiting for user to press enter
cout << "Starting playback of " <<songfile << endl;
cout << "Starting playback of " <<params.midiSong << endl;
sleep(1);
//Get current time point, will be used to know elapsed time
......@@ -172,7 +201,7 @@ void playSong(libusb_device_handle *steamcontroller_handle, const char* songfile
//Iterate through events
MidiFileEvent_t currentEvent = MidiFile_getFirstEvent(midifile);
while(currentEvent != NULL){
usleep(sleepIntervalUsec);
usleep(params.intervalUSec);
//We now need to play all events with tick < currentTime
long currentTick = MidiFile_getTickFromTime(midifile,timeElapsedSince(tOrigin));
......@@ -213,7 +242,7 @@ void playSong(libusb_device_handle *steamcontroller_handle, const char* songfile
eventNote = MidiFileNoteStartEvent_getNote(selectedEvent);
}
SteamController_PlayNote(steamcontroller_handle,currentChannel,eventNote);
SteamController_PlayNote(controller,currentChannel,eventNote);
displayCurrentNote(currentChannel, eventNote);
}
......@@ -225,13 +254,6 @@ void playSong(libusb_device_handle *steamcontroller_handle, const char* songfile
struct ParamsStruct{
const char* midiSong;
unsigned int intervalUSec;
int libusbDebugLevel;
bool repeatSong;
};
bool parseArguments(int argc, char** argv, ParamsStruct* params){
int c;
......@@ -267,13 +289,21 @@ bool parseArguments(int argc, char** argv, ParamsStruct* params){
}
}
int main(int argc, char** argv)
{
libusb_device_handle *dev_handle; //a device handle
int interface_num = 0;
int r; //for return values
void abortPlaying(int){
for(int i = 0 ; i < CHANNEL_COUNT ; i++){
SteamController_PlayNote(&steamController1,i,NOTE_STOP);
}
SteamController_Close(&steamController1);
cout << endl<< "Aborted " << endl;
exit(1);
}
int main(int argc, char** argv)
{
cout <<"Steam Controller Singer by Pila"<<endl;
ParamsStruct params;
......@@ -291,7 +321,7 @@ int main(int argc, char** argv)
//Initializing LIBUSB
r = libusb_init(NULL);
int r = libusb_init(NULL);
if(r < 0) {
cout<<"LIBUSB Init Error "<<r<<endl;
std::cin.ignore();
......@@ -301,25 +331,21 @@ int main(int argc, char** argv)
libusb_set_debug(NULL, params.libusbDebugLevel);
//Gaining access to Steam Controller
dev_handle = SteamController_OpenAndClaim(&interface_num);
if(dev_handle == NULL)
if(!SteamController_OpenAndClaim(&steamController1)){
return 1;
}
//Set mecanism to stop playing when closing process
signal(SIGINT, abortPlaying);
//Playing song
do{
playSong(dev_handle,params.midiSong,params.intervalUSec);
playSong(&steamController1,params);
}while(params.repeatSong);
//Releasing access to Steam Controller
r = libusb_release_interface(dev_handle,interface_num);
if(r < 0) {
cout<<"Interface release Error "<<r<<endl;
std::cin.ignore();
return 1;
}
libusb_close(dev_handle);
SteamController_Close(&steamController1);
libusb_exit(NULL);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment