Advertisement
IrvinHeslan

Arduino_Souris_Acceleromètre_Nunchuck_1

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