/* ************************************************************************* * Program ID : TelnetClient_I2CSlave.pde * Created by CH, Chen (2011.9.11/Taiwan) * Two roles of this device: * 1. I2C Slave: Receive request from Master device(NXT or another Arduino) * 2. Telent client: According to the request from the I2C master, * build command string and then transfer to telnet server for executing. * ************************************************************************* */ #include #include #include // I2C Slave address #define THIS_DEVICE 0x31 // I2C Register layout #define THIS_DEVICE_INFO 0x00 #define TELNET_DISPLAY_RGB 0x41 #define TELNET_DISPLAY_TEXT 0x42 #define TELNET_DISPLAY_INTEGER 0x43 #define TELNET_GET_TEMPERATURE 0x44 #define TELNET_READ_TEMPERATURE 0x45 #define MAX_DATA_LENGTH 8 char receiveBuf[MAX_DATA_LENGTH+1]; byte requestRegister, requestCommand; boolean isDisplayRGB=false, isDisplayText=false, isDisplayInteger=false; uint8_t deviceInfo[9] = "TELNET-S"; // Ethernet shield byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x52, 0xE0}; byte ip[] = {192, 168, 11, 112}; byte server[] = {192, 168, 11, 110}; Client client(server, 23); // Telenet formatting data code const char COMMAND_LEADING_CODE = '@'; const char COMMAND_DISPLAY_RGB = 'L'; const char COMMAND_DISPLAY_TEXT = 'T'; const char COMMAND_DISPLAY_INTEGER = 'N'; const char COMMAND_ENDING_CODE = '%'; void setup() { Serial.begin(9600); Ethernet.begin(mac, ip); delay(1000); Wire.begin(THIS_DEVICE); Wire.onReceive(receiveEvent); Wire.onRequest(requestEvent); } void loop() { if (isDisplayRGB) requestDisplayRGB(receiveBuf); else if (isDisplayText) requestDisplayText(receiveBuf); else if (isDisplayInteger) requestDisplayInteger(atoi(receiveBuf)); } // I2C - onReceive() Handler void receiveEvent(int howMany) { int byteCnt = 0; requestRegister = Wire.receive(); int nbytesReady = Wire.available(); switch (requestRegister) { case TELNET_DISPLAY_RGB: isDisplayRGB = true; isDisplayText = false; isDisplayInteger = false; for (byteCnt=0; byteCnt