Advertisement
xerpi

NXTDuino beta 1

Sep 23rd, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.68 KB | None | 0 0
  1. /* ------------------------------ NXTDUINO.NXC ------------------------------ */
  2.  
  3.  
  4. #define PORT    S1
  5. #define ADDR    0x22<<1
  6. #define BUF_LEN 16
  7.  
  8. enum
  9. {
  10.     LOW = 0,
  11.     HIGH = 1
  12. };
  13.  
  14. enum
  15. {
  16.     INPUT = 0,
  17.     OUTPUT = 1
  18. };
  19.  
  20. enum
  21. {
  22.     CMD_PINMODE = 0x11, //{PIN, INPUT/OUTPUT}
  23.     CMD_DIGITALWRITE, //{PIN, HIGH/LOW}
  24. };
  25.  
  26. //NXTDuino
  27.     void pinMode(int pin, int what);
  28.     void digitalWrite(int pin, int what);
  29. //
  30.  
  31. byte outBuf[] = {ADDR, 0,0,0};  //NXT --> Arduino
  32. byte inBuf [16];                //Arduino --> NXT
  33.  
  34. int inBytes  = 0;   //Received bytes
  35. int outBytes = 0;   //Bytes to send
  36. long status = 0;
  37. int bytesReady = 1;
  38.  
  39.  
  40. void waitPending();
  41. void printStatus();
  42.  
  43.  
  44. task main()
  45. {
  46.     SetSensorLowspeed(PORT, false);
  47.     pinMode(12, OUTPUT);
  48.  
  49.    
  50.     while (true) {
  51.         ClearScreen();
  52.  
  53.         printStatus();
  54.        
  55.         //TextOut(0, LCD_LINE3, I2CDeviceInfo(PORT, ADDR, I2C_REG_DEVICE_ID));
  56.         //TextOut(0, LCD_LINE4, I2CDeviceInfo(PORT, ADDR, I2C_REG_VENDOR_ID));
  57.         //TextOut(0, LCD_LINE5, I2CDeviceInfo(PORT, ADDR, I2C_REG_VERSION));       
  58.        
  59.         TextOut(0, LCD_LINE7, StrCat("Received: ", NumToStr(bytesReady)));
  60.         TextOut(0, LCD_LINE8, StrCat("inBuf[0]: ", NumToStr(inBuf[0])));
  61.        
  62.         digitalWrite(12, HIGH);
  63.         Wait(500);
  64.         digitalWrite(12, LOW);
  65.         Wait(500);     
  66.        
  67.     }
  68. }
  69.  
  70.  
  71. //NXTDuino
  72.     void pinMode(int pin, int what)
  73.     {
  74.         outBuf[1] = CMD_PINMODE;
  75.         outBuf[2] = pin;
  76.         outBuf[3] = what;
  77.         I2CWrite(PORT, 0x0, outBuf);
  78.     }
  79.    
  80.     void digitalWrite(int pin, int what)
  81.     {
  82.         outBuf[1] = CMD_DIGITALWRITE;
  83.         outBuf[2] = pin;
  84.         outBuf[3] = what;
  85.         I2CWrite(PORT, 0x0, outBuf);   
  86.     }
  87. //
  88.  
  89. void waitPending()
  90. {
  91.     while(I2CCheckStatus(PORT) == STAT_COMM_PENDING)
  92.     {
  93.         Wait(1);
  94.     }
  95. }
  96. void printStatus()
  97. {
  98.     if(I2CCheckStatus(PORT) == NO_ERR)
  99.     {
  100.         TextOut(0, LCD_LINE1, "CONNECTED");
  101.     }
  102.     else
  103.     {
  104.         TextOut(0, LCD_LINE1, "NOT CONNECTED");
  105.     }
  106. }
  107.  
  108. /* ------------------------------ NXTDUINO.INO ------------------------------ */
  109.  
  110. #include <Wire.h>
  111.  
  112. #define ADDR 0x22 // the double in the NXT (e.g. Arduino: 0x13, NXT: 0x26)
  113.  
  114. enum
  115. {
  116.     CMD_PINMODE = 0x11, //{PIN, INPUT/OUTPUT}
  117.     CMD_DIGITALWRITE,   //{PIN, HIGH/LOW}
  118. };
  119.  
  120. byte SensorName[9] = "Arduino ";
  121. byte SensorVersion[9] = "v1.0    ";
  122. byte SensorType[9] = "amazin  ";
  123.  
  124. void requestEvent(void);
  125. void receiveEvent(int howMany);
  126.  
  127. byte bufferReceived[16];
  128. byte bufferToSend[16];
  129. byte receivedCount = 0;
  130.  
  131. void setup()
  132. {
  133.     Serial.begin(115200);
  134.     Wire.begin(ADDR);
  135.     Wire.onRequest(requestEvent);
  136.     Wire.onReceive(receiveEvent);
  137.     Serial.println("NXTDuino Inited");
  138. }
  139.  
  140. void loop()
  141. {
  142.     delay(100);
  143. }
  144.  
  145. void requestEvent(void)
  146. {
  147.     Serial.println("Data request!\n");
  148.    
  149.     if(receivedCount == 0) //What does this guy want?
  150.     {
  151.         Wire.write(0x0);
  152.         return;
  153.     }
  154.    
  155.     //First byte (aka Register)
  156.         switch(bufferReceived[0])
  157.         {
  158.             //"Sensor" stuff
  159.                 case 0x0: //Version
  160.                     Wire.write(SensorVersion, 8);
  161.                     break;
  162.                 case 0x08: //Name
  163.                     Wire.write(SensorName, 8);
  164.                     break;
  165.                 case 0x10: //Type
  166.                     Wire.write(SensorType, 8);
  167.                     break;
  168.                
  169.                                        
  170.             //What? Nothing!?
  171.                 default:
  172.                     Wire.write(0x0);
  173.                     break;
  174.         }
  175. }
  176.  
  177. void receiveEvent(int howMany)
  178. {
  179.     Serial.print("-->Received: ");
  180.     Serial.print(howMany, HEX);
  181.     Serial.println(" byte(s)\n");
  182.    
  183.     for(receivedCount = 0; Wire.available() > 0; receivedCount++)
  184.     {
  185.         bufferReceived[receivedCount] = Wire.read();
  186.         //Serial.println(bufferReceived[receivedCount], HEX);
  187.     }
  188.    
  189.     switch(bufferReceived[0])
  190.     {
  191.         //Commands, from 0x11 to 0xFF
  192.        
  193.             case 0x11:  //pinMode
  194.                 pinMode(bufferReceived[1], bufferReceived[2]);
  195.                 Wire.write(0x1);
  196.                 break;         
  197.             case 0x12:  //digitalWrite
  198.                 digitalWrite(bufferReceived[1], bufferReceived[2]);
  199.                 Wire.write(0x1);
  200.                 break;
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement