monocoque initial commit
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../src/monocoque/simulatorapi/simdata.h"
|
||||
#include "../src/monocoque/simulatorapi/test.h"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int res;
|
||||
int fd;
|
||||
pid_t pid;
|
||||
void* addr;
|
||||
SimData* data = malloc(sizeof(SimData));
|
||||
|
||||
pid = getpid();
|
||||
|
||||
// get shared memory file descriptor (NOT a file)
|
||||
fd = shm_open(TEST_MEM_FILE_LOCATION, O_RDONLY, S_IRUSR | S_IWUSR);
|
||||
if (fd == -1)
|
||||
{
|
||||
return 10;
|
||||
}
|
||||
|
||||
// map shared memory to process address space
|
||||
addr = mmap(NULL, sizeof(SimData), PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (addr == MAP_FAILED)
|
||||
{
|
||||
return 30;
|
||||
}
|
||||
|
||||
// place data into memory
|
||||
|
||||
memcpy(data, addr, sizeof(SimData));
|
||||
|
||||
printf("PID %d: Read from shared memory: \"%s\"\n", pid, data);
|
||||
printf("PID %d: velocity: %i\n", pid, data->velocity);
|
||||
printf("PID %d: rpms: %i\n", pid, data->rpms);
|
||||
printf("PID %d: gear: %i\n", pid, data->gear);
|
||||
|
||||
return 0;
|
||||
}
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
#include <stdio.h> // printf
|
||||
#include <wchar.h> // wprintf
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <hidapi/hidapi.h>
|
||||
|
||||
#define MAX_STR 255
|
||||
|
||||
int buf_size = 64;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
buf_size++;
|
||||
int res;
|
||||
unsigned char buf[65];
|
||||
wchar_t wstr[MAX_STR];
|
||||
hid_device* handle;
|
||||
int i;
|
||||
|
||||
// Initialize the hidapi library
|
||||
res = hid_init();
|
||||
|
||||
// Open the device using the VID, PID,
|
||||
// and optionally the Serial number.
|
||||
handle = hid_open(0x4d8, 0x102, NULL);
|
||||
|
||||
int num = 321;
|
||||
char snum[5];
|
||||
|
||||
// convert 123 to string [buf]
|
||||
sprintf(snum, "%d", num);
|
||||
|
||||
// print our string
|
||||
printf("test%s\n", snum);
|
||||
|
||||
if (!handle)
|
||||
{
|
||||
printf("Could not find attached rev burner\n");
|
||||
res = hid_exit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read the Manufacturer String
|
||||
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
|
||||
wprintf(L"Manufacturer String: %s\n", wstr);
|
||||
|
||||
// Read the Product String
|
||||
res = hid_get_product_string(handle, wstr, MAX_STR);
|
||||
wprintf(L"Product String: %s\n", wstr);
|
||||
|
||||
// Read the Serial Number String
|
||||
res = hid_get_serial_number_string(handle, wstr, MAX_STR);
|
||||
wprintf(L"Serial Number String: (%d) %s\n", wstr[0], wstr);
|
||||
|
||||
|
||||
int rpm_array[21] = {41574, 48580, 54308, 58612, 60160, 61113, 61916, 62347, 62777, 63089, 63243, 63505, 63673, 63778, 63908, 64002, 64084, 64140, 64175, 64224, 64313};
|
||||
|
||||
buf[0] = 0x00;
|
||||
buf[1] = 0x00;
|
||||
buf[2] = 0x00;
|
||||
buf[3] = 0x00;
|
||||
res = hid_write(handle, buf, buf_size);
|
||||
|
||||
for (i = 0; i<21; i++)
|
||||
{
|
||||
int rpms = i * 500;
|
||||
|
||||
fprintf(stderr, "Setting RPM to %i hex as decimal %i\n", rpms, rpm_array[i]);
|
||||
|
||||
unsigned char bytes[buf_size];
|
||||
unsigned long nnnnn = rpm_array[i];
|
||||
unsigned long xlong = 0;
|
||||
for (int x = 0; x < buf_size; x++)
|
||||
{
|
||||
bytes[x] = 0x00;
|
||||
}
|
||||
|
||||
bytes[3] = (nnnnn >> 8) & 0xFF;
|
||||
bytes[2] = nnnnn & 0xFF;
|
||||
|
||||
FILE* write_ptr;
|
||||
if ( i == 0 )
|
||||
{
|
||||
write_ptr = fopen("test0.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 1 )
|
||||
{
|
||||
write_ptr = fopen("test1.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 2 )
|
||||
{
|
||||
write_ptr = fopen("test2.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 3 )
|
||||
{
|
||||
write_ptr = fopen("test3.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 4 )
|
||||
{
|
||||
write_ptr = fopen("test4.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 5 )
|
||||
{
|
||||
write_ptr = fopen("test5.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 6 )
|
||||
{
|
||||
write_ptr = fopen("test6.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 7 )
|
||||
{
|
||||
write_ptr = fopen("test7.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 8 )
|
||||
{
|
||||
write_ptr = fopen("test8.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 9 )
|
||||
{
|
||||
write_ptr = fopen("test9.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 10 )
|
||||
{
|
||||
write_ptr = fopen("test10.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 11 )
|
||||
{
|
||||
write_ptr = fopen("test11.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 12 )
|
||||
{
|
||||
write_ptr = fopen("test12.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 13 )
|
||||
{
|
||||
write_ptr = fopen("test13.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 14 )
|
||||
{
|
||||
write_ptr = fopen("test14.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 15 )
|
||||
{
|
||||
write_ptr = fopen("test15.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 16 )
|
||||
{
|
||||
write_ptr = fopen("test16.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 17 )
|
||||
{
|
||||
write_ptr = fopen("test17.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 18 )
|
||||
{
|
||||
write_ptr = fopen("test18.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 19 )
|
||||
{
|
||||
write_ptr = fopen("test19.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 20 )
|
||||
{
|
||||
write_ptr = fopen("test20.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 21 )
|
||||
{
|
||||
write_ptr = fopen("test21.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 22 )
|
||||
{
|
||||
write_ptr = fopen("test22.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 23 )
|
||||
{
|
||||
write_ptr = fopen("test23.bin","wb");
|
||||
}
|
||||
|
||||
if ( i == 24 )
|
||||
{
|
||||
write_ptr = fopen("test24.bin","wb");
|
||||
}
|
||||
|
||||
fwrite(bytes,sizeof(bytes),1,write_ptr);
|
||||
fclose(write_ptr);
|
||||
|
||||
if (handle)
|
||||
{
|
||||
res = hid_write(handle, bytes, buf_size);
|
||||
}
|
||||
|
||||
sleep(4);
|
||||
}
|
||||
|
||||
buf[0] = 0x00;
|
||||
buf[1] = 0x00;
|
||||
buf[2] = 0x00;
|
||||
buf[3] = 0x00;
|
||||
res = hid_write(handle, buf, buf_size);
|
||||
|
||||
sleep(2);
|
||||
// Close the device
|
||||
hid_close(handle);
|
||||
|
||||
// Finalize the hidapi library
|
||||
res = hid_exit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
+269
@@ -0,0 +1,269 @@
|
||||
/** @file pa_devs.c
|
||||
@ingroup examples_src
|
||||
@brief List available devices, including device information.
|
||||
@author Phil Burk http://www.softsynth.com
|
||||
|
||||
@note Define PA_USE_ASIO=0 to compile this code on Windows without
|
||||
ASIO support.
|
||||
*/
|
||||
/*
|
||||
* $Id: pa_devs.c 1891 2013-05-05 14:00:02Z rbencina $
|
||||
*
|
||||
* This program uses the PortAudio Portable Audio Library.
|
||||
* For more information see: http://www.portaudio.com
|
||||
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge,
|
||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
* and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
||||
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The text above constitutes the entire PortAudio license; however,
|
||||
* the PortAudio community also makes the following non-binding requests:
|
||||
*
|
||||
* Any person wishing to distribute modifications to the Software is
|
||||
* requested to send the modifications to the original developer so that
|
||||
* they can be incorporated into the canonical version. It is also
|
||||
* requested that these non-binding requests be included along with the
|
||||
* license above.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include "portaudio.h"
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
|
||||
#if PA_USE_ASIO
|
||||
#include "pa_asio.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*******************************************************************/
|
||||
static void PrintSupportedStandardSampleRates(
|
||||
const PaStreamParameters* inputParameters,
|
||||
const PaStreamParameters* outputParameters )
|
||||
{
|
||||
static double standardSampleRates[] =
|
||||
{
|
||||
8000.0, 9600.0, 11025.0, 12000.0, 16000.0, 22050.0, 24000.0, 32000.0,
|
||||
44100.0, 48000.0, 88200.0, 96000.0, 192000.0, -1 /* negative terminated list */
|
||||
};
|
||||
int i, printCount;
|
||||
PaError err;
|
||||
|
||||
printCount = 0;
|
||||
for( i=0; standardSampleRates[i] > 0; i++ )
|
||||
{
|
||||
err = Pa_IsFormatSupported( inputParameters, outputParameters, standardSampleRates[i] );
|
||||
if( err == paFormatIsSupported )
|
||||
{
|
||||
if( printCount == 0 )
|
||||
{
|
||||
printf( "\t%8.2f", standardSampleRates[i] );
|
||||
printCount = 1;
|
||||
}
|
||||
else
|
||||
if( printCount == 4 )
|
||||
{
|
||||
printf( ",\n\t%8.2f", standardSampleRates[i] );
|
||||
printCount = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf( ", %8.2f", standardSampleRates[i] );
|
||||
++printCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( !printCount )
|
||||
{
|
||||
printf( "None\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
printf( "\n" );
|
||||
}
|
||||
}
|
||||
|
||||
/*******************************************************************/
|
||||
int main(void);
|
||||
int main(void)
|
||||
{
|
||||
int i, numDevices, defaultDisplayed;
|
||||
const PaDeviceInfo* deviceInfo;
|
||||
PaStreamParameters inputParameters, outputParameters;
|
||||
PaError err;
|
||||
|
||||
|
||||
err = Pa_Initialize();
|
||||
if( err != paNoError )
|
||||
{
|
||||
printf( "ERROR: Pa_Initialize returned 0x%x\n", err );
|
||||
goto error;
|
||||
}
|
||||
|
||||
printf( "PortAudio version number = %d\nPortAudio version text = '%s'\n",
|
||||
Pa_GetVersion(), Pa_GetVersionText() );
|
||||
|
||||
|
||||
numDevices = Pa_GetDeviceCount();
|
||||
if( numDevices < 0 )
|
||||
{
|
||||
printf( "ERROR: Pa_GetDeviceCount returned 0x%x\n", numDevices );
|
||||
err = numDevices;
|
||||
goto error;
|
||||
}
|
||||
|
||||
printf( "Number of devices = %d\n", numDevices );
|
||||
for( i=0; i<numDevices; i++ )
|
||||
{
|
||||
deviceInfo = Pa_GetDeviceInfo( i );
|
||||
printf( "--------------------------------------- device #%d\n", i );
|
||||
|
||||
/* Mark global and API specific default devices */
|
||||
defaultDisplayed = 0;
|
||||
if( i == Pa_GetDefaultInputDevice() )
|
||||
{
|
||||
printf( "[ Default Input" );
|
||||
defaultDisplayed = 1;
|
||||
}
|
||||
else
|
||||
if( i == Pa_GetHostApiInfo( deviceInfo->hostApi )->defaultInputDevice )
|
||||
{
|
||||
const PaHostApiInfo* hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi );
|
||||
printf( "[ Default %s Input", hostInfo->name );
|
||||
defaultDisplayed = 1;
|
||||
}
|
||||
|
||||
if( i == Pa_GetDefaultOutputDevice() )
|
||||
{
|
||||
printf( (defaultDisplayed ? "," : "[") );
|
||||
printf( " Default Output" );
|
||||
defaultDisplayed = 1;
|
||||
}
|
||||
else
|
||||
if( i == Pa_GetHostApiInfo( deviceInfo->hostApi )->defaultOutputDevice )
|
||||
{
|
||||
const PaHostApiInfo* hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi );
|
||||
printf( (defaultDisplayed ? "," : "[") );
|
||||
printf( " Default %s Output", hostInfo->name );
|
||||
defaultDisplayed = 1;
|
||||
}
|
||||
|
||||
if( defaultDisplayed )
|
||||
{
|
||||
printf( " ]\n" );
|
||||
}
|
||||
|
||||
/* print device info fields */
|
||||
#ifdef WIN32
|
||||
{
|
||||
/* Use wide char on windows, so we can show UTF-8 encoded device names */
|
||||
wchar_t wideName[MAX_PATH];
|
||||
MultiByteToWideChar(CP_UTF8, 0, deviceInfo->name, -1, wideName, MAX_PATH-1);
|
||||
wprintf( L"Name = %s\n", wideName );
|
||||
}
|
||||
#else
|
||||
printf( "Name = %s\n", deviceInfo->name );
|
||||
#endif
|
||||
printf( "Host API = %s\n", Pa_GetHostApiInfo( deviceInfo->hostApi )->name );
|
||||
printf( "Max inputs = %d", deviceInfo->maxInputChannels );
|
||||
printf( ", Max outputs = %d\n", deviceInfo->maxOutputChannels );
|
||||
|
||||
printf( "Default low input latency = %8.4f\n", deviceInfo->defaultLowInputLatency );
|
||||
printf( "Default low output latency = %8.4f\n", deviceInfo->defaultLowOutputLatency );
|
||||
printf( "Default high input latency = %8.4f\n", deviceInfo->defaultHighInputLatency );
|
||||
printf( "Default high output latency = %8.4f\n", deviceInfo->defaultHighOutputLatency );
|
||||
|
||||
#ifdef WIN32
|
||||
#if PA_USE_ASIO
|
||||
/* ASIO specific latency information */
|
||||
if( Pa_GetHostApiInfo( deviceInfo->hostApi )->type == paASIO )
|
||||
{
|
||||
long minLatency, maxLatency, preferredLatency, granularity;
|
||||
|
||||
err = PaAsio_GetAvailableLatencyValues( i,
|
||||
&minLatency, &maxLatency, &preferredLatency, &granularity );
|
||||
|
||||
printf( "ASIO minimum buffer size = %ld\n", minLatency );
|
||||
printf( "ASIO maximum buffer size = %ld\n", maxLatency );
|
||||
printf( "ASIO preferred buffer size = %ld\n", preferredLatency );
|
||||
|
||||
if( granularity == -1 )
|
||||
{
|
||||
printf( "ASIO buffer granularity = power of 2\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
printf( "ASIO buffer granularity = %ld\n", granularity );
|
||||
}
|
||||
}
|
||||
#endif /* PA_USE_ASIO */
|
||||
#endif /* WIN32 */
|
||||
|
||||
printf( "Default sample rate = %8.2f\n", deviceInfo->defaultSampleRate );
|
||||
|
||||
/* poll for standard sample rates */
|
||||
inputParameters.device = i;
|
||||
inputParameters.channelCount = deviceInfo->maxInputChannels;
|
||||
inputParameters.sampleFormat = paInt16;
|
||||
inputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */
|
||||
inputParameters.hostApiSpecificStreamInfo = NULL;
|
||||
|
||||
outputParameters.device = i;
|
||||
outputParameters.channelCount = deviceInfo->maxOutputChannels;
|
||||
outputParameters.sampleFormat = paInt16;
|
||||
outputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */
|
||||
outputParameters.hostApiSpecificStreamInfo = NULL;
|
||||
|
||||
if( inputParameters.channelCount > 0 )
|
||||
{
|
||||
printf("Supported standard sample rates\n for half-duplex 16 bit %d channel input = \n",
|
||||
inputParameters.channelCount );
|
||||
PrintSupportedStandardSampleRates( &inputParameters, NULL );
|
||||
}
|
||||
|
||||
if( outputParameters.channelCount > 0 )
|
||||
{
|
||||
printf("Supported standard sample rates\n for half-duplex 16 bit %d channel output = \n",
|
||||
outputParameters.channelCount );
|
||||
PrintSupportedStandardSampleRates( NULL, &outputParameters );
|
||||
}
|
||||
|
||||
if( inputParameters.channelCount > 0 && outputParameters.channelCount > 0 )
|
||||
{
|
||||
printf("Supported standard sample rates\n for full-duplex 16 bit %d channel input, %d channel output = \n",
|
||||
inputParameters.channelCount, outputParameters.channelCount );
|
||||
PrintSupportedStandardSampleRates( &inputParameters, &outputParameters );
|
||||
}
|
||||
}
|
||||
|
||||
Pa_Terminate();
|
||||
|
||||
printf("----------------------------------------------\n");
|
||||
return 0;
|
||||
|
||||
error:
|
||||
Pa_Terminate();
|
||||
fprintf( stderr, "Error number: %d\n", err );
|
||||
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
|
||||
return err;
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
|
||||
/** @file patest_longsine.c
|
||||
@ingroup test_src
|
||||
@brief Play a sine wave until ENTER hit.
|
||||
@author Phil Burk http://www.softsynth.com
|
||||
*/
|
||||
/*
|
||||
* $Id: patest_longsine.c 1097 2006-08-26 08:27:53Z rossb $
|
||||
*
|
||||
* This program uses the PortAudio Portable Audio Library.
|
||||
* For more information see: http://www.portaudio.com
|
||||
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge,
|
||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
* and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
||||
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The text above constitutes the entire PortAudio license; however,
|
||||
* the PortAudio community also makes the following non-binding requests:
|
||||
*
|
||||
* Any person wishing to distribute modifications to the Software is
|
||||
* requested to send the modifications to the original developer so that
|
||||
* they can be incorporated into the canonical version. It is also
|
||||
* requested that these non-binding requests be included along with the
|
||||
* license above.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "portaudio.h"
|
||||
|
||||
#define SAMPLE_RATE (44100)
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI (3.14159265)
|
||||
#endif
|
||||
|
||||
|
||||
#define MAX_TABLE_SIZE (6000)
|
||||
//#define TABLE_SIZE (3200)
|
||||
//#define HALF_TABLE (1600)
|
||||
typedef struct
|
||||
{
|
||||
float sine[MAX_TABLE_SIZE];
|
||||
float pitch;
|
||||
int left_phase;
|
||||
int right_phase;
|
||||
int n;
|
||||
int table_size;
|
||||
int amp;
|
||||
}
|
||||
|
||||
paTestData;
|
||||
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
long n = 0;
|
||||
//float note = 256;
|
||||
float note = 60;
|
||||
//float note = 161.626;
|
||||
/* This routine will be called by the PortAudio engine when audio is needed.
|
||||
** It may called at interrupt level on some machines so don't do anything
|
||||
** that could mess up the system like calling malloc() or free().
|
||||
*/
|
||||
static int patestCallback(const void* inputBuffer,
|
||||
void* outputBuffer,
|
||||
unsigned long framesPerBuffer,
|
||||
const PaStreamCallbackTimeInfo* timeInfo,
|
||||
PaStreamCallbackFlags statusFlags,
|
||||
void* userData)
|
||||
{
|
||||
paTestData* data = (paTestData*)userData;
|
||||
float* out = (float*)outputBuffer;
|
||||
unsigned int i;
|
||||
unsigned int n;
|
||||
n = data->n;
|
||||
(void) inputBuffer; /* Prevent unused argument warning. */
|
||||
for( i=0; i<framesPerBuffer; i++,n++ )
|
||||
{
|
||||
|
||||
|
||||
//data->sine[i] = (float) sin( ((double)i/(double) MAX_TABLE_SIZE) * ( ( M_PI * 2. ) ) );
|
||||
//data->sine[n] = sin ( 2 * M_PI * ((float) n / (float) MAX_TABLE_SIZE) );
|
||||
//data->right_phase = data->left_phase;
|
||||
//data->left_phase = n;
|
||||
//data->right_phase = n;
|
||||
float v = data->amp * sin (2 * M_PI * ((float) n) / (float) SAMPLE_RATE);
|
||||
//*out++ = data->sine[data->left_phase];
|
||||
//*out++ = data->sine[data->right_phase];
|
||||
|
||||
if (n>=data->table_size)
|
||||
{
|
||||
n=0;
|
||||
}
|
||||
|
||||
*out++ = v;
|
||||
*out++ = v;
|
||||
|
||||
data->left_phase += 1;
|
||||
data->right_phase += 1;
|
||||
/*
|
||||
float v = 800.81;
|
||||
if (i>=512)
|
||||
{
|
||||
v = -v;
|
||||
}
|
||||
*out++ = v;
|
||||
*out++ = v;
|
||||
*/
|
||||
//if( data->left_phase >= MAX_TABLE_SIZE ) data->left_phase -= MAX_TABLE_SIZE;
|
||||
//if( data->right_phase >= MAX_TABLE_SIZE ) data->right_phase -= MAX_TABLE_SIZE;
|
||||
if( data->left_phase >= data->table_size )
|
||||
{
|
||||
data->left_phase -= data->table_size;
|
||||
}
|
||||
if( data->right_phase >= data->table_size )
|
||||
{
|
||||
data->right_phase -= data->table_size;
|
||||
}
|
||||
}
|
||||
data->n=n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void generatesamples(int table_size, int half_table, int datum, paTestData* data)
|
||||
{
|
||||
datum = 1;
|
||||
int j;
|
||||
for( j=data->n; j<MAX_TABLE_SIZE; j++)
|
||||
{
|
||||
for( int i=0; i<data->table_size; i++,j++ )
|
||||
{
|
||||
//data.sine[i] = (float) amp * sin( ((double)i/(double) TABLE_SIZE) * ( ( M_PI * 2. * freq) ) );
|
||||
data->sine[j] = (float) datum * sin( ((double)i/(double) data->table_size) * ( ( M_PI * 2. ) ) );
|
||||
/*
|
||||
data->sine[i] = datum;
|
||||
if (i>=half_table) {
|
||||
data->sine[i] = -datum;
|
||||
}
|
||||
*/
|
||||
data->n=j;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*******************************************************************/
|
||||
int main(void);
|
||||
int main(void)
|
||||
{
|
||||
PaStreamParameters outputParameters;
|
||||
PaStream* stream;
|
||||
PaError err;
|
||||
paTestData data;
|
||||
int i;
|
||||
printf("PortAudio Test: output sine wave.\n");
|
||||
|
||||
float amp = ((float)32);
|
||||
amp = ((float)32);
|
||||
//float freq = ((float) freq1/(SAMPLE_RATE/TABLE_SIZE));
|
||||
int freq = 32;
|
||||
// 500 rpms
|
||||
int table_size = 5292;
|
||||
int half_table = 2646;
|
||||
table_size = 200;
|
||||
half_table = 50;
|
||||
data.pitch = 1;
|
||||
data.pitch = 261.626;
|
||||
data.amp = 2;
|
||||
//data.pitch = 523.25;
|
||||
//table_size = 2646;
|
||||
//half_table = 1323;
|
||||
//table_size = 3200;
|
||||
//half_table = 1600;
|
||||
/* initialise sinusoidal wavetable */
|
||||
data.table_size=5292;
|
||||
//generatesamples(table_size,half_table,5,&data);
|
||||
data.left_phase = data.right_phase = 0;
|
||||
|
||||
err = Pa_Initialize();
|
||||
if( err != paNoError )
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
|
||||
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
|
||||
outputParameters.channelCount = 2; /* stereo output */
|
||||
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
|
||||
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
|
||||
outputParameters.hostApiSpecificStreamInfo = NULL;
|
||||
|
||||
err = Pa_OpenStream( &stream,
|
||||
NULL, /* No input. */
|
||||
&outputParameters, /* As above. */
|
||||
SAMPLE_RATE,
|
||||
440, /* Frames per buffer. */
|
||||
paClipOff, /* No out of range samples expected. */
|
||||
patestCallback,
|
||||
&data );
|
||||
if( err != paNoError )
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
|
||||
fprintf(stdout,"Revving to 500 rpm...\n");
|
||||
|
||||
err = Pa_StartStream( stream );
|
||||
if( err != paNoError )
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
|
||||
//printf("Hit ENTER to stop program.\n");
|
||||
//getchar();
|
||||
|
||||
sleep(10);
|
||||
|
||||
table_size = 2646;
|
||||
half_table = 1323;
|
||||
table_size = 100;
|
||||
data.table_size = 2646;
|
||||
half_table = 100;
|
||||
|
||||
fprintf(stdout,"Revving to 1000 rpm...\n");
|
||||
//generatesamples(table_size,half_table,5,&data);
|
||||
data.pitch = 523.25;
|
||||
|
||||
sleep(10);
|
||||
|
||||
table_size = 1764;
|
||||
half_table = 882;
|
||||
table_size = 400;
|
||||
half_table = 200;
|
||||
data.table_size = 1764;
|
||||
|
||||
fprintf(stdout,"Revving to 1500 rpm...\n");
|
||||
//generatesamples(table_size,half_table,5,&data);
|
||||
|
||||
sleep(5);
|
||||
|
||||
table_size = 1323;
|
||||
half_table = 661;
|
||||
data.table_size = 1323;
|
||||
|
||||
fprintf(stdout,"Revving to 2000 rpm...\n");
|
||||
//generatesamples(table_size,half_table,5,&data);
|
||||
|
||||
sleep(5);
|
||||
err = Pa_CloseStream( stream );
|
||||
if( err != paNoError )
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
Pa_Terminate();
|
||||
|
||||
printf("Test finished.\n");
|
||||
return err;
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <libxml/xmlmemory.h>
|
||||
#include <libxml/parser.h>
|
||||
|
||||
void parseStory (xmlDocPtr doc, xmlNodePtr cur)
|
||||
{
|
||||
|
||||
xmlChar* key;
|
||||
cur = cur->xmlChildrenNode;
|
||||
while (cur != NULL)
|
||||
{
|
||||
if ((!xmlStrcmp(cur->name, (const xmlChar*)"Value")))
|
||||
{
|
||||
key = xmlNodeGetContent(cur);
|
||||
printf("Revs x100: %s\n", (char*) key);
|
||||
xmlFree(key);
|
||||
}
|
||||
if ((!xmlStrcmp(cur->name, (const xmlChar*)"TimerValue")))
|
||||
{
|
||||
key = xmlNodeGetContent(cur);
|
||||
printf("Value to send to RevBurner: %s\n", (char*) key);
|
||||
xmlFree(key);
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void parseDoc(char* docname)
|
||||
{
|
||||
|
||||
xmlDocPtr doc;
|
||||
xmlNodePtr cur;
|
||||
|
||||
doc = xmlParseFile(docname);
|
||||
|
||||
if (doc == NULL )
|
||||
{
|
||||
fprintf(stderr,"Document not parsed successfully. \n");
|
||||
return;
|
||||
}
|
||||
|
||||
cur = xmlDocGetRootElement(doc);
|
||||
|
||||
if (cur == NULL)
|
||||
{
|
||||
fprintf(stderr,"empty document\n");
|
||||
xmlFreeDoc(doc);
|
||||
return;
|
||||
}
|
||||
|
||||
if (xmlStrcmp(cur->name, (const xmlChar*) "TachometerSettings"))
|
||||
{
|
||||
fprintf(stderr,"document of the wrong type, root node != TachometerSettings");
|
||||
xmlFreeDoc(doc);
|
||||
return;
|
||||
}
|
||||
|
||||
cur = cur->xmlChildrenNode;
|
||||
cur = cur->next;
|
||||
cur = cur->xmlChildrenNode;
|
||||
while (cur != NULL)
|
||||
{
|
||||
|
||||
if (!(xmlStrcmp(cur->name, (const xmlChar*)"SettingsItem")))
|
||||
{
|
||||
parseStory (doc, cur);
|
||||
}
|
||||
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
xmlFreeDoc(doc);
|
||||
return;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
|
||||
char* docname;
|
||||
|
||||
if (argc <= 1)
|
||||
{
|
||||
printf("Usage: %s docname\n", argv[0]);
|
||||
return(0);
|
||||
}
|
||||
|
||||
docname = argv[1];
|
||||
parseDoc (docname);
|
||||
|
||||
return (1);
|
||||
}
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
function is_linux
|
||||
{
|
||||
if [[ "$(uname)" == "Linux" ]]; then
|
||||
echo 1
|
||||
return
|
||||
fi
|
||||
|
||||
echo 0
|
||||
}
|
||||
|
||||
SET="setmem"
|
||||
GET="getmem"
|
||||
CFLAGS=""
|
||||
if [[ "$(is_linux)" == "1" ]]; then
|
||||
CFLAGS="-lrt"
|
||||
fi
|
||||
|
||||
cc ${CFLAGS} -o ${SET} ${SET}.c
|
||||
cc ${CFLAGS} -o ${GET} ${GET}.c
|
||||
|
||||
./${SET} &
|
||||
sleep 1
|
||||
./${GET}
|
||||
if [[ "$(is_linux)" == "1" ]]; then
|
||||
echo "/dev/shm:"
|
||||
ls -l /dev/shm
|
||||
fi
|
||||
sleep 1
|
||||
|
||||
rm ${SET} ${GET}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
|
||||
#include "../src/monocoque/simulatorapi/simdata.h"
|
||||
#include "../src/monocoque/simulatorapi/test.h"
|
||||
|
||||
#define DATA "Hello, World! From PID %d"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int res;
|
||||
int fd;
|
||||
int len;
|
||||
pid_t pid;
|
||||
void* addr;
|
||||
|
||||
pid = getpid();
|
||||
|
||||
fd = shm_open(TEST_MEM_FILE_LOCATION, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
return 10;
|
||||
}
|
||||
|
||||
SimData* data = malloc(sizeof(SimData));
|
||||
|
||||
data->gear=0;
|
||||
data->rpms=0;
|
||||
data->maxrpm=0;
|
||||
data->velocity=0;
|
||||
//SimData.pulses=0;
|
||||
|
||||
res = ftruncate(fd, sizeof(SimData));
|
||||
if (res == -1)
|
||||
{
|
||||
perror("ftruncate");
|
||||
return 20;
|
||||
}
|
||||
|
||||
data->gear=1;
|
||||
data->rpms=500;
|
||||
data->maxrpm=6500;
|
||||
data->velocity=25;
|
||||
//SimData.pulses=38792;
|
||||
|
||||
// map .shared memory to process address space
|
||||
addr = mmap(NULL, sizeof(SimData), PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (addr == MAP_FAILED)
|
||||
{
|
||||
perror("mmap");
|
||||
return 30;
|
||||
}
|
||||
|
||||
printf("size of int %i\n", sizeof(int));
|
||||
printf("size of struct %i\n", sizeof(SimData));
|
||||
memcpy(addr, data, sizeof(SimData));
|
||||
//memcpy(addr, &SimData, sizeof(int)*3);
|
||||
|
||||
printf("PID %d: velocity: %i\n", pid, data->velocity);
|
||||
printf("PID %d: rpms: %i\n", pid, data->rpms);
|
||||
printf("PID %d: gear: %i\n", pid, data->gear);
|
||||
sleep(4);
|
||||
|
||||
|
||||
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);
|
||||
char ch;
|
||||
|
||||
int go=1;
|
||||
while (go>0)
|
||||
{
|
||||
|
||||
struct termios info;
|
||||
tcgetattr(0, &info);
|
||||
info.c_lflag &= (~ICANON & ~ECHO);
|
||||
info.c_cc[VMIN] = 1;
|
||||
info.c_cc[VTIME] = 0;
|
||||
tcsetattr(0, TCSANOW, &info);
|
||||
char ch;
|
||||
scanf("%c", &ch);
|
||||
|
||||
|
||||
if (ch == 'q')
|
||||
{
|
||||
go=0;
|
||||
}
|
||||
|
||||
}
|
||||
tcsetattr(0, TCSANOW, &canonicalmode);
|
||||
|
||||
fd = shm_unlink(TEST_MEM_FILE_LOCATION);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("unlink");
|
||||
return 100;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../src/monocoque/simulatorapi/simdata.h"
|
||||
#include "../src/monocoque/simulatorapi/test.h"
|
||||
|
||||
#define DATA "Hello, World! From PID %d"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int res;
|
||||
int fd;
|
||||
int len;
|
||||
pid_t pid;
|
||||
void* addr;
|
||||
|
||||
pid = getpid();
|
||||
|
||||
// get shared memory file descriptor (NOT a file)
|
||||
fd = shm_open(TEST_MEM_FILE_LOCATION, O_RDWR, S_IRUSR | S_IWUSR);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
return 10;
|
||||
}
|
||||
|
||||
// extend shared memory object as by default it's initialized with size 0
|
||||
//res = ftruncate(fd, STORAGE_SIZE);
|
||||
SimData* data = malloc(sizeof(SimData));
|
||||
data->velocity=1;
|
||||
data->gear=4;
|
||||
data->maxrpm=6500;
|
||||
data->rpms=1000;
|
||||
//SimData.pulses=398273;
|
||||
|
||||
res = ftruncate(fd, sizeof(SimData));
|
||||
if (res == -1)
|
||||
{
|
||||
//perror("ftruncate");
|
||||
//return 20;
|
||||
}
|
||||
|
||||
data->velocity=atoi(argv[1]);
|
||||
data->rpms=atoi(argv[2]);
|
||||
data->gear=atoi(argv[3]);
|
||||
//SimData.pulses=39993;
|
||||
// map .shared memory to process address space
|
||||
addr = mmap(NULL, sizeof(SimData), PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (addr == MAP_FAILED)
|
||||
{
|
||||
perror("mmap");
|
||||
return 30;
|
||||
}
|
||||
|
||||
memcpy(addr, data, sizeof(SimData));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
|
||||
void display(char* prog, char* bytes, int n);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char* name = "/shm-example"; // file name
|
||||
const int SIZE = 4096; // file size
|
||||
|
||||
const char* message0 = "Studying ";
|
||||
const char* message1 = "Operating Systems ";
|
||||
const char* message2 = "Is Fun!";
|
||||
const char* msg_end = "\n";
|
||||
|
||||
int shm_fd; // file descriptor, from shm_open()
|
||||
char* shm_base; // base address, from mmap()
|
||||
char* ptr; // shm_base is fixed, ptr is movable
|
||||
|
||||
/* create the shared memory segment as if it was a file */
|
||||
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
|
||||
if (shm_fd == -1)
|
||||
{
|
||||
printf("prod: Shared memory failed: %s\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* configure the size of the shared memory segment */
|
||||
ftruncate(shm_fd, SIZE);
|
||||
|
||||
/* map the shared memory segment to the address space of the process */
|
||||
shm_base = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
|
||||
if (shm_base == MAP_FAILED)
|
||||
{
|
||||
printf("prod: Map failed: %s\n", strerror(errno));
|
||||
// close and shm_unlink?
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to the mapped shared memory region.
|
||||
*
|
||||
* We increment the value of ptr after each write, but we
|
||||
* are ignoring the possibility that sprintf() fails.
|
||||
*/
|
||||
display("prod", shm_base, 64);
|
||||
ptr = shm_base;
|
||||
ptr += sprintf(ptr, "%s", message0);
|
||||
ptr += sprintf(ptr, "%s", message1);
|
||||
ptr += sprintf(ptr, "%s", message2);
|
||||
ptr += sprintf(ptr, "%s", msg_end);
|
||||
display("prod", shm_base, 64);
|
||||
|
||||
/* remove the mapped memory segment from the address space of the process */
|
||||
if (munmap(shm_base, SIZE) == -1)
|
||||
{
|
||||
printf("prod: Unmap failed: %s\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* close the shared memory segment as if it was a file */
|
||||
if (close(shm_fd) == -1)
|
||||
{
|
||||
printf("prod: Close failed: %s\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void display(char* prog, char* bytes, int n)
|
||||
{
|
||||
printf("display: %s\n", prog);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
printf("%02x%c", bytes[i], ((i+1)%16) ? ' ' : '\n');
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <libserialport.h>
|
||||
#include "../src/monocoque/simulatorapi/simdata.h"
|
||||
|
||||
/* Helper function for error handling. */
|
||||
int check(enum sp_return result);
|
||||
|
||||
int main()
|
||||
{
|
||||
SimData sd;
|
||||
sd.maxrpm = 6500;
|
||||
sd.rpms = 0;
|
||||
sd.altitude = 10;
|
||||
sd.gear = 1;
|
||||
sd.velocity = 74;
|
||||
|
||||
char* port_name = "/dev/ttyACM0";
|
||||
|
||||
/* The ports we will use. */
|
||||
struct sp_port* port;
|
||||
|
||||
/* Open and configure each port. */
|
||||
printf("Looking for port %s.\n", port_name);
|
||||
check(sp_get_port_by_name(port_name, &port));
|
||||
|
||||
printf("Opening port.\n");
|
||||
check(sp_open(port, SP_MODE_READ_WRITE));
|
||||
|
||||
printf("Setting port to 9600 8N1, no flow control.\n");
|
||||
check(sp_set_baudrate(port, 9600));
|
||||
check(sp_set_bits(port, 8));
|
||||
check(sp_set_parity(port, SP_PARITY_NONE));
|
||||
check(sp_set_stopbits(port, 1));
|
||||
check(sp_set_flowcontrol(port, SP_FLOWCONTROL_NONE));
|
||||
|
||||
while ( sd.rpms <= sd.maxrpm )
|
||||
{
|
||||
|
||||
|
||||
unsigned int timeout = 2000;
|
||||
|
||||
/* On success, sp_blocking_write() and sp_blocking_read()
|
||||
* return the number of bytes sent/received before the
|
||||
* timeout expired. We'll store that result here. */
|
||||
int result;
|
||||
int size = sizeof(SimData);
|
||||
|
||||
/* Send data. */
|
||||
result = check(sp_blocking_write(port, &sd, size, timeout));
|
||||
|
||||
/* Check whether we sent all of the data. */
|
||||
if (result == size)
|
||||
{
|
||||
printf("Sent %d bytes successfully, %i rpm.\n", size, sd.rpms);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Timed out, %d/%d bytes sent.\n", result, size);
|
||||
}
|
||||
|
||||
sd.rpms += 1000;
|
||||
if ( sd.rpms == sd.maxrpm + 1000 )
|
||||
{
|
||||
sd.rpms = 0;
|
||||
}
|
||||
if ( sd.rpms > sd.maxrpm )
|
||||
{
|
||||
sd.rpms = sd.maxrpm;
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
check(sp_close(port));
|
||||
sp_free_port(port);
|
||||
}
|
||||
|
||||
/* Helper function for error handling. */
|
||||
int check(enum sp_return result)
|
||||
{
|
||||
/* For this example we'll just exit on any error by calling abort(). */
|
||||
char* error_message;
|
||||
|
||||
switch (result)
|
||||
{
|
||||
case SP_ERR_ARG:
|
||||
printf("Error: Invalid argument.\n");
|
||||
abort();
|
||||
case SP_ERR_FAIL:
|
||||
error_message = sp_last_error_message();
|
||||
printf("Error: Failed: %s\n", error_message);
|
||||
sp_free_error_message(error_message);
|
||||
abort();
|
||||
case SP_ERR_SUPP:
|
||||
printf("Error: Not supported.\n");
|
||||
abort();
|
||||
case SP_ERR_MEM:
|
||||
printf("Error: Couldn't allocate memory.\n");
|
||||
abort();
|
||||
case SP_OK:
|
||||
default:
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,381 @@
|
||||
/*
|
||||
* Test suite program based of libusb-0.1-compat testlibusb
|
||||
* Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.ccom>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "libusb-1.0/libusb.h"
|
||||
|
||||
int verbose = 0;
|
||||
|
||||
static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor* ep_comp)
|
||||
{
|
||||
printf(" USB 3.0 Endpoint Companion:\n");
|
||||
printf(" bMaxBurst: %u\n", ep_comp->bMaxBurst);
|
||||
printf(" bmAttributes: %02xh\n", ep_comp->bmAttributes);
|
||||
printf(" wBytesPerInterval: %u\n", ep_comp->wBytesPerInterval);
|
||||
}
|
||||
|
||||
static void print_endpoint(const struct libusb_endpoint_descriptor* endpoint)
|
||||
{
|
||||
int i, ret;
|
||||
|
||||
printf(" Endpoint:\n");
|
||||
printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
|
||||
printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
|
||||
printf(" wMaxPacketSize: %u\n", endpoint->wMaxPacketSize);
|
||||
printf(" bInterval: %u\n", endpoint->bInterval);
|
||||
printf(" bRefresh: %u\n", endpoint->bRefresh);
|
||||
printf(" bSynchAddress: %u\n", endpoint->bSynchAddress);
|
||||
|
||||
for (i = 0; i < endpoint->extra_length;)
|
||||
{
|
||||
if (LIBUSB_DT_SS_ENDPOINT_COMPANION == endpoint->extra[i + 1])
|
||||
{
|
||||
struct libusb_ss_endpoint_companion_descriptor* ep_comp;
|
||||
|
||||
ret = libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
|
||||
if (LIBUSB_SUCCESS != ret)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
print_endpoint_comp(ep_comp);
|
||||
|
||||
libusb_free_ss_endpoint_companion_descriptor(ep_comp);
|
||||
}
|
||||
|
||||
i += endpoint->extra[i];
|
||||
}
|
||||
}
|
||||
|
||||
static void print_altsetting(const struct libusb_interface_descriptor* interface)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
printf(" Interface:\n");
|
||||
printf(" bInterfaceNumber: %u\n", interface->bInterfaceNumber);
|
||||
printf(" bAlternateSetting: %u\n", interface->bAlternateSetting);
|
||||
printf(" bNumEndpoints: %u\n", interface->bNumEndpoints);
|
||||
printf(" bInterfaceClass: %u\n", interface->bInterfaceClass);
|
||||
printf(" bInterfaceSubClass: %u\n", interface->bInterfaceSubClass);
|
||||
printf(" bInterfaceProtocol: %u\n", interface->bInterfaceProtocol);
|
||||
printf(" iInterface: %u\n", interface->iInterface);
|
||||
|
||||
for (i = 0; i < interface->bNumEndpoints; i++)
|
||||
{
|
||||
print_endpoint(&interface->endpoint[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_2_0_ext_cap(struct libusb_usb_2_0_extension_descriptor* usb_2_0_ext_cap)
|
||||
{
|
||||
printf(" USB 2.0 Extension Capabilities:\n");
|
||||
printf(" bDevCapabilityType: %u\n", usb_2_0_ext_cap->bDevCapabilityType);
|
||||
printf(" bmAttributes: %08xh\n", usb_2_0_ext_cap->bmAttributes);
|
||||
}
|
||||
|
||||
static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor* ss_usb_cap)
|
||||
{
|
||||
printf(" USB 3.0 Capabilities:\n");
|
||||
printf(" bDevCapabilityType: %u\n", ss_usb_cap->bDevCapabilityType);
|
||||
printf(" bmAttributes: %02xh\n", ss_usb_cap->bmAttributes);
|
||||
printf(" wSpeedSupported: %u\n", ss_usb_cap->wSpeedSupported);
|
||||
printf(" bFunctionalitySupport: %u\n", ss_usb_cap->bFunctionalitySupport);
|
||||
printf(" bU1devExitLat: %u\n", ss_usb_cap->bU1DevExitLat);
|
||||
printf(" bU2devExitLat: %u\n", ss_usb_cap->bU2DevExitLat);
|
||||
}
|
||||
|
||||
static void print_bos(libusb_device_handle* handle)
|
||||
{
|
||||
struct libusb_bos_descriptor* bos;
|
||||
uint8_t i;
|
||||
int ret;
|
||||
|
||||
ret = libusb_get_bos_descriptor(handle, &bos);
|
||||
if (ret < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
printf(" Binary Object Store (BOS):\n");
|
||||
printf(" wTotalLength: %u\n", bos->wTotalLength);
|
||||
printf(" bNumDeviceCaps: %u\n", bos->bNumDeviceCaps);
|
||||
|
||||
for (i = 0; i < bos->bNumDeviceCaps; i++)
|
||||
{
|
||||
struct libusb_bos_dev_capability_descriptor* dev_cap = bos->dev_capability[i];
|
||||
|
||||
if (dev_cap->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION)
|
||||
{
|
||||
struct libusb_usb_2_0_extension_descriptor* usb_2_0_extension;
|
||||
|
||||
ret = libusb_get_usb_2_0_extension_descriptor(NULL, dev_cap, &usb_2_0_extension);
|
||||
if (ret < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
print_2_0_ext_cap(usb_2_0_extension);
|
||||
libusb_free_usb_2_0_extension_descriptor(usb_2_0_extension);
|
||||
}
|
||||
else
|
||||
if (dev_cap->bDevCapabilityType == LIBUSB_BT_SS_USB_DEVICE_CAPABILITY)
|
||||
{
|
||||
struct libusb_ss_usb_device_capability_descriptor* ss_dev_cap;
|
||||
|
||||
ret = libusb_get_ss_usb_device_capability_descriptor(NULL, dev_cap, &ss_dev_cap);
|
||||
if (ret < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
print_ss_usb_cap(ss_dev_cap);
|
||||
libusb_free_ss_usb_device_capability_descriptor(ss_dev_cap);
|
||||
}
|
||||
}
|
||||
|
||||
libusb_free_bos_descriptor(bos);
|
||||
}
|
||||
|
||||
static void print_interface(const struct libusb_interface* interface)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < interface->num_altsetting; i++)
|
||||
{
|
||||
print_altsetting(&interface->altsetting[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_configuration(struct libusb_config_descriptor* config)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
printf(" Configuration:\n");
|
||||
printf(" wTotalLength: %u\n", config->wTotalLength);
|
||||
printf(" bNumInterfaces: %u\n", config->bNumInterfaces);
|
||||
printf(" bConfigurationValue: %u\n", config->bConfigurationValue);
|
||||
printf(" iConfiguration: %u\n", config->iConfiguration);
|
||||
printf(" bmAttributes: %02xh\n", config->bmAttributes);
|
||||
printf(" MaxPower: %u\n", config->MaxPower);
|
||||
|
||||
for (i = 0; i < config->bNumInterfaces; i++)
|
||||
{
|
||||
print_interface(&config->interface[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_device(libusb_device* dev, libusb_device_handle* handle)
|
||||
{
|
||||
struct libusb_device_descriptor desc;
|
||||
unsigned char string[256];
|
||||
const char* speed;
|
||||
int ret;
|
||||
uint8_t i;
|
||||
|
||||
switch (libusb_get_device_speed(dev))
|
||||
{
|
||||
case LIBUSB_SPEED_LOW:
|
||||
speed = "1.5M";
|
||||
break;
|
||||
case LIBUSB_SPEED_FULL:
|
||||
speed = "12M";
|
||||
break;
|
||||
case LIBUSB_SPEED_HIGH:
|
||||
speed = "480M";
|
||||
break;
|
||||
case LIBUSB_SPEED_SUPER:
|
||||
speed = "5G";
|
||||
break;
|
||||
case LIBUSB_SPEED_SUPER_PLUS:
|
||||
speed = "10G";
|
||||
break;
|
||||
default:
|
||||
speed = "Unknown";
|
||||
}
|
||||
|
||||
ret = libusb_get_device_descriptor(dev, &desc);
|
||||
if (ret < 0)
|
||||
{
|
||||
fprintf(stderr, "failed to get device descriptor");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Dev (bus %u, device %u): %04X - %04X speed: %s\n",
|
||||
libusb_get_bus_number(dev), libusb_get_device_address(dev),
|
||||
desc.idVendor, desc.idProduct, speed);
|
||||
|
||||
if (!handle)
|
||||
{
|
||||
libusb_open(dev, &handle);
|
||||
}
|
||||
|
||||
if (handle)
|
||||
{
|
||||
if (desc.iManufacturer)
|
||||
{
|
||||
ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
|
||||
if (ret > 0)
|
||||
{
|
||||
printf(" Manufacturer: %s\n", (char*)string);
|
||||
}
|
||||
}
|
||||
|
||||
if (desc.iProduct)
|
||||
{
|
||||
ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
|
||||
if (ret > 0)
|
||||
{
|
||||
printf(" Product: %s\n", (char*)string);
|
||||
}
|
||||
}
|
||||
|
||||
if (desc.iSerialNumber && verbose)
|
||||
{
|
||||
ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
|
||||
if (ret > 0)
|
||||
{
|
||||
printf(" Serial Number: %s\n", (char*)string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
{
|
||||
for (i = 0; i < desc.bNumConfigurations; i++)
|
||||
{
|
||||
struct libusb_config_descriptor* config;
|
||||
|
||||
ret = libusb_get_config_descriptor(dev, i, &config);
|
||||
if (LIBUSB_SUCCESS != ret)
|
||||
{
|
||||
printf(" Couldn't retrieve descriptors\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
print_configuration(config);
|
||||
|
||||
libusb_free_config_descriptor(config);
|
||||
}
|
||||
|
||||
if (handle && desc.bcdUSB >= 0x0201)
|
||||
{
|
||||
print_bos(handle);
|
||||
}
|
||||
}
|
||||
|
||||
if (handle)
|
||||
{
|
||||
libusb_close(handle);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int test_wrapped_device(const char* device_name)
|
||||
{
|
||||
libusb_device_handle* handle;
|
||||
int r, fd;
|
||||
|
||||
fd = open(device_name, O_RDWR);
|
||||
if (fd < 0)
|
||||
{
|
||||
printf("Error could not open %s: %s\n", device_name, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
r = libusb_wrap_sys_device(NULL, fd, &handle);
|
||||
if (r)
|
||||
{
|
||||
printf("Error wrapping device: %s: %s\n", device_name, libusb_strerror(r));
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
print_device(libusb_get_device(handle), handle);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
static int test_wrapped_device(const char* device_name)
|
||||
{
|
||||
(void)device_name;
|
||||
printf("Testing wrapped devices is not supported on your platform\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const char* device_name = NULL;
|
||||
libusb_device** devs;
|
||||
ssize_t cnt;
|
||||
int r, i;
|
||||
|
||||
for (i = 1; i < argc; i++)
|
||||
{
|
||||
if (!strcmp(argv[i], "-v"))
|
||||
{
|
||||
verbose = 1;
|
||||
}
|
||||
else
|
||||
if (!strcmp(argv[i], "-d") && (i + 1) < argc)
|
||||
{
|
||||
i++;
|
||||
device_name = argv[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Usage %s [-v] [-d </dev/bus/usb/...>]\n", argv[0]);
|
||||
printf("Note use -d to test libusb_wrap_sys_device()\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
r = libusb_init(NULL);
|
||||
if (r < 0)
|
||||
{
|
||||
return r;
|
||||
}
|
||||
|
||||
if (device_name)
|
||||
{
|
||||
r = test_wrapped_device(device_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
cnt = libusb_get_device_list(NULL, &devs);
|
||||
if (cnt < 0)
|
||||
{
|
||||
libusb_exit(NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (i = 0; devs[i]; i++)
|
||||
{
|
||||
print_device(devs[i], NULL);
|
||||
}
|
||||
|
||||
libusb_free_device_list(devs, 1);
|
||||
}
|
||||
|
||||
libusb_exit(NULL);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,532 @@
|
||||
/*
|
||||
* Test suite program based of libusb-0.1-compat testlibusb
|
||||
* Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.ccom>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "libusb-1.0/libusb.h"
|
||||
|
||||
#define ENDPOINT_OUT 0x01
|
||||
#define ENDPOINT_IN 0x81
|
||||
int verbose = 0;
|
||||
|
||||
static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor* ep_comp)
|
||||
{
|
||||
printf(" USB 3.0 Endpoint Companion:\n");
|
||||
printf(" bMaxBurst: %u\n", ep_comp->bMaxBurst);
|
||||
printf(" bmAttributes: %02xh\n", ep_comp->bmAttributes);
|
||||
printf(" wBytesPerInterval: %u\n", ep_comp->wBytesPerInterval);
|
||||
}
|
||||
|
||||
static void print_endpoint(const struct libusb_endpoint_descriptor* endpoint)
|
||||
{
|
||||
int i, ret;
|
||||
|
||||
printf(" Endpoint:\n");
|
||||
printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
|
||||
printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
|
||||
printf(" wMaxPacketSize: %u\n", endpoint->wMaxPacketSize);
|
||||
printf(" bInterval: %u\n", endpoint->bInterval);
|
||||
printf(" bRefresh: %u\n", endpoint->bRefresh);
|
||||
printf(" bSynchAddress: %u\n", endpoint->bSynchAddress);
|
||||
|
||||
for (i = 0; i < endpoint->extra_length;)
|
||||
{
|
||||
if (LIBUSB_DT_SS_ENDPOINT_COMPANION == endpoint->extra[i + 1])
|
||||
{
|
||||
struct libusb_ss_endpoint_companion_descriptor* ep_comp;
|
||||
|
||||
ret = libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
|
||||
if (LIBUSB_SUCCESS != ret)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
print_endpoint_comp(ep_comp);
|
||||
|
||||
libusb_free_ss_endpoint_companion_descriptor(ep_comp);
|
||||
}
|
||||
|
||||
i += endpoint->extra[i];
|
||||
}
|
||||
}
|
||||
|
||||
static void print_altsetting(const struct libusb_interface_descriptor* interface)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
printf(" Interface:\n");
|
||||
printf(" bInterfaceNumber: %u\n", interface->bInterfaceNumber);
|
||||
printf(" bAlternateSetting: %u\n", interface->bAlternateSetting);
|
||||
printf(" bNumEndpoints: %u\n", interface->bNumEndpoints);
|
||||
printf(" bInterfaceClass: %u\n", interface->bInterfaceClass);
|
||||
printf(" bInterfaceSubClass: %u\n", interface->bInterfaceSubClass);
|
||||
printf(" bInterfaceProtocol: %u\n", interface->bInterfaceProtocol);
|
||||
printf(" iInterface: %u\n", interface->iInterface);
|
||||
|
||||
for (i = 0; i < interface->bNumEndpoints; i++)
|
||||
{
|
||||
print_endpoint(&interface->endpoint[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_2_0_ext_cap(struct libusb_usb_2_0_extension_descriptor* usb_2_0_ext_cap)
|
||||
{
|
||||
printf(" USB 2.0 Extension Capabilities:\n");
|
||||
printf(" bDevCapabilityType: %u\n", usb_2_0_ext_cap->bDevCapabilityType);
|
||||
printf(" bmAttributes: %08xh\n", usb_2_0_ext_cap->bmAttributes);
|
||||
}
|
||||
|
||||
static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor* ss_usb_cap)
|
||||
{
|
||||
printf(" USB 3.0 Capabilities:\n");
|
||||
printf(" bDevCapabilityType: %u\n", ss_usb_cap->bDevCapabilityType);
|
||||
printf(" bmAttributes: %02xh\n", ss_usb_cap->bmAttributes);
|
||||
printf(" wSpeedSupported: %u\n", ss_usb_cap->wSpeedSupported);
|
||||
printf(" bFunctionalitySupport: %u\n", ss_usb_cap->bFunctionalitySupport);
|
||||
printf(" bU1devExitLat: %u\n", ss_usb_cap->bU1DevExitLat);
|
||||
printf(" bU2devExitLat: %u\n", ss_usb_cap->bU2DevExitLat);
|
||||
}
|
||||
|
||||
static void print_bos(libusb_device_handle* handle)
|
||||
{
|
||||
struct libusb_bos_descriptor* bos;
|
||||
uint8_t i;
|
||||
int ret;
|
||||
|
||||
ret = libusb_get_bos_descriptor(handle, &bos);
|
||||
if (ret < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
printf(" Binary Object Store (BOS):\n");
|
||||
printf(" wTotalLength: %u\n", bos->wTotalLength);
|
||||
printf(" bNumDeviceCaps: %u\n", bos->bNumDeviceCaps);
|
||||
|
||||
for (i = 0; i < bos->bNumDeviceCaps; i++)
|
||||
{
|
||||
struct libusb_bos_dev_capability_descriptor* dev_cap = bos->dev_capability[i];
|
||||
|
||||
if (dev_cap->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION)
|
||||
{
|
||||
struct libusb_usb_2_0_extension_descriptor* usb_2_0_extension;
|
||||
|
||||
ret = libusb_get_usb_2_0_extension_descriptor(NULL, dev_cap, &usb_2_0_extension);
|
||||
if (ret < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
print_2_0_ext_cap(usb_2_0_extension);
|
||||
libusb_free_usb_2_0_extension_descriptor(usb_2_0_extension);
|
||||
}
|
||||
else
|
||||
if (dev_cap->bDevCapabilityType == LIBUSB_BT_SS_USB_DEVICE_CAPABILITY)
|
||||
{
|
||||
struct libusb_ss_usb_device_capability_descriptor* ss_dev_cap;
|
||||
|
||||
ret = libusb_get_ss_usb_device_capability_descriptor(NULL, dev_cap, &ss_dev_cap);
|
||||
if (ret < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
print_ss_usb_cap(ss_dev_cap);
|
||||
libusb_free_ss_usb_device_capability_descriptor(ss_dev_cap);
|
||||
}
|
||||
}
|
||||
|
||||
libusb_free_bos_descriptor(bos);
|
||||
}
|
||||
|
||||
static void print_interface(const struct libusb_interface* interface)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < interface->num_altsetting; i++)
|
||||
{
|
||||
print_altsetting(&interface->altsetting[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_configuration(struct libusb_config_descriptor* config)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
printf(" Configuration:\n");
|
||||
printf(" wTotalLength: %u\n", config->wTotalLength);
|
||||
printf(" bNumInterfaces: %u\n", config->bNumInterfaces);
|
||||
printf(" bConfigurationValue: %u\n", config->bConfigurationValue);
|
||||
printf(" iConfiguration: %u\n", config->iConfiguration);
|
||||
printf(" bmAttributes: %02xh\n", config->bmAttributes);
|
||||
printf(" MaxPower: %u\n", config->MaxPower);
|
||||
|
||||
for (i = 0; i < config->bNumInterfaces; i++)
|
||||
{
|
||||
print_interface(&config->interface[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void set_rpms(libusb_device* dev, libusb_device_handle* handle)
|
||||
{
|
||||
|
||||
int rpms = 0;
|
||||
int i = 0;
|
||||
for (i = 1; i<=10; i++)
|
||||
{
|
||||
int rpms = i * 1000;
|
||||
printf("Setting RPM to %i\n", rpms);
|
||||
//sleep(5);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_device(libusb_device* dev, libusb_device_handle* handle)
|
||||
{
|
||||
struct libusb_device_descriptor desc;
|
||||
unsigned char string[256];
|
||||
const char* speed;
|
||||
int ret;
|
||||
uint8_t i;
|
||||
|
||||
switch (libusb_get_device_speed(dev))
|
||||
{
|
||||
case LIBUSB_SPEED_LOW:
|
||||
speed = "1.5M";
|
||||
break;
|
||||
case LIBUSB_SPEED_FULL:
|
||||
speed = "12M";
|
||||
break;
|
||||
case LIBUSB_SPEED_HIGH:
|
||||
speed = "480M";
|
||||
break;
|
||||
case LIBUSB_SPEED_SUPER:
|
||||
speed = "5G";
|
||||
break;
|
||||
case LIBUSB_SPEED_SUPER_PLUS:
|
||||
speed = "10G";
|
||||
break;
|
||||
default:
|
||||
speed = "Unknown";
|
||||
}
|
||||
|
||||
ret = libusb_get_device_descriptor(dev, &desc);
|
||||
if (ret < 0)
|
||||
{
|
||||
fprintf(stderr, "failed to get device descriptor");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Dev (bus %u, device %u): %04X - %04X speed: %s\n",
|
||||
libusb_get_bus_number(dev), libusb_get_device_address(dev),
|
||||
desc.idVendor, desc.idProduct, speed);
|
||||
|
||||
if (!handle)
|
||||
{
|
||||
libusb_open(dev, &handle);
|
||||
}
|
||||
|
||||
if (handle)
|
||||
{
|
||||
if (desc.iManufacturer)
|
||||
{
|
||||
ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
|
||||
if (ret > 0)
|
||||
{
|
||||
printf(" Manufacturer: %s\n", (char*)string);
|
||||
}
|
||||
}
|
||||
|
||||
if (desc.iProduct)
|
||||
{
|
||||
ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
|
||||
if (ret > 0)
|
||||
{
|
||||
printf(" Product: %s\n", (char*)string);
|
||||
}
|
||||
}
|
||||
|
||||
if (desc.iSerialNumber && verbose)
|
||||
{
|
||||
ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
|
||||
if (ret > 0)
|
||||
{
|
||||
printf(" Serial Number: %s\n", (char*)string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
{
|
||||
for (i = 0; i < desc.bNumConfigurations; i++)
|
||||
{
|
||||
struct libusb_config_descriptor* config;
|
||||
|
||||
ret = libusb_get_config_descriptor(dev, i, &config);
|
||||
if (LIBUSB_SUCCESS != ret)
|
||||
{
|
||||
printf(" Couldn't retrieve descriptors\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
print_configuration(config);
|
||||
|
||||
libusb_free_config_descriptor(config);
|
||||
}
|
||||
|
||||
if (handle && desc.bcdUSB >= 0x0201)
|
||||
{
|
||||
print_bos(handle);
|
||||
}
|
||||
}
|
||||
|
||||
if (handle)
|
||||
{
|
||||
libusb_close(handle);
|
||||
}
|
||||
}
|
||||
/*
|
||||
static void print_device(libusb_device *dev, libusb_device_handle *handle)
|
||||
{
|
||||
struct libusb_device_descriptor desc;
|
||||
unsigned char string[256];
|
||||
const char *speed;
|
||||
int ret;
|
||||
uint8_t i;
|
||||
|
||||
switch (libusb_get_device_speed(dev)) {
|
||||
case LIBUSB_SPEED_LOW: speed = "1.5M"; break;
|
||||
case LIBUSB_SPEED_FULL: speed = "12M"; break;
|
||||
case LIBUSB_SPEED_HIGH: speed = "480M"; break;
|
||||
case LIBUSB_SPEED_SUPER: speed = "5G"; break;
|
||||
case LIBUSB_SPEED_SUPER_PLUS: speed = "10G"; break;
|
||||
default: speed = "Unknown";
|
||||
}
|
||||
|
||||
ret = libusb_get_device_descriptor(dev, &desc);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "failed to get device descriptor");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Dev (bus %u, device %u): %04X - %04X speed: %s\n",
|
||||
libusb_get_bus_number(dev), libusb_get_device_address(dev),
|
||||
desc.idVendor, desc.idProduct, speed);
|
||||
|
||||
if (!handle)
|
||||
libusb_open(dev, &handle);
|
||||
|
||||
if (handle) {
|
||||
if (desc.iManufacturer) {
|
||||
ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
|
||||
if (ret > 0)
|
||||
printf(" Manufacturer: %s\n", (char *)string);
|
||||
}
|
||||
|
||||
if (desc.iProduct) {
|
||||
ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
|
||||
if (ret > 0)
|
||||
printf(" Product: %s\n", (char *)string);
|
||||
}
|
||||
|
||||
if (desc.iSerialNumber && verbose) {
|
||||
ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
|
||||
if (ret > 0)
|
||||
printf(" Serial Number: %s\n", (char *)string);
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
for (i = 0; i < desc.bNumConfigurations; i++) {
|
||||
struct libusb_config_descriptor *config;
|
||||
ret = libusb_get_config_descriptor(dev, i, &config);
|
||||
if (LIBUSB_SUCCESS != ret) {
|
||||
printf(" Couldn't retrieve descriptors\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
print_configuration(config);
|
||||
|
||||
libusb_free_config_descriptor(config);
|
||||
}
|
||||
|
||||
if (handle && desc.bcdUSB >= 0x0201)
|
||||
print_bos(handle);
|
||||
}
|
||||
|
||||
if (handle)
|
||||
libusb_close(handle);
|
||||
}
|
||||
*/
|
||||
#ifdef __linux__
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int test_wrapped_device(const char* device_name)
|
||||
{
|
||||
libusb_device_handle* handle;
|
||||
int r, fd;
|
||||
|
||||
fd = open(device_name, O_RDWR);
|
||||
if (fd < 0)
|
||||
{
|
||||
printf("Error could not open %s: %s\n", device_name, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
r = libusb_wrap_sys_device(NULL, fd, &handle);
|
||||
if (r)
|
||||
{
|
||||
printf("Error wrapping device: %s: %s\n", device_name, libusb_strerror(r));
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
print_device(libusb_get_device(handle), handle);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
static int test_wrapped_device(const char* device_name)
|
||||
{
|
||||
(void)device_name;
|
||||
printf("Testing wrapped devices is not supported on your platform\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const char* device_name = NULL;
|
||||
libusb_device** devs;
|
||||
libusb_device* dev;
|
||||
ssize_t cnt;
|
||||
int r, i;
|
||||
|
||||
r = libusb_init(NULL);
|
||||
if (r < 0)
|
||||
{
|
||||
return r;
|
||||
}
|
||||
|
||||
cnt = libusb_get_device_list(NULL, &devs);
|
||||
if (cnt < 0)
|
||||
{
|
||||
libusb_exit(NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
}
|
||||
*/
|
||||
|
||||
int devindx = 0;
|
||||
for (i = 0; i<12; i++)
|
||||
{
|
||||
//int b = print_device(&devs[i], NULL);
|
||||
//if (b == "9823") {
|
||||
devindx = i;
|
||||
//break;
|
||||
//}
|
||||
}
|
||||
i = 0;
|
||||
dev = devs[devindx];
|
||||
|
||||
print_device(dev, NULL);
|
||||
|
||||
int d = 99;
|
||||
libusb_device_handle* handle = NULL;
|
||||
if (!handle)
|
||||
{
|
||||
d=libusb_open(dev, &handle);
|
||||
}
|
||||
|
||||
int len = 0;
|
||||
|
||||
if(libusb_kernel_driver_active(handle, 0) == 1)
|
||||
{
|
||||
d = libusb_detach_kernel_driver(handle, 0);
|
||||
}
|
||||
|
||||
fprintf(stderr,"%i",d);
|
||||
d = libusb_claim_interface(handle, 0);
|
||||
|
||||
fprintf(stderr,"%i",d);
|
||||
|
||||
//int rpm_array[20] = {41574, 48580, 54308, 58612, 60160, 61916, 62347, 62777, 63089, 63243, 63505, 63673, 63778, 63908, 64002, 64084, 64140, 64175, 64224, 64313};
|
||||
//int rpm_array[20] = {41574, 48580, 54308, 58612, 60160, 61416, 61952, 62470, 62969, 63231, 63305, 63673, 63749, 63998, 64002, 64084, 64140, 64175, 64224, 64313};
|
||||
//int rpm_array[10] = {58612, 62112, 63053, 63719, 64044, 64313, 64480, 64622, 64719, 64807};
|
||||
//int rpm_array[10] = {24701, 42244, 54308, 54308, 64255, 64313, 64480, 64622, 64719, 64807};
|
||||
int rpms = 0;
|
||||
int iiii = 189;
|
||||
|
||||
unsigned char bytes[65];
|
||||
|
||||
int rpm_array[21] = {41574, 48580, 54308, 58612, 60160, 61113, 61916, 62347, 62777, 63089, 63243, 63505, 63673, 63778, 63908, 64002, 64084, 64140, 64175, 64224, 64313};
|
||||
for (i = 0; i<21; i++)
|
||||
{
|
||||
int rpms = i * 500;
|
||||
|
||||
printf("Setting RPM to %i hex as decimal %i\n", rpms, rpm_array[i]);
|
||||
//printf("Setting RPM to %i\n", i);
|
||||
|
||||
unsigned char bytes[65];
|
||||
unsigned long nnnnn = rpm_array[i];
|
||||
//unsigned long nnnnn = i;
|
||||
unsigned long xlong = 0;
|
||||
for (int x = 0; x < 64; x++)
|
||||
{
|
||||
bytes[x] = 0x00;
|
||||
}
|
||||
bytes[3] = (nnnnn >> 8) & 0xFF;
|
||||
bytes[2] = nnnnn & 0xFF;
|
||||
|
||||
//bytes[0] = (nnnnn >> 24) & 0xFF;
|
||||
//bytes[1] = (nnnnn >> 16) & 0xFF;
|
||||
//bytes[2] = (nnnnn >> 8) & 0xFF;
|
||||
//bytes[3] = nnnnn & 0xFF;
|
||||
if (handle)
|
||||
{
|
||||
d = libusb_interrupt_transfer(handle, ENDPOINT_OUT, bytes, sizeof(bytes), &len, 0);
|
||||
}
|
||||
|
||||
sleep(4);
|
||||
}
|
||||
|
||||
for (int x = 0; x < 65; x++)
|
||||
{
|
||||
bytes[x] = 0x00;
|
||||
}
|
||||
|
||||
if (handle)
|
||||
{
|
||||
d = libusb_interrupt_transfer(handle, ENDPOINT_OUT, bytes, sizeof(bytes), &len, 0);
|
||||
}
|
||||
|
||||
if (handle)
|
||||
{
|
||||
libusb_close(handle);
|
||||
}
|
||||
|
||||
//set_rpms(dev, NULL);
|
||||
|
||||
libusb_free_device_list(devs, 1);
|
||||
|
||||
libusb_exit(NULL);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Simple program demonstrating shared memory in POSIX systems.
|
||||
*
|
||||
* This is the consumer process
|
||||
*
|
||||
* Figure 3.18
|
||||
*
|
||||
* @author Gagne, Galvin, Silberschatz
|
||||
* Operating System Concepts - Ninth Edition
|
||||
* Copyright John Wiley & Sons - 2013
|
||||
*
|
||||
* modifications by dheller@cse.psu.edu, 31 Jan. 2014
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
void display(char* prog, char* bytes, int n);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char* name = "/shm-example"; // file name
|
||||
const int SIZE = 4096; // file size
|
||||
|
||||
int shm_fd; // file descriptor, from shm_open()
|
||||
char* shm_base; // base address, from mmap()
|
||||
|
||||
/* open the shared memory segment as if it was a file */
|
||||
shm_fd = shm_open(name, O_RDONLY, 0666);
|
||||
if (shm_fd == -1)
|
||||
{
|
||||
printf("cons: Shared memory failed: %s\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* map the shared memory segment to the address space of the process */
|
||||
shm_base = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
|
||||
if (shm_base == MAP_FAILED)
|
||||
{
|
||||
printf("cons: Map failed: %s\n", strerror(errno));
|
||||
// close and unlink?
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* read from the mapped shared memory segment */
|
||||
display("cons", shm_base, 64); // first as bytes, then as a string
|
||||
printf("%s", shm_base);
|
||||
|
||||
/* remove the mapped shared memory segment from the address space of the process */
|
||||
if (munmap(shm_base, SIZE) == -1)
|
||||
{
|
||||
printf("cons: Unmap failed: %s\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* close the shared memory segment as if it was a file */
|
||||
if (close(shm_fd) == -1)
|
||||
{
|
||||
printf("cons: Close failed: %s\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* remove the shared memory segment from the file system */
|
||||
if (shm_unlink(name) == -1)
|
||||
{
|
||||
printf("cons: Error removing %s: %s\n", name, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void display(char* prog, char* bytes, int n)
|
||||
{
|
||||
printf("display: %s\n", prog);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
printf("%02x%c", bytes[i], ((i+1)%16) ? ' ' : '\n');
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
Reference in New Issue
Block a user