/* ******************************************************** * Program ID: NXT_7SEG_DEMO.nxc * Created by: CH, Chen (Taiwan) * Date : 2011.9.11 * ******************************************************** */ #define TELNET_DEVICE 0x31 #define TELNET_DEVICE_SEND 0x62 #define TELNET_DEVICE_INFO 0x00 #define REQUEST_DISPLAY_RGB 0x41 #define REQUEST_DISPLAY_TEXT 0x42 #define REQUEST_DISPLAY_INTEGER 0x43 #define REQUEST_GET_TEMPERATURE 0x44 #define REQUEST_READ_TEMPERATURE 0x45 #define I2C_PORT S1 void sendRequestCommand(const byte deviceRegister, byte transferBuf[]) { byte I2C_req_buf[]; ArrayBuild(I2C_req_buf, TELNET_DEVICE_SEND, deviceRegister, transferBuf); while(I2CCheckStatus(I2C_PORT) == STAT_COMM_PENDING); LowspeedWrite(I2C_PORT, 0, I2C_req_buf); } string getDeviceInfo() { byte I2C_req_buf[], I2C_get_buf[], num_of_bytes=8; short status_code; ArrayBuild(I2C_req_buf, TELNET_DEVICE_SEND, TELNET_DEVICE_INFO); ArrayInit( I2C_get_buf, " " , 9); while(I2CCheckStatus(I2C_PORT) == STAT_COMM_PENDING); status_code = I2CBytes(I2C_PORT, I2C_req_buf, num_of_bytes, I2C_get_buf); if (status_code < 0) return "Failed !"; else return (ByteArrayToStr(I2C_get_buf)); } void requestDisplayRGB(string strRGB) { TextOut(0, LCD_LINE5, strRGB); byte transferBuf[]; StrToByteArray(strRGB, transferBuf); sendRequestCommand(REQUEST_DISPLAY_RGB, transferBuf); } void requestDisplayTEXT(string strText) { TextOut(0, LCD_LINE5, strText); byte transferBuf[]; StrToByteArray(strText, transferBuf); sendRequestCommand(REQUEST_DISPLAY_TEXT, transferBuf); } void requestDisplayINTEGER(int intVal) { NumOut(0, LCD_LINE5, intVal); byte transferBuf[]; StrToByteArray(NumToStr(intVal), transferBuf); sendRequestCommand(REQUEST_DISPLAY_INTEGER, transferBuf); } task main() { SetSensorLowspeed(I2C_PORT); int intVal = 0; bool isIncrement = TRUE; while (TRUE) { ClearScreen(); TextOut(0, LCD_LINE1, " NXT-I2C-Telnet " ); TextOut(0, LCD_LINE2, " " ); TextOut(0, LCD_LINE3, " " ); TextOut(0, LCD_LINE4, " " ); TextOut(0, LCD_LINE5, " " ); TextOut(0, LCD_LINE6, " " ); TextOut(0, LCD_LINE7, " " ); TextOut(0, LCD_LINE8, " Run" ); //until (ButtonPressed(BTNRIGHT, TRUE)); //Wait(50); // Get telentClient_I2CSlave device info TextOut(0, LCD_LINE3, "Get TelnetClient" ); TextOut(0, LCD_LINE5, getDeviceInfo()); TextOut(0, LCD_LINE8, "Next " ); Wait(2000); //until (ButtonPressed(BTNLEFT, TRUE)); //Wait(50); // Request display RGB LED TextOut(0, LCD_LINE3, "Tri-Color LED: " ); TextOut(0, LCD_LINE5, " " ); requestDisplayRGB("RGB" ); TextOut(0, LCD_LINE8, " Next" ); Wait(3000); //until (ButtonPressed(BTNRIGHT, TRUE)); //Wait(50); // Request display text TextOut(0, LCD_LINE3, "Text on 7SEG LED" ); TextOut(0, LCD_LINE5, " " ); requestDisplayTEXT("LEGO" ); TextOut(0, LCD_LINE8, "Next " ); Wait(5000); //until (ButtonPressed(BTNLEFT, TRUE)); //Wait(50); // Request display Integer TextOut(0, LCD_LINE3, "Int on 7SEG LED " ); TextOut(0, LCD_LINE5, " " ); requestDisplayINTEGER(intVal); TextOut(0, LCD_LINE8, " Next" ); Wait(4000); //until (ButtonPressed(BTNRIGHT, TRUE)); //Wait(50); if (isIncrement) { intVal += 1; if (intVal == 10) isIncrement = FALSE; } else { intVal -= 1; if (intVal == -10) isIncrement = TRUE; } } }