Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- trying to read data from a PS3 controller... (by xerpi)
- */
- #include <gccore.h>
- #include <gcutil.h>
- #include <ogcsys.h>
- #include <ogc/ipc.h>
- #include <ogc/usb.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <wiiuse/wpad.h>
- #define MAX_DEV 7
- #define VID 0x054C //Sony Corp.
- #define PID 0x0268 //Sixaxis and DS3
- static void *xfb = NULL;
- static GXRModeObj *rmode = NULL;
- void init_video();
- int find_and_set_mac();
- void set_controller_mac(int32_t fd, uint8_t *mac);
- void get_controller_mac(int32_t fd, uint8_t *mac);
- int main(int argc, char **argv) {
- IOS_ReloadIOS(58);
- usleep(500 * 1000);
- init_video();
- WPAD_Init();
- iosCreateHeap(1024 * 16);
- USB_Initialize();
- usleep(10000);
- while (find_and_set_mac() <= 0) {
- printf("connect a ps3 controller\n");
- usleep(1500 * 1000);
- }
- USB_Deinitialize();
- printf("ps3 mac set, now press the PS button\n");
- //Bluetooth connection...
- while(1) {
- WPAD_ScanPads();
- u32 pressed = WPAD_ButtonsDown(0);
- if ( pressed & WPAD_BUTTON_HOME ) exit(0);
- VIDEO_WaitVSync();
- }
- return 0;
- }
- int find_and_set_mac()
- {
- uint8_t dev_count;
- usb_device_entry usb_dev_entry_list[MAX_DEV];
- if (USB_GetDeviceList(usb_dev_entry_list, MAX_DEV, USB_CLASS_HID, &dev_count) < 0) {
- printf("Error getting device list.\n");
- return -1;
- }
- printf("found %i USB devices, looking for the PS3 controller...", dev_count);
- if (dev_count == 0) return -2;
- int i;
- usb_device_entry *de;
- for(i = 0; i < dev_count; i++) {
- de = &usb_dev_entry_list[i];
- //Is it a PS3 controller?
- if (de->vid == VID && de->pid == PID) {
- printf("found!\n");
- //Yeah it is, let's try to open it!
- int32_t fd;
- if (USB_OpenDevice(de->device_id, VID, PID, &fd) < 0) {
- printf ("error opening the controller\n");
- return -3;
- }
- printf("now let's set local BD MAC!...");
- uint8_t local_mac[6] = {0,0,0,0xff,0xff,0xff};
- set_controller_mac(fd, local_mac);
- printf("checking if it was set correctly:\n");
- uint8_t mac[6];
- get_controller_mac(fd, mac);
- printf("we got: %02X:%02X:%02X:%02X:%02X:%02X from the controller\n", mac[0], mac[1], mac[2],
- mac[3], mac[4], mac[5]);
- int j, equals = 1;
- for (j = 0; j < 6 && equals; ++j) {
- if (local_mac[j] != mac[j]) equals = 0;
- }
- if (equals) {
- printf ("mac set correctly!!\n");
- USB_CloseDevice(&fd);
- return 1;
- } else {
- printf ("mac was not set correctly\n");
- USB_CloseDevice(&fd);
- return -4;
- }
- }
- }
- printf ("not found\n");
- return 0;
- }
- void set_controller_mac(int32_t fd, uint8_t *mac)
- {
- uint8_t ATTRIBUTE_ALIGN(32) msg[8] = {0x01, 0x00, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]};
- USB_WriteCtrlMsg(fd,
- USB_REQTYPE_INTERFACE_SET,
- USB_REQ_SETREPORT,
- (USB_REPTYPE_FEATURE<<8) | 0xf5,
- 0,
- 8,
- msg);
- }
- void get_controller_mac(int32_t fd, uint8_t *mac)
- {
- uint8_t ATTRIBUTE_ALIGN(32) msg[8];
- USB_WriteCtrlMsg(fd,
- USB_REQTYPE_INTERFACE_GET,
- USB_REQ_GETREPORT,
- (USB_REPTYPE_FEATURE<<8) | 0xf5,
- 0,
- 8,
- msg);
- mac[0] = msg[2];
- mac[1] = msg[3];
- mac[2] = msg[4];
- mac[3] = msg[5];
- mac[4] = msg[6];
- mac[5] = msg[7];
- }
- void init_video()
- {
- VIDEO_Init();
- rmode = VIDEO_GetPreferredMode(NULL);
- xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
- console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
- VIDEO_Configure(rmode);
- VIDEO_SetNextFramebuffer(xfb);
- // Make the display visible
- VIDEO_SetBlack(FALSE);
- // Flush the video register changes to the hardware
- VIDEO_Flush();
- // Wait for Video setup to complete
- VIDEO_WaitVSync();
- if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
- printf("\x1b[2;0H");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement