Advertisement
Guest User

pad ps2

a guest
Apr 9th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. pad.h
  2.  
  3. #include <avr/io.h>
  4. #include <util/delay.h>
  5.  
  6. // CMD - wyjście
  7. #define CMD_SET PORTC |= (1<<4)
  8. #define CMD_CLR PORTC &= ~(1<<4)
  9. #define CMD_DIR_OUT DDRC |= (1<<4)
  10. // ATT - wyjście
  11. #define ATT_SET PORTC |= (1<<5)
  12. #define ATT_CLR PORTC &= ~(1<<5)
  13. #define ATT_DIR_OUT DDRC |= (1<<5)
  14. // DATA - wejście
  15. #define DATA_DIR_IN DDRC &= ~(1<<2)
  16. #define DATA_IN    (PINC&(1<<2))
  17. // CLK - wyjście
  18. #define CLK_SET PORTC |= (1<<3)
  19. #define CLK_CLR PORTC &= ~(1<<3)
  20. #define CLK_DIR_OUT DDRC |= (1<<3)
  21. // LED
  22. #define LED_SET DDRB |= (1<<5)
  23. #define LED_ON PORTB |= (1<<5)
  24. #define LED_OFF PORTB &= ~(1<<5)
  25.  
  26. extern unsigned char tab[9];
  27. void pad_init(void);
  28. void pad_cmd(unsigned char t[],unsigned char n);
  29. bool pad_get_state();
  30. void pad_loop();
  31.  
  32. pad.c:
  33.  
  34. #include "pad.h"
  35. #include "motor.h"
  36. #include "buzzer.h"
  37.  
  38. // Krotkie opóźnienie
  39. #define _NOP_ asm volatile("nop\n\t""nop\n\t" "nop\n\t" "nop\n\t" ::)
  40.  
  41. unsigned char tab[9];
  42.  
  43. // Wysyła/odczytuje jeden bajt do/z gamepada
  44. unsigned char pad_byte(unsigned char byte)
  45. {
  46.    unsigned char i,r=0;
  47.  
  48.    for(i=0; i<8; i++, byte>>=1)
  49.    {       
  50.        CLK_CLR;
  51.        if(byte& 0x01) CMD_SET; else CMD_CLR;       
  52.        _NOP_;    
  53.        CLK_SET;
  54.        r>>=1;
  55.        if(DATA_IN) r|=0x80;
  56.    }
  57.    CMD_SET;
  58.    _delay_us(20);
  59.    return r;  
  60. }
  61.  
  62. void pad_cmd(unsigned char t[],unsigned char n)
  63. {
  64.    unsigned char i;
  65.     LED_ON;
  66.    ATT_CLR;
  67.  
  68.    for(i=0; i<n; i++)
  69.       t[i] = pad_byte(t[i]);
  70.  
  71.    ATT_SET;
  72.    _delay_us(50);
  73.    LED_OFF;
  74. }
  75.  
  76.  
  77. void pad_config(void)
  78. {
  79.    // Komenda 0x43 "Go into configuration mode"
  80.    tab[0]= 0x01;
  81.    tab[1]= 0x43;
  82.    tab[2]= 0x00;
  83.    tab[3]= 0x01;
  84.    tab[4]= 0x00;
  85.    pad_cmd(tab, 5);
  86.  
  87.    // Komenda 0x44 "Turn on analog mode"
  88.    tab[0]= 0x01;
  89.    tab[1]= 0x44;
  90.    tab[2]= 0x00;
  91.    tab[3]= 0x01;
  92.    tab[4]= 0x03; //
  93.    tab[5]= 0x00;
  94.    tab[6]= 0x00;
  95.    tab[7]= 0x00;
  96.    tab[8]= 0x00;
  97.    pad_cmd(tab, 9);
  98.  
  99.    // Komenda 0x43 "Exit config mode", niekonieczna, gdyż 0x42 automatycznie wychodzi z config mode
  100.    tab[0] = 0x01;
  101.    tab[1] = 0x43;
  102.    tab[2] = 0x00;
  103.    tab[3] = 0x00;
  104.    tab[4] = 0x5A;
  105.    tab[5] = 0x5A;
  106.    tab[6] = 0x5A;
  107.    tab[7] = 0x5A;
  108.    tab[8] = 0x5A;
  109.    pad_cmd(tab, 9);
  110. }
  111.  
  112. void pad_init(void)
  113. {
  114.     CMD_DIR_OUT;
  115.     ATT_DIR_OUT;
  116.     CLK_DIR_OUT;
  117.    
  118.     DATA_DIR_IN;
  119.  
  120.     ATT_SET;
  121.     CLK_SET;
  122.     CMD_SET;
  123.    
  124.     LED_SET;
  125.    
  126.     pad_config();
  127. }
  128.  
  129. bool pad_get_state()
  130. {
  131.     // komenda 0x42 - Controller poll
  132.       tab[0] = 0x01;
  133.       tab[1] = 0x42;
  134.       tab[2] = 0x00;
  135.  
  136.       tab[3] = 0x00; // przyciski
  137.       tab[4] = 0x00; // przyciski
  138.       tab[5] = 0x00; // dżojstik prawy  lewo-prawo
  139.       tab[6] = 0x00; // dżojstik prawy  góra-dół
  140.       tab[7] = 0x00; // dżojstik lewy   lewo-prawo
  141.       tab[8] = 0x00; // dżojstik lewy   gora-dół
  142.       pad_cmd(tab, 9);
  143.       // analogi w stanie spocczynkowym (na środku) zwracają 0b10000000 = 128, góra = 0, lewo = 0, dół = 255, prawo = 255
  144.      
  145.       return (tab[0] == 255 && tab[1] == 115);      //zwracamy czy komunikacja zakończyła się sukcesesm
  146. }
  147.  
  148. void pad_loop()
  149. {
  150.             int tmpl = 0, tmpr = 0;
  151.             bool back = false; 
  152.             if(tab[8] > 128) // jeśli analog w górę
  153.             {
  154.                 beep(500, 200);    
  155.                 back = true;   
  156.                 tmpl = tab[8]-127;     
  157.                 tmpr = tmpl;           
  158.             }  
  159.             else if(tab[8] < 128) // jeśli w dół
  160.             {          
  161.                 tmpl = 128-tab[8];                         
  162.                 tmpr = tmpl;   
  163.             }
  164.            
  165.             int p = 20;
  166.             if(tab[7] > 128) // jeśli analog w prawo
  167.             {  
  168.                 tmpr -= (p*(tab[7]-127))/100;              
  169.                
  170.             }  
  171.             else if(tab[7] < 128) // jeśli w lewo
  172.             {          
  173.                 tmpl -= (p*(127-tab[7]))/100;              
  174.             }
  175.            
  176.             if(!(tab[4] & (1 << 3))) // jeśli wciśnięty R1 to turbo lvl1
  177.             {
  178.                 if(tab[8] == 128) // jesli stoimy i chcemy zrobic baczka
  179.                 {
  180.                         if(tab[7] > 128) // jeśli analog w prawo
  181.                         {  
  182.                             tmpl = -1 * tmpr;          
  183.                            
  184.                         }  
  185.                         else if(tab[7] < 128) // jeśli w lewo
  186.                         {          
  187.                             tmpr = -1 * tmpl;      
  188.                         }
  189.                 }  
  190.                 else
  191.                 {
  192.                     tmpl += 47;
  193.                     tmpr += 47;
  194.                 }
  195.             }
  196.                
  197.             if(!(tab[4] & (1 << 1))) // jeśli wciśnięty R2 to turbo lvl2
  198.             {
  199.                     tmpl += 80;
  200.                     tmpr += 80;
  201.             }
  202.            
  203.             if(back)
  204.             {
  205.                 tmpl *= -1;
  206.                 tmpr *= -1;
  207.             }
  208.            
  209.             if(tab[8] == 128 && tab[7] == 128)
  210.             {
  211.                 m1_stop();
  212.                 m2_stop();
  213.             }
  214.             else
  215.             {          
  216.                 tmpl = tmpl * 100 / 255;   
  217.                 tmpr = tmpr * 100 / 255;
  218.                
  219.                 m1_set(tmpl);
  220.                 m2_set(tmpr);
  221.                
  222.                 m1_start();
  223.                 m2_start();        
  224.             }
  225.            
  226.            
  227.             if(!(tab[4] & (1 << 6))) // jeśli wciśnięty X to klakson
  228.             {              
  229.                 beep(4000, 200);
  230.             }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement