Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ------------------------------ NXTDUINO.NXC ------------------------------ */
- #define PORT S1
- #define ADDR 0x22<<1
- #define BUF_LEN 16
- enum{LOW = 0, HIGH = 1};
- enum{INPUT = 0, OUTPUT = 1};
- //Macro functions
- #define BytesToShort(byte1,byte2) (byte1 | (byte2<<8))
- #define ShortHI(val) (val>>8)
- #define ShortLO(val) (val & 0xFF)
- //
- enum
- {
- CMD_PINMODE = 0x11, //{PIN, INPUT/OUTPUT}
- CMD_DIGITALWRITE, //{PIN, HIGH/LOW}
- CMD_DIGITALREAD, //{PIN} -> (byte)digitalRead(PIN)
- CMD_ANALOGWRITE, //{PIN,(int)VALUE}
- CMD_ANALOGREAD, //{PIN} -> (int)analogRead(PIN)
- };
- //NXTDuino
- void pinMode(int pin, int what);
- void digitalWrite(int pin, int what);
- int digitalRead(int pin);
- void analogWrite(int pin, int value);
- int analogRead(int pin);
- //
- byte outBuf[8] = {ADDR, 0,0,0, 0}; //NXT --> Arduino
- byte inBuf [8] = {0 ,0 ,0, 0}; //Arduino --> NXT
- int inBytes = 0; //Received bytes
- int outBytes = 0; //Bytes to send
- long status = 0;
- int bytesReady = 1;
- inline void waitPending(){while(I2CCheckStatus(PORT) == STAT_COMM_PENDING) Wait(1);}
- void printStatus();
- task main()
- {
- SetSensorLowspeed(PORT, false);
- //pinMode(8, INPUT);
- //Wait(100);
- int value = 0;
- while (true)
- {
- ClearScreen();
- printStatus();
- value = analogRead(0);
- TextOut(0, LCD_LINE5, NumToStr(value));
- TextOut(0, LCD_LINE7, StrCat("inBuf[0]: ", NumToStr(inBuf[0])));
- TextOut(0, LCD_LINE8, StrCat("inBuf[1]: ", NumToStr(inBuf[1])));
- //TextOut(0, LCD_LINE6, StrCat("Received: ", NumToStr(I2CBytesReady(PORT))));
- Wait(50);
- }
- }
- //NXTDuino functions
- void pinMode(int pin, int what)
- {
- ArrayBuild(outBuf, ADDR, CMD_PINMODE, pin, what);
- I2CWrite(PORT, 0x0, outBuf);
- }
- void digitalWrite(int pin, int what)
- {
- ArrayBuild(outBuf, ADDR, CMD_DIGITALWRITE, pin, what);
- I2CWrite(PORT, 0x0, outBuf);
- }
- int digitalRead(int pin)
- {
- ArrayBuild(outBuf, ADDR, CMD_DIGITALREAD, pin);
- I2CWrite(PORT, 0x1, outBuf);
- while(I2CBytesReady(PORT) == 0);
- I2CRead(PORT, 0x1, inBuf);
- return inBuf[0];
- }
- void analogWrite(int pin, int value)
- {
- ArrayBuild(outBuf, ADDR, CMD_ANALOGWRITE, pin, value);
- I2CWrite(PORT, 0x0, outBuf);
- }
- int analogRead(int pin)
- {
- ArrayBuild(outBuf, ADDR, CMD_ANALOGREAD, pin);
- I2CWrite(PORT, 0x2, outBuf);
- waitPending();
- I2CRead(PORT, 0x2, inBuf);
- return BytesToShort(inBuf[0], inBuf[1]);
- }
- //
- void printStatus()
- {
- if(I2CCheckStatus(PORT) == NO_ERR)
- {
- TextOut(0, LCD_LINE1, "CONNECTED");
- }
- else
- {
- TextOut(0, LCD_LINE1, "NOT CONNECTED");
- }
- }
- /* ------------------------------ NXTDUINO.INO ------------------------------ */
- #include <Wire.h>
- #define ADDR 0x22 // the double in the NXT (e.g. Arduino: 0x13, NXT: 0x26)
- //Macro functions
- #define BytesToShort(byte1,byte2) (byte1 | (byte2<<8))
- #define ShortHI(val) (val>>8)
- #define ShortLO(val) (val & 0xFF)
- //
- enum
- {
- CMD_PINMODE = 0x11, //{PIN, INPUT/OUTPUT}
- CMD_DIGITALWRITE, //{PIN, HIGH/LOW}
- CMD_DIGITALREAD, //{PIN} -> (byte)digitalRead(PIN)
- CMD_ANALOGWRITE, //{PIN,(int)VALUE}
- CMD_ANALOGREAD, //{PIN} -> (int)analogRead(PIN)
- };
- byte SensorName[9] = "Arduino ";
- byte SensorVersion[9] = "v1.0 ";
- byte SensorType[9] = "amazin ";
- void requestEvent(void);
- void receiveEvent(int howMany);
- byte bufferReceived[16];
- byte bufferToSend[16];
- byte receivedCount = 0;
- void setup()
- {
- Serial.begin(115200);
- Wire.begin(ADDR);
- Wire.onRequest(requestEvent);
- Wire.onReceive(receiveEvent);
- Serial.println("NXTDuino Inited");
- }
- void loop()
- {
- delay(100);
- }
- void requestEvent(void)
- {
- Serial.println("Data request!\n");
- //Serial.println(bufferReceived[0], HEX);
- int value;
- //First byte (aka Register)
- switch(bufferReceived[0])
- {
- //"Sensor" stuff
- case 0x0: //Version
- Wire.write(SensorVersion, 8);
- break;
- case 0x08: //Name
- Wire.write(SensorName, 8);
- break;
- case 0x10: //Type
- Wire.write(SensorType, 8);
- break;
- //Read commands, from 0x11 to 0xFF
- case CMD_DIGITALREAD: //digitalRead
- Wire.write(digitalRead(bufferReceived[1]));
- break;
- case CMD_ANALOGREAD: //digitalRead
- value = analogRead(bufferReceived[1]);
- Wire.write((byte[]){ShortLO(value), ShortHI(value)}, 2);
- break;
- //What? Nothing!?
- default:
- Wire.write(0x0);
- break;
- }
- }
- void receiveEvent(int howMany)
- {
- Serial.print("-->Received: ");
- Serial.print(howMany, HEX);
- Serial.println(" byte(s)\n");
- for(receivedCount = 0; Wire.available() > 0; receivedCount++)
- {
- bufferReceived[receivedCount] = Wire.read();
- //Serial.println(bufferReceived[receivedCount], HEX);
- }
- switch(bufferReceived[0])
- {
- //Write only commands, from 0x11 to 0xFF
- case CMD_PINMODE: //pinMode
- pinMode(bufferReceived[1], bufferReceived[2]);
- break;
- case CMD_DIGITALWRITE: //digitalWrite
- digitalWrite(bufferReceived[1], bufferReceived[2]);
- break;
- case CMD_ANALOGWRITE: //analogWrite
- analogWrite(bufferReceived[1], bufferReceived[2]);
- break;
- //Maybe a read command, maybe nothing...
- default:
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement