Live for Speed - LFS Main Site
- LFS World
- LFS News
- LFS Manual
- LFS Merchandise
  http://www.lfsworld.net/

Go Back   Live for Speed > Main > LFS Programmer Forum

Reply
 
Thread Tools Search this Thread Display Modes
Old 5th June 2010, 02:09   #1  -   
MadCatX
S2 licensed
 
Racername: M4DC4T
Join Date: Nov 2004
Location: Prague (Praha)
Posts: 1,172
MadCatX's online stats
Highly experimental G27 LEDs OutGauge app

So, I finally "did" it. This is very likely not to work and because I don't have G27 wheel I have no way how to try it out myself. Anyway, I attached a VS 2005 solution and an EXE for those of you who'd like to test it or play around with the source (it's C++).
To get it running you have to edit "cfg.txt" file in your LFS directory so that it contains "OutGauge Port 30000" line. App has no GUI whatsoever, just a console output. Warning, it might screw things up, use with care!

BTW, Logitech API seems to require ATL, so if you have VS Express edition, you're probably out of luck. My big thanks goes to DarkTimes, this app is basically his OutGauge example with the Logitech API added.

The Source

EDIT: EXE removed, see the thread to get a working one
__________________
If there are still errors occuring in the system, open a window and enable Free-Fall Hardware Acceleration

Last edited by MadCatX; 20th July 2010 at 20:16.
Reply With Quote
Old 5th June 2010, 06:31   #2  -   
Dygear
S2 licensed
 
Dygear's Avatar
 
Racername: (EAGLE)Dygear
Join Date: Feb 2005
Location: Levittown, NY.
Posts: 2,145
Dygear's online stats
You get major points for sharing the source code! Thanks!
__________________
Reply With Quote
Old 5th June 2010, 06:33   #3  -   
dawesdust_12
S2 licensed
 
dawesdust_12's Avatar
 
Join Date: Feb 2004
Location: Nanaimo, BC
Posts: 7,673
dawesdust_12's online stats
Now to port it for rotation
Reply With Quote
Old 5th June 2010, 12:32   #4  -   
MadCatX
S2 licensed
 
Racername: M4DC4T
Join Date: Nov 2004
Location: Prague (Praha)
Posts: 1,172
MadCatX's online stats
EXE removed - not working. See follow up posts for the working one

I've been toying with this a little more and I think I have the basic concepts right. I attached updated EXE and g27led.cpp as the rest of the source hasn't changed. As for the wheel range adjustment, check out the source, it can be done pretty easily

Code:
// g27led.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <winsock2.h>

#define BUFFER_SIZE 512 // Receive buffer size (basically maximum packet size in UDP).
#define HOST "127.0.0.1" // Host to connect to.
#define PORT 30000 // Port to connect to the host through.

#define FIRST_LEDS_ON 4000.00f
#define REDLINE_LEDS 8000.00f

// Include Logitech headers
#include "LogiWheel.h"
#include "LogiControllerInput.h"

// Define types used by InSim.
typedef unsigned char byte;
typedef unsigned short word;
typedef struct
{
int X, Y, Z;
} Vec;

typedef struct
{
float X, Y, Z;
} Vector;

// Include InSim header.
#include "InSim.h"

using namespace LogitechSteeringWheel;
using namespace LogitechControllerInput;

// Declare variables for the Logitech Wheel
Wheel* g_wheel;
ControllerInput* g_controllerInput;

// Declare RPM variable too
float inRpm = 0;

// Level of debbuging, passed by external argument
// 0 = Default, no verbosity
// 1 = Print what RPM are we getting from OutGauge
// 2 = Confirm that LED data is being sent to wheel
// 3 = Only output RPM info from OutGauge, don't send anything to wheel
int debugLevel = 0;

// More than one Logitech wheel can be connected. We have to take care of all of them.
int wheelIdx = 0;

int initInSimConnection();
void processOutGaugePacket(const OutGaugePack packet);

int _tmain(int argc, char** argv)
{
	if(argc > 1)
	{
		int debugArg = atoi(argv[1]);
		if((debugArg >= 0) && (debugArg < 4))
		{
			debugLevel = debugArg;
		}
	}

	//Bit of a hack, we need the HWND of LFS's window
	CString winTitle = "Live for Speed"; 
	HWND hLfsWin = FindWindow(NULL, winTitle);

	if(hLfsWin == NULL)
	{
		CString errMsg = "Cannot find active LFS instance.\nMake sure LFS is running.";
		MessageBox(NULL, errMsg, NULL, NULL);
		return EXIT_FAILURE;
	}

	//Initialize the wheel input
	g_controllerInput = new ControllerInput(hLfsWin, TRUE);
	g_wheel = new Wheel(g_controllerInput);

	//Get some wheel settings
	
	//This is an example of controlling wheel settings
	//in the same way Logitech Profiler does. Particularly
	//"propsData.wheelRange" can be very useful.
	ControllerPropertiesData propsData;
    ZeroMemory(&propsData, sizeof(propsData));
	g_controllerInput->Update();
	g_wheel->Update();
	for(wheelIdx = 0; wheelIdx < LogitechSteeringWheel::LG_MAX_CONTROLLERS; wheelIdx++)
	{
		g_wheel->GetCurrentControllerProperties(wheelIdx, propsData);
	}
	
	//Some adjustments, I will comment them out as we don't
	//want to adjust anything right now.
	/*propsData.wheelRange = 720;
	propsData.overallGain = 10;
	propsData.forceEnable = 0;
	g_wheel->SetPreferredControllerProperties(propsData);*/

	std::cout << "Init completeted, connecting to LFS" << std::endl;

	//Connect to InSim
	int retState = initInSimConnection();

	return retState;
}

int initInSimConnection()
{

	// Initialise WinSock version 2.2.
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
	{
		WSACleanup();
		std::cerr << "Error: Failed to init WinSock" << std::endl;
		return EXIT_FAILURE;
	}

	// Create UDP socket.
	SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (sock == INVALID_SOCKET)
	{
		WSACleanup();
		std::cerr << "Error: Could not create socket." << std::endl;
		return EXIT_FAILURE;
	}

	// Bind to receive UDP packets.
	sockaddr_in saddr;
	saddr.sin_family = AF_INET;
	saddr.sin_addr.s_addr = inet_addr(HOST);
	saddr.sin_port = htons(PORT);
	if (bind(sock, (sockaddr *)&saddr, sizeof(sockaddr)) == SOCKET_ERROR)
	{
		closesocket(sock);
		WSACleanup();
		std::cerr << "Error: Could not connect to LFS" << std::endl;
		return EXIT_FAILURE;
	}


	std::cout << "Connection established, recieving data" << std::endl;

	// Packet receive loop.
	char recvbuf[BUFFER_SIZE];
	memset(recvbuf, 0, sizeof(recvbuf)); // Set recvbuf to zero.
	int bytes = 0;
	do
	{
		bytes = recv(sock, recvbuf, BUFFER_SIZE, 0);
		if (bytes > 0)
		{
			processOutGaugePacket((OutGaugePack &)recvbuf);
		}
		else if (bytes == 0)
			std::cerr << "Error: Lost connection with LFS" << std::endl;
		else
			std::cerr << "Error: " << WSAGetLastError() << std::endl;
	} while (bytes > 0);

	// Cleanup and exit.
	closesocket(sock);
	WSACleanup();
	delete g_wheel;
	delete g_controllerInput;
	return EXIT_SUCCESS;
}

void processOutGaugePacket(const OutGaugePack packet)
{
	inRpm = packet.RPM;

	if (debugLevel > 0)
	{
		std::cout << "Current RPM is: " << inRpm << std::endl;
	}

	//We are confident enough to send LED data to the wheel
	if(debugLevel < 3)
	{
		g_controllerInput->Update();
		g_wheel->Update();

		for(wheelIdx = 0; wheelIdx < LogitechSteeringWheel::LG_MAX_CONTROLLERS; wheelIdx++)
		{
			if(g_wheel->IsConnected(wheelIdx))
			{
				g_wheel->PlayLeds(wheelIdx, inRpm, FIRST_LEDS_ON, REDLINE_LEDS);

				//Print whether the data has been sent
				if (debugLevel == 2)
				{
					std::cout << "RPM data sent to device ID = " << wheelIdx << std::endl;
				}
			}
		}
	}
}
Attached Files
File Type: txt readme.txt (1.3 KB, 41 views)
__________________
If there are still errors occuring in the system, open a window and enable Free-Fall Hardware Acceleration

Last edited by MadCatX; 5th June 2010 at 22:35. Reason: Added a readme file
Reply With Quote
Old 5th June 2010, 20:26   #5  -   
DarkKostas
S2 licensed
 
Racername: [OPC] DK!
Join Date: Aug 2008
Location: Greece, Athens
Posts: 39
DarkKostas's online stats
When im trying to run the .exe im getting an error something about parameters.

What will happen if you change the
"int _tmain(int argc, char** argv)"
to
"int main(int argc, char *argv[])"
or just
"int main()"

Last edited by DarkKostas; 5th June 2010 at 20:34.
Reply With Quote
Old 5th June 2010, 20:41   #6  -   
MadCatX
S2 licensed
 
Racername: M4DC4T
Join Date: Nov 2004
Location: Prague (Praha)
Posts: 1,172
MadCatX's online stats
Can you be more specific about the error or post a screenshot? "char** argv" and "char* argv[]" should be equal. Can you run the program from console with command like "g27led.exe 0"? Do you have VS 2005 libraries installed?
__________________
If there are still errors occuring in the system, open a window and enable Free-Fall Hardware Acceleration
Reply With Quote
Reply

  Live for Speed > Main > LFS Programmer Forum

  • Submit Thread to Digg
  • Submit Thread to del.icio.us
  • Submit Thread to StumbleUpon
  • Submit Thread to Google

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 17:32.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Live for Speed © 2002-2009 - Scawen Roberts, Eric Bailey, Victor van Vlaardingen