Compare commits

..

10 Commits

Author SHA1 Message Date
Paul Dino Jones 8065929618 Adding new items to configuration example 2023-05-17 15:54:21 +00:00
Paul Dino Jones 5211753a7e Add simwind arduino sketch 2023-05-15 14:30:23 +00:00
Paul Dino Jones d2ff88aabb More refactoring to improve serial devices 2023-05-15 14:23:33 +00:00
Paul Dino Jones 978c607d66 Improved gameloop for serial devices. More simwind hacks 2023-05-13 15:33:02 -04:00
Paul Dino Jones 0d4a1fae1c Use mph on arduino updates 2023-05-12 21:44:22 +00:00
Paul Dino Jones a50b68f3b6 Add udev rules for arduino devices 2023-05-09 23:20:02 -04:00
Paul Dino Jones d8df1890b3 Refactor into C classes. Add support for SimWind. 2023-05-09 14:59:41 -04:00
Paul Dino Jones af7077b07a Adding velocity to test function 2023-04-25 10:33:05 +00:00
Paul Dino Jones 61825e4ddf Refactoring gear change vibration effect 2023-04-08 08:52:06 +00:00
Paul Dino Jones 5eb136312b Refactoring to use function pointers for devices updates 2023-04-07 07:19:43 +00:00
43 changed files with 399 additions and 618 deletions

1
.gitmodules vendored
View File

@ -1,4 +1,3 @@
[submodule "src/monocoque/simulatorapi/simapi"] [submodule "src/monocoque/simulatorapi/simapi"]
path = src/monocoque/simulatorapi/simapi path = src/monocoque/simulatorapi/simapi
url = https://github.com/spacefreak18/simapi url = https://github.com/spacefreak18/simapi
# url = git@github.com:spacefreak18/simapi # ssh is better but Microsoft requires an account

View File

@ -33,24 +33,16 @@ FIND_LIBRARY(LIBUSB_LIBRARY NAMES usb-1.0
set(HIDAPI_WITH_LIBUSB TRUE) # surely will be used only on Linux set(HIDAPI_WITH_LIBUSB TRUE) # surely will be used only on Linux
set(BUILD_SHARED_LIBS TRUE) # HIDAPI as static library on all platforms set(BUILD_SHARED_LIBS TRUE) # HIDAPI as static library on all platforms
add_executable(monocoque src/monocoque/monocoque.c)
if(USE_PULSEAUDIO)
message("Using pulseaudio backend...")
add_compile_definitions(USE_PULSEAUDIO=true)
target_link_libraries(monocoque m ${LIBUSB_LIBRARY} hidapi-libusb pulse serialport xml2 argtable2 config gameloop helper devices slog simulatorapi)
else()
message("Using portaudio backend...")
target_link_libraries(monocoque m ${LIBUSB_LIBRARY} hidapi-libusb portaudio serialport xml2 argtable2 config gameloop helper devices slog simulatorapi)
endif()
target_include_directories(monocoque PUBLIC config ${LIBUSB_INCLUDE_DIR} ${LIBXML_INCLUDE_DIR})
add_subdirectory(src/monocoque/gameloop) add_subdirectory(src/monocoque/gameloop)
add_subdirectory(src/monocoque/simulatorapi) add_subdirectory(src/monocoque/simulatorapi)
add_subdirectory(src/monocoque/helper) add_subdirectory(src/monocoque/helper)
add_subdirectory(src/monocoque/devices) add_subdirectory(src/monocoque/devices)
add_subdirectory(src/monocoque/slog) add_subdirectory(src/monocoque/slog)
add_executable(monocoque src/monocoque/monocoque.c)
target_include_directories(monocoque PUBLIC config ${LIBUSB_INCLUDE_DIR} ${LIBXML_INCLUDE_DIR})
target_link_libraries(monocoque m ${LIBUSB_LIBRARY} hidapi-libusb portaudio serialport xml2 argtable2 config gameloop helper devices slog simulatorapi)
add_executable(listusb tests/testlibusb.c) add_executable(listusb tests/testlibusb.c)
target_include_directories(listusb PUBLIC ${LIBUSB_INCLUDE_DIR}) target_include_directories(listusb PUBLIC ${LIBUSB_INCLUDE_DIR})
target_link_libraries(listusb ${LIBUSB_LIBRARY} portaudio) target_link_libraries(listusb ${LIBUSB_LIBRARY} portaudio)

View File

@ -16,20 +16,18 @@ Cross Platform device manager for driving and flight simulators, for use with co
- Includes utility to configure revburner tachometer - Includes utility to configure revburner tachometer
- Can send data to any serial device. So far only tested with arduino. Includes sample arduino sketch for sim lights. - Can send data to any serial device. So far only tested with arduino. Includes sample arduino sketch for sim lights.
- The support for haptic bass shakers is limited and needs the most work. So far the engine rev is a simple sine wave, which I find convincing. The gear shift event works but not convincing enough for me. - The support for haptic bass shakers is limited and needs the most work. So far the engine rev is a simple sine wave, which I find convincing. The gear shift event works but not convincing enough for me.
- Choice of Portaudio or Pulseaudio backend.
## Dependencies ## Dependencies
- libserialport - arduino serial devices - libserialport - arduino serial devices
- hidapi - usb hid devices - hidapi - usb hid devices
- libusb - used by hidapi - libusb - used by hidapi
- portaudio - sound devices (haptic bass shakers) - portaudio - sound devices (haptic bass shakers)
- pulseaudio - sound devices (haptic bass shakers)
- libenet - UDP support (not yet implemented) - libenet - UDP support (not yet implemented)
- libxml2 - libxml2
- argtable2 - argtable2
- libconfig - libconfig
- [slog](https://github.com/kala13x/slog) (static) - [slog](https://github.com/kala13x/slog) (static)
- [wine-linux-shm-adapter](https://github.com/spacefreak18/simshmbridge) - for sims that need shared memory mapping like AC. - [wine-linux-shm-adapter](https://github.com/spacefreak18/wine-linux-shm-adapter)
- [simapi](https://github.com/spacefreak18/simapi) - [simapi](https://github.com/spacefreak18/simapi)
## Building ## Building
@ -38,23 +36,14 @@ This code depends on the shared memory data headers in the simapi [repo](https:/
git submodule sync --recursive git submodule sync --recursive
git submodule update --init --recursive git submodule update --init --recursive
``` ```
Then to compile simply: Then to compile simply:
``` ```
mkdir build; cd build mkdir build; cd build
cmake .. cmake ..
make make
``` ```
to use the pulseaudio backend use this cmake command
```
cmake -DUSE_PULSEAUDIO=YES ..
```
## Testing ## Testing
### Setting up Your Arduino Device
### Static Analysis ### Static Analysis
``` ```
mkdir build; cd build mkdir build; cd build
@ -71,6 +60,7 @@ cmake -DUSE_PULSEAUDIO=YES ..
## ToDo ## ToDo
- windows port - windows port
- more memory testing - more memory testing
- handling null mallocs
- move config code around - move config code around
- cleanup tests which are basically just copies of the example from their respective projects - cleanup tests which are basically just copies of the example from their respective projects
- much, much more - much, much more

View File

@ -6,18 +6,11 @@ devices = ( { device = "USB";
granularity = 4; granularity = 4;
config = "/home/paul/.config/monocoque/revburner1.xml"; }, config = "/home/paul/.config/monocoque/revburner1.xml"; },
{ device = "Sound"; { device = "Sound";
type = "Engine"; type = "Engine"
devid = "alsa_output.usb-Generic_USB2.0_Device_20170726905959-00.analog-stereo"; devid = "98FD:83AC"; },
pan = 1;
volume = 90;
frequency = 27; },
{ device = "Sound"; { device = "Sound";
type = "Gear" type = "Gear"
devid = "alsa_output.usb-Generic_USB2.0_Device_20170726905959-00.analog-stereo"; devid = "98FD:83AC"; },
pan = 1;
duration = .25;
volume = 100;
frequency = 27; },
{ device = "Serial"; { device = "Serial";
type = "ShiftLights"; type = "ShiftLights";
config = "None"; config = "None";

View File

@ -1,7 +1,7 @@
#include <FastLED.h> #include <FastLED.h>
#include "simdata.h" #include "../../monocoque/simulatorapi/simdata.h"
#define SIMDATA_SIZE sizeof(SimData) #define BYTE_SIZE sizeof(SimData)
#define LED_PIN 7 #define LED_PIN 7
#define NUM_LEDS 6 #define NUM_LEDS 6
@ -39,12 +39,12 @@ void setup()
void loop() void loop()
{ {
int l = 0; int l = 0;
char buff[SIMDATA_SIZE]; char buff[BYTE_SIZE];
if (Serial.available() >= SIMDATA_SIZE) if (Serial.available() >= BYTE_SIZE)
{ {
Serial.readBytes(buff, SIMDATA_SIZE); Serial.readBytes(buff, BYTE_SIZE);
memcpy(&sd, &buff, SIMDATA_SIZE); memcpy(&sd, &buff, BYTE_SIZE);
rpm = sd.rpms; rpm = sd.rpms;
maxrpm = sd.maxrpm; maxrpm = sd.maxrpm;

View File

@ -1 +0,0 @@
../../monocoque/simulatorapi/simapi/simapi/simdata.h

View File

@ -1 +0,0 @@
../../monocoque/simulatorapi/simapi/simapi/simdata.h

View File

@ -1,5 +1,5 @@
#include <Adafruit_MotorShield.h> #include <Adafruit_MotorShield.h>
#include "simdata.h" #include "../../monocoque/simulatorapi/simdata.h"
#define BYTE_SIZE sizeof(SimData) #define BYTE_SIZE sizeof(SimData)
#define KPHTOMPH .621317 #define KPHTOMPH .621317

View File

@ -9,13 +9,10 @@ set(devices_source_files
serialdevice.c serialdevice.c
tachdevice.h tachdevice.h
tachdevice.c tachdevice.c
sound.h
sound.c
usb/revburner.h usb/revburner.h
usb/revburner.c usb/revburner.c
sound/usb_generic_shaker.h sound/usb_generic_shaker.h
sound/usb_generic_shaker.c sound/usb_generic_shaker.c
sound/usb_generic_shaker_pulse.c
serial/arduino.h serial/arduino.h
serial/arduino.c serial/arduino.c
) )

View File

@ -8,7 +8,7 @@
#include "serialdevice.h" #include "serialdevice.h"
#include "serial/arduino.h" #include "serial/arduino.h"
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simdata.h"
#include "../slog/slog.h" #include "../slog/slog.h"
int serialdev_update(SimDevice* this, SimData* simdata) int serialdev_update(SimDevice* this, SimData* simdata)

View File

@ -4,7 +4,7 @@
#include "simdevice.h" #include "simdevice.h"
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simdata.h"
#include "../slog/slog.h" #include "../slog/slog.h"
@ -30,8 +30,7 @@ int devfree(SimDevice* simdevices, int numdevices)
slogi("freeing %i simdevices...", numdevices); slogi("freeing %i simdevices...", numdevices);
int devices = 0; int devices = 0;
int freed = 0; for (int j = 0; j < numdevices; j++)
for (int j = 0; j < 1; j++)
{ {
SimDevice simdev = simdevices[j]; SimDevice simdev = simdevices[j];
if (simdev.initialized == true) if (simdev.initialized == true)
@ -40,7 +39,7 @@ int devfree(SimDevice* simdevices, int numdevices)
} }
} }
//free(simdevices); free(simdevices);
return 0; return 0;
} }

View File

@ -7,8 +7,7 @@
#include "sounddevice.h" #include "sounddevice.h"
#include "serialdevice.h" #include "serialdevice.h"
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simdata.h"
typedef struct SimDevice SimDevice; typedef struct SimDevice SimDevice;
@ -84,13 +83,9 @@ typedef struct
int id; int id;
SoundType type; SoundType type;
VibrationEffectType effecttype; VibrationEffectType effecttype;
SoundData sounddata; PATestData sounddata;
#ifdef USE_PULSEAUDIO
pa_stream *stream;
#else
PaStreamParameters outputParameters; PaStreamParameters outputParameters;
PaStream* stream; PaStream* stream;
#endif
} }
SoundDevice; SoundDevice;

View File

@ -1,79 +0,0 @@
#include "sound.h"
#include "../slog/slog.h"
#ifdef USE_PULSEAUDIO
pa_threaded_mainloop* mainloop;
pa_context* context;
void context_state_cb(pa_context* context, void* mainloop) {
pa_threaded_mainloop_signal(mainloop, 0);
}
int setupsound()
{
pa_mainloop_api *mainloop_api;
slogi("connecting pulseaudio...");
// Get a mainloop and its context
mainloop = pa_threaded_mainloop_new();
assert(mainloop);
mainloop_api = pa_threaded_mainloop_get_api(mainloop);
context = pa_context_new(mainloop_api, "Monocoque");
assert(context);
pa_context_set_state_callback(context, &context_state_cb, mainloop);
// Lock the mainloop so that it does not run and crash before the context is ready
pa_threaded_mainloop_lock(mainloop);
// Start the mainloop
assert(pa_threaded_mainloop_start(mainloop) == 0);
assert(pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) == 0);
// Wait for the context to be ready
for(;;) {
pa_context_state_t context_state = pa_context_get_state(context);
assert(PA_CONTEXT_IS_GOOD(context_state));
if (context_state == PA_CONTEXT_READY) break;
pa_threaded_mainloop_wait(mainloop);
}
slogi("successfully connected pulseaudio...");
return 1;
}
int freesound()
{
if (context)
pa_context_unref(context);
if (mainloop) {
pa_signal_done();
pa_threaded_mainloop_free(mainloop);
}
}
#else
int setupsound()
{
slogi("connecting portaudio...");
}
int freesound()
{
}
#endif

View File

@ -1,13 +0,0 @@
#ifndef _SOUND_H
#define _SOUND_H
#include <pulse/pulseaudio.h>
extern pa_threaded_mainloop* mainloop;
extern pa_context* context;
int setupsound();
int freesound();
#endif

View File

@ -1,5 +1,3 @@
#ifndef USE_PULSEAUDIO
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
@ -12,9 +10,6 @@
#define SAMPLE_RATE (48000) #define SAMPLE_RATE (48000)
#define DURATION 4.0
#define AMPLITUDE .5
#ifndef M_PI #ifndef M_PI
#define M_PI (3.14159265) #define M_PI (3.14159265)
#endif #endif
@ -27,27 +22,39 @@ int patestCallbackEngineRPM(const void* inputBuffer,
PaStreamCallbackFlags statusFlags, PaStreamCallbackFlags statusFlags,
void* userData) void* userData)
{ {
SoundData* data = (SoundData*)userData; PATestData* data = (PATestData*)userData;
float* out = (float*)outputBuffer; float* out = (float*)outputBuffer;
memset(out, 0, framesPerBuffer * 2 * sizeof(float)); memset(out, 0, framesPerBuffer * 2 * sizeof(float));
unsigned int i; unsigned int i;
unsigned int n; unsigned int n;
//n = data->n; n = data->n;
(void) inputBuffer; /* Prevent unused argument warning. */ (void) inputBuffer; /* Prevent unused argument warning. */
for( i=0; i<framesPerBuffer; i++,n++ ) for( i=0; i<framesPerBuffer; i++,n++ )
{ {
static double t = 0.0; float v = 0;
double sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * t); v = data->amp * sin (2 * M_PI * ((float) n) / (float) SAMPLE_RATE);
t += 1.0 / SAMPLE_RATE; if (n>=data->table_size)
if (t >= DURATION) { {
t = 0.0; n=0;
}
*out++ = sample;
*out++ = sample;
} }
if (data->gear_sound_data > 0)
{
*out++ = 0;
*out++ = 0;
}
else
{
*out++ = v;
*out++ = v;
}
}
data->gear_sound_data = 0;
data->n=n;
return 0; return 0;
} }
@ -59,33 +66,39 @@ int patestCallbackGearShift(const void* inputBuffer,
PaStreamCallbackFlags statusFlags, PaStreamCallbackFlags statusFlags,
void* userData) void* userData)
{ {
SoundData* data = (SoundData*)userData; PATestData* data = (PATestData*)userData;
float* out = (float*)outputBuffer; float* out = (float*)outputBuffer;
memset(out, 0, framesPerBuffer * 2 * sizeof(float)); memset(out, 0, framesPerBuffer * 2 * sizeof(float));
unsigned int i; unsigned int i;
unsigned int n; unsigned int n;
//n = data->n; n = data->n;
(void) inputBuffer; /* Prevent unused argument warning. */ (void) inputBuffer; /* Prevent unused argument warning. */
for( i=0; i<framesPerBuffer; i++,n++ ) for( i=0; i<framesPerBuffer; i++,n++ )
{ {
static double t = 0.0; float v = 0;
double sample = 0; v = data->amp * sin (2 * M_PI * ((float) n) / (float) SAMPLE_RATE);
if (data->frequency>0)
if (n>=data->table_size)
{ {
sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * data->curr_duration); n=0;
} }
if(data->gear_sound_data > 0)
data->curr_duration += 1.0 / SAMPLE_RATE; {
if (data->curr_duration >= data->duration) { *out++ = v;
data->curr_duration = 0.0; *out++ = v;
data->curr_frequency = 0;
} }
else
*out++ = sample; {
*out++ = sample; *out++ = 0;
*out++ = 0;
} }
}
data->gear_sound_data = 0;
data->n=n;
return 0;
} }
int usb_generic_shaker_free(SoundDevice* sounddevice) int usb_generic_shaker_free(SoundDevice* sounddevice)
@ -156,5 +169,3 @@ error:
Pa_Terminate(); Pa_Terminate();
return err; return err;
} }
#endif

View File

@ -1,13 +1,10 @@
#ifndef _USB_GENERIC_SHAKER_H #ifndef _USB_GENERIC_SHAKER_H
#define _USB_GENERIC_SHAKER_H #define _USB_GENERIC_SHAKER_H
//#include "../sounddevice.h"
#include "../simdevice.h" #include "../simdevice.h"
#ifdef USE_PULSEAUDIO
int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop, pa_context* context, const char* devname, int volume, int pan, const char* streamname);
#else
int usb_generic_shaker_init(SoundDevice* sounddevice); int usb_generic_shaker_init(SoundDevice* sounddevice);
#endif
int usb_generic_shaker_free(SoundDevice* sounddevice); int usb_generic_shaker_free(SoundDevice* sounddevice);
#endif #endif

View File

@ -1,163 +0,0 @@
#ifdef USE_PULSEAUDIO
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "usb_generic_shaker.h"
#include "../sounddevice.h"
#define FORMAT PA_SAMPLE_S16LE
#define SAMPLE_RATE (48000)
#define AMPLITUDE .5
#define DURATION 4.0
#ifndef M_PI
#define M_PI (3.14159265)
#endif
void gear_sound_stream(pa_stream *s, size_t length, void *userdata) {
SoundData* data = (SoundData*)userdata;
size_t num_samples = length / sizeof(int16_t);
int16_t buffer[num_samples];
for (size_t i = 0; i < num_samples; i++) {
static double t = 0.0;
double sample = 0;
if (data->frequency>0)
{
sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * data->curr_duration);
}
buffer[i] = (int16_t)sample;
data->curr_duration += 1.0 / SAMPLE_RATE;
if (data->curr_duration >= data->duration) {
data->curr_duration = 0.0;
data->curr_frequency = 0;
}
}
pa_stream_write(s, buffer, length, NULL, 0LL, PA_SEEK_RELATIVE);
}
void engine_sound_stream(pa_stream *s, size_t length, void *userdata) {
SoundData* data = (SoundData*)userdata;
size_t num_samples = length / sizeof(int16_t);
int16_t buffer[num_samples];
for (size_t i = 0; i < num_samples; i++) {
static double t = 0.0;
double sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * t);
buffer[i] = (int16_t)sample;
t += 1.0 / SAMPLE_RATE;
if (t >= DURATION) {
t = 0.0;
}
}
pa_stream_write(s, buffer, length, NULL, 0LL, PA_SEEK_RELATIVE);
}
void stream_success_cb(pa_stream *stream, int success, void *userdata) {
return;
}
void stream_state_cb(pa_stream *s, void *mainloop) {
pa_threaded_mainloop_signal(mainloop, 0);
}
int usb_generic_shaker_free(SoundDevice* sounddevice)
{
int err = 0;
//err = Pa_CloseStream( sounddevice->stream );
//if( err != paNoError )
//{
// err = Pa_Terminate();
//}
if (sounddevice->stream)
{
pa_stream_unref(sounddevice->stream);
pa_xfree(sounddevice->stream);
}
return err;
}
int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop, pa_context* context, const char* devname, int volume, int pan, const char* streamname)
{
pa_stream *stream;
// Create a playback stream
pa_sample_spec sample_specifications;
sample_specifications.format = FORMAT;
sample_specifications.rate = SAMPLE_RATE;
sample_specifications.channels = 2;
pa_channel_map channel_map;
pa_channel_map_init_stereo(&channel_map);
pa_channel_map_parse(&channel_map, "front-left,front-right");
stream = pa_stream_new(context, streamname, &sample_specifications, &channel_map);
pa_stream_set_state_callback(stream, stream_state_cb, mainloop);
if (sounddevice->effecttype == SOUNDEFFECT_GEARSHIFT)
{
pa_stream_set_write_callback(stream, gear_sound_stream, &sounddevice->sounddata);
}
else
{
pa_stream_set_write_callback(stream, engine_sound_stream, &sounddevice->sounddata);
}
// recommended settings, i.e. server uses sensible values
pa_buffer_attr buffer_attr;
buffer_attr.maxlength = (uint32_t) 32767;
buffer_attr.tlength = (uint32_t) -1;
buffer_attr.prebuf = (uint32_t) -1;
buffer_attr.minreq = (uint32_t) -1;
pa_cvolume cv;
uint16_t pvolume = ceil(((double) volume/100.0d)*65535);
// Settings copied as per the chromium browser source
pa_stream_flags_t stream_flags;
stream_flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_NOT_MONOTONIC | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
// Connect stream to the default audio output sink
pa_cvolume_set(&cv, sample_specifications.channels, pvolume);
pa_cvolume_set_balance(&cv, &channel_map, pan);
assert(pa_stream_connect_playback(stream, devname, &buffer_attr, stream_flags, &cv, NULL) == 0);
// Wait for the stream to be ready
for(;;) {
pa_stream_state_t stream_state = pa_stream_get_state(stream);
assert(PA_STREAM_IS_GOOD(stream_state));
if (stream_state == PA_STREAM_READY) break;
pa_threaded_mainloop_wait(mainloop);
}
//pa_threaded_mainloop_unlock(mainloop);
// Uncork the stream so it will start playing
pa_stream_cork(stream, 0, stream_success_cb, mainloop);
sounddevice->stream = stream;
return 0;
}
#endif

View File

@ -5,26 +5,20 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "sound.h"
#include "simdevice.h" #include "simdevice.h"
#include "sounddevice.h" #include "sounddevice.h"
#include "sound/usb_generic_shaker.h" #include "sound/usb_generic_shaker.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simdata.h"
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../slog/slog.h" #include "../slog/slog.h"
int gear_sound_set(SoundDevice* sounddevice, SimData* simdata) int gear_sound_set(SoundDevice* sounddevice, SimData* simdata)
{ {
if (sounddevice->sounddata.last_gear != simdata->gear && simdata->gear > 1) if (sounddevice->sounddata.last_gear != simdata->gear && simdata->gear != 0)
{ {
//sounddevice->sounddata.gear_sound_data = 3.14; sounddevice->sounddata.gear_sound_data = 3.14;
sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency;
sounddevice->sounddata.curr_duration = 0;
} }
sounddevice->sounddata.last_gear = simdata->gear; sounddevice->sounddata.last_gear = simdata->gear;
slogt("set gear frequency to %i", sounddevice->sounddata.frequency);
} }
// we could make a vtable for these different effects too // we could make a vtable for these different effects too
@ -32,10 +26,9 @@ int sounddev_engine_update(SimDevice* this, SimData* simdata)
{ {
SoundDevice* sounddevice = (void *) this->derived; SoundDevice* sounddevice = (void *) this->derived;
sounddevice->sounddata.curr_frequency = simdata->rpms/60; gear_sound_set(sounddevice, simdata);
//sounddevice->sounddata.table_size = 48000/(sounddevice->sounddata.frequency);
slogt("set engine frequency to %i", sounddevice->sounddata.frequency); sounddevice->sounddata.table_size = 44100/(simdata->rpms/60);
} }
int sounddev_gearshift_update(SimDevice* this, SimData* simdata) int sounddev_gearshift_update(SimDevice* this, SimData* simdata)
@ -56,51 +49,28 @@ int sounddev_free(SimDevice* this)
return 0; return 0;
} }
int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int frequency, int pan, double duration) int sounddev_init(SoundDevice* sounddevice)
{ {
slogi("initializing standalone sound device..."); slogi("initializing standalone sound device...");
sounddevice->sounddata.pitch = 1;
sounddevice->sounddata.pitch = 261.626;
sounddevice->sounddata.amp = 32;
sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0;
sounddevice->sounddata.table_size = 44100/(100/60);
sounddevice->sounddata.last_gear = 0;
slogi("volume is: %i", volume);
slogi("frequency is: %i", frequency);
slogi("pan is: %i", pan);
slogi("duration is: %f", duration);
sounddevice->sounddata.frequency = frequency;
//sounddevice->sounddata.pitch = 1;
//sounddevice->sounddata.pitch = 261.626;
//sounddevice->sounddata.amp = 32;
//sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0;
//sounddevice->sounddata.table_size = 48000/(100/60);
sounddevice->sounddata.curr_frequency = 100/60;
const char* streamname= "Engine";
switch (sounddevice->effecttype) { switch (sounddevice->effecttype) {
case (SOUNDEFFECT_GEARSHIFT): case (SOUNDEFFECT_GEARSHIFT):
sounddevice->sounddata.last_gear = 0; sounddevice->sounddata.pitch = 500;
//sounddevice->sounddata.pitch = 500; sounddevice->sounddata.amp = 128;
//sounddevice->sounddata.amp = 128; sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0;
//sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0; sounddevice->sounddata.table_size = 44100/(1);
//sounddevice->sounddata.table_size = 48000/(1);
sounddevice->sounddata.duration = duration;
sounddevice->sounddata.curr_duration = duration;
streamname = "Gear";
break; break;
} }
#ifdef USE_PULSEAUDIO
//pa_threaded_mainloop* mainloop;
//pa_context* context;
usb_generic_shaker_init(sounddevice, mainloop, context, devname, volume, pan, streamname);
#else
usb_generic_shaker_init(sounddevice); usb_generic_shaker_init(sounddevice);
#endif
} }
static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free }; static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free };
@ -128,9 +98,7 @@ SoundDevice* new_sound_device(DeviceSettings* ds) {
break; break;
} }
slogt("Attempting to use device %s", ds->sounddevsettings.dev); int error = sounddev_init(this);
int error = sounddev_init(this, ds->sounddevsettings.dev, ds->sounddevsettings.volume, ds->sounddevsettings.frequency, ds->sounddevsettings.pan, ds->sounddevsettings.duration);
if (error != 0) if (error != 0)
{ {
free(this); free(this);

View File

@ -1,11 +1,7 @@
#ifndef _SOUNDDEVICE_H #ifndef _SOUNDDEVICE_H
#define _SOUNDDEVICE_H #define _SOUNDDEVICE_H
#ifdef USE_PULSEAUDIO
#include <pulse/pulseaudio.h>
#else
#include "portaudio.h" #include "portaudio.h"
#endif
typedef enum typedef enum
{ {
@ -24,13 +20,16 @@ VibrationEffectType;
#define MAX_TABLE_SIZE (6000) #define MAX_TABLE_SIZE (6000)
typedef struct typedef struct
{ {
float sine[MAX_TABLE_SIZE];
float pitch;
int last_gear; int last_gear;
int volume; int left_phase;
int frequency; int right_phase;
double duration; int n;
int curr_frequency; int table_size;
double curr_duration; int amp;
int gear_sound_data;
} }
SoundData; PATestData;
#endif #endif

View File

@ -5,7 +5,7 @@
#include "tachdevice.h" #include "tachdevice.h"
#include "revburner.h" #include "revburner.h"
#include "../../helper/confighelper.h" #include "../../helper/confighelper.h"
#include "../../simulatorapi/simapi/simapi/simdata.h" #include "../../simulatorapi/simdata.h"
#include "../../slog/slog.h" #include "../../slog/slog.h"
int tachdev_update(TachDevice* tachdevice, SimData* simdata) int tachdev_update(TachDevice* tachdevice, SimData* simdata)

View File

@ -3,7 +3,7 @@
#include <hidapi/hidapi.h> #include <hidapi/hidapi.h>
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simdata.h"
//typedef int (*tachdev_update)(int revs); //typedef int (*tachdev_update)(int revs);

View File

@ -6,7 +6,7 @@
#include "usbdevice.h" #include "usbdevice.h"
#include "simdevice.h" #include "simdevice.h"
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simdata.h"
#include "../slog/slog.h" #include "../slog/slog.h"
int usbdev_update(SimDevice* this, SimData* simdata) int usbdev_update(SimDevice* this, SimData* simdata)

View File

@ -1,4 +1,3 @@
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -11,12 +10,11 @@
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
#include "../devices/simdevice.h" #include "../devices/simdevice.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simdata.h"
#include "../simulatorapi/simapi/simapi/simmapper.h" #include "../simulatorapi/simmapper.h"
#include "../slog/slog.h" #include "../slog/slog.h"
#define DEFAULT_UPDATE_RATE 240.0 #define DEFAULT_UPDATE_RATE 240.0
#define SIM_CHECK_RATE 1.0
int showstats(SimData* simdata) int showstats(SimData* simdata)
{ {
@ -167,12 +165,19 @@ int showstats(SimData* simdata)
fflush(stdout); fflush(stdout);
} }
int looper(SimDevice* devices, int numdevices, Simulator simulator)
int clilooper(SimDevice* devices, int numdevices, Parameters* p, SimData* simdata, SimMap* simmap)
{ {
slogi("preparing game loop with %i devices...", numdevices); slogi("preparing game loop with %i devices...", numdevices);
SimData* simdata = malloc(sizeof(SimData));
SimMap* simmap = malloc(sizeof(SimMap));
int error = siminit(simdata, simmap, simulator);
if (error != MONOCOQUE_ERROR_NONE)
{
return error;
}
slogi("sending initial data to devices"); slogi("sending initial data to devices");
simdata->velocity = 16; simdata->velocity = 16;
@ -183,56 +188,6 @@ int clilooper(SimDevice* devices, int numdevices, Parameters* p, SimData* simdat
} }
sleep(3); sleep(3);
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
double update_rate = DEFAULT_UPDATE_RATE;
char ch;
int t=0;
int s=0;
bool go = true;
while (go == true && simdata->simstatus > 1)
{
simdatamap(simdata, simmap, p->sim);
showstats(simdata);
t++;
s++;
if(simdata->rpms<100)
{
simdata->rpms=100;
}
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
if( poll(&mypoll, 1, 1000.0/update_rate) )
{
scanf("%c", &ch);
if(ch == 'q')
{
go = false;
}
}
}
simdata->velocity = 0;
simdata->rpms = 100;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
fprintf(stdout, "\n");
return 0;
}
int looper(SimDevice* devices, int numdevices, Parameters* p)
{
SimData* simdata = malloc(sizeof(SimData));
SimMap* simmap = malloc(sizeof(SimMap));
struct termios newsettings, canonicalmode; struct termios newsettings, canonicalmode;
tcgetattr(0, &canonicalmode); tcgetattr(0, &canonicalmode);
newsettings = canonicalmode; newsettings = canonicalmode;
@ -243,33 +198,25 @@ int looper(SimDevice* devices, int numdevices, Parameters* p)
char ch; char ch;
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI }; struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
fprintf(stdout, "Searching for sim data... Press q to quit...\n"); double update_rate = DEFAULT_UPDATE_RATE;
int t=0;
p->simon = false; int s=0;
double update_rate = SIM_CHECK_RATE;
int go = true; int go = true;
while (go == true) while (go == true)
{ {
simdatamap(simdata, simmap, simulator);
showstats(simdata);
t++;
s++;
if (p->simon == false) if(simdata->rpms<100)
{ {
getSim(simdata, simmap, &p->simon, &p->sim); simdata->rpms=100;
} }
for (int x = 0; x < numdevices; x++)
if (p->simon == true)
{ {
clilooper(devices, numdevices, p, simdata, simmap); devices[x].update(&devices[x], simdata);
}
if (p->simon == true)
{
p->simon = false;
fprintf(stdout, "Searching for sim data... Press q again to quit...\n");
sleep(2);
}
}
if( poll(&mypoll, 1, 1000.0/update_rate) ) if( poll(&mypoll, 1, 1000.0/update_rate) )
{ {
scanf("%c", &ch); scanf("%c", &ch);
@ -279,11 +226,17 @@ int looper(SimDevice* devices, int numdevices, Parameters* p)
} }
} }
} }
fprintf(stdout, "\n"); fprintf(stdout, "\n");
fflush(stdout); fflush(stdout);
tcsetattr(0, TCSANOW, &canonicalmode); 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(simdata);
free(simmap); free(simmap);
@ -320,7 +273,7 @@ int tester(SimDevice* devices, int numdevices)
sleep(3); sleep(3);
fprintf(stdout, "Shifting into first gear\n"); fprintf(stdout, "Shifting into first gear\n");
simdata->gear = 2; simdata->gear = 1;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
@ -340,7 +293,7 @@ int tester(SimDevice* devices, int numdevices)
sleep(3); sleep(3);
fprintf(stdout, "Shifting into second gear\n"); fprintf(stdout, "Shifting into second gear\n");
simdata->gear = 3; simdata->gear = 2;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
@ -360,7 +313,7 @@ int tester(SimDevice* devices, int numdevices)
sleep(3); sleep(3);
fprintf(stdout, "Shifting into third gear\n"); fprintf(stdout, "Shifting into third gear\n");
simdata->gear = 4; simdata->gear = 3;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
@ -388,7 +341,7 @@ int tester(SimDevice* devices, int numdevices)
sleep(3); sleep(3);
fprintf(stdout, "Shifting into fourth gear\n"); fprintf(stdout, "Shifting into fourth gear\n");
simdata->gear = 5; simdata->gear = 4;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);

View File

@ -2,4 +2,4 @@
#include "../helper/parameters.h" #include "../helper/parameters.h"
int tester(SimDevice* devices, int numdevices); int tester(SimDevice* devices, int numdevices);
int looper(SimDevice* devices, int numdevices, Parameters* p); int looper (SimDevice* devices, int numdevices, Simulator simulator);

View File

@ -15,7 +15,7 @@
#include <libxml/tree.h> #include <libxml/tree.h>
#include "../devices/simdevice.h" #include "../devices/simdevice.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simdata.h"
#include "../slog/slog.h" #include "../slog/slog.h"
#define DEFAULT_UPDATE_RATE 30.0 #define DEFAULT_UPDATE_RATE 30.0

View File

@ -2,7 +2,7 @@
#define _TACHCONFIG_H #define _TACHCONFIG_H
#include "../devices/simdevice.h" #include "../devices/simdevice.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simdata.h"
int config_tachometer(int max_revs, int granularity, const char* save_file, SimDevice* simdevice, SimData* simdata); int config_tachometer(int max_revs, int granularity, const char* save_file, SimDevice* simdevice, SimData* simdata);

View File

@ -11,9 +11,7 @@
#include "confighelper.h" #include "confighelper.h"
#include "../slog/slog.h" #include "../slog/slog.h"
#include "parameters.h"
#include <pulse/pulseaudio.h>
int strcicmp(char const *a, char const *b) int strcicmp(char const *a, char const *b)
{ {
@ -42,7 +40,7 @@ int strtogame(const char* game, MonocoqueSettings* ms)
if (strcicmp(game, "test") == 0) if (strcicmp(game, "test") == 0)
{ {
slogd("Setting simulator to Test Data"); slogd("Setting simulator to Test Data");
ms->sim_name = SIMULATOR_SIMAPI_TEST; ms->sim_name = SIMULATOR_MONOCOQUE_TEST;
} }
else else
{ {
@ -86,11 +84,6 @@ int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev)
ds->dev_subtype = SIMDEVTYPE_GEARSOUND; ds->dev_subtype = SIMDEVTYPE_GEARSOUND;
break; break;
} }
if (strcicmp(device_subtype, "ABS") == 0)
{
ds->dev_subtype = SIMDEVTYPE_ABSBRAKES;
break;
}
default: default:
ds->is_valid = false; 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); slogw("%s does not appear to be a valid device sub type, but attempting to continue with other devices", device_subtype);
@ -235,7 +228,7 @@ int loadconfig(const char* config_file, DeviceSettings* ds)
int devsetup(const char* device_type, const char* device_subtype, const char* config_file, MonocoqueSettings* ms, DeviceSettings* ds, config_setting_t* device_settings) int devsetup(const char* device_type, const char* device_subtype, const char* config_file, MonocoqueSettings* ms, DeviceSettings* ds, config_setting_t* device_settings)
{ {
int error = MONOCOQUE_ERROR_NONE; int error = MONOCOQUE_ERROR_NONE;
//slogt("Called device setup with %s %s %s", device_type, device_subtype, config_file); slogi("Called device setup with %s %s %s", device_type, device_subtype, config_file);
ds->dev_type = SIMDEV_UNKNOWN; ds->dev_type = SIMDEV_UNKNOWN;
error = strtodev(device_type, device_subtype, ds); error = strtodev(device_type, device_subtype, ds);
@ -253,41 +246,6 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
return error; return error;
} }
if (ds->dev_type == SIMDEV_SOUND)
{
slogi("reading configured sound device settings");
ds->sounddevsettings.frequency = -1;
ds->sounddevsettings.volume = -1;
ds->sounddevsettings.lowbound_frequency = -1;
ds->sounddevsettings.upperbound_frequency = -1;
ds->sounddevsettings.pan = 0;
ds->sounddevsettings.duration = 2.0;
if (ds->dev_subtype == SIMDEVTYPE_GEARSOUND)
{
ds->sounddevsettings.duration = .125;
}
if (device_settings != NULL)
{
config_setting_lookup_int(device_settings, "volume", &ds->sounddevsettings.volume);
config_setting_lookup_int(device_settings, "frequency", &ds->sounddevsettings.frequency);
config_setting_lookup_int(device_settings, "pan", &ds->sounddevsettings.pan);
config_setting_lookup_float(device_settings, "duration", &ds->sounddevsettings.duration);
const char* temp;
int found = 0;
found = config_setting_lookup_string(device_settings, "devid", &temp);
if (found == 0)
{
ds->sounddevsettings.dev = NULL;
}
else
{
ds->sounddevsettings.dev = strdup(temp);
}
}
}
if (ds->dev_subtype == SIMDEVTYPE_TACHOMETER) if (ds->dev_subtype == SIMDEVTYPE_TACHOMETER)
{ {
if (device_settings != NULL) if (device_settings != NULL)
@ -306,8 +264,6 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
ds->tachsettings.use_pulses = false; ds->tachsettings.use_pulses = false;
} }
} }
if (ds->dev_subtype == SIMDEVTYPE_SIMWIND || ds->dev_subtype == SIMDEVTYPE_SHIFTLIGHTS) if (ds->dev_subtype == SIMDEVTYPE_SIMWIND || ds->dev_subtype == SIMDEVTYPE_SHIFTLIGHTS)
{ {
if (device_settings != NULL) if (device_settings != NULL)
@ -330,14 +286,7 @@ int settingsfree(DeviceSettings ds)
{ {
free(ds.serialdevsettings.portdev); free(ds.serialdevsettings.portdev);
} }
}
}
if (ds.dev_type == SIMDEV_SOUND)
{
if (ds.sounddevsettings.dev != NULL)
{
free(ds.sounddevsettings.dev);
}
}
return 0; return 0;
} }

View File

@ -24,11 +24,18 @@ typedef enum
SIMDEVTYPE_SHIFTLIGHTS = 2, SIMDEVTYPE_SHIFTLIGHTS = 2,
SIMDEVTYPE_SIMWIND = 3, SIMDEVTYPE_SIMWIND = 3,
SIMDEVTYPE_ENGINESOUND = 4, SIMDEVTYPE_ENGINESOUND = 4,
SIMDEVTYPE_GEARSOUND = 5, SIMDEVTYPE_GEARSOUND = 5
SIMDEVTYPE_ABSBRAKES = 6
} }
DeviceSubType; DeviceSubType;
typedef enum
{
SIMULATOR_MONOCOQUE_TEST = 0,
SIMULATOR_ASSETTO_CORSA = 1,
SIMULATOR_RFACTOR = 2,
SIMULATOR_RFACTOR2 = 3
}
Simulator;
typedef enum typedef enum
{ {
@ -75,18 +82,6 @@ typedef struct
} }
SerialDeviceSettings; SerialDeviceSettings;
typedef struct
{
int frequency;
int volume;
int lowbound_frequency;
int upperbound_frequency;
int pan;
double duration;
char* dev;
}
SoundDeviceSettings;
typedef struct typedef struct
{ {
bool is_valid; bool is_valid;
@ -94,7 +89,6 @@ typedef struct
DeviceSubType dev_subtype; DeviceSubType dev_subtype;
TachometerSettings tachsettings; TachometerSettings tachsettings;
SerialDeviceSettings serialdevsettings; SerialDeviceSettings serialdevsettings;
SoundDeviceSettings sounddevsettings;
} }
DeviceSettings; DeviceSettings;

View File

@ -104,7 +104,6 @@ ConfigError getParameters(int argc, char** argv, Parameters* p)
else if (nerrors3==0) else if (nerrors3==0)
{ {
p->program_action = A_TEST; p->program_action = A_TEST;
p->verbosity_count = arg_verbosity3->count;
exitcode = E_SUCCESS_AND_DO; exitcode = E_SUCCESS_AND_DO;
} }
else else

View File

@ -1,9 +1,6 @@
#ifndef _PARAMETERS_H #ifndef _PARAMETERS_H
#define _PARAMETERS_H #define _PARAMETERS_H
#include <stdbool.h>
#include "../simulatorapi/simapi/simapi/simapi.h"
typedef struct typedef struct
{ {
int program_action; int program_action;
@ -12,9 +9,6 @@ typedef struct
int max_revs; int max_revs;
int granularity; int granularity;
int verbosity_count; int verbosity_count;
Simulator sim;
bool simon;
} }
Parameters; Parameters;

View File

@ -7,14 +7,12 @@
#include "gameloop/gameloop.h" #include "gameloop/gameloop.h"
#include "gameloop/tachconfig.h" #include "gameloop/tachconfig.h"
#include "devices/simdevice.h" #include "devices/simdevice.h"
#include "devices/sound.h"
#include "helper/parameters.h" #include "helper/parameters.h"
#include "helper/dirhelper.h" #include "helper/dirhelper.h"
#include "helper/confighelper.h" #include "helper/confighelper.h"
#include "simulatorapi/simapi/simapi/simdata.h" #include "simulatorapi/simdata.h"
#include "slog/slog.h" #include "slog/slog.h"
int create_dir(char* dir) int create_dir(char* dir)
{ {
struct stat st = {0}; struct stat st = {0};
@ -151,11 +149,11 @@ int main(int argc, char** argv)
DeviceSettings ds[configureddevices]; DeviceSettings ds[configureddevices];
slogi("found %i devices in configuration", configureddevices); slogi("found %i devices in configuration", configureddevices);
int i = 0; int i = 0;
error = MONOCOQUE_ERROR_NONE;
while (i<configureddevices) while (i<configureddevices)
{ {
error = MONOCOQUE_ERROR_NONE;
DeviceSettings settings; DeviceSettings settings;
ds[i] = settings;
config_setting_t* config_device = config_setting_get_elem(config_devices, i); config_setting_t* config_device = config_setting_get_elem(config_devices, i);
const char* device_type; const char* device_type;
const char* device_subtype; const char* device_subtype;
@ -164,18 +162,14 @@ int main(int argc, char** argv)
config_setting_lookup_string(config_device, "type", &device_subtype); config_setting_lookup_string(config_device, "type", &device_subtype);
config_setting_lookup_string(config_device, "config", &device_config_file); config_setting_lookup_string(config_device, "config", &device_config_file);
//slogt("device type: %s", device_type);
//slogt("device sub type: %s", device_subtype);
//slogt("device config file: %s", device_config_file);
if (error == MONOCOQUE_ERROR_NONE) if (error == MONOCOQUE_ERROR_NONE)
{ {
error = devsetup(device_type, device_subtype, device_config_file, ms, &settings, config_device); error = devsetup(device_type, device_subtype, device_config_file, ms, &ds[i], config_device);
} }
if (error == MONOCOQUE_ERROR_NONE) if (error == MONOCOQUE_ERROR_NONE)
{ {
numdevices++; numdevices++;
} }
ds[i] = settings;
i++; i++;
@ -184,24 +178,20 @@ int main(int argc, char** argv)
i = 0; i = 0;
int j = 0; int j = 0;
error = MONOCOQUE_ERROR_NONE; error = MONOCOQUE_ERROR_NONE;
setupsound();
SimDevice* devices = malloc(numdevices * sizeof(SimDevice)); SimDevice* devices = malloc(numdevices * sizeof(SimDevice));
int initdevices = devinit(devices, configureddevices, ds); int initdevices = devinit(devices, configureddevices, ds);
if (p->program_action == A_PLAY) if (p->program_action == A_PLAY)
{ {
slogi("running monocoque in gameloop mode.."); slogi("running monocoque in gameloop mode..");
//error = strtogame(p->sim_string, ms); error = strtogame(p->sim_string, ms);
//if (error != MONOCOQUE_ERROR_NONE) if (error != MONOCOQUE_ERROR_NONE)
//{ {
// goto cleanup_final; goto cleanup_final;
//} }
#ifdef USE_PULSEAUDIO
pa_threaded_mainloop_unlock(mainloop);
#endif
error = looper(devices, initdevices, p);
error = looper(devices, initdevices, ms->sim_name);
if (error == MONOCOQUE_ERROR_NONE) if (error == MONOCOQUE_ERROR_NONE)
{ {
slogi("Game loop exited succesfully with error code: %i", error); slogi("Game loop exited succesfully with error code: %i", error);
@ -214,11 +204,6 @@ int main(int argc, char** argv)
else else
{ {
slogi("running monocoque in test mode..."); slogi("running monocoque in test mode...");
#ifdef USE_PULSEAUDIO
pa_threaded_mainloop_unlock(mainloop);
#endif
error = tester(devices, initdevices); error = tester(devices, initdevices);
if (error == MONOCOQUE_ERROR_NONE) if (error == MONOCOQUE_ERROR_NONE)
{ {

View File

@ -1,15 +1,10 @@
set(simulatorapi_source_files set(simulatorapi_source_files
simapi/simapi/simmapper.c simmapper.c
simapi/simapi/simmapper.h simmapper.h
simapi/simapi/simdata.h simdata.h
simapi/simapi/test.h test.h
simapi/include/acdata.h simapi/acdata.h
simapi/include/rf2data.h ac.h
simapi/simmap/basicmap.h
simapi/simmap/mapacdata.c
simapi/simmap/mapacdata.h
simapi/simapi/ac.h
simapi/simapi/rf2.h
) )
add_library(simulatorapi STATIC ${simulatorapi_source_files}) add_library(simulatorapi STATIC ${simulatorapi_source_files})

21
src/monocoque/simulatorapi/ac.h Executable file
View File

@ -0,0 +1,21 @@
#ifndef _AC_H
#define _AC_H
#include <stdbool.h>
#include "simapi/acdata.h"
#define AC_PHYSICS_FILE "acpmf_physics"
#define AC_STATIC_FILE "acpmf_static"
typedef struct
{
bool has_physics;
bool has_static;
void* physics_map_addr;
void* static_map_addr;
struct SPageFilePhysics ac_physics;
struct SPageFileStatic ac_static;
}
ACMap;
#endif

View File

@ -0,0 +1,21 @@
#ifndef _RF2_H
#define _RF2_H
#include <stdbool.h>
#include "simapi/rf2data.h"
#define RF2_TELEMETRY_FILE "rFactor2SMMP_Telemetry"
#define RF2_SCORING_FILE "rFactor2SMMP_Scoring"
typedef struct
{
bool has_telemetry;
bool has_scoring;
void* telemetry_map_addr;
void* scoring_map_addr;
struct rF2Telemetry rf2_telemetry;
//struct rF2Scoring rf2_scoring;
}
RF2Map;
#endif

@ -1 +1 @@
Subproject commit c77e96339caf1eb79479d89ce342161ad83ca70e Subproject commit 04fd2ca8ed590f562ad6423d9cf9f076daa28b95

View File

@ -0,0 +1,17 @@
#ifndef _SIMDATA_H
#define _SIMDATA_H
#include <stdint.h>
typedef struct
{
uint32_t velocity;
uint32_t rpms;
uint32_t gear;
uint32_t pulses;
uint32_t maxrpm;
uint32_t altitude;
}
SimData;
#endif

View File

@ -0,0 +1,136 @@
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>
#include "simmapper.h"
#include "simdata.h"
#include "test.h"
#include "ac.h"
#include "rf2.h"
#include "../helper/confighelper.h"
#include "../slog/slog.h"
#include "simapi/acdata.h"
#include "simapi/rf2data.h"
int simdatamap(SimData* simdata, SimMap* simmap, Simulator simulator)
{
char* a;
char* b;
switch ( simulator )
{
case SIMULATOR_MONOCOQUE_TEST :
memcpy(simdata, simmap->addr, sizeof(SimData));
break;
case SIMULATOR_ASSETTO_CORSA :
a = simmap->d.ac.physics_map_addr;
if (simmap->d.ac.has_static == true )
{
b = simmap->d.ac.static_map_addr;
simdata->maxrpm = *(uint32_t*) (char*) (b + offsetof(struct SPageFileStatic, maxRpm));
}
simdata->rpms = *(uint32_t*) (char*) (a + offsetof(struct SPageFilePhysics, rpms));
simdata->gear = *(uint32_t*) (char*) (a + offsetof(struct SPageFilePhysics, gear));
simdata->velocity = ceil( *(float*) (char*) (a + offsetof(struct SPageFilePhysics, speedKmh)));
simdata->altitude = 1;
break;
case SIMULATOR_RFACTOR2 :
a = simmap->d.rf2.telemetry_map_addr;
simdata->velocity = ceil(3.6 * ( *(double*) (char*) (a + offsetof(struct rF2Telemetry, mVehicles[0].mLocalVel.z))));
simdata->rpms = ceil( *(double*) (char*) (a + offsetof(struct rF2Telemetry, mVehicles[0].mEngineRPM)));
simdata->maxrpm = ceil( *(double*) (char*) (a + offsetof(struct rF2Telemetry, mVehicles[0].mEngineMaxRPM)));
simdata->gear = *(uint32_t*) (char*) (a + offsetof(struct rF2Telemetry, mVehicles[0].mGear));
break;
}
}
int siminit(SimData* simdata, SimMap* simmap, Simulator simulator)
{
slogi("searching for simulator data...");
int error = MONOCOQUE_ERROR_NONE;
void* a;
switch ( simulator )
{
case SIMULATOR_MONOCOQUE_TEST :
simmap->fd = shm_open(TEST_MEM_FILE_LOCATION, O_RDONLY, S_IRUSR | S_IWUSR);
if (simmap->fd == -1)
{
return 10;
}
simmap->addr = mmap(NULL, sizeof(SimData), PROT_READ, MAP_SHARED, simmap->fd, 0);
if (simmap->addr == MAP_FAILED)
{
return 30;
}
slogi("found data for monocoque test...");
break;
case SIMULATOR_ASSETTO_CORSA :
simmap->d.ac.has_physics=false;
simmap->d.ac.has_static=false;
simmap->fd = shm_open(AC_PHYSICS_FILE, O_RDONLY, S_IRUSR | S_IWUSR);
if (simmap->fd == -1)
{
slogd("could not open Assetto Corsa physics engine");
return MONOCOQUE_ERROR_NODATA;
}
simmap->d.ac.physics_map_addr = mmap(NULL, sizeof(simmap->d.ac.ac_physics), PROT_READ, MAP_SHARED, simmap->fd, 0);
if (simmap->d.ac.physics_map_addr == MAP_FAILED)
{
slogd("could not retrieve Assetto Corsa physics data");
return 30;
}
simmap->d.ac.has_physics=true;
simmap->fd = shm_open(AC_STATIC_FILE, O_RDONLY, S_IRUSR | S_IWUSR);
if (simmap->fd == -1)
{
slogd("could not open Assetto Corsa static data");
return 10;
}
simmap->d.ac.static_map_addr = mmap(NULL, sizeof(simmap->d.ac.ac_static), PROT_READ, MAP_SHARED, simmap->fd, 0);
if (simmap->d.ac.static_map_addr == MAP_FAILED)
{
slogd("could not retrieve Assetto Corsa static data");
return 30;
}
simmap->d.ac.has_static=true;
slogi("found data for Assetto Corsa...");
break;
case SIMULATOR_RFACTOR2 :
simmap->d.rf2.has_telemetry=false;
simmap->d.rf2.has_scoring=false;
simmap->fd = shm_open(RF2_TELEMETRY_FILE, O_RDONLY, S_IRUSR | S_IWUSR);
if (simmap->fd == -1)
{
slogd("could not open RFactor2 Telemetry engine");
return MONOCOQUE_ERROR_NODATA;
}
simmap->d.rf2.telemetry_map_addr = mmap(NULL, sizeof(simmap->d.rf2.rf2_telemetry), PROT_READ, MAP_SHARED, simmap->fd, 0);
if (simmap->d.rf2.telemetry_map_addr == MAP_FAILED)
{
slogd("could not retrieve RFactor2 telemetry data");
return 30;
}
simmap->d.rf2.has_telemetry=true;
slogi("found data for RFactor2...");
break;
}
return error;
}

View File

@ -0,0 +1,28 @@
#ifndef _SIMMAPPER_H
#define _SIMMAPPER_H
#include "ac.h"
#include "rf2.h"
#include "simdata.h"
#include "../helper/confighelper.h"
#include "simapi/acdata.h"
#include "simapi/rf2data.h"
typedef struct
{
void* addr;
int fd;
union
{
ACMap ac;
RF2Map rf2;
} d;
}
SimMap;
int siminit(SimData* simdata, SimMap* simmap, Simulator simulator);
int simdatamap(SimData* simdata, SimMap* simmap, Simulator simulator);
#endif

View File

@ -0,0 +1,6 @@
#ifndef _TEST_H
#define _TEST_H
#define TEST_MEM_FILE_LOCATION "/monocoque_test"
#endif

View File

@ -5,8 +5,8 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include "../src/monocoque/simulatorapi/simapi/simapi/simdata.h" #include "../src/monocoque/simulatorapi/simdata.h"
#include "../src/monocoque/simulatorapi/simapi/simapi/test.h" #include "../src/monocoque/simulatorapi/test.h"
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {

View File

@ -6,8 +6,8 @@
#include <string.h> #include <string.h>
#include <termios.h> #include <termios.h>
#include "../src/monocoque/simulatorapi/simapi/simapi/simdata.h" #include "../src/monocoque/simulatorapi/simdata.h"
#include "../src/monocoque/simulatorapi/simapi/simapi/test.h" #include "../src/monocoque/simulatorapi/test.h"
#define DATA "Hello, World! From PID %d" #define DATA "Hello, World! From PID %d"

View File

@ -5,8 +5,8 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include "../src/monocoque/simulatorapi/simapi/simapi/simdata.h" #include "../src/monocoque/simulatorapi/simdata.h"
#include "../src/monocoque/simulatorapi/simapi/simapi/test.h" #include "../src/monocoque/simulatorapi/test.h"
#define DATA "Hello, World! From PID %d" #define DATA "Hello, World! From PID %d"

View File

@ -2,7 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <libserialport.h> #include <libserialport.h>
#include "../src/monocoque/simulatorapi/simapi/simapi/simdata.h" #include "../src/monocoque/simulatorapi/simdata.h"
/* Helper function for error handling. */ /* Helper function for error handling. */
int check(enum sp_return result); int check(enum sp_return result);