diff --git a/conf/monocoque.config b/conf/monocoque.config index 233a58b..23ee7f6 100644 --- a/conf/monocoque.config +++ b/conf/monocoque.config @@ -6,10 +6,16 @@ devices = ( { device = "USB"; granularity = 4; config = "/home/paul/.config/monocoque/revburner1.xml"; }, { device = "Sound"; - type = "Shaker" - devid = "98FD:83AC"; - config = "shaker1.config"; }, + type = "Engine" + devid = "98FD:83AC"; }, + { device = "Sound"; + type = "Gear" + devid = "98FD:83AC"; }, { device = "Serial"; type = "ShiftLights"; config = "None"; - devpath = "/dev/ttyACM0"; } ); + devpath = "/dev/ttyACM0"; } ), + { device = "Serial"; + type = "SimWind"; + config = "None"; + devpath = "/dev/ttyACM1"; } ); diff --git a/src/arduino/shift_lights/shift_lights.ino.orig b/src/arduino/shift_lights/shift_lights.ino.orig deleted file mode 100644 index 4bd7cd6..0000000 --- a/src/arduino/shift_lights/shift_lights.ino.orig +++ /dev/null @@ -1,93 +0,0 @@ -#include -#include "../../monocoque/simulatorapi/simdata.h" - -#define BYTE_SIZE sizeof(SimData) - -#define LED_PIN 7 -#define NUM_LEDS 6 - -CRGB leds[NUM_LEDS]; -SimData sd; -int maxrpm = 0; -int rpm = 0; -int numlights = NUM_LEDS; -int pin = LED_PIN; -int lights[6]; - - -void setup() { - - Serial.begin(9600); - FastLED.addLeds(leds, NUM_LEDS); - FastLED.setMaxPowerInVoltsAndMilliamps(5, 500); - FastLED.setBrightness(40); - for (int i = 0; i < numlights; i++) - { - leds[i] = CRGB ( 0, 0, 0); - lights[i] = 0; - } - FastLED.clear(); - - sd.rpms = 0; - sd.maxrpm = 6500; - sd.altitude = 10; - sd.pulses = 40000; - sd.velocity = 10; -} - -void loop() { - - int l = 0; - char buff[BYTE_SIZE]; - - if (Serial.available() >= BYTE_SIZE) { - Serial.readBytes(buff, BYTE_SIZE); - memcpy(&sd, &buff, BYTE_SIZE); - rpm = sd.rpms; - maxrpm = sd.maxrpm; - - } - - - while (l < numlights) - { - lights[l] = 0; - l++; - } - l = -1; - int rpmlights = 0; - while (rpm > rpmlights) - { - if (l>=0) - { - lights[l] = 1; - } - l++; - rpmlights = rpmlights + (((maxrpm-250)/numlights)); - } - - l = 0; - FastLED.clear(); - while (l < numlights) - { - - if (l >= numlights / 2) - { - leds[l] = CRGB ( 0, 0, 255); - } - if (l < numlights / 2) - { - leds[l] = CRGB ( 0, 255, 0); - } - if (l == numlights - 1) - { - leds[l] = CRGB ( 255, 0, 0); - } - if (lights[l] <= 0) - { - leds[l] = CRGB ( 0, 0, 0); - } - FastLED.show(); - l++; - } -} diff --git a/src/arduino/shift_lights/shift_lights.ino b/src/arduino/shiftlights/shift_lights.ino similarity index 100% rename from src/arduino/shift_lights/shift_lights.ino rename to src/arduino/shiftlights/shift_lights.ino diff --git a/src/arduino/simwind/simwind.ino b/src/arduino/simwind/simwind.ino new file mode 100644 index 0000000..64d424f --- /dev/null +++ b/src/arduino/simwind/simwind.ino @@ -0,0 +1,51 @@ +#include +#include "../../monocoque/simulatorapi/simdata.h" + +#define BYTE_SIZE sizeof(SimData) +#define KPHTOMPH .621317 + + +Adafruit_MotorShield AFMS = Adafruit_MotorShield(); + +Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1); +Adafruit_DCMotor *myMotor2 = AFMS.getMotor(3); + +SimData sd; +int velocity = 0; + +void setup() { + Serial.begin(9600); + if (!AFMS.begin()) { + Serial.println("Could not find Motor Shield. Check wiring."); + while (1); + } + sd.rpms = 0; + sd.maxrpm = 6500; + sd.altitude = 10; + sd.pulses = 40000; + sd.velocity = 10; + + myMotor1->setSpeed(0); + myMotor1->run(FORWARD); + + myMotor2->setSpeed(0); + myMotor2->run(FORWARD); +} + +void loop() { + char buff[BYTE_SIZE]; + + if (Serial.available() >= BYTE_SIZE) + { + Serial.readBytes(buff, BYTE_SIZE); + memcpy(&sd, &buff, BYTE_SIZE); + velocity = sd.velocity; + } + int v = ceil(velocity * KPHTOMPH); + if (v >= 255) + { + v = 255; + } + myMotor1->setSpeed(v); + myMotor2->setSpeed(v); +} diff --git a/src/monocoque/devices/serial/arduino.c b/src/monocoque/devices/serial/arduino.c index 1f27c36..10c7800 100644 --- a/src/monocoque/devices/serial/arduino.c +++ b/src/monocoque/devices/serial/arduino.c @@ -1,9 +1,10 @@ #include #include #include +#include #include "arduino.h" -#include "../slog/slog.h" +#include "../../slog/slog.h" #define arduino_timeout 2000 @@ -18,11 +19,12 @@ int arduino_update(SerialDevice* serialdevice, SimData* simdata) return result; } -int arduino_init(SerialDevice* serialdevice) +int arduino_init(SerialDevice* serialdevice, const char* portdev) { slogi("initializing arduino serial device..."); int error = 0; - char* port_name = "/dev/ttyACM0"; + char* port_name = strdup(portdev); + slogd("Looking for port %s.\n", port_name); error = check(sp_get_port_by_name(port_name, &serialdevice->port)); if (error != 0) @@ -40,6 +42,7 @@ int arduino_init(SerialDevice* serialdevice) check(sp_set_stopbits(serialdevice->port, 1)); check(sp_set_flowcontrol(serialdevice->port, SP_FLOWCONTROL_NONE)); + free(port_name); slogd("Successfully setup arduino serial device..."); return 0; } diff --git a/src/monocoque/devices/serial/arduino.h b/src/monocoque/devices/serial/arduino.h index 061ac49..49ab499 100644 --- a/src/monocoque/devices/serial/arduino.h +++ b/src/monocoque/devices/serial/arduino.h @@ -1,10 +1,11 @@ #ifndef _ARDUINO_H #define _ARDUINO_H +#include "../simdevice.h" #include "../serialdevice.h" int arduino_update(SerialDevice* serialdevice, SimData* simdata); -int arduino_init(SerialDevice* serialdevice); +int arduino_init(SerialDevice* serialdevice, const char* portdev); int arduino_free(SerialDevice* serialdevice); int check(enum sp_return result); diff --git a/src/monocoque/devices/serialdevice.c b/src/monocoque/devices/serialdevice.c index 5c1b078..ffcf146 100644 --- a/src/monocoque/devices/serialdevice.c +++ b/src/monocoque/devices/serialdevice.c @@ -2,26 +2,34 @@ #include #include #include +#include +#include "simdevice.h" #include "serialdevice.h" #include "serial/arduino.h" #include "../helper/parameters.h" #include "../simulatorapi/simdata.h" #include "../slog/slog.h" -int serialdev_update(SerialDevice* serialdevice, SimData* simdata) +int serialdev_update(SimDevice* this, SimData* simdata) { + SerialDevice* serialdevice = (void *) this->derived; + arduino_update(serialdevice, simdata); return 0; } -int serialdev_free(SerialDevice* serialdevice) +int serialdev_free(SimDevice* this) { + SerialDevice* serialdevice = (void *) this->derived; + arduino_free(serialdevice); + + free(serialdevice); return 0; } -int serialdev_init(SerialDevice* serialdevice) +int serialdev_init(SerialDevice* serialdevice, const char* portdev) { slogi("initializing serial device..."); int error = 0; @@ -29,7 +37,29 @@ int serialdev_init(SerialDevice* serialdevice) serialdevice->type = SERIALDEV_UNKNOWN; serialdevice->type = SERIALDEV_ARDUINO; - error = arduino_init(serialdevice); + error = arduino_init(serialdevice, portdev); return error; } + +static const vtable serial_simdevice_vtable = { &serialdev_update, &serialdev_free }; + +SerialDevice* new_serial_device(DeviceSettings* ds) { + + SerialDevice* this = (SerialDevice*) malloc(sizeof(SerialDevice)); + + this->m.update = &update; + this->m.free = &simdevfree; + this->m.derived = this; + this->m.vtable = &serial_simdevice_vtable; + + int error = serialdev_init(this, ds->serialdevsettings.portdev); + + if (error != 0) + { + free(this); + return NULL; + } + + return this; +} diff --git a/src/monocoque/devices/serialdevice.h b/src/monocoque/devices/serialdevice.h index 6b34d1e..3ec39a5 100644 --- a/src/monocoque/devices/serialdevice.h +++ b/src/monocoque/devices/serialdevice.h @@ -2,26 +2,6 @@ #define _SERIALDEVICE_H #include -#include "../helper/parameters.h" -#include "../simulatorapi/simdata.h" -typedef enum -{ - SERIALDEV_UNKNOWN = 0, - SERIALDEV_ARDUINO = 1 -} -SerialType; - -typedef struct -{ - int id; - SerialType type; - struct sp_port* port; -} -SerialDevice; - -int serialdev_update(SerialDevice* serialdevice, SimData* simdata); -int serialdev_init(SerialDevice* serialdevice); -int serialdev_free(SerialDevice* serialdevice); #endif diff --git a/src/monocoque/devices/simdevice.c b/src/monocoque/devices/simdevice.c index c57bf0c..f737991 100644 --- a/src/monocoque/devices/simdevice.c +++ b/src/monocoque/devices/simdevice.c @@ -1,4 +1,5 @@ #include +#include #include "simdevice.h" #include "../helper/parameters.h" @@ -6,80 +7,101 @@ #include "../simulatorapi/simdata.h" #include "../slog/slog.h" -int devupdate(SimDevice* simdevice, SimData* simdata) + +int update(SimDevice* this, SimData* simdata) { - if (simdevice->initialized==false) - { - return 0; - } - switch ( simdevice->type ) - { - case SIMDEV_USB : - usbdev_update(&simdevice->d.usbdevice, simdata); - break; - case SIMDEV_SOUND : - sounddev_update(&simdevice->d.sounddevice, simdata); - break; - case SIMDEV_SERIAL : - serialdev_update(&simdevice->d.serialdevice, simdata); - break; - } + ((vtable*)this->vtable)->update(this, simdata); +} + +int simdevfree(SimDevice* this) +{ + ((vtable*)this->vtable)->free(this); +} + +int devupdate(SimDevice* this, SimData* simdata) +{ + return 0; } -int devfree(SimDevice* simdevice) +int devfree(SimDevice* simdevices, int numdevices) { - if (simdevice->initialized==false) + slogi("freeing %i simdevices...", numdevices); + int devices = 0; + + for (int j = 0; j < numdevices; j++) { - slogw("Attempt to free an uninitialized device"); - return MONOCOQUE_ERROR_INVALID_DEV; - } - switch ( simdevice->type ) - { - case SIMDEV_USB : - usbdev_free(&simdevice->d.usbdevice); - break; - case SIMDEV_SOUND : - sounddev_free(&simdevice->d.sounddevice); - break; - case SIMDEV_SERIAL : - serialdev_free(&simdevice->d.serialdevice); - break; + SimDevice simdev = simdevices[j]; + if (simdev.initialized == true) + { + simdev.free(&simdev); + } } + + free(simdevices); + return 0; } -int devinit(SimDevice* simdevice, DeviceSettings* ds) +int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds) { - slogi("initializing simdevice..."); - simdevice->initialized = false; - int err = 0; + slogi("initializing simdevices..."); + int devices = 0; - switch ( ds->dev_type ) + for (int j = 0; j < numdevices; j++) { - case SIMDEV_USB : - simdevice->type = SIMDEV_USB; - simdevice->d.usbdevice.type = USBDEV_UNKNOWN; - err = usbdev_init(&simdevice->d.usbdevice, ds); - break; - case SIMDEV_SOUND : - simdevice->type = SIMDEV_SOUND; - err = sounddev_init(&simdevice->d.sounddevice); - break; - case SIMDEV_SERIAL : - simdevice->type = SIMDEV_SERIAL; - err = serialdev_init(&simdevice->d.serialdevice); - break; - default : - sloge("Unknown device type"); - err = MONOCOQUE_ERROR_UNKNOWN_DEV; - break; + simdevices[j].initialized = false; + + if (ds[j].dev_type == SIMDEV_USB) { + USBDevice* sim = new_usb_device(&ds[j]); + if (sim != NULL) + { + simdevices[j] = sim->m; + simdevices[j].initialized = true; + simdevices[j].type = SIMDEV_USB; + devices++; + } + else + { + slogw("Could not initialize USB Device"); + } + } + + if (ds[j].dev_type == SIMDEV_SOUND) { + + SoundDevice* sim = new_sound_device(&ds[j]); + if (sim != NULL) + { + + simdevices[j] = sim->m; + simdevices[j].initialized = true; + simdevices[j].type = SIMDEV_SOUND; + devices++; + } + else + { + slogw("Could not initialize Sound Device"); + } + } + + if (ds[j].dev_type == SIMDEV_SERIAL) { + + SerialDevice* sim = new_serial_device(&ds[j]); + if (sim != NULL) + { + simdevices[j] = sim->m; + simdevices[j].initialized = true; + simdevices[j].type = SIMDEV_SERIAL; + devices++; + + } + else + { + slogw("Could not initialize Serial Device"); + } + } } - if (err==0) - { - simdevice->initialized = true; - } - return err; + return devices; } diff --git a/src/monocoque/devices/simdevice.h b/src/monocoque/devices/simdevice.h index 06ac09f..eb93632 100644 --- a/src/monocoque/devices/simdevice.h +++ b/src/monocoque/devices/simdevice.h @@ -9,26 +9,102 @@ #include "../helper/confighelper.h" #include "../simulatorapi/simdata.h" +typedef struct SimDevice SimDevice; -typedef struct +struct SimDevice { + const void* vtable; + int (*update)(SimDevice*, SimData*); + int (*free)(SimDevice*); + void* derived; int id; bool initialized; DeviceType type; +}; + +typedef struct { + int (*update)(SimDevice*, SimData*); + int (*free)(SimDevice*); +} vtable; + +/********* Serial Devices *****/ +typedef enum +{ + SERIALDEV_UNKNOWN = 0, + SERIALDEV_ARDUINO = 1 +} +SerialType; + +typedef struct +{ + SimDevice m; + int id; + SerialType type; + struct sp_port* port; +} +SerialDevice; + +int serialdev_update(SimDevice* this, SimData* simdata); +int serialdev_free(SimDevice* this); + +SerialDevice* new_serial_device(DeviceSettings* ds); + +/********* USB HID Devices *****/ +typedef enum +{ + USBDEV_UNKNOWN = 0, + USBDEV_TACHOMETER = 1 +} +USBType; + +typedef struct +{ + SimDevice m; + int id; + USBType type; union { - USBDevice usbdevice; - SoundDevice sounddevice; - SerialDevice serialdevice; - } d; + TachDevice tachdevice; + } u; } -SimDevice; +USBDevice; + +int usbdev_update(SimDevice* this, SimData* simdata); +int usbdev_free(SimDevice* this); + +USBDevice* new_usb_device(DeviceSettings* ds); + + +/********* Sound Devices *****/ +typedef struct +{ + SimDevice m; + int id; + SoundType type; + VibrationEffectType effecttype; + PATestData sounddata; + PaStreamParameters outputParameters; + PaStream* stream; +} +SoundDevice; + +int sounddev_engine_update(SimDevice* this, SimData* simdata); +int sounddev_gearshift_update(SimDevice* this, SimData* simdata); +int sounddev_free(SimDevice* this); + +SoundDevice* new_sound_device(DeviceSettings* ds); + +/***** Generic Methods *********/ + +int update(SimDevice* simdevice, SimData* simdata); int devupdate(SimDevice* simdevice, SimData* simdata); -int devinit(SimDevice* simdevice, DeviceSettings* ds); +int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds); -int devfree(SimDevice* simdevice); +int devfree(SimDevice* simdevices, int numdevices); + +int simdevfree(SimDevice* this); #endif diff --git a/src/monocoque/devices/sound/usb_generic_shaker.c b/src/monocoque/devices/sound/usb_generic_shaker.c index 5d660d9..a140f12 100644 --- a/src/monocoque/devices/sound/usb_generic_shaker.c +++ b/src/monocoque/devices/sound/usb_generic_shaker.c @@ -15,8 +15,7 @@ #endif - -int patestCallback(const void* inputBuffer, +int patestCallbackEngineRPM(const void* inputBuffer, void* outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, @@ -36,27 +35,16 @@ int patestCallback(const void* inputBuffer, float v = 0; v = data->amp * sin (2 * M_PI * ((float) n) / (float) SAMPLE_RATE); - if ( data->gear_sound_data > 0 ) + if (n>=data->table_size) { - if (n>=1764) - { - n=0; - } - } - else - { - if (n>=data->table_size) - { - n=0; - } + n=0; } - if ( data->gear_sound_data > 0 ) + if (data->gear_sound_data > 0) { - // right channel only? - // i have my butt hooked up to right channel... make this configurable? - *out++ = v; + *out++ = 0; + *out++ = 0; } else { @@ -65,6 +53,50 @@ int patestCallback(const void* inputBuffer, } } + data->gear_sound_data = 0; + data->n=n; + return 0; +} + + +int patestCallbackGearShift(const void* inputBuffer, + void* outputBuffer, + unsigned long framesPerBuffer, + const PaStreamCallbackTimeInfo* timeInfo, + PaStreamCallbackFlags statusFlags, + void* userData) +{ + PATestData* data = (PATestData*)userData; + float* out = (float*)outputBuffer; + memset(out, 0, framesPerBuffer * 2 * sizeof(float)); + unsigned int i; + unsigned int n; + n = data->n; + (void) inputBuffer; /* Prevent unused argument warning. */ + + for( i=0; iamp * sin (2 * M_PI * ((float) n) / (float) SAMPLE_RATE); + + if (n>=data->table_size) + { + n=0; + } + + if(data->gear_sound_data > 0) + { + *out++ = v; + *out++ = v; + } + else + { + *out++ = 0; + *out++ = 0; + } + } + data->gear_sound_data = 0; + data->n=n; return 0; } @@ -97,14 +129,28 @@ int usb_generic_shaker_init(SoundDevice* sounddevice) sounddevice->outputParameters.suggestedLatency = Pa_GetDeviceInfo( sounddevice->outputParameters.device )->defaultLowOutputLatency; sounddevice->outputParameters.hostApiSpecificStreamInfo = NULL; - err = Pa_OpenStream( &sounddevice->stream, - NULL, /* No input. */ - &sounddevice->outputParameters, /* As above. */ - SAMPLE_RATE, - 440, /* Frames per buffer. */ - paClipOff, /* No out of range samples expected. */ - patestCallback, - &sounddevice->sounddata ); + if (sounddevice->effecttype == SOUNDEFFECT_GEARSHIFT) + { + err = Pa_OpenStream( &sounddevice->stream, + NULL, /* No input. */ + &sounddevice->outputParameters, /* As above. */ + SAMPLE_RATE, + 440, /* Frames per buffer. */ + paClipOff, /* No out of range samples expected. */ + patestCallbackGearShift, + &sounddevice->sounddata ); + } + else + { + err = Pa_OpenStream( &sounddevice->stream, + NULL, /* No input. */ + &sounddevice->outputParameters, /* As above. */ + SAMPLE_RATE, + 440, /* Frames per buffer. */ + paClipOff, /* No out of range samples expected. */ + patestCallbackEngineRPM, + &sounddevice->sounddata ); + } if( err != paNoError ) { goto error; @@ -121,8 +167,5 @@ int usb_generic_shaker_init(SoundDevice* sounddevice) error: Pa_Terminate(); - //fprintf( stderr, "An error occured while using the portaudio stream\n" ); - //fprintf( stderr, "Error number: %d\n", err ); - //fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; } diff --git a/src/monocoque/devices/sound/usb_generic_shaker.h b/src/monocoque/devices/sound/usb_generic_shaker.h index 25b9045..d8c3f01 100644 --- a/src/monocoque/devices/sound/usb_generic_shaker.h +++ b/src/monocoque/devices/sound/usb_generic_shaker.h @@ -1,7 +1,8 @@ #ifndef _USB_GENERIC_SHAKER_H #define _USB_GENERIC_SHAKER_H -#include "../sounddevice.h" +//#include "../sounddevice.h" +#include "../simdevice.h" int usb_generic_shaker_init(SoundDevice* sounddevice); int usb_generic_shaker_free(SoundDevice* sounddevice); diff --git a/src/monocoque/devices/sounddevice.c b/src/monocoque/devices/sounddevice.c index 14a6f4d..54b2668 100644 --- a/src/monocoque/devices/sounddevice.c +++ b/src/monocoque/devices/sounddevice.c @@ -1,26 +1,52 @@ #include +#include +#include +#include +#include + +#include "simdevice.h" #include "sounddevice.h" #include "sound/usb_generic_shaker.h" #include "../simulatorapi/simdata.h" #include "../helper/parameters.h" #include "../slog/slog.h" -int sounddev_update(SoundDevice* sounddevice, SimData* simdata) +int gear_sound_set(SoundDevice* sounddevice, SimData* simdata) { - sounddevice->sounddata.table_size = 44100/(simdata->rpms/60); - - sounddevice->sounddata.gear_sound_data = 0; - if (sounddevice->sounddata.last_gear != simdata->gear) + if (sounddevice->sounddata.last_gear != simdata->gear && simdata->gear != 0) { - sounddevice->sounddata.gear_sound_data = sounddevice->sounddata.amp; + sounddevice->sounddata.gear_sound_data = 3.14; } sounddevice->sounddata.last_gear = simdata->gear; } -int sounddev_free(SoundDevice* sounddevice) +// we could make a vtable for these different effects too +int sounddev_engine_update(SimDevice* this, SimData* simdata) { - return usb_generic_shaker_free(sounddevice); + SoundDevice* sounddevice = (void *) this->derived; + + gear_sound_set(sounddevice, simdata); + + sounddevice->sounddata.table_size = 44100/(simdata->rpms/60); +} + +int sounddev_gearshift_update(SimDevice* this, SimData* simdata) +{ + SoundDevice* sounddevice = (void *) this->derived; + + gear_sound_set(sounddevice, simdata); +} + +int sounddev_free(SimDevice* this) +{ + SoundDevice* sounddevice = (void *) this->derived; + + usb_generic_shaker_free(sounddevice); + + free(sounddevice); + + return 0; } int sounddev_init(SoundDevice* sounddevice) @@ -34,5 +60,51 @@ int sounddev_init(SoundDevice* sounddevice) sounddevice->sounddata.table_size = 44100/(100/60); sounddevice->sounddata.last_gear = 0; + switch (sounddevice->effecttype) { + case (SOUNDEFFECT_GEARSHIFT): + + sounddevice->sounddata.pitch = 500; + sounddevice->sounddata.amp = 128; + sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0; + sounddevice->sounddata.table_size = 44100/(1); + break; + } + usb_generic_shaker_init(sounddevice); } + +static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free }; +static const vtable gear_sound_simdevice_vtable = { &sounddev_gearshift_update, &sounddev_free }; + +SoundDevice* new_sound_device(DeviceSettings* ds) { + + SoundDevice* this = (SoundDevice*) malloc(sizeof(SoundDevice)); + + this->m.update = &update; + this->m.free = &simdevfree; + this->m.derived = this; + + slogt("Attempting to configure sound device with subtype: %i", ds->dev_subtype); + switch (ds->dev_subtype) { + case (SIMDEVTYPE_ENGINESOUND): + this->effecttype = SOUNDEFFECT_ENGINERPM; + this->m.vtable = &engine_sound_simdevice_vtable; + slogi("Initializing sound device for engine vibrations."); + break; + case (SIMDEVTYPE_GEARSOUND): + this->effecttype = SOUNDEFFECT_GEARSHIFT; + this->m.vtable = &gear_sound_simdevice_vtable; + slogi("Initializing sound device for gear shift vibrations."); + break; + } + + int error = sounddev_init(this); + if (error != 0) + { + free(this); + return NULL; + } + + return this; +} + diff --git a/src/monocoque/devices/sounddevice.h b/src/monocoque/devices/sounddevice.h index ef6453b..8d1e36b 100644 --- a/src/monocoque/devices/sounddevice.h +++ b/src/monocoque/devices/sounddevice.h @@ -3,9 +3,6 @@ #include "portaudio.h" -#include "../simulatorapi/simdata.h" -#include "../helper/parameters.h" - typedef enum { SOUNDDEV_UNKNOWN = 0, @@ -13,6 +10,13 @@ typedef enum } SoundType; +typedef enum +{ + SOUNDEFFECT_ENGINERPM = 0, + SOUNDEFFECT_GEARSHIFT = 1 +} +VibrationEffectType; + #define MAX_TABLE_SIZE (6000) typedef struct { @@ -28,18 +32,4 @@ typedef struct } PATestData; -typedef struct -{ - int id; - SoundType type; - PATestData sounddata; - PaStreamParameters outputParameters; - PaStream* stream; -} -SoundDevice; - -int sounddev_update(SoundDevice* sounddevice, SimData* simdata); -int sounddev_init(SoundDevice* sounddevice); -int sounddev_free(SoundDevice* sounddevice); - #endif diff --git a/src/monocoque/devices/usbdevice.c b/src/monocoque/devices/usbdevice.c index 2f3ad24..b5ec0fc 100644 --- a/src/monocoque/devices/usbdevice.c +++ b/src/monocoque/devices/usbdevice.c @@ -1,14 +1,18 @@ #include #include #include +#include #include "usbdevice.h" +#include "simdevice.h" #include "../helper/parameters.h" #include "../simulatorapi/simdata.h" #include "../slog/slog.h" -int usbdev_update(USBDevice* usbdevice, SimData* simdata) +int usbdev_update(SimDevice* this, SimData* simdata) { + USBDevice* usbdevice = (void *) this->derived; + switch ( usbdevice->type ) { case USBDEV_UNKNOWN : @@ -20,8 +24,10 @@ int usbdev_update(USBDevice* usbdevice, SimData* simdata) return 0; } -int usbdev_free(USBDevice* usbdevice) +int usbdev_free(SimDevice* this) { + USBDevice* usbdevice = (void *) this->derived; + switch ( usbdevice->type ) { case USBDEV_UNKNOWN : @@ -30,6 +36,8 @@ int usbdev_free(USBDevice* usbdevice) break; } + free(usbdevice); + return 0; } @@ -37,6 +45,8 @@ int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds) { slogi("initializing usb device..."); int error = 0; + + usbdevice->type = USBDEV_TACHOMETER; switch ( usbdevice->type ) { case USBDEV_UNKNOWN : @@ -47,3 +57,24 @@ int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds) return error; } + +static const vtable usb_simdevice_vtable = { &usbdev_update, &usbdev_free }; + +USBDevice* new_usb_device(DeviceSettings* ds) { + + USBDevice* this = (USBDevice*) malloc(sizeof(USBDevice)); + + this->m.update = &update; + this->m.free = &simdevfree; + this->m.derived = this; + this->m.vtable = &usb_simdevice_vtable; + + int error = usbdev_init(this, ds); + + if (error != 0) + { + free(this); + return NULL; + } + return this; +} diff --git a/src/monocoque/devices/usbdevice.h b/src/monocoque/devices/usbdevice.h index 4355042..b35dc0a 100644 --- a/src/monocoque/devices/usbdevice.h +++ b/src/monocoque/devices/usbdevice.h @@ -2,29 +2,5 @@ #define _USBDEVICE_H #include "tachdevice.h" -#include "../helper/confighelper.h" -#include "../simulatorapi/simdata.h" - -typedef enum -{ - USBDEV_UNKNOWN = 0, - USBDEV_TACHOMETER = 1 -} -USBType; - -typedef struct -{ - int id; - USBType type; - union - { - TachDevice tachdevice; - } u; -} -USBDevice; - -int usbdev_update(USBDevice* usbdevice, SimData* simdata); -int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds); -int usbdev_free(USBDevice* usbdevice); #endif diff --git a/src/monocoque/gameloop/gameloop.c b/src/monocoque/gameloop/gameloop.c index 8073537..56257ed 100644 --- a/src/monocoque/gameloop/gameloop.c +++ b/src/monocoque/gameloop/gameloop.c @@ -14,7 +14,7 @@ #include "../simulatorapi/simmapper.h" #include "../slog/slog.h" -#define DEFAULT_UPDATE_RATE 120.0 +#define DEFAULT_UPDATE_RATE 240.0 int showstats(SimData* simdata) { @@ -165,7 +165,7 @@ int showstats(SimData* simdata) fflush(stdout); } -int looper(SimDevice* devices[], int numdevices, Simulator simulator) +int looper(SimDevice* devices, int numdevices, Simulator simulator) { slogi("preparing game loop with %i devices...", numdevices); @@ -179,6 +179,15 @@ int looper(SimDevice* devices[], int numdevices, Simulator simulator) return error; } + slogi("sending initial data to devices"); + simdata->velocity = 16; + simdata->rpms = 100; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + struct termios newsettings, canonicalmode; tcgetattr(0, &canonicalmode); newsettings = canonicalmode; @@ -191,35 +200,23 @@ int looper(SimDevice* devices[], int numdevices, Simulator simulator) double update_rate = DEFAULT_UPDATE_RATE; int t=0; + int s=0; int go = true; while (go == true) { simdatamap(simdata, simmap, simulator); showstats(simdata); t++; - if(simdata->rpms<250) + s++; + if(simdata->rpms<100) { - simdata->rpms=250; + simdata->rpms=100; } for (int x = 0; x < numdevices; x++) { - if (devices[x]->type == SIMDEV_SERIAL) - { - if(t>=update_rate) - { - devupdate(devices[x], simdata); - } - } - else - { - devupdate(devices[x], simdata); - } + devices[x].update(&devices[x], simdata); } - if(t>=update_rate) - { - t=0; - } if( poll(&mypoll, 1, 1000.0/update_rate) ) { scanf("%c", &ch); @@ -233,8 +230,156 @@ int looper(SimDevice* devices[], int numdevices, Simulator simulator) fflush(stdout); tcsetattr(0, TCSANOW, &canonicalmode); + simdata->velocity = 0; + simdata->rpms = 100; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + free(simdata); free(simmap); return 0; } + +int tester(SimDevice* devices, int numdevices) +{ + + slogi("preparing test with %i devices...", numdevices); + SimData* simdata = malloc(sizeof(SimData)); + + struct termios newsettings, canonicalmode; + tcgetattr(0, &canonicalmode); + newsettings = canonicalmode; + newsettings.c_lflag &= (~ICANON & ~ECHO); + newsettings.c_cc[VMIN] = 1; + newsettings.c_cc[VTIME] = 0; + tcsetattr(0, TCSANOW, &newsettings); + + fprintf(stdout, "\n"); + simdata->gear = 0; + simdata->velocity = 16; + simdata->rpms = 100; + simdata->maxrpm = 8000; + sleep(3); + + fprintf(stdout, "Setting rpms to 1000\n"); + simdata->rpms = 1000; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + fprintf(stdout, "Shifting into first gear\n"); + simdata->gear = 1; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + fprintf(stdout, "Setting speed to 100\n"); + simdata->velocity = 100; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + fprintf(stdout, "Shifting into second gear\n"); + simdata->gear = 2; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + fprintf(stdout, "Setting speed to 200\n"); + simdata->velocity = 200; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + fprintf(stdout, "Shifting into third gear\n"); + simdata->gear = 3; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + fprintf(stdout, "Setting rpms to 2000\n"); + simdata->rpms = 2000; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + fprintf(stdout, "Setting rpms to 4000\n"); + simdata->rpms = 4000; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + fprintf(stdout, "Shifting into fourth gear\n"); + simdata->gear = 4; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + fprintf(stdout, "Setting speed to 300\n"); + simdata->velocity = 300; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + fprintf(stdout, "Setting rpms to 8000\n"); + simdata->rpms = 8000; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + + simdata->velocity = 0; + simdata->rpms = 100; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(1); + + fflush(stdout); + tcsetattr(0, TCSANOW, &canonicalmode); + + free(simdata); + + return 0; +} diff --git a/src/monocoque/gameloop/gameloop.h b/src/monocoque/gameloop/gameloop.h index a37822c..07496ca 100644 --- a/src/monocoque/gameloop/gameloop.h +++ b/src/monocoque/gameloop/gameloop.h @@ -1,4 +1,5 @@ #include "../devices/simdevice.h" #include "../helper/parameters.h" -int looper (SimDevice* devices[], int numdevices, Simulator simulator); +int tester(SimDevice* devices, int numdevices); +int looper (SimDevice* devices, int numdevices, Simulator simulator); diff --git a/src/monocoque/gameloop/tachconfig.c b/src/monocoque/gameloop/tachconfig.c index df1bb03..f490e3b 100644 --- a/src/monocoque/gameloop/tachconfig.c +++ b/src/monocoque/gameloop/tachconfig.c @@ -183,7 +183,7 @@ int config_tachometer(int max_revs, int granularity, const char* save_file, SimD WriteXmlFromArrays(nodes, rpm_array, values_array, max_revs, save_file); sleep(2); simdata->pulses = 0; - devupdate(simdevice, simdata); + simdevice->update(simdevice, simdata); fflush(stdout); return 0; diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index 82353dc..a039907 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -11,21 +12,32 @@ #include "../slog/slog.h" + +int strcicmp(char const *a, char const *b) +{ + for (;; a++, b++) { + int d = tolower((unsigned char)*a) - tolower((unsigned char)*b); + if (d != 0 || !*a) + return d; + } +} + + int strtogame(const char* game, MonocoqueSettings* ms) { slogd("Checking for %s in list of supported simulators.", game); - if (strcmp(game, "ac") == 0) + if (strcicmp(game, "ac") == 0) { slogd("Setting simulator to Assetto Corsa"); ms->sim_name = SIMULATOR_ASSETTO_CORSA; } - else if (strcmp(game, "rf2") == 0) + else if (strcicmp(game, "rf2") == 0) { slogd("Setting simulator to RFactor 2"); ms->sim_name = SIMULATOR_RFACTOR2; } else - if (strcmp(game, "test") == 0) + if (strcicmp(game, "test") == 0) { slogd("Setting simulator to Test Data"); ms->sim_name = SIMULATOR_MONOCOQUE_TEST; @@ -38,22 +50,68 @@ int strtogame(const char* game, MonocoqueSettings* ms) return MONOCOQUE_ERROR_NONE; } -int strtodev(const char* device_type, DeviceSettings* ds) +int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev) { ds->is_valid = false; - if (strcmp(device_type, "USB") == 0) + ds->dev_subtype = SIMDEVTYPE_UNKNOWN; + + switch (simdev) { + case SIMDEV_USB: + if (strcicmp(device_subtype, "Tachometer") == 0) + { + ds->dev_subtype = SIMDEVTYPE_TACHOMETER; + break; + } + case SIMDEV_SERIAL: + if (strcicmp(device_subtype, "ShiftLights") == 0) + { + ds->dev_subtype = SIMDEVTYPE_SHIFTLIGHTS; + break; + } + if (strcicmp(device_subtype, "SimWind") == 0) + { + ds->dev_subtype = SIMDEVTYPE_SIMWIND; + break; + } + case SIMDEV_SOUND: + if (strcicmp(device_subtype, "Engine") == 0) + { + ds->dev_subtype = SIMDEVTYPE_ENGINESOUND; + break; + } + if (strcicmp(device_subtype, "Gear") == 0) + { + ds->dev_subtype = SIMDEVTYPE_GEARSOUND; + break; + } + default: + ds->is_valid = false; + slogw("%s does not appear to be a valid device sub type, but attempting to continue with other devices", device_subtype); + return MONOCOQUE_ERROR_INVALID_DEV; + } + ds->is_valid = true; + return MONOCOQUE_ERROR_NONE; +} + +int strtodev(const char* device_type, const char* device_subtype, DeviceSettings* ds) +{ + ds->is_valid = false; + if (strcicmp(device_type, "USB") == 0) { ds->dev_type = SIMDEV_USB; + strtodevsubtype(device_subtype, ds, SIMDEV_USB); } else - if (strcmp(device_type, "Sound") == 0) + if (strcicmp(device_type, "Sound") == 0) { ds->dev_type = SIMDEV_SOUND; + strtodevsubtype(device_subtype, ds, SIMDEV_SOUND); } else - if (strcmp(device_type, "Serial") == 0) + if (strcicmp(device_type, "Serial") == 0) { ds->dev_type = SIMDEV_SERIAL; + strtodevsubtype(device_subtype, ds, SIMDEV_SERIAL); } else { @@ -65,33 +123,6 @@ int strtodev(const char* device_type, DeviceSettings* ds) return MONOCOQUE_ERROR_NONE; } -int strtodevtype(const char* device_subtype, DeviceSettings* ds) -{ - ds->is_valid = false; - if (strcmp(device_subtype, "Tachometer") == 0) - { - ds->dev_subtype = SIMDEVTYPE_TACHOMETER; - } - else - if (strcmp(device_subtype, "ShiftLights") == 0) - { - ds->dev_subtype = SIMDEVTYPE_SHIFTLIGHTS; - } - else - if (strcmp(device_subtype, "Shaker") == 0) - { - ds->dev_subtype = SIMDEVTYPE_SHAKER; - } - else - { - ds->is_valid = false; - slogi("%s does not appear to be a valid device sub type, but attempting to continue with other devices", device_subtype); - return MONOCOQUE_ERROR_INVALID_DEV; - } - ds->is_valid = true; - return MONOCOQUE_ERROR_NONE; -} - int loadtachconfig(const char* config_file, DeviceSettings* ds) { @@ -131,7 +162,7 @@ int loadtachconfig(const char* config_file, DeviceSettings* ds) { slogt("Xml Element name %s", cursubsubnode->name); } - if (strcmp(cursubsubnode->name, "SettingsItem") == 0) + if (strcicmp(cursubsubnode->name, "SettingsItem") == 0) { arraysize++; } @@ -153,13 +184,13 @@ int loadtachconfig(const char* config_file, DeviceSettings* ds) { for (cursubsubsubnode = cursubsubnode->children; cursubsubsubnode; cursubsubsubnode = cursubsubsubnode->next) { - if (strcmp(cursubsubsubnode->name, "Value") == 0) + if (strcicmp(cursubsubsubnode->name, "Value") == 0) { xmlChar* a = xmlNodeGetContent(cursubsubsubnode); rpms_array[i] = strtol((char*) a, &buf, 10); xmlFree(a); } - if (strcmp(cursubsubsubnode->name, "TimeValue") == 0) + if (strcicmp(cursubsubsubnode->name, "TimeValue") == 0) { xmlChar* a = xmlNodeGetContent(cursubsubsubnode); pulses_array[i] = strtol((char*) a, &buf, 10); @@ -199,18 +230,14 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co int error = MONOCOQUE_ERROR_NONE; slogi("Called device setup with %s %s %s", device_type, device_subtype, config_file); ds->dev_type = SIMDEV_UNKNOWN; - ds->dev_subtype = SIMDEVTYPE_UNKNOWN; - error = strtodev(device_type, ds); + + error = strtodev(device_type, device_subtype, ds); if (error != MONOCOQUE_ERROR_NONE) { return error; } - error = strtodevtype(device_subtype, ds); - if (error != MONOCOQUE_ERROR_NONE) - { - return error; - } - if (ms->program_action == A_PLAY) + + if (ms->program_action == A_PLAY || ms->program_action == A_TEST) { error = loadconfig(config_file, ds); } @@ -232,11 +259,34 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co slogi("Tachometer granularity set to %i", ds->tachsettings.granularity); } ds->tachsettings.use_pulses = true; - if (ms->program_action == A_PLAY) + if (ms->program_action == A_PLAY || ms->program_action == A_TEST) { ds->tachsettings.use_pulses = false; } } + if (ds->dev_subtype == SIMDEVTYPE_SIMWIND || ds->dev_subtype == SIMDEVTYPE_SHIFTLIGHTS) + { + if (device_settings != NULL) + { + const char* temp; + config_setting_lookup_string(device_settings, "devpath", &temp); + ds->serialdevsettings.portdev = strdup(temp); + } + } return error; } + +int settingsfree(DeviceSettings ds) +{ + + if (ds.dev_subtype == SIMDEVTYPE_SIMWIND || ds.dev_subtype == SIMDEVTYPE_SHIFTLIGHTS) + { + if (ds.serialdevsettings.portdev != NULL) + { + free(ds.serialdevsettings.portdev); + } + } + + return 0; +} diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index ee1177d..69952d5 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -21,8 +21,10 @@ typedef enum { SIMDEVTYPE_UNKNOWN = 0, SIMDEVTYPE_TACHOMETER = 1, - SIMDEVTYPE_SHAKER = 2, - SIMDEVTYPE_SHIFTLIGHTS = 3 + SIMDEVTYPE_SHIFTLIGHTS = 2, + SIMDEVTYPE_SIMWIND = 3, + SIMDEVTYPE_ENGINESOUND = 4, + SIMDEVTYPE_GEARSOUND = 5 } DeviceSubType; @@ -74,12 +76,19 @@ typedef struct } TachometerSettings; +typedef struct +{ + char* portdev; +} +SerialDeviceSettings; + typedef struct { bool is_valid; DeviceType dev_type; DeviceSubType dev_subtype; TachometerSettings tachsettings; + SerialDeviceSettings serialdevsettings; } DeviceSettings; @@ -87,4 +96,6 @@ int strtogame(const char* game, MonocoqueSettings* ms); int devsetup(const char* device_type, const char* device_subtype, const char* config_files, MonocoqueSettings* ms, DeviceSettings* ds, config_setting_t* device_settings); +int settingsfree(DeviceSettings ds); + #endif diff --git a/src/monocoque/helper/parameters.c b/src/monocoque/helper/parameters.c index 9e35939..923448d 100644 --- a/src/monocoque/helper/parameters.c +++ b/src/monocoque/helper/parameters.c @@ -23,6 +23,7 @@ ConfigError getParameters(int argc, char** argv, Parameters* p) struct arg_lit* arg_verbosity1 = arg_litn("v","verbose", 0, 2, "increase logging verbosity"); struct arg_lit* arg_verbosity2 = arg_litn("v","verbose", 0, 2, "increase logging verbosity"); + struct arg_lit* arg_verbosity3 = arg_litn("v","verbose", 0, 2, "increase logging verbosity"); struct arg_rex* cmd1 = arg_rex1(NULL, NULL, "play", NULL, REG_ICASE, NULL); struct arg_str* arg_sim = arg_strn("s", "sim", "", 0, 1, NULL); @@ -43,6 +44,13 @@ ConfigError getParameters(int argc, char** argv, Parameters* p) void* argtable2[] = {cmd2a,cmd2b,arg_max_revs,arg_granularity,arg_save,arg_verbosity2,help2,vers2,end2}; int nerrors2; + struct arg_rex* cmd3 = arg_rex1(NULL, NULL, "test", NULL, REG_ICASE, NULL); + struct arg_lit* help3 = arg_litn(NULL,"help", 0, 1, "print this help and exit"); + struct arg_lit* vers3 = arg_litn(NULL,"version", 0, 1, "print version information and exit"); + struct arg_end* end3 = arg_end(20); + void* argtable3[] = {cmd3,arg_verbosity3,help3,vers3,end3}; + int nerrors3; + struct arg_lit* help0 = arg_lit0(NULL,"help", "print this help and exit"); struct arg_lit* version0 = arg_lit0(NULL,"version", "print version information and exit"); struct arg_end* end0 = arg_end(20); @@ -70,6 +78,7 @@ ConfigError getParameters(int argc, char** argv, Parameters* p) nerrors0 = arg_parse(argc,argv,argtable0); nerrors1 = arg_parse(argc,argv,argtable1); nerrors2 = arg_parse(argc,argv,argtable2); + nerrors3 = arg_parse(argc,argv,argtable3); if (nerrors1==0) { @@ -92,6 +101,11 @@ ConfigError getParameters(int argc, char** argv, Parameters* p) p->verbosity_count = arg_verbosity2->count; exitcode = E_SUCCESS_AND_DO; } + else if (nerrors3==0) + { + p->program_action = A_TEST; + exitcode = E_SUCCESS_AND_DO; + } else { if (cmd1->count > 0) @@ -111,12 +125,13 @@ ConfigError getParameters(int argc, char** argv, Parameters* p) { if (help->count==0 && vers->count==0) { - printf("%s: missing command.\n",progname); + printf("%s: missing command.\n",progname); printf("Usage 1: %s ", progname); arg_print_syntax(stdout,argtable1,"\n"); printf("Usage 2: %s ", progname); arg_print_syntax(stdout,argtable2,"\n"); - + printf("Usage 3: %s ", progname); + arg_print_syntax(stdout,argtable3,"\n"); } } exitcode = E_SUCCESS_AND_EXIT; @@ -131,6 +146,8 @@ ConfigError getParameters(int argc, char** argv, Parameters* p) arg_print_syntax(stdout,argtable1,"\n"); printf("Usage 2: %s ", progname); arg_print_syntax(stdout,argtable2,"\n"); + printf("Usage 3: %s ", progname); + arg_print_syntax(stdout,argtable3,"\n"); printf("\nReport bugs on the github github.com/spacefreak18/monocoque.\n"); exitcode = E_SUCCESS_AND_EXIT; goto cleanup; @@ -148,6 +165,7 @@ cleanup: arg_freetable(argtable0,sizeof(argtable0)/sizeof(argtable0[0])); arg_freetable(argtable1,sizeof(argtable1)/sizeof(argtable1[0])); arg_freetable(argtable2,sizeof(argtable2)/sizeof(argtable2[0])); + arg_freetable(argtable3,sizeof(argtable3)/sizeof(argtable3[0])); return exitcode; } diff --git a/src/monocoque/helper/parameters.h b/src/monocoque/helper/parameters.h index b3f3776..f50dcb3 100644 --- a/src/monocoque/helper/parameters.h +++ b/src/monocoque/helper/parameters.h @@ -15,8 +15,9 @@ Parameters; typedef enum { A_PLAY = 0, - A_CONFIG_TACH = 1, - A_CONFIG_SHAKER = 2 + A_TEST = 1, + A_CONFIG_TACH = 2, + A_CONFIG_SHAKER = 3 } ProgramAction; diff --git a/src/monocoque/monocoque.c b/src/monocoque/monocoque.c index f90df68..79d7cdb 100644 --- a/src/monocoque/monocoque.c +++ b/src/monocoque/monocoque.c @@ -113,9 +113,8 @@ int main(int argc, char** argv) SimDevice* tachdev = malloc(sizeof(SimDevice)); SimData* sdata = malloc(sizeof(SimData)); DeviceSettings* ds = malloc(sizeof(DeviceSettings)); + error = devsetup("USB", "Tachometer", "None", ms, ds, NULL); - error = devinit(tachdev, ds); - slogi("configuring tachometer with max revs: %i, granularity: %i, saving to %s", p->max_revs, p->granularity, p->save_file); if (error != MONOCOQUE_ERROR_NONE) { @@ -123,33 +122,37 @@ int main(int argc, char** argv) } else { + error = devinit(tachdev, 1, ds); + } + + if (error != MONOCOQUE_ERROR_NONE) + { + sloge("Could not proceed with tachometer configuration due to error: %i", error); + } + else + { + slogi("configuring tachometer with max revs: %i, granularity: %i, saving to %s", p->max_revs, p->granularity, p->save_file); config_tachometer(p->max_revs, p->granularity, p->save_file, tachdev, sdata); } - devfree(tachdev); - free(tachdev); + devfree(tachdev, 1); + //free(tachdev); free(sdata); free(ds); } else { - slogi("running monocoque in gameloop mode.."); - int error = 0; - error = strtogame(p->sim_string, ms); - if (error != MONOCOQUE_ERROR_NONE) - { - goto cleanup_final; - } + int error = 0; int configureddevices = config_setting_length(config_devices); int numdevices = 0; - DeviceSettings* ds[configureddevices]; + DeviceSettings ds[configureddevices]; slogi("found %i devices in configuration", configureddevices); int i = 0; while (iis_valid == true) - { - SimDevice* device = malloc(sizeof(SimDevice)); - devinit(device, ds[i]); - devices[j] = device; - j++; - } - i++; - } + SimDevice* devices = malloc(numdevices * sizeof(SimDevice)); + int initdevices = devinit(devices, configureddevices, ds); - error = looper(devices, numdevices, ms->sim_name); - if (error == MONOCOQUE_ERROR_NONE) + if (p->program_action == A_PLAY) { - slogi("Game loop exited succesfully with error code: %i", error); + slogi("running monocoque in gameloop mode.."); + error = strtogame(p->sim_string, ms); + if (error != MONOCOQUE_ERROR_NONE) + { + goto cleanup_final; + } + + + error = looper(devices, initdevices, ms->sim_name); + if (error == MONOCOQUE_ERROR_NONE) + { + slogi("Game loop exited succesfully with error code: %i", error); + } + else + { + sloge("Game loop exited with error code: %i", error); + } } else { - sloge("Game loop exited with error code: %i", error); - } - - i = 0; - while (idev_subtype == SIMDEV_USB) + slogi("running monocoque in test mode..."); + error = tester(devices, initdevices); + if (error == MONOCOQUE_ERROR_NONE) { - free(ds[i]->tachsettings.pulses_array); - free(ds[i]->tachsettings.rpms_array); + slogi("Test exited succesfully with error code: %i", error); + } + else + { + sloge("Test exited with error code: %i", error); } - free(ds[i]); - i++; } + + devfree(devices, initdevices); + i = 0; - while (i