Advertisement
WacKEDmaN

irplugin

Sep 17th, 2013
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.45 KB | None | 0 0
  1. // irplugin.cpp : Defines the entry point for the console application.
  2. //
  3. // an example iracing plug applcation displaying drivers nam, number and car
  4. // and real time rpm and gear.
  5. //
  6. // provided as is and has been tested to compile correctly on my machine.
  7. // no warranty or otherwise esponsablilty implied.
  8. //
  9. // some sections of code are from community and iracing staff feedback and are referenced
  10. // throughout where appropriate.
  11. //
  12. // ive added an additional header (irplugin.h) where i stick all my own functions as a habit
  13. // and to neaten up the code to save from twalling lines for debugging issues.
  14. //
  15. // p.s i havne gone through and update the c string to c++ string declarations, but they work ok.
  16. //
  17. // have fun! :-)
  18. //
  19. #define _USE_MATH_DEFINES
  20. #include "stdafx.h"
  21.  
  22. //bring in some usual headers for various tasks////
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <conio.h>
  26. #include <time.h>
  27. #include <string>
  28. #include <signal.h>
  29. // added for filestream
  30. #include <iostream>
  31. #include <fstream>
  32. #include <cmath>
  33. #include <sstream>
  34. #include <iomanip>
  35.  
  36. //bring in the irsdk headers
  37. #include "irsdk_defines.h"  //irsdk
  38. #include "yaml_parser.h"    //irsdk
  39.  
  40. // set stanard namespace
  41. using namespace std;
  42.  
  43. // 16 ms timeout
  44. #define TIMEOUT 16
  45.  
  46. 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.
  47. std::string ledstr = "0img.jpg";
  48. std::string gearstr = "N";
  49. std::string driver;
  50. std::string number;
  51. std::string trackname;
  52. std::string cartype;
  53.  
  54. char *data = NULL;
  55. int nData = 0;
  56.  
  57. /////////////////////////////////////////////////////////
  58. #include "irplugin.h"  //imports application specific custom functions
  59. ////////////////////////////////////////////////////////
  60.  
  61.  
  62. int _tmain(int argc, _TCHAR* argv[])
  63. {
  64.  
  65.     //inital application startup.
  66.  
  67.     ofstream dataFile; //create file stream for data.txt
  68.  
  69.     system("cls"); //clear console
  70.     printf("%s\n\n", strAppVers);
  71.     printf("Press CTRL-C to exit.\n");
  72.  
  73.     // trap ctrl-c
  74.     signal(SIGINT, ex_program);
  75.        
  76.     // bump priority up so we get time from the sim
  77.     SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  78.  
  79.     // ask for 1ms timer so sleeps are more precise
  80.     timeBeginPeriod(1);
  81.  
  82.     printf("Waiting for session to start..\n");
  83.  
  84.     while(true)
  85.     {
  86.         if(irsdk_waitForDataReady(TIMEOUT, data))
  87.         {
  88.             const irsdk_header *pHeader = irsdk_getHeader();
  89.             if(pHeader)
  90.             {
  91.                 // if header changes size, assume a new connection
  92.                 if(!data || nData != pHeader->bufLen)
  93.                 {
  94.                     if(data)
  95.                         delete [] data;
  96.                     nData = pHeader->bufLen;
  97.                     data = new char[nData];
  98.  
  99.                     ///////////////////////////
  100.                     system("cls");
  101.                     printf("%s\n\n", strAppVers);
  102.                     printf("Press CTRL-C to exit.\n");
  103.                     printf("connected to sim...\n\n");
  104.                     // process header here///////
  105.                     // this is where you access and display your session info, like track, driver, etc.
  106.                     // these are the 'sessioninfo' and 'weekendinfo' sections.
  107.                     //
  108.  
  109.                        
  110.                         dataFile.open("idata.csv");
  111.                         dataFile.seekp(0);
  112.  
  113.  
  114.                     //////////////////////////////////////////
  115.                     const char *valuestr;
  116.                     int valuelen;
  117.                     if(parseYaml(irsdk_getSessionInfoStr(), "WeekendInfo:TrackName:" , &valuestr, &valuelen)){
  118.                         printf("Track: %.*s\n",valuelen,valuestr);
  119.                         trackname = valuestr;
  120.                        
  121.  
  122.                         }else{
  123.                             printf("not found\n");
  124.                     }
  125.                     // note: printf: .* = The precision is not specified in the format string,
  126.                     // but as an additional integer value argument preceding the argument that has to be formatted.
  127.                     // this code Thanks to Peter holt.
  128.                     //////////////////////////////////////////
  129.  
  130.                     //////////////////////////////GET LOADED CAR AND DRIVER///////////////////////////////////
  131.                     ////thx to Dave Tucker for this section
  132.                     const char *valstr;
  133.                     int valstrlen;
  134.                     char str[512];
  135.                     //
  136.                     int carIdx = -1;
  137.                     char nameStr[512];
  138.                     char pathStr[512];
  139.                     char numbStr[512];
  140.                     //
  141.                     // get the playerCarIdx
  142.                     if(parseYaml(irsdk_getSessionInfoStr(), "DriverInfo:DriverCarIdx:", &valstr, &valstrlen))
  143.                         carIdx = atoi(valstr);
  144.                     ////
  145.                     if(carIdx >= 0)
  146.                     {
  147.                         //////////////////////////////////
  148.                         // get drivers name
  149.                         sprintf_s(str, 512, "DriverInfo:Drivers:CarIdx:{%d}UserName:", carIdx);
  150.                         if(parseYaml(irsdk_getSessionInfoStr(), str, &valstr, &valstrlen))
  151.                         {
  152.                             strncpy_s(nameStr, 512, valstr, valstrlen);
  153.                             nameStr[valstrlen] = '\0'; //driversname
  154.                         }//
  155.                         //////////////////////////////////
  156.                         // get drivers car path
  157.                         sprintf_s(str, 512, "DriverInfo:Drivers:CarIdx:{%d}CarPath:", carIdx);
  158.                         if(parseYaml(irsdk_getSessionInfoStr(), str, &valstr, &valstrlen))
  159.                         {
  160.                             strncpy_s(pathStr, 512, valstr, valstrlen);
  161.                             pathStr[valstrlen] = '\0'; //drivers car
  162.                         }//
  163.                         //////////////////////////////////
  164.                         // get drivers car number
  165.                         sprintf_s(str, 512, "DriverInfo:Drivers:CarIdx:{%d}CarNumber:", carIdx);
  166.                         if(parseYaml(irsdk_getSessionInfoStr(), str, &valstr, &valstrlen))
  167.                         {
  168.                             strncpy_s(numbStr, 512, valstr, valstrlen);
  169.                             numbStr[valstrlen] = '\0'; //drivers number
  170.                         }//
  171.                         //////////////////////////////////
  172.                     }////
  173.                     //////////////////////////////GET LOADED CAR AND DRIVER///////////////////////////////////
  174.  
  175.                     printf ( "Driver: %s %s %s \n\n", numbStr ,nameStr, pathStr );
  176.                         number = numbStr;
  177.                         driver = nameStr;
  178.                         cartype = pathStr;
  179.                     //////////////////////////////GET LOADED CAR AND DRIVER///////////////////////////////////
  180.                     //where
  181.                     // numbstr = drivers car number
  182.                     // namestr = drivers text full name
  183.                     // namestr = car/chassis in use by driver.
  184.  
  185.                     //
  186.                     //
  187.                     /////////////////////////////end process headers here
  188.                 }
  189.                 else if(data)
  190.                 {
  191.                                         // process data here///////////
  192.                     // this is where you access and work with the telemitry vars, in 1/60th itterations.
  193.                     // see 'irsdk 1_0 data vars.csv' for specific ones your after.
  194.                     //
  195.                     ////////////////////////////////////
  196.                     // fetch current gear value
  197.                     const char g_gearoffset[] = "Gear";
  198.                     int gearoffset = irsdk_varNameToOffset(g_gearoffset);              
  199.                     int CurrentGear = * ((int *)(data + gearoffset));
  200.                     if (CurrentGear == 0)
  201.                     {
  202.                         gearstr = "N";
  203.                     }
  204.                     else if (CurrentGear == -1)
  205.                     {
  206.                         gearstr = "R";
  207.                     }
  208.                     else
  209.                     {
  210.                         std::stringstream out;
  211.                         out << CurrentGear;
  212.                         gearstr = out.str();
  213.                     }
  214.                     /////////////////////////////////////
  215.                     //
  216.                     ////////////////////////////////////
  217.                     // fetch current RPM value
  218.                     const char g_RPMoffset[] = "RPM";
  219.                     int RPMoffset = irsdk_varNameToOffset(g_RPMoffset);            
  220.                     float engRPM = * ((float *)(data + RPMoffset));
  221.                     /////////////////////////////////////
  222.                     //
  223.                     /////////////////////////////////////
  224.                     // fetch current Speed value
  225.                     const char g_speedoffset[] = "Speed";
  226.                     int speedoffset = irsdk_varNameToOffset(g_speedoffset);            
  227.                     float speed = * ((float *)(data + speedoffset));
  228.                     float kph = speed * 3.6; //convert m/s to km/h
  229.                     float mph = kph * 5 / 8; //convert km/h to mi/h
  230.                     /////////////////////////////////////
  231.                     //
  232.                     /////////////////////////////////////
  233.                     // fetch current fuellevel value
  234.                     const char g_fuelloffset[] = "FuelLevel";
  235.                     int fuelloffset = irsdk_varNameToOffset(g_fuelloffset);            
  236.                     float fuell = * ((float *)(data + fuelloffset));
  237.                     double gal = fuell * 0.264;// convert lts to gal
  238.                     /////////////////////////////////////
  239.                     //
  240.                     /////////////////////////////////////
  241.                     // fetch current Throttle value
  242.                     const char g_throttleoffset[] = "Throttle";
  243.                     int throttleoffset = irsdk_varNameToOffset(g_throttleoffset);              
  244.                     float throttle = * ((float *)(data + throttleoffset));
  245.                     int throttlep = throttle * 100;
  246.                     /////////////////////////////////////
  247.                     //
  248.                     /////////////////////////////////////
  249.                     // fetch current Brake value
  250.                     const char g_brakeoffset[] = "Brake";
  251.                     int brakeoffset = irsdk_varNameToOffset(g_brakeoffset);            
  252.                     float brake = * ((float *)(data + brakeoffset));
  253.                     int brakep = brake * 100;
  254.                     /////////////////////////////////////
  255.                     //
  256.                     /////////////////////////////////////
  257.                     // fetch current Clutch value
  258.                     const char g_clutchoffset[] = "Clutch";
  259.                     int clutchoffset = irsdk_varNameToOffset(g_clutchoffset);              
  260.                     float clutch = * ((float *)(data + clutchoffset));
  261.                     int clutchp = clutch * 100;
  262.                     /////////////////////////////////////
  263.                     //
  264.                     ////////////////////////////////////
  265.                     // fetch current ShiftIndicatorPct value (0.0-1.0)
  266.                     const char g_ShiftPCToffset[] = "ShiftIndicatorPct";
  267.                     int ShiftPCToffset = irsdk_varNameToOffset(g_ShiftPCToffset);              
  268.                     float ShiftPCT = * ((float *)(data + ShiftPCToffset));
  269.                     int Shiftp = ShiftPCT * 100;
  270.                         ////// Rev LEDs to string...ledstr..
  271.                         if (Shiftp > 80)
  272.                         {
  273.                             ledstr = "80img.jpg";
  274.                         }
  275.                         else if (Shiftp > 60)
  276.                         {
  277.                             ledstr = "60img.jpg";
  278.                         }
  279.                         else if (Shiftp > 40)
  280.                         {
  281.                             ledstr ="40img.jpg";
  282.                         }
  283.                         else if (Shiftp > 20)
  284.                         {
  285.                             ledstr = "20img.jpg";
  286.                         }
  287.                         else // Shiftp < 20..no leds on..
  288.                         {
  289.                             ledstr= "0img.jpg";
  290.                         }
  291.                     /////////////////////////////////////
  292.                     //
  293.                     ////////////////////////////////////
  294.                     // get Steering Wheel Angle
  295.                     const char g_swaoffset[] = "SteeringWheelAngle";
  296.                     int swaoffset = irsdk_varNameToOffset(g_swaoffset);            
  297.                     float swr = * ((float *)(data + swaoffset)); // in radians
  298.                     float swa = swr * 180 / M_PI;
  299.                     /////////////////////////////////////
  300.                     //
  301.                     ////////////////////////////////////
  302.                     // get lap count
  303.                     const char g_lapsoffset[] = "Lap";
  304.                     int lapsoffset = irsdk_varNameToOffset(g_lapsoffset);              
  305.                     int laps = * ((float *)(data + lapsoffset));
  306.                     /////////////////////////////////////
  307.                     //
  308.                     ////////////////////////////////////
  309.                     // fetch current lap distance %
  310.                     const char g_lapdistPCToffset[] = "LapDistPct";
  311.                     int lapdistPCToffset = irsdk_varNameToOffset(g_lapdistPCToffset);              
  312.                     float lapdistPCT = * ((float *)(data + lapdistPCToffset));
  313.                     int lapdistp = lapdistPCT * 100;
  314.                     /////////////////////////////////////
  315.                     //
  316.                     ////////////////////////////////////
  317.                     // fetch current Engine Voltage
  318.                     const char g_voltsoffset[] = "Voltage";
  319.                     int voltsoffset = irsdk_varNameToOffset(g_voltsoffset);            
  320.                     float volts = * ((float *)(data + voltsoffset));
  321.                     /////////////////////////////////////
  322.                     //
  323.                     ////////////////////////////////////
  324.                     // get water level and temp
  325.                     const char g_wleveloffset[] = "WaterLevel";
  326.                     const char g_wtempoffset[] = "WaterTemp";
  327.                     int wleveloffset = irsdk_varNameToOffset(g_wleveloffset);
  328.                     int wtempoffset = irsdk_varNameToOffset(g_wtempoffset);
  329.                     float wlevel = * ((float *)(data + wleveloffset));
  330.                     float wtemp = * ((float *)(data + wtempoffset));
  331.                     /////////////////////////////////////
  332.                     //
  333.                     ////////////////////////////////////
  334.                     // get oil level temp and pressure
  335.                     const char g_oilloffset[] = "OilLevel";
  336.                     const char g_oiltoffset[] = "OilTemp";
  337.                     const char g_oilpoffset[] = "OilPress";
  338.                     int oilloffset = irsdk_varNameToOffset(g_oilloffset);
  339.                     int oiltoffset = irsdk_varNameToOffset(g_oiltoffset);
  340.                     int oilpoffset = irsdk_varNameToOffset(g_oilpoffset);
  341.                     float oill = * ((float *)(data + oilloffset));
  342.                     float oilt = * ((float *)(data + oiltoffset));
  343.                     float oilp = * ((float *)(data + oilpoffset));
  344.                     /////////////////////////////////////
  345.                     //
  346.  
  347.                     ////////////////////////////////////
  348.                     // is car on track..
  349.                     const char g_ontrackoffset[] = "IsOnTrack";
  350.                     int ontrackoffset = irsdk_varNameToOffset(g_ontrackoffset);            
  351.                     bool ontrack = * ((float *)(data + ontrackoffset));
  352.                     /////////////////////////////////////
  353.  
  354.                     //
  355.                     if (ontrack)
  356.                     {
  357.                     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
  358.                    
  359.                     dataFile    << number << ","
  360.                                 << driver << ","
  361.                                 << cartype << ","
  362.                                 << int (engRPM) << ","
  363.                                 << CurrentGear << ","
  364.                                 << int(kph) << ","
  365.                                 << int(mph) << ","
  366.                                 << int(fuell) << ","
  367.                                 << int(gal) << ","
  368.                                 << throttlep << ","
  369.                                 << Shiftp << ","
  370.                                 << brakep << ","
  371.                                 << clutchp << ","
  372.                                 << swa << ","
  373.                                 << laps << ","
  374.                                 << lapdistp << ","
  375.                                 << volts << ","
  376.                                 << wlevel << ","
  377.                                 << wtemp << ","
  378.                                 << oilp << ","
  379.                                 << oilt << ","
  380.                                 << oill << ","
  381.                                 << "eof" << endl;
  382.                     dataFile.seekp(0);
  383.                     //dataFile.close();
  384.  
  385.                     }
  386.                     //
  387.                     //
  388.                     //printf("RPM Current: %d Gear: %d \r", int (engRPM), CurrentGear); //current rpm and gear
  389.                     //
  390.  
  391.                     //
  392.                     //
  393.                     /////////////////////////////// end pocess data here
  394.                 }
  395.             }//no data being recieved.
  396.         }
  397.         //optional test, in case you need to close out a file...
  398.         else if(!irsdk_isConnected())
  399.         {
  400.             // session ended
  401.             if(data)
  402.                 delete[] data;
  403.             data = NULL;
  404.             dataFile.close();          
  405.         }
  406.     }
  407.  
  408.     // call on exit to close memory mapped file
  409.     irsdk_shutdown();
  410.  
  411.     return 0;
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement