Advertisement
xerpi

NXTDuino v1

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