Advertisement
Guest User

Leapfrog TV controller

a guest
Jan 14th, 2016
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.39 KB | None | 0 0
  1.  
  2. LeapTV controller code (reversed by is0-mick 2015)
  3.  
  4. #include <dlfcn.h>
  5. #include "SDL.h"
  6.  
  7. typedef int boolean;
  8. enum {false,true};
  9.  
  10. struct LFController {char Abutton;char Bbutton; char HomeButton; char QuestionButton; char WandShape; char VShape; char StickX; char StickY;char AccelX; char unknown; char AccelY; char unknown1; char AccelZ; char Counter; char Batt; char unknown2;};
  11.  
  12. boolean leftpressed = false;
  13. boolean rightpressed = false;
  14. boolean uppressed = false;
  15. boolean downpressed = false;
  16. boolean buttonapressed = false;
  17. boolean buttonbpressed = false;
  18. boolean buttonhomepressed = false;
  19. boolean buttonquestionpressed = false;
  20.  
  21. boolean debug = false;
  22.  
  23. void CallbackFunction(void* a,char* data,int length)
  24. {
  25.   printf("This is our callback1!\n");
  26. }
  27.  
  28. void GATMCallbackFunction(void* a,struct LFController* LFC,int length)
  29. {
  30.     //struct LFController LFC;
  31.    
  32.    
  33.    
  34.     //printf("%p %p %d \n",a,data,length);
  35.     //int i=0;
  36.     //char* temp = (char*)b;
  37.     /*
  38.     for(i=0;i<length;i++)
  39.     {
  40.         printf("%0x, ",data[i]);
  41.     }
  42.  
  43.         */
  44.     if (debug)
  45.     {
  46.     printf("is0-micks callback! \n");
  47.     printf("BUTTON A %0x\r\n",LFC->Abutton);
  48.     printf("BUTTON B %0x\r\n",LFC->Bbutton);
  49.     printf("BUTTON HOME %0x\r\n",LFC->HomeButton);
  50.     printf("BUTTON QUESTION %0x\r\n",LFC->QuestionButton);
  51.     printf("BUTTON Wand Shape %0x\r\n",LFC->WandShape);
  52.     printf("BUTTON V Shape %0x\r\n",LFC->VShape);
  53.     printf("BUTTON Stick X %0x\r\n",LFC->StickX);
  54.     printf("BUTTON Stick Y %0x\r\n",LFC->StickY);
  55.     printf("Accel X %0x\r\n",LFC->AccelX);
  56.     printf("Accel Y %0x\r\n",LFC->AccelY);
  57.     printf("Accel Z %0x\r\n",LFC->AccelZ);
  58.     printf("Batt Level %0x\r\n",LFC->Batt);
  59.     }
  60.  
  61.     if ((LFC->StickY > 0xC0) && (uppressed == false))
  62.     {
  63.         printf("UP\n");
  64.         SDL_Event sdlevent;
  65.         sdlevent.type=SDL_KEYDOWN;
  66.         sdlevent.key.keysym.sym=SDLK_UP;
  67.         SDL_PushEvent(&sdlevent);
  68.         uppressed = true;
  69.     }
  70.     else
  71.     {  
  72.         if ((LFC->StickY < 0xC0) &&(uppressed == true))
  73.         {
  74.         uppressed = false;
  75.         SDL_Event sdlevent;
  76.         sdlevent.type=SDL_KEYUP;
  77.         sdlevent.key.keysym.sym=SDLK_UP;
  78.         SDL_PushEvent(&sdlevent);
  79.         }
  80.        
  81.     }
  82.  
  83.     if (LFC->StickY < 0x3F && downpressed == false)
  84.     {
  85.         printf("DOWN\n");
  86.         SDL_Event sdlevent;
  87.         sdlevent.type=SDL_KEYDOWN;
  88.         sdlevent.key.keysym.sym=SDLK_DOWN;
  89.         SDL_PushEvent(&sdlevent);
  90.         downpressed = true;
  91.     }
  92.     else
  93.     {  
  94.         if (LFC->StickY > 0x3F && downpressed == true)
  95.         {
  96.         downpressed = false;
  97.         SDL_Event sdlevent;
  98.         sdlevent.type=SDL_KEYUP;
  99.         sdlevent.key.keysym.sym=SDLK_DOWN;
  100.         SDL_PushEvent(&sdlevent);
  101.         }
  102.        
  103.     }
  104.     if (LFC->StickX > 0xC0 && rightpressed == false)
  105.     {
  106.         printf("RIGHT\n");
  107.         SDL_Event sdlevent;
  108.         sdlevent.type=SDL_KEYDOWN;
  109.         sdlevent.key.keysym.sym=SDLK_RIGHT;
  110.         SDL_PushEvent(&sdlevent);
  111.         rightpressed = true;
  112.     }
  113.     else
  114.     {  
  115.         if (LFC->StickX < 0xC0 && rightpressed == true)
  116.         {
  117.         rightpressed = false;
  118.         SDL_Event sdlevent;
  119.         sdlevent.type=SDL_KEYUP;
  120.         sdlevent.key.keysym.sym=SDLK_RIGHT;
  121.         SDL_PushEvent(&sdlevent);
  122.         }
  123.        
  124.     }
  125.     if (LFC->StickX < 0x3F && leftpressed == false)
  126.     {
  127.         printf("LEFT\n");
  128.         SDL_Event sdlevent;
  129.         sdlevent.type=SDL_KEYDOWN;
  130.         sdlevent.key.keysym.sym=SDLK_LEFT;
  131.         SDL_PushEvent(&sdlevent);
  132.         leftpressed = true;
  133.     }
  134.     else
  135.     {  
  136.         if (LFC->StickX > 0x3F && leftpressed == true)
  137.         {
  138.         leftpressed = false;
  139.         SDL_Event sdlevent;
  140.         sdlevent.type=SDL_KEYUP;
  141.         sdlevent.key.keysym.sym=SDLK_LEFT;
  142.         SDL_PushEvent(&sdlevent);
  143.         }
  144.        
  145.     }
  146.    
  147.     if ((LFC->Abutton == 0x01) && (buttonapressed == false))
  148.     {
  149.         printf("BUTTONA\n");
  150.         SDL_Event sdlevent;
  151.         sdlevent.type=SDL_KEYDOWN;
  152.         sdlevent.key.keysym.sym=SDLK_LCTRL;
  153.         SDL_PushEvent(&sdlevent);
  154.         buttonapressed = true;
  155.     }
  156.     else
  157.     {  
  158.         if ((LFC->Abutton == 0x00) && (buttonapressed == true))
  159.         {
  160.         buttonapressed = false;
  161.         SDL_Event sdlevent;
  162.         sdlevent.type=SDL_KEYUP;
  163.         sdlevent.key.keysym.sym=SDLK_LCTRL;
  164.         SDL_PushEvent(&sdlevent);
  165.         }
  166.        
  167.     }
  168.  
  169.     if ((LFC->Bbutton == 0x01) && (buttonbpressed == false))
  170.     {
  171.         printf("BUTTONB\n");
  172.         SDL_Event sdlevent;
  173.         sdlevent.type=SDL_KEYDOWN;
  174.         sdlevent.key.keysym.sym=SDLK_LALT;
  175.         SDL_PushEvent(&sdlevent);
  176.         buttonbpressed = true;
  177.     }
  178.     else
  179.     {  
  180.         if ((LFC->Bbutton == 0x00) && (buttonbpressed == true))
  181.         {
  182.         buttonbpressed = false;
  183.         SDL_Event sdlevent;
  184.         sdlevent.type=SDL_KEYUP;
  185.         sdlevent.key.keysym.sym=SDLK_LALT;
  186.         SDL_PushEvent(&sdlevent);
  187.         }
  188.        
  189.     }
  190.  
  191.     if (LFC->HomeButton == 0x01 && buttonhomepressed == false)
  192.     {
  193.         printf("HomeButton\n");
  194.         SDL_Event sdlevent;
  195.         sdlevent.type=SDL_KEYDOWN;
  196.         sdlevent.key.keysym.sym=SDLK_ESCAPE;
  197.         SDL_PushEvent(&sdlevent);
  198.         buttonhomepressed = true;
  199.     }
  200.     else
  201.     {  
  202.         if (buttonhomepressed == true && LFC->HomeButton == 0x00)
  203.         {
  204.         buttonhomepressed = false;
  205.         SDL_Event sdlevent;
  206.         sdlevent.type=SDL_KEYUP;
  207.         sdlevent.key.keysym.sym=SDLK_ESCAPE;
  208.         SDL_PushEvent(&sdlevent);
  209.         }
  210.        
  211.     }
  212.  
  213.  
  214.     if (LFC->QuestionButton == 0x01 && buttonquestionpressed == false)
  215.     {
  216.         printf("BUTTONQuestion\n");
  217.         SDL_Event sdlevent;
  218.         sdlevent.type=SDL_KEYDOWN;
  219.         sdlevent.key.keysym.sym=SDLK_5;
  220.         SDL_PushEvent(&sdlevent);
  221.         sdlevent.type=SDL_KEYDOWN;
  222.         sdlevent.key.keysym.sym=SDLK_1;
  223.         SDL_PushEvent(&sdlevent);
  224.         buttonquestionpressed = true;
  225.     }
  226.     else
  227.     {  
  228.         if (buttonquestionpressed == true && LFC->QuestionButton == 0x00)
  229.         {
  230.         buttonquestionpressed = false;
  231.         SDL_Event sdlevent;
  232.         sdlevent.type=SDL_KEYUP;
  233.         sdlevent.key.keysym.sym=SDLK_5;
  234.         SDL_PushEvent(&sdlevent);
  235.         sdlevent.type=SDL_KEYUP;
  236.         sdlevent.key.keysym.sym=SDLK_1;
  237.         SDL_PushEvent(&sdlevent);
  238.         }
  239.        
  240.     }
  241.  
  242. }
  243.  
  244.  
  245.  
  246. void* handle;
  247.  
  248.  
  249. typedef char* (*tdBTIO_GetLocalAddress)();
  250. typedef int* (*tdBTIO_QueryStatus)();
  251. typedef int* (*tdBTIO_WriteValue)(int, int, void* ,int, char*);
  252. typedef int* (*tdBTIO_SendCommand)(void*, int, void*, int);
  253. typedef int* (*tdBTIO_ConnectToDevice)(int, char *);
  254. typedef int* (*tdBTIO_Init)(void*);
  255. typedef int* (*tdBTIO_ScanForDevices)(int,int);
  256. typedef int* (*tdBTIO_Exit)(void);
  257.  
  258. int StartControls() {
  259.    
  260.     // Declare Functions
  261.  
  262.     //arbitrary BTIO_DisconnectDevice;
  263.  
  264.  
  265.    
  266.     char *error;
  267.    
  268.     // Open libBRIO.so
  269.     void* handle = dlopen ("/usr/lib/libBTIO.so", RTLD_LAZY);
  270.     if (!handle) {
  271.         fputs (dlerror(), stderr);
  272.         return(1);
  273.     }
  274.    
  275.    
  276.     tdBTIO_SendCommand BTIO_SendCommand = (tdBTIO_SendCommand)dlsym(handle, "BTIO_SendCommand");
  277.     if ((error = dlerror()) != NULL)  {
  278.         fputs(error, stderr);
  279.         fputs(" @ BTIO_SendCommand\n", stderr);
  280.         return(1);
  281.     }
  282.  
  283.  
  284.     tdBTIO_Init BTIO_Init =
  285.                 (tdBTIO_Init)dlsym(handle, "BTIO_Init");
  286.     if ((error = dlerror()) != NULL)  {
  287.         fputs(error, stderr);
  288.         fputs(" @ BTIO_Init\n", stderr);
  289.         return(1);
  290.     }
  291.  
  292.  
  293.    
  294.     int* ControllerNum = BTIO_Init((void*)&CallbackFunction);//param seems to be *void callback pointer?
  295.     printf("BTIO_Init returned val of %d \n", ControllerNum);
  296.    
  297.     //sleep(5);
  298.     int* SendComRet = 0;
  299.  
  300.     void* data = (void*)&GATMCallbackFunction;
  301.     SendComRet = BTIO_SendCommand(ControllerNum,1,data,4);
  302.  
  303.     printf("Send Command returned val of %d \n", SendComRet);
  304.     SendComRet = BTIO_SendCommand(ControllerNum,2,data,4);
  305.  
  306.     printf("call back pointer = %p\n",&GATMCallbackFunction);
  307.     SendComRet = BTIO_SendCommand(ControllerNum,9,(void*)&GATMCallbackFunction,4);
  308.     printf("Send Command returned val of %d \n", SendComRet);
  309.  
  310.  
  311.     return 0;
  312. }
  313.  
  314.  
  315. void StopControls()
  316. {
  317.     char *error;
  318.     tdBTIO_Exit BTIO_Exit = (tdBTIO_Exit)dlsym(handle, "BTIO_Exit");
  319.     if ((error = dlerror()) != NULL)  {
  320.         fputs(error, stderr);
  321.         fputs(" @ BTIO_Exit\n", stderr);
  322.        
  323.     }
  324.     BTIO_Exit();
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement