Retro Controller

Snes Ctrl Over PSP


Code Pc (Java):


Exported from Notepad++

/* Retro Controller Adapter - SoftKeyboard Adaptador Sio de control (Emulador de teclado) Desarrollado por Davis Nuñez. Licenciado por Creative Commons Reconocimiento-CompartirIgual 4.0 http://creativecommons.org/licenses/by-sa/4.0/ Version: 1.1 Full - 18/12/2015 - 14:02 pm */ import processing.serial.*; import java.awt.*; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; Serial sio; // Obj Serial call sio :P Robot VKey; // Manipulador de Hw //BUTTON_SELECT-BUTTON_L3-BUTTON_R3-BUTTON_START - BUTTON_UP-BUTTON_RIGHT-BUTTON_DOWN-BUTTON_LEFT //BUTTON_L2-BUTTON_R2-BUTTON_L1-BUTTON_R1 - BUTTON_TRI-BUTTON_CIRCLE-BUTTON_CROSS-BUTTON_SQUARE int[] keysmask = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00}; int[] keyskbd = { KeyEvent.VK_V, KeyEvent.VK_F, KeyEvent.VK_G, KeyEvent.VK_SPACE, KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_E, KeyEvent.VK_R, KeyEvent.VK_Q, KeyEvent.VK_W, KeyEvent.VK_S, KeyEvent.VK_X, KeyEvent.VK_Z, KeyEvent.VK_A }; int[] count = { 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0}; int[] data = new int[10]; void setup(){ // Init more stuff xDD size(140,140); frameRate(60); // println(Serial.list()); String portName = Serial.list()[2]; // Check over any pc sio = new Serial(this, portName, 19200); try{ VKey = new Robot(); } catch(AWTException a){} thread("SioHandle"); // Separate thread for max speed } void draw(){ // Puff Only for default background(0); fill(255, 255, 0); delay(2); } void SioHandle(){ // Hilo independiente | thread poll while(true){ SioHandleKBD(); } } int SioHandleKBD(){ // Read and emulate keyboard from sio //println("Read Init"); sio.write(52); // Key Get data if (sio.available() > 0){// If data is available, int ch = sio.read(); // Key response device if(ch==-1) // Error (syncronis) return 0; //println(ch); if(ch==172){ // Device ready! ch = sio.read(); // Number of reads (data) if(ch==-1) // Error (syncronis) return 0; //println(ch); for(int i=0; i<ch; i++){ // fill array data[i] = sio.read(); if(data[i]==-1) // Error (syncronis) return 0; //println(data[i]); } for (int i = 0; i < 8; i++){ if((((~data[0]) & keysmask[i])!=0) && count[i] == 0){ VKey.keyPress(keyskbd[i]); count[i] = 1; } else if( !(((~data[0]) & keysmask[i])!=0) && count[i] == 1){ VKey.keyRelease(keyskbd[i]); count[i] = 0; } } for (int i = 8; i < 16; i++){ if((((~data[1]) & keysmask[i])!=0) && count[i] == 0){ VKey.keyPress(keyskbd[i]); count[i] = 1; } else if( !(((~data[1]) & keysmask[i])!=0) && count[i] == 1){ VKey.keyRelease(keyskbd[i]); count[i] = 0; } } } } }



Code PSP:


Exported from Notepad++

#include "main.h" #include <time.h> #include <pspdisplay.h> #include "blit.h" PSP_MODULE_INFO("Plugin", 0x1006, 1, 1); #define BUTTON_SELECT 0x01 #define BUTTON_L3 0x02 #define BUTTON_R3 0x04 #define BUTTON_START 0x08 #define BUTTON_UP 0x10 #define BUTTON_RIGHT 0x20 #define BUTTON_DOWN 0x40 #define BUTTON_LEFT 0x80 #define BUTTON_L2 0x01 #define BUTTON_R2 0x02 #define BUTTON_L1 0x04 #define BUTTON_R1 0x08 #define BUTTON_TRI 0x10 #define BUTTON_CIRCLE 0x20 #define BUTTON_CROSS 0x40 #define BUTTON_SQUARE 0x80 int gholdmask=0x00; u8 analogx = 128; u8 analogy = 128; int data[10]; int thid,running=1; int getRunning(){ return running; } void setRunning(int r){ running = r; } int getTHID(){ return thid; } int getMask(){ return gholdmask; } u8 getAX(){ return analogx; } u8 getAY(){ return analogy; } void stuff_keys(int mask){ gholdmask = mask; } int show_msg_alert = 0; // Nothing... void show_alert(const char * msg){ show_msg_alert = 1; blit_set_msg(msg); } int blit_thread(){ // sleep here to avoid corrupting the startup animation sceKernelDelayThread(4 * 1000 * 1000); time_t initialtime; time_t currentTime; time(&initialtime); // Mensaje de Bienvenida.. show_alert("Retro Controller is now ACTIVE"); // Mensajes del plugin "info" while(1){ // Ciclo infinito a la espera de mostrar algo XD // Espera requerida.. sceKernelDelayThread(100); sceDisplayWaitVblankStart(); if(show_msg_alert==1){ // Peticion de seteo para mostrar una alerta.. show_msg_alert=2; time(&initialtime); } if(show_msg_alert==2){ // Estamos mostrando un mensaje.. time(&currentTime); double diff_t = difftime(currentTime, initialtime); if(diff_t>3){ // 3 sec show, clean.. blit_clear_msg(); blit_show_msg(); show_msg_alert=0; // nothing.. }else{ // Durante al menos 3 segundos mostramos alerta.. blit_show_msg(); } } } return 0; } int main_thread(){ sioInit(19200); sioPutchar(0xFE); sceKernelDelayThread(100000); int ch,numChars,mask,i; int connect = 0; // 0 no | 1 yes loop: if(connect==1){ show_alert("Controller is not Conected..."); connect = 0; } while(1){ sioPutchar(0x34); ch = sioReadChar(); if(ch==-1){ resetFBuf(); goto loop; }else if(ch==0xEE){ sceKernelDelayThread(20000); resetFBuf(); goto loop; }else if(ch==0xAC){ numChars = sioReadChar(); if(numChars==-1){ resetFBuf(); goto loop; } for(i=0; i<numChars; i++){ data[i] = sioReadChar(); if(data[i]==-1){ resetFBuf(); goto loop; } } } resetFBuf(); if(connect==0){ // Llegamos hasta aqui es por que todo va ok, por lo cual revisamos msg, si no esta dicho.. show_alert("Controller is Conected..."); connect = 1; } int chk = 0; for(i = 0; i < 6; i++){ chk = chk | data[i]; } if(chk == 0){ sceKernelDelayThread(20000); goto loop; } mask=0; if(((~data[0]) & BUTTON_SELECT)!=0){ mask |= SYSCON_CTRL_SELECT; } if(((~data[0]) & BUTTON_START)!=0){ mask |= SYSCON_CTRL_START; } if(((~data[0]) & BUTTON_UP)!=0){ mask |= SYSCON_CTRL_ALLOW_UP; } if(((~data[0]) & BUTTON_RIGHT)!=0){ mask |= SYSCON_CTRL_ALLOW_RT; } if(((~data[0]) & BUTTON_DOWN)!=0){ mask |= SYSCON_CTRL_ALLOW_DN; } if(((~data[0]) & BUTTON_LEFT)!=0){ mask |= SYSCON_CTRL_ALLOW_LT; } if(((~data[1]) & BUTTON_L1)!=0){ mask |= SYSCON_CTRL_LTRG; } if(((~data[1]) & BUTTON_R1)!=0){ mask |= SYSCON_CTRL_RTRG; } if(((~data[1]) & BUTTON_TRI)!=0){ mask |= SYSCON_CTRL_TRIANGLE; } if(((~data[1]) & BUTTON_CIRCLE)!=0){ mask |= SYSCON_CTRL_CIRCLE; } if(((~data[1]) & BUTTON_CROSS)!=0){ mask |= SYSCON_CTRL_CROSS; } if(((~data[1]) & BUTTON_SQUARE)!=0){ mask |= SYSCON_CTRL_RECTANGLE; } if(((~data[0]) & BUTTON_L3)!=0){ mask |= SYSCON_CTRL_HOME; } if(((~data[0]) & BUTTON_R3)!=0){ mask |= SYSCON_CTRL_NOTE; } if(((~data[1]) & BUTTON_L2)!=0){ mask |= SYSCON_CTRL_VOL_DN; } if(((~data[1]) & BUTTON_R2)!=0){ mask |= SYSCON_CTRL_VOL_UP; } if(data[2] > 188){ mask |= SYSCON_CTRL_CIRCLE; }else if(data[2] < 68){ mask |= SYSCON_CTRL_RECTANGLE; } if(data[3] > 188){ mask |= SYSCON_CTRL_CROSS; }else if(data[3] < 68){ mask |= SYSCON_CTRL_TRIANGLE; } analogx = data[4]; analogy = data[5]; stuff_keys(mask); sceKernelDelayThread(25000); } sceKernelSleepThread(); return 0; } /* Power Callback */ int power_callback(int unknown, int pwrflags, void *common){ /* check for power switch and suspending as one is manual and the other automatic */ if (pwrflags & PSP_POWER_CB_POWER_SWITCH || pwrflags & PSP_POWER_CB_SUSPENDING) { sioShutdown(); // Suspendemos y liberamos el Puerto Sio. } else if (pwrflags & PSP_POWER_CB_RESUME_COMPLETE) { /* Al volver de una suspension */ sioInit(19200); //Reiniciamos el Puerto Sio a 19200 bytes. } return 0; } /* Callback thread */ int CallbackThread(SceSize args, void *argp){ int cbid; cbid = sceKernelCreateCallback("Power Callback", power_callback, NULL); //Registramos el Callback Power scePowerRegisterCallback(0, cbid); sceKernelSleepThreadCB(); return 0; } int module_start(SceSize args, void *argp) { install_syscon_hook(); thid = sceKernelCreateThread("cb_sio_thread", CallbackThread, 0x11, 0xFA0, 0, 0); if (thid >= 0) sceKernelStartThread(thid, 0, 0); thid = sceKernelCreateThread("msg_ctrl_thread", blit_thread, 0x18, 0x1000, 0, NULL); if(thid >= 0) sceKernelStartThread(thid, 0, 0); thid = sceKernelCreateThread("HookTest", main_thread, 0x18, 0x1000, 0, NULL); if(thid >= 0) sceKernelStartThread(thid, args, argp); return 0; } int module_stop(){ return 0; }



Code Arduino:


Exported from Notepad++

/* Retro Controller Adapter - NES Adaptador Sio de control NES (nintendo 8bits) Desarrollado por Davis Nuñez. Licenciado por Creative Commons Reconocimiento-CompartirIgual 4.0 http://creativecommons.org/licenses/by-sa/4.0/ Version: 1.1 Full - 05/01/2016 - 6:30 pm */ #define sio Serial // More easy for my :D - davis #define main setup // More easy for my :D - davis #define delayUs delayMicroseconds // More easy for my :D - davis #define iowrite digitalWrite // More easy for my :D - davis #define ioread digitalRead // More easy for my :D - davis #define ioset pinMode // More easy for my :D - davis // Definimos los pines del control #define latch 7 //signals controller to latch button states #define pulse 5 //toggle to shift out button states #define data 9 //data in line void main(){ // Init Stuff... ioset(latch, OUTPUT); ioset(pulse, OUTPUT); ioset(data, INPUT); sio.begin(19200); //set a clean low signal iowrite(latch, LOW); iowrite(pulse, LOW); // Declare Var´s char count[] = {0,0,0,0,0,0,0,0}; //each value stores 1 or 0 indicating whether a key is currently pressed // A- B- Select- Start- Up- Down- Left-Right char keys[] = {64,32,1,8,16,64,128,32}; //keyboard keys to be pressed char dato[] = {172,6,255,255,128,128,128,128}; // Buffer to SIO Send xD int i,index, tempbit, //iterates over the data_byte to grab each button's state data_byte; //byte that stores button states while(1){// infinity cycle iowrite(latch, HIGH); //Send 12uS signal to latch button data delayUs(12); iowrite(latch, LOW); delayUs(6); data_byte = B00000000; //initialize each bit as 0, it will be OR-ed with each value shifted in. Those values are in turn shifted for(i = 0; i < 8; i++){ //so they take up one bit of the whole byte data_byte = data_byte | (ioread(data) << i); iowrite(pulse, HIGH); delayUs(5); iowrite(pulse, LOW); delayUs(5); } index = 3; for(i = 0; i < 8; i++){ tempbit = (((data_byte) >> (i)) & 0x01); if(tempbit == 0 && count[i] == 0){ // Check if count index is not press and add keymask count[i] = 1; // toggle count as 1 previously press dato[index] -= keys[i]; } else if(tempbit == 1 && count[i] == 1){ // Check if count index is press and quit keymask count[i] = 0; // toggle count as 0 previously released dato[index] += keys[i]; } if(i==2) index--; } for(i = 0; i < 8; i++){ sio.write(dato[i]); } delay(20); } } void loop(){}





Con eso me despido bla bla bla xD

zerozelta (Collaborator)