// irplugin.cpp : Defines the entry point for the console application. // // an example iracing plug applcation displaying drivers nam, number and car // and real time rpm and gear. // // provided as is and has been tested to compile correctly on my machine. // no warranty or otherwise esponsablilty implied. // // some sections of code are from community and iracing staff feedback and are referenced // throughout where appropriate. // // ive added an additional header (irplugin.h) where i stick all my own functions as a habit // and to neaten up the code to save from twalling lines for debugging issues. // // p.s i havne gone through and update the c string to c++ string declarations, but they work ok. // // have fun! :-) // #define _USE_MATH_DEFINES #include "stdafx.h" //bring in some usual headers for various tasks//// #include #include #include #include #include #include // added for filestream #include #include #include #include #include //bring in the irsdk headers #include "irsdk_defines.h" //irsdk #include "yaml_parser.h" //irsdk // set stanard namespace using namespace std; // 16 ms timeout #define TIMEOUT 16 std::string strAppVers = "App Version 0.4 - Console and HTML file output!\n"; //defines a simple string for output as your app version, and newline. std::string ledstr = "0img.jpg"; std::string gearstr = "N"; std::string driver; std::string number; std::string trackname; std::string cartype; char *data = NULL; int nData = 0; ///////////////////////////////////////////////////////// #include "irplugin.h" //imports application specific custom functions //////////////////////////////////////////////////////// int _tmain(int argc, _TCHAR* argv[]) { //inital application startup. ofstream dataFile; //create file stream for data.txt system("cls"); //clear console printf("%s\n\n", strAppVers); printf("Press CTRL-C to exit.\n"); // trap ctrl-c signal(SIGINT, ex_program); // bump priority up so we get time from the sim SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); // ask for 1ms timer so sleeps are more precise timeBeginPeriod(1); printf("Waiting for session to start..\n"); while(true) { if(irsdk_waitForDataReady(TIMEOUT, data)) { const irsdk_header *pHeader = irsdk_getHeader(); if(pHeader) { // if header changes size, assume a new connection if(!data || nData != pHeader->bufLen) { if(data) delete [] data; nData = pHeader->bufLen; data = new char[nData]; /////////////////////////// system("cls"); printf("%s\n\n", strAppVers); printf("Press CTRL-C to exit.\n"); printf("connected to sim...\n\n"); // process header here/////// // this is where you access and display your session info, like track, driver, etc. // these are the 'sessioninfo' and 'weekendinfo' sections. // dataFile.open("idata.csv"); dataFile.seekp(0); ////////////////////////////////////////// const char *valuestr; int valuelen; if(parseYaml(irsdk_getSessionInfoStr(), "WeekendInfo:TrackName:" , &valuestr, &valuelen)){ printf("Track: %.*s\n",valuelen,valuestr); trackname = valuestr; }else{ printf("not found\n"); } // note: printf: .* = The precision is not specified in the format string, // but as an additional integer value argument preceding the argument that has to be formatted. // this code Thanks to Peter holt. ////////////////////////////////////////// //////////////////////////////GET LOADED CAR AND DRIVER/////////////////////////////////// ////thx to Dave Tucker for this section const char *valstr; int valstrlen; char str[512]; // int carIdx = -1; char nameStr[512]; char pathStr[512]; char numbStr[512]; // // get the playerCarIdx if(parseYaml(irsdk_getSessionInfoStr(), "DriverInfo:DriverCarIdx:", &valstr, &valstrlen)) carIdx = atoi(valstr); //// if(carIdx >= 0) { ////////////////////////////////// // get drivers name sprintf_s(str, 512, "DriverInfo:Drivers:CarIdx:{%d}UserName:", carIdx); if(parseYaml(irsdk_getSessionInfoStr(), str, &valstr, &valstrlen)) { strncpy_s(nameStr, 512, valstr, valstrlen); nameStr[valstrlen] = '\0'; //driversname }// ////////////////////////////////// // get drivers car path sprintf_s(str, 512, "DriverInfo:Drivers:CarIdx:{%d}CarPath:", carIdx); if(parseYaml(irsdk_getSessionInfoStr(), str, &valstr, &valstrlen)) { strncpy_s(pathStr, 512, valstr, valstrlen); pathStr[valstrlen] = '\0'; //drivers car }// ////////////////////////////////// // get drivers car number sprintf_s(str, 512, "DriverInfo:Drivers:CarIdx:{%d}CarNumber:", carIdx); if(parseYaml(irsdk_getSessionInfoStr(), str, &valstr, &valstrlen)) { strncpy_s(numbStr, 512, valstr, valstrlen); numbStr[valstrlen] = '\0'; //drivers number }// ////////////////////////////////// }//// //////////////////////////////GET LOADED CAR AND DRIVER/////////////////////////////////// printf ( "Driver: %s %s %s \n\n", numbStr ,nameStr, pathStr ); number = numbStr; driver = nameStr; cartype = pathStr; //////////////////////////////GET LOADED CAR AND DRIVER/////////////////////////////////// //where // numbstr = drivers car number // namestr = drivers text full name // namestr = car/chassis in use by driver. // // /////////////////////////////end process headers here } else if(data) { // process data here/////////// // this is where you access and work with the telemitry vars, in 1/60th itterations. // see 'irsdk 1_0 data vars.csv' for specific ones your after. // //////////////////////////////////// // fetch current gear value const char g_gearoffset[] = "Gear"; int gearoffset = irsdk_varNameToOffset(g_gearoffset); int CurrentGear = * ((int *)(data + gearoffset)); if (CurrentGear == 0) { gearstr = "N"; } else if (CurrentGear == -1) { gearstr = "R"; } else { std::stringstream out; out << CurrentGear; gearstr = out.str(); } ///////////////////////////////////// // //////////////////////////////////// // fetch current RPM value const char g_RPMoffset[] = "RPM"; int RPMoffset = irsdk_varNameToOffset(g_RPMoffset); float engRPM = * ((float *)(data + RPMoffset)); ///////////////////////////////////// // ///////////////////////////////////// // fetch current Speed value const char g_speedoffset[] = "Speed"; int speedoffset = irsdk_varNameToOffset(g_speedoffset); float speed = * ((float *)(data + speedoffset)); float kph = speed * 3.6; //convert m/s to km/h float mph = kph * 5 / 8; //convert km/h to mi/h ///////////////////////////////////// // ///////////////////////////////////// // fetch current fuellevel value const char g_fuelloffset[] = "FuelLevel"; int fuelloffset = irsdk_varNameToOffset(g_fuelloffset); float fuell = * ((float *)(data + fuelloffset)); double gal = fuell * 0.264;// convert lts to gal ///////////////////////////////////// // ///////////////////////////////////// // fetch current Throttle value const char g_throttleoffset[] = "Throttle"; int throttleoffset = irsdk_varNameToOffset(g_throttleoffset); float throttle = * ((float *)(data + throttleoffset)); int throttlep = throttle * 100; ///////////////////////////////////// // ///////////////////////////////////// // fetch current Brake value const char g_brakeoffset[] = "Brake"; int brakeoffset = irsdk_varNameToOffset(g_brakeoffset); float brake = * ((float *)(data + brakeoffset)); int brakep = brake * 100; ///////////////////////////////////// // ///////////////////////////////////// // fetch current Clutch value const char g_clutchoffset[] = "Clutch"; int clutchoffset = irsdk_varNameToOffset(g_clutchoffset); float clutch = * ((float *)(data + clutchoffset)); int clutchp = clutch * 100; ///////////////////////////////////// // //////////////////////////////////// // fetch current ShiftIndicatorPct value (0.0-1.0) const char g_ShiftPCToffset[] = "ShiftIndicatorPct"; int ShiftPCToffset = irsdk_varNameToOffset(g_ShiftPCToffset); float ShiftPCT = * ((float *)(data + ShiftPCToffset)); int Shiftp = ShiftPCT * 100; ////// Rev LEDs to string...ledstr.. if (Shiftp > 80) { ledstr = "80img.jpg"; } else if (Shiftp > 60) { ledstr = "60img.jpg"; } else if (Shiftp > 40) { ledstr ="40img.jpg"; } else if (Shiftp > 20) { ledstr = "20img.jpg"; } else // Shiftp < 20..no leds on.. { ledstr= "0img.jpg"; } ///////////////////////////////////// // //////////////////////////////////// // get Steering Wheel Angle const char g_swaoffset[] = "SteeringWheelAngle"; int swaoffset = irsdk_varNameToOffset(g_swaoffset); float swr = * ((float *)(data + swaoffset)); // in radians float swa = swr * 180 / M_PI; ///////////////////////////////////// // //////////////////////////////////// // get lap count const char g_lapsoffset[] = "Lap"; int lapsoffset = irsdk_varNameToOffset(g_lapsoffset); int laps = * ((float *)(data + lapsoffset)); ///////////////////////////////////// // //////////////////////////////////// // fetch current lap distance % const char g_lapdistPCToffset[] = "LapDistPct"; int lapdistPCToffset = irsdk_varNameToOffset(g_lapdistPCToffset); float lapdistPCT = * ((float *)(data + lapdistPCToffset)); int lapdistp = lapdistPCT * 100; ///////////////////////////////////// // //////////////////////////////////// // fetch current Engine Voltage const char g_voltsoffset[] = "Voltage"; int voltsoffset = irsdk_varNameToOffset(g_voltsoffset); float volts = * ((float *)(data + voltsoffset)); ///////////////////////////////////// // //////////////////////////////////// // get water level and temp const char g_wleveloffset[] = "WaterLevel"; const char g_wtempoffset[] = "WaterTemp"; int wleveloffset = irsdk_varNameToOffset(g_wleveloffset); int wtempoffset = irsdk_varNameToOffset(g_wtempoffset); float wlevel = * ((float *)(data + wleveloffset)); float wtemp = * ((float *)(data + wtempoffset)); ///////////////////////////////////// // //////////////////////////////////// // get oil level temp and pressure const char g_oilloffset[] = "OilLevel"; const char g_oiltoffset[] = "OilTemp"; const char g_oilpoffset[] = "OilPress"; int oilloffset = irsdk_varNameToOffset(g_oilloffset); int oiltoffset = irsdk_varNameToOffset(g_oiltoffset); int oilpoffset = irsdk_varNameToOffset(g_oilpoffset); float oill = * ((float *)(data + oilloffset)); float oilt = * ((float *)(data + oiltoffset)); float oilp = * ((float *)(data + oilpoffset)); ///////////////////////////////////// // //////////////////////////////////// // is car on track.. const char g_ontrackoffset[] = "IsOnTrack"; int ontrackoffset = irsdk_varNameToOffset(g_ontrackoffset); bool ontrack = * ((float *)(data + ontrackoffset)); ///////////////////////////////////// // if (ontrack) { printf("RPM:%d Gear:%d Speed:%dkph/%dmph Fuel:%dlts/%dgal Throttle:%d Shift:%d \r", int (engRPM), CurrentGear, int(kph), int(mph), int(fuell), int(gal), throttlep, Shiftp); //current rpm and gear dataFile << number << "," << driver << "," << cartype << "," << int (engRPM) << "," << CurrentGear << "," << int(kph) << "," << int(mph) << "," << int(fuell) << "," << int(gal) << "," << throttlep << "," << Shiftp << "," << brakep << "," << clutchp << "," << swa << "," << laps << "," << lapdistp << "," << volts << "," << wlevel << "," << wtemp << "," << oilp << "," << oilt << "," << oill << "," << "eof" << endl; dataFile.seekp(0); //dataFile.close(); } // // //printf("RPM Current: %d Gear: %d \r", int (engRPM), CurrentGear); //current rpm and gear // // // /////////////////////////////// end pocess data here } }//no data being recieved. } //optional test, in case you need to close out a file... else if(!irsdk_isConnected()) { // session ended if(data) delete[] data; data = NULL; dataFile.close(); } } // call on exit to close memory mapped file irsdk_shutdown(); return 0; }