Advertisement
j0h

esand

j0h
Sep 8th, 2023
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.26 KB | None | 0 0
  1. //ESand somewhere between working and having features
  2. #include <SDL/SDL.h>
  3. #include <libfreenect.h>
  4. #include <libfreenect_sync.h>
  5. #include <stdio.h>
  6. #include <unistd.h>  //tcgetattr and sleep
  7. #include <iostream>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <string>
  11.  
  12. //g++ -g -Wall -o "ppplotKinect" "ppplotKinect.cpp"  -lSDL -lSDL_ttf -lSDL_mixer -lSDL_image   -lfreenect -lfreenect_sync
  13. SDL_Surface* Backbuffer = NULL;
  14.  /*
  15.   * Config file: all integers for now, file: pplot.conf
  16.   * initX,Y: relate to bounding area of the sensor. if we dont need to read more data points, dont.
  17.   * pixelation reffers to limiting resolution by skipping pixels, which results in pixelation.
  18.   * pixelation, brightness, minDepth, maxDepth, depthScalling.
  19.   * pxX,pxY,brightness,minD, maxD, DScale, initX1,initX2, initY1, initY2
  20.   * */
  21. freenect_context *f_ctx;
  22. freenect_device *f_dev;
  23. #define WIDTH 640
  24. #define HEIGHT 480
  25.  
  26.     //sysConfig vars
  27.     int pxX=0;
  28.     int pxY=0;
  29.     int brightness=30;
  30.     int minD=0;
  31.     int maxD=2047;
  32.     int DScale=1;
  33.     int initX1=0;
  34.     int initY1=0;
  35.     int initX2=640;
  36.     int initY2=480;
  37.     int accelThreshX=0;
  38.     int accelThreshY=0;
  39.     int accelThreshZ=0;
  40.    
  41.  
  42. using namespace std;
  43.      int die = 0;
  44.  
  45. int kInit();                 //initiallize / test kinect library and device connection
  46. void kClose();         //close the kinect device
  47. void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp);
  48. void no_kinect_quit(void);    
  49. bool ProgramIsRunning();
  50. void DrawPixel(SDL_Surface *surface, int x, int y , unsigned int depth_value);
  51. void IR(freenect_device *dev, uint16_t brightness);
  52. int getCFG(string configFile);
  53.  
  54. int main(int argc, char* args[]){
  55.     //read config file, and assign variables, unless the config file isnt pressent.
  56.         string configFile="pplot.cfg";
  57. getCFG(configFile);     //get or reset config variables from file or use system
  58.  
  59.     if(SDL_Init(SDL_INIT_EVERYTHING) < 0){
  60.         printf("Failed to initialize SDL!\n");
  61.         return 0;
  62.     }
  63.     kInit();  //init kinect lib, and device
  64.         int ire = freenect_get_ir_brightness(f_dev);
  65.         printf("%d brightness\n", ire);
  66.     //freenect_start_ir(f_dev);
  67.     IR(f_dev,30);  //brightness 1 to 50 may effect resolution depending on conditions.
  68.  
  69.     freenect_set_depth_callback(f_dev, depth_cb);
  70.     // Start the depth stream
  71.     freenect_start_depth(f_dev);
  72.     Backbuffer = SDL_SetVideoMode(640 ,480, 32, SDL_SWSURFACE);
  73.     SDL_WM_SetCaption("Ghost Data", NULL);
  74.     while(ProgramIsRunning())    {
  75.                 if (freenect_process_events(f_ctx) < 0){
  76.             break;
  77.         }
  78.     }
  79.    SDL_Quit();
  80.     kClose();
  81.     return 0;
  82. }
  83.  
  84. void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp){
  85.     // Cast the depth data to unsigned short (16-bit) xbox uses 11 bits of depth
  86.     uint16_t *depth_data = (uint16_t *)depth;
  87.  
  88. unsigned int depth_value =0;
  89.     int x = 0;
  90.     int y = 0;
  91.     int index = 0; //index 0 to 307840
  92.  
  93. for (x=0; x<=WIDTH; x+=pxX){   //pxX cant B 3 I guess
  94.      index = y*WIDTH+x;  
  95.     depth_value = depth_data[index];  
  96.     DrawPixel(Backbuffer, x,y, depth_value);
  97.  
  98.     if(x>=WIDTH){
  99.         if(y>=HEIGHT){
  100.         x=0;
  101.         //y+=pxY;
  102.         y=0;
  103.         SDL_Flip(Backbuffer);  
  104.         return;
  105.         }
  106.         y+=pxY;  //goto next line: pxY
  107.         x=0;
  108.         }    
  109.     }
  110. }
  111. bool ProgramIsRunning(){
  112.     SDL_Event event;
  113.     int brightness = freenect_get_ir_brightness(f_dev);
  114.  
  115.     bool running = true;
  116.     while(SDL_PollEvent(&event))   {
  117.         if(event.type == SDL_QUIT)
  118.             running = false;
  119.     }
  120.     if(event.type==SDL_KEYDOWN){
  121.         switch(event.key.keysym.sym){
  122.             case SDLK_ESCAPE :
  123.                     running=false;
  124.                     break;
  125.                    
  126.             case SDLK_a :
  127.             //get brightness add 1
  128.             brightness = freenect_get_ir_brightness(f_dev);
  129.             freenect_set_ir_brightness(f_dev, brightness+1);
  130.             printf("brightness: %d \n", brightness);
  131.             if (brightness >=50){
  132.                 brightness=50;
  133.                 break;
  134.                 }
  135.             break;
  136.             case SDLK_d :
  137.             //get brightness sub 1
  138.             brightness = freenect_get_ir_brightness(f_dev);
  139.             freenect_set_ir_brightness(f_dev, brightness-1);
  140.             printf("brightness: %d \n", brightness);
  141.             if (brightness <=1){
  142.                 brightness=1;
  143.                 break;
  144.                 }
  145.             break;
  146.                
  147.              default :
  148.                     break;         
  149.             }
  150.         }
  151.  
  152.     return running;
  153. }
  154.  
  155. void DrawPixel(SDL_Surface *surface,int x , int y , unsigned  int depth_value){
  156.     if(SDL_MUSTLOCK(surface)){
  157.         if(SDL_LockSurface(surface) < 0)
  158.             return;
  159.     }
  160.     Uint32 *buffer;
  161.     Uint32 color;
  162.  
  163.     color = SDL_MapRGB(surface->format, 0, 0, 0);
  164.     if(x >= surface->w || x < 0 || y >= surface->h || y < 0)
  165.         return;
  166. //write a switch to convert depth to rgb values.
  167. switch (depth_value) {
  168.     //depth value fckery
  169.     case 0 ... 400:
  170.     color = SDL_MapRGB(surface->format, depth_value+100,0,0);  //red
  171.     break;
  172.    
  173.     case 401 ... 624:
  174.   color = SDL_MapRGB(surface->format, 255,depth_value/3,0); //orange scale
  175.     break;
  176.    
  177.     case 625 ... 824:
  178.     color = SDL_MapRGB(surface->format, depth_value-300,depth_value-300,0);  //yello
  179.     break;
  180.    
  181.     case  825 ... 925:
  182.     color = SDL_MapRGB(surface->format, 0,depth_value/5,0); //green scale
  183.     break;
  184.      
  185.     case 926 ... 1480:
  186.     color = SDL_MapRGB(surface->format, depth_value/20,0,depth_value/7); //indiogo
  187.     break;
  188.    
  189.     case 1481 ... 1999:
  190.     color = SDL_MapRGB(surface->format, 0,255, 255);
  191.     break;
  192.  
  193.    case 2000 ... 2047:
  194.         color = SDL_MapRGB(surface->format, depth_value,depth_value,depth_value);
  195.      break;
  196.     default:
  197. color = SDL_MapRGB(surface->format, 0,0,0);
  198.     break;
  199.         }
  200.  
  201.     buffer = (Uint32*)surface->pixels  + y*surface->pitch/4+x;
  202.     (*buffer) = color;
  203.  
  204.     if(SDL_MUSTLOCK(surface))
  205.         SDL_UnlockSurface(surface);
  206. }
  207. //Kinect Stuff
  208. int kInit(){
  209.    // Initialize libfreenect
  210.     if (freenect_init(&f_ctx, NULL) < 0)    {
  211.         printf("Failed to initialize libfreenect!\n");
  212.         exit(1);
  213.     }
  214.  
  215.     // Set the log level (optional)
  216.    //  freenect_set_log_level(f_ctx, FREENECT_LOG_DEBUG);
  217.    // Open the Kinect device
  218.     if (freenect_open_device(f_ctx, &f_dev, 0) < 0)    {
  219.        printf("Failed to open Kinect device!\n");
  220.        exit(1);
  221.     }
  222.     return 0;
  223.     }
  224.  
  225. void IR(freenect_device *dev, uint16_t brightness){
  226.         int ire = freenect_get_ir_brightness(f_dev);
  227.         freenect_set_ir_brightness(f_dev, brightness);
  228.         printf("%d brightness\n", ire);
  229.         cout << "pxX: " << pxX << " pxY:"<<pxY <<endl;
  230.         //int freenect_set_ir_brightness(f_dev,  brightness);
  231.         }
  232.  
  233. void kClose(){
  234.         //shutdown functions
  235.     freenect_stop_depth(f_dev);
  236.     freenect_close_device(f_dev);
  237.     freenect_shutdown(f_ctx);
  238.    // SDL_Quit();
  239.         }
  240. void no_kinect_quit(void){
  241.     printf("Error: Kinect not connected?\n");
  242.     exit(1);
  243. }
  244.  
  245. //Get ConFiG File
  246. int getCFG(string x){
  247.         ifstream inputFile(x); // Replace "data.csv" with your CSV file's name
  248.     if (!inputFile.is_open()) {
  249.         std::cerr << "Failed to open the file, Using system values." << std::endl;
  250.     }
  251.  /*
  252.   * Config file: all integers for now, file: pplot.conf
  253.   * initX,Y: relate to bounding area of the sensor. if we dont need to read more data points, dont.
  254.   * pixelation reffers to limiting resolution by skipping pixels, which results in pixelation.
  255.   * pixelation, brightness, minDepth, maxDepth, depthScalling. xyLimits, accelorometerThreshohlds.
  256.   *
  257.   * pxX,pxY,brightness,minD, maxD, DScale, initX1,initX2, initY1, initY2,accellThreshX,Y,Z
  258.   *
  259.   * */
  260.     std::string line;
  261.     if (std::getline(inputFile, line)) {
  262.         std::istringstream ss(line);
  263.         std::string xStr, yStr, brightnessStr,minDStr,maxDStr,DScaleStr,initX1Str,initX2Str,initY1Str,initY2Str, accelThreshXStr, accelThreshYStr, accelThreshZStr;
  264.         //dont forget its all strings in the file
  265.         if (getline(ss, xStr, ',') &&
  266.             getline(ss, yStr, ',') &&
  267.             getline(ss, brightnessStr, ',') &&
  268.             getline(ss, minDStr, ',') &&
  269.             getline(ss, maxDStr, ',') &&
  270.             getline(ss, DScaleStr, ',') &&
  271.             getline(ss, initX1Str, ',') &&
  272.             getline(ss, initX2Str, ',') &&
  273.             getline(ss, initY1Str, ',') &&
  274.             getline(ss, initY2Str, ',')  &&
  275.             getline(ss, accelThreshXStr , ',') &&
  276.             getline(ss,  accelThreshYStr, ',') &&
  277.             getline(ss, accelThreshZStr )) {
  278.              pxX = stoi(xStr);
  279.              pxY = stoi(yStr);
  280.              brightness = stoi(brightnessStr);
  281.              minD= stoi(minDStr);
  282.              maxD=stoi(maxDStr);
  283.              initX1=stoi(initX1Str);
  284.              initX2=stoi(initX2Str);
  285.              initY1=stoi(initY1Str);
  286.              initY2=stoi(initY2Str);
  287.              accelThreshX=stoi(accelThreshXStr);
  288.              accelThreshY=stoi(accelThreshYStr);
  289.              accelThreshZ=stoi(accelThreshZStr);
  290.             //readd from file
  291.            std::cout << "PxX: " << xStr << ", PxY: " << yStr << ", Brightness: " << brightness <<  " , minDepth: " << minD<< " maxDepth: "<< maxD << " Depth Scale:" << DScale
  292.            <<" \n initX1:" << initX1 <<" , initY1: "<< initY1 << " , initX2: " <<initX2 << " , initY2: "  << initY2
  293.            << "\nAccelorometer Threshholds X,Y,Z:"<< accelThreshX << ","<< accelThreshY << ","<<accelThreshZ << std::endl;
  294.         } else {
  295.             cerr << "Partialy read config file.\n filing in missing system Variables\n" ;
  296.             cout << "PxX: " << pxX << ", PxY: " << pxY << ", Brightness: " << brightness <<  " , minDepth: " << minD<< " maxDepth: "<< maxD
  297.             <<" \n initX1:" << initX1 <<" , initY1: "<< initY1 << " , initX2: " <<initX2 << " , initY2: "  << initY2
  298.             << "\nAccelorometer Threshhold X, Y, Z: "<< accelThreshX << "," << accelThreshY << "," <<accelThreshZ << endl;
  299.             }
  300.     } else {
  301.         cerr << "Empty file: using system Variables\n";
  302.        cout << "PxX: " << pxX << ", PxY: " << pxY << ", Brightness: " << brightness <<  " , minDepth: " << minD<< "maxDepth: "<< maxD
  303.             <<" \n initX1:" << initX1 <<" , initY1: "<< initY1 << " , initX2: " <<initX2 << " , initY2: "  << initY2
  304.             << "\nAccelorometer Threshhold X, Y, Z: "<< accelThreshX << "," << accelThreshY << "," <<accelThreshZ << endl;  
  305.              }
  306.     inputFile.close();
  307.     return 0;
  308.     }
  309.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement