Advertisement
IrvinHeslan

Arduino_Souris_Joystick_Nunchuck

Feb 18th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.11 KB | None | 0 0
  1. //Wii_Joystick_Souris
  2. //Irvin Heslan TS4
  3. //Ce programme est basé sur celui existant créer par les concepteurs du logiciel Arduino
  4. #define POWER_VIA_PORT_C2_C3 1 //définit les entrées d'alimentation de la carte Arduino pour le Nunchuck
  5.  
  6. #define USE_NEW_WAY_INIT 1
  7. #define WII_IDENT_LEN ((byte)8)  //définit la longueur de la donnée identité du Nunchuck
  8. #define WII_TELEGRAM_LEN ((byte)8) //définit la longueur des données envoyés
  9. #define WII_NUNCHUCK_TWI_ADR ((byte)0x52) //Adresse en I2C du Nunchuck
  10.  
  11. #include <Wire.h>   //import de la librairie Wire qui permet la communication en I2C
  12. #include <string.h> //import de la librairie string qui servira pour la conversion des données sortant du Nunchuck
  13. #include <utility\twi.h>
  14. #undef int
  15. #include <stdio.h>
  16. uint8_t outbuf[WII_TELEGRAM_LEN]; //on indique que les information seront de 8bits
  17. int cnt = 0;
  18. void setup ()
  19. {
  20. Serial.begin (19200);
  21. Serial.print("Start");
  22. Mouse.begin(); //on indique que le protocole de prise de contrôle de la souris commence
  23.  
  24. #ifdef POWER_VIA_PORT_C2_C3 //l'alimentation
  25. delay(100);
  26. #endif
  27.  
  28. Wire.begin(); // la connection i2C s'initialise  
  29. #define TWI_FREQ_NUNCHUCK 400000L //on indique la fréquence de l'horloge du nunchuck
  30.  
  31.  
  32. nunchuck_init(0);
  33. byte i;
  34. if(readControllerIdent(outbuf) == 0)
  35. {
  36. Serial.print("Ident=");
  37. for (i = 0; i < WII_TELEGRAM_LEN; i++)
  38. {
  39. Serial.print(outbuf[i], HEX);
  40. Serial.print(' ');
  41. }
  42. Serial.println();
  43. }
  44.  
  45. Serial.println("Finished setup");
  46. }
  47.  
  48. byte nunchuck_init (unsigned short timeout)
  49. {
  50. byte rc = 1;
  51.  
  52. #ifndef USE_NEW_WAY_INIT
  53. Wire.beginTransmission (WII_NUNCHUCK_TWI_ADR);
  54. Wire.write (0x40);
  55. Wire.write (0x00);
  56. Wire.endTransmission ();
  57. #else
  58.  
  59.  
  60. unsigned long time = millis();
  61. do
  62. {
  63. Wire.beginTransmission (WII_NUNCHUCK_TWI_ADR);
  64. Wire.write (0xF0);
  65. Wire.write (0x55);
  66. if(Wire.endTransmission() == 0)
  67. {
  68. Wire.beginTransmission (WII_NUNCHUCK_TWI_ADR);
  69. Wire.write (0xFB);
  70. Wire.write (0x00);
  71. if(Wire.endTransmission () == 0)
  72. rc = 0;
  73. }
  74. }
  75. while (rc != 0 && (!timeout || ((millis() - time) < timeout)));
  76. #endif
  77.  
  78. return rc;
  79. }
  80.  
  81.  
  82.  
  83. byte readControllerIdent(byte* pIdent)
  84. {
  85. byte rc = 1;
  86.  
  87. Wire.beginTransmission (WII_NUNCHUCK_TWI_ADR);
  88. Wire.write (0xFA);
  89. if(Wire.endTransmission () == 0)
  90. {
  91. byte i;
  92. Wire.requestFrom (WII_NUNCHUCK_TWI_ADR, WII_TELEGRAM_LEN);
  93. for (i = 0; (i < WII_TELEGRAM_LEN) && Wire.available (); i++)
  94. {
  95. pIdent[i] = Wire.read();
  96. }
  97. if(i == WII_TELEGRAM_LEN)
  98. {
  99. rc = 0;
  100. }
  101. }
  102. return rc;
  103. }
  104.  
  105. void clearTwiInputBuffer(void)
  106. {
  107.  
  108. while( Wire.available ())
  109. Wire.read ();
  110. }
  111.  
  112.  
  113. void write_zero ()
  114. {
  115.  
  116. for(byte i = 0; i < 3; i++)
  117. {
  118. Wire.beginTransmission (WII_NUNCHUCK_TWI_ADR);
  119. Wire.write (0x00);
  120. Wire.endTransmission ();
  121. }
  122. }
  123.  
  124. void loop ()
  125. {
  126. Wire.requestFrom (WII_NUNCHUCK_TWI_ADR, WII_TELEGRAM_LEN);
  127. for (cnt = 0; (cnt < WII_TELEGRAM_LEN) && Wire.available (); cnt++)
  128. {
  129. outbuf[cnt] = nunchuk_decode_byte (Wire.read ());
  130. digitalWrite (ledPin, HIGH);
  131. }
  132.  
  133. // debug en cas de problème
  134. #ifdef DEBUG_RCV_TEL
  135. Serial.print("avail=");
  136. Serial.print(Wire.available());
  137. Serial.print(" cnt=");
  138. Serial.println(cnt);
  139. #endif
  140.  
  141. clearTwiInputBuffer();
  142.  
  143.  
  144. if (cnt >= WII_TELEGRAM_LEN)
  145. {
  146. print ();
  147. }
  148.  
  149. write_zero ();
  150. delay (20);
  151. }
  152.  
  153.  
  154. void print ()
  155. {
  156. int joy_x_axis = outbuf[0];
  157. int joy_y_axis = outbuf[1];
  158. int accel_x_axis = outbuf[2] * 2 * 2;
  159. int accel_y_axis = outbuf[3] * 2 * 2;
  160. int accel_z_axis = outbuf[4] * 2 * 2;
  161.  
  162. int z_button = 0;
  163. int c_button = 0;
  164.  
  165.  
  166. if ((outbuf[5] >> 0) & 1)
  167. {
  168. z_button = 1;
  169. }
  170. if ((outbuf[5] >> 1) & 1)
  171. {
  172. c_button = 1;
  173. }
  174.  
  175. if ((outbuf[5] >> 2) & 1)
  176. {
  177. accel_x_axis += 2;
  178. }
  179. if ((outbuf[5] >> 3) & 1)
  180. {
  181. accel_x_axis += 1;
  182. }
  183.  
  184. if ((outbuf[5] >> 4) & 1)
  185. {
  186. accel_y_axis += 2;
  187. }
  188. if ((outbuf[5] >> 5) & 1)
  189. {
  190. accel_y_axis += 1;
  191. }
  192.  
  193. if ((outbuf[5] >> 6) & 1)
  194. {
  195. accel_z_axis += 2;
  196. }
  197. if ((outbuf[5] >> 7) & 1)
  198. {
  199. accel_z_axis += 1;
  200. }
  201.  
  202. //Partie permettant de bouger la souris lorsque le joystick est bougé
  203. if (joy_x_axis >140) { //Boucle conditionnelle Si l'axe du joystick en x est supérieur a 140 alors
  204.  Serial.println("Droite"); //il affiche droite dans la console
  205.  Mouse.move(10, 0, 0); //il bouge la souris de 10 pixels sur l'axe X par rapport à la position actuel de la souris
  206. }
  207. if (joy_x_axis >140) {
  208.  Serial.println("Droite");
  209.  Mouse.move(10, 0, 0);
  210. }
  211. if (joy_x_axis <140) {
  212.  Serial.println("Gauche");
  213.   Mouse.move(-10, 0, 0);
  214. }
  215. if (joy_y_axis >134) {
  216.  Serial.println("Haut");
  217.   Mouse.move(0, -10, 0);
  218. }
  219. if (joy_y_axis <134) {
  220.  Serial.println("Bas");
  221.   Mouse.move(0, 10, 0);
  222. }
  223.  
  224. /*if(c_button = 0) {
  225.    Serial.print("Clique Gauche");
  226.    if (!Mouse.isPressed(MOUSE_LEFT)) {
  227.       Mouse.press(MOUSE_LEFT);
  228.    }
  229. if(c_button = 1) {
  230.     if (Mouse.isPressed(MOUSE_LEFT)) {
  231.       Mouse.release(MOUSE_LEFT);
  232.     }
  233.   }
  234. }
  235.  
  236. if(z_button = 0) {
  237.    Serial.print("Clique Droit");
  238.       if (!Mouse.isPressed(MOUSE_RIGHT)) {
  239.       Mouse.press(MOUSE_RIGHT);
  240.    }
  241. if(z_button = 1) {
  242.     if (Mouse.isPressed(MOUSE_RIGHT)) {
  243.       Mouse.release(MOUSE_RIGHT);
  244.     }
  245.   }
  246. }
  247.  
  248. }
  249. */
  250.  
  251.  
  252. char nunchuk_decode_byte (char x)
  253. {
  254. #ifndef USE_NEW_WAY_INIT
  255. x = (x ^ 0x17) + 0x17;
  256. #endif
  257. return x;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement