久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 12820|回復(fù): 10
打印 上一主題 下一主題
收起左側(cè)

Arduino ps2手柄庫PS2X_lib下載

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:310666 發(fā)表于 2018-4-18 10:30 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
可以使用ps2和兼容ps2手柄來控制程式,但要使用4個信號引腳

Arduino源程序如下:
  1. #include "PS2X_lib.h"
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. //#include <avr/io.h>
  6. #if defined(__SAM3X8E__)
  7.     #include <sam/pio.h> // For the Due
  8. #else
  9.     #include <avr/io.h>    // For anything else before the Due
  10. #endif
  11. #if ARDUINO > 22
  12.   #include "Arduino.h"
  13. #else
  14.   #include "WProgram.h"
  15.   #include "pins_arduino.h"
  16. #endif

  17. static byte enter_config[]={0x01,0x43,0x00,0x01,0x00};
  18. static byte set_mode[]={0x01,0x44,0x00,0x01,0x03,0x00,0x00,0x00,0x00};
  19. static byte set_bytes_large[]={0x01,0x4F,0x00,0xFF,0xFF,0x03,0x00,0x00,0x00};
  20. static byte exit_config[]={0x01,0x43,0x00,0x00,0x5A,0x5A,0x5A,0x5A,0x5A};
  21. static byte enable_rumble[]={0x01,0x4D,0x00,0x00,0x01};
  22. static byte type_read[]={0x01,0x45,0x00,0x5A,0x5A,0x5A,0x5A,0x5A,0x5A};

  23. /****************************************************************************************/
  24. boolean PS2X::NewButtonState() {
  25.   return ((last_buttons ^ buttons) > 0);
  26. }

  27. /****************************************************************************************/
  28. boolean PS2X::NewButtonState(unsigned int button) {
  29.   return (((last_buttons ^ buttons) & button) > 0);
  30. }

  31. /****************************************************************************************/
  32. boolean PS2X::ButtonPressed(unsigned int button) {
  33.   return(NewButtonState(button) & Button(button));
  34. }

  35. /****************************************************************************************/
  36. boolean PS2X::ButtonReleased(unsigned int button) {
  37.   return((NewButtonState(button)) & ((~last_buttons & button) > 0));
  38. }

  39. /****************************************************************************************/
  40. boolean PS2X::Button(uint16_t button) {
  41.   return ((~buttons & button) > 0);
  42. }

  43. /****************************************************************************************/
  44. unsigned int PS2X::ButtonDataByte() {
  45.    return (~buttons);
  46. }

  47. /****************************************************************************************/
  48. byte PS2X::Analog(byte button) {
  49.    return PS2data[button];
  50. }

  51. /****************************************************************************************/
  52. unsigned char PS2X::_gamepad_shiftinout (char byte) {
  53.    unsigned char tmp = 0;
  54.    for(unsigned char i=0;i<8;i++) {
  55.       if(CHK(byte,i)) CMD_SET();
  56.       else CMD_CLR();
  57.          
  58.       CLK_CLR();
  59.       delayMicroseconds(CTRL_CLK);

  60.       //if(DAT_CHK()) SET(tmp,i);
  61.       if(DAT_CHK()) bitSet(tmp,i);

  62.       CLK_SET();
  63. #if CTRL_CLK_HIGH
  64.       delayMicroseconds(CTRL_CLK_HIGH);
  65. #endif
  66.    }
  67.    CMD_SET();
  68.    delayMicroseconds(CTRL_BYTE_DELAY);
  69.    return tmp;
  70. }

  71. /****************************************************************************************/
  72. void PS2X::read_gamepad() {
  73.    read_gamepad(false, 0x00);
  74. }

  75. /****************************************************************************************/
  76. boolean PS2X::read_gamepad(boolean motor1, byte motor2) {
  77.    double temp = millis() - last_read;

  78.    if (temp > 1500) //waited to long
  79.       reconfig_gamepad();

  80.    if(temp < read_delay)  //waited too short
  81.       delay(read_delay - temp);

  82.    if(motor2 != 0x00)
  83.       motor2 = map(motor2,0,255,0x40,0xFF); //noting below 40 will make it spin

  84.    char dword[9] = {0x01,0x42,0,motor1,motor2,0,0,0,0};
  85.    byte dword2[12] = {0,0,0,0,0,0,0,0,0,0,0,0};

  86.    // Try a few times to get valid data...
  87.    for (byte RetryCnt = 0; RetryCnt < 5; RetryCnt++) {
  88.       CMD_SET();
  89.       CLK_SET();
  90.       ATT_CLR(); // low enable joystick

  91.       delayMicroseconds(CTRL_BYTE_DELAY);
  92.       //Send the command to send button and joystick data;
  93.       for (int i = 0; i<9; i++) {
  94.          PS2data[i] = _gamepad_shiftinout(dword[i]);
  95.       }

  96.       if(PS2data[1] == 0x79) {  //if controller is in full data return mode, get the rest of data
  97.          for (int i = 0; i<12; i++) {
  98.             PS2data[i+9] = _gamepad_shiftinout(dword2[i]);
  99.          }
  100.       }

  101.       ATT_SET(); // HI disable joystick
  102.       // Check to see if we received valid data or not.  
  103.           // We should be in analog mode for our data to be valid (analog == 0x7_)
  104.       if ((PS2data[1] & 0xf0) == 0x70)
  105.          break;

  106.       // If we got to here, we are not in analog mode, try to recover...
  107.       reconfig_gamepad(); // try to get back into Analog mode.
  108.       delay(read_delay);
  109.    }

  110.    // If we get here and still not in analog mode (=0x7_), try increasing the read_delay...
  111.    if ((PS2data[1] & 0xf0) != 0x70) {
  112.       if (read_delay < 10)
  113.          read_delay++;   // see if this helps out...
  114.    }

  115. #ifdef PS2X_COM_DEBUG
  116.    Serial.println("OUT:IN");
  117.    for(int i=0; i<9; i++){
  118.       Serial.print(dword[i], HEX);
  119.       Serial.print(":");
  120.       Serial.print(PS2data[i], HEX);
  121.       Serial.print(" ");
  122.    }
  123.    for (int i = 0; i<12; i++) {
  124.       Serial.print(dword2[i], HEX);
  125.       Serial.print(":");
  126.       Serial.print(PS2data[i+9], HEX);
  127.       Serial.print(" ");
  128.    }
  129.    Serial.println("");
  130. #endif

  131.    last_buttons = buttons; //store the previous buttons states

  132. #if defined(__AVR__)
  133.    buttons = *(uint16_t*)(PS2data+3);   //store as one value for multiple functions
  134. #else
  135.    buttons =  (uint16_t)(PS2data[4] << 8) + PS2data[3];   //store as one value for multiple functions
  136. #endif
  137.    last_read = millis();
  138.    return ((PS2data[1] & 0xf0) == 0x70);  // 1 = OK = analog mode - 0 = NOK
  139. }

  140. …………
  141. …………
  142. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
PS2X_lib.zip (11.19 KB, 下載次數(shù): 250)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:330315 發(fā)表于 2018-5-15 15:00 | 只看該作者
正好需要
回復(fù)

使用道具 舉報

板凳
ID:333209 發(fā)表于 2018-9-23 16:42 | 只看該作者
lz,程序燒進去后串口監(jiān)視器顯示的是亂碼的東西,能請教一下是怎么回事嗎

回復(fù)

使用道具 舉報

地板
ID:407151 發(fā)表于 2018-10-9 17:47 | 只看該作者
我正在研究
回復(fù)

使用道具 舉報

5#
ID:446108 發(fā)表于 2018-12-14 06:40 | 只看該作者
您好!請問如果將其編譯成.o文件后,調(diào)用出錯,該如何解決?
回復(fù)

使用道具 舉報

6#
ID:666461 發(fā)表于 2019-12-17 17:07 | 只看該作者
怎么添加庫文件呢
回復(fù)

使用道具 舉報

7#
ID:993462 發(fā)表于 2021-12-16 22:19 | 只看該作者

項目欄里的加載庫,點開有一個添加庫,可以直接導(dǎo)入zip文件
回復(fù)

使用道具 舉報

8#
ID:1080573 發(fā)表于 2023-5-29 02:24 | 只看該作者
您好!請問如果將其編譯成.o文件后,調(diào)用出錯,該如何解決?
回復(fù)

使用道具 舉報

9#
ID:1080573 發(fā)表于 2023-5-29 02:24 | 只看該作者
您好!請問如果將其編譯成.o文件后,調(diào)用出錯,該如何解決?
回復(fù)

使用道具 舉報

10#
ID:1114607 發(fā)表于 2024-3-28 22:03 | 只看該作者
您好,這個第五行//#include <avr/io.h>是什么呀,我的arduino一直顯示不存在這個頭文件
回復(fù)

使用道具 舉報

11#
ID:367081 發(fā)表于 2025-3-14 01:41 | 只看該作者
下載PS2X軟件
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产精品久久久久久av公交车 | 久久区二区 | 亚洲va欧美va人人爽午夜 | 羞羞在线观看视频 | 国产一区三区在线 | 久久99精品久久久久久秒播九色 | 日韩欧美黄色 | 在线午夜 | 成年人黄色一级片 | 天天综合网天天综合色 | 请别相信他免费喜剧电影在线观看 | 在线欧美激情 | av在线免费观看网站 | 精品一区国产 | 免费永久av | 超碰美女在线 | 亚洲成人二区 | 影音先锋亚洲资源 | 在线免费观看黄色 | 成人精品国产免费网站 | 高清成人免费视频 | 国产在线视频一区二区 | 91国产在线视频在线 | 我想看国产一级毛片 | av一区二区三区在线观看 | 国产成人一区二区三区精 | 91精品国产色综合久久 | 欧美激情久久久 | 亚洲精品中文字幕 | 国产精品久久久久久久久久久免费看 | 国产xxxx岁13xxxxhd | 龙珠z国语版在线观看 | 欧美一级欧美一级在线播放 | 免费日韩av网站 | 在线一级片 | 亚洲国产专区 | 亚洲午夜视频 | 久久国产精品免费一区二区三区 | 欧美一区 | 成人av看片 | 一区在线免费视频 |