Advertisement
xerpi

NXTDuino alpha 1

Sep 23rd, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. #define ADDR 0x22 // the double in the NXT (e.g. Arduino: 0x13, NXT: 0x26)
  4.  
  5. byte SensorName[9] = "Arduino ";
  6. byte SensorVersion[9] = "v1.0    ";
  7. byte SensorType[9] = "amazin  ";
  8.  
  9. void requestEvent(void);
  10. void receiveEvent(int howMany)
  11.  
  12. byte bufferReceived[16];
  13. byte bufferToSend[16];
  14. byte receivedCount = 0;
  15.  
  16. void setup()
  17. {
  18.     Serial.begin(115200);
  19.     Wire.begin(ADDR);
  20.     Wire.onRequest(requestEvent);
  21.     Wire.onReceive(receiveEvent);
  22.     Serial.println("NXTDuino Inited");
  23. }
  24.  
  25. void loop()
  26. {
  27.     delay(100);
  28. }
  29.  
  30. void requestEvent(void)
  31. {
  32.     Serial.println("Data request!\n");
  33.    
  34.     if(receivedCount == 0) //What does this guy want?
  35.     {
  36.         Wire.write(0x0);
  37.         return;
  38.     }
  39.    
  40.     //First byte (aka Register)
  41.         switch(bufferReceived[0])
  42.         {
  43.             //"Sensor" stuff
  44.                 case 0x0: //Version
  45.                     Wire.write(SensorVersion, 8);
  46.                     break;
  47.                 case 0x08: //Name
  48.                     Wire.write(SensorName, 8);
  49.                     break;
  50.                 case 0x10: //Type
  51.                     Wire.write(SensorType, 8);
  52.                     break;
  53.                    
  54.                    
  55.             //Commands, from 0x11 to 0xFF
  56.            
  57.                 case 0x11:
  58.                     Wire.write(0x1);
  59.                     break;
  60.                    
  61.                    
  62.             //What? Nothing!?
  63.                 default:
  64.                     Wire.write(0x0);
  65.                     break;
  66.         }
  67. }
  68.  
  69. void receiveEvent(int howMany)
  70. {
  71.     Serial.print("-->Received: ");
  72.     Serial.print(howMany, HEX);
  73.     Serial.println(" byte(s)\n");
  74.    
  75.     for(receivedCount = 0; Wire.available() > 0; receivedCount++)
  76.     {
  77.         bufferReceived[receivedCount] = Wire.read();
  78.         //Serial.println(bufferReceived[receivedCount], HEX);
  79.        
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement