/* ******************************************************** * Program ID: NXT_MSG_BOARD * Created by: CH, Chen (Taiwan) * Date : 2012.7.22 * ******************************************************** */ #define I2C_MSG_BOARD 0x62 #define MBOD_SW_VERSION 0x00 #define MBOD_VENDOR_ID 0x08 #define MBOD_DEVICE_ID 0x10 #define IS_NEXT_DISPLAY 0x40 #define SET_STR_BACK_COLOR 0x41 #define DISPLAY_BLACK_FONT 0x42 #define DISPLAY_RED_FONT 0x43 #define DISPLAY_GREEN_FONT 0x44 #define DISPLAY_BLUE_FONT 0x45 #define DISPLAY_BROWN_FONT 0x46 #define DISPLAY_PURPLE_FONT 0x47 #define DISPLAY_TEAL_FONT 0x48 #define DISPLAY_WHITE_FONT 0x49 #define DISPLAY_CODE 0x50 //#define SET_CODE_FONT_COLOR 0x51 //#define SET_CODE_BACK_COLOR 0x52 #define CODE_1_GOGO '1' #define CODE_9_TOUCH '9' #define RGB_COLOR_BLACK 'b' #define RGB_COLOR_RED 'R' #define RGB_COLOR_GREEN 'G' #define RGB_COLOR_BLUE 'B' #define RGB_COLOR_BROWN 'r' #define RGB_COLOR_PURPLE 'P' #define RGB_COLOR_TEAL 'T' #define RGB_COLOR_WHITE 'W' #define I2C_PORT S1 byte displayText[]; string strLego = "LEGO"; string strNXT = "Mindstorms NXT"; string strWin = "Win!"; string strLuck = "Good Luck!"; string strHBD = "Happy Birthday"; /* ******************************************************** * Run NXT Message board command * ******************************************************** */ void sendDeviceCommand(const byte deviceAddress, const byte deviceRegister, const byte deviceCommand) { byte I2C_req_buf[]; ArrayBuild(I2C_req_buf, deviceAddress, deviceRegister, deviceCommand); while(I2CCheckStatus(I2C_PORT) == STAT_COMM_PENDING); LowspeedWrite(I2C_PORT, 0, I2C_req_buf); } /* ******************************************************** * * ******************************************************** */ string getDeviceInfo(const byte deviceAddress, const byte deviceRegister, byte num_of_bytes) { byte I2C_req_buf[], I2C_get_buf[]; short status_code; ArrayBuild(I2C_req_buf, deviceAddress, deviceRegister); 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)); } /* ******************************************************** * * ******************************************************** */ bool isNextDisplay() { byte I2C_req_buf[], I2C_get_buf[]; byte num_of_bytes=1; short status_code; ArrayBuild(I2C_req_buf, I2C_MSG_BOARD, IS_NEXT_DISPLAY); ArrayInit( I2C_get_buf, ' ' , 1); 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 FALSE; else if (I2C_get_buf[0] == 'Y') return TRUE; else return FALSE; } /* ******************************************************** * Set back color of string * ******************************************************** */ void setStringBackColor(const byte rgb_color) { sendDeviceCommand(I2C_MSG_BOARD, SET_STR_BACK_COLOR, rgb_color); } /* ******************************************************** * Display self-defined font * ******************************************************** */ void displayCodeOnMsgBoard(const byte displayCode, const byte codeFontColor, const byte codeBackColor) { TextOut(0, LCD_LINE4, "Display fontcode"); byte I2C_req_buf[]; ArrayBuild(I2C_req_buf, I2C_MSG_BOARD, DISPLAY_CODE, displayCode, codeFontColor, codeBackColor); while(I2CCheckStatus(I2C_PORT) == STAT_COMM_PENDING); LowspeedWrite(I2C_PORT, 0, I2C_req_buf); } /* ******************************************************** * Display string * ******************************************************** */ void displayTextOnMsgBoard(const byte fontColorRegister, string displayString) { TextOut(0, LCD_LINE4, " "); TextOut(0, LCD_LINE4, displayString); byte displayText[]; StrToByteArray(displayString, displayText); byte I2C_req_buf[]; ArrayBuild(I2C_req_buf, I2C_MSG_BOARD, fontColorRegister, displayText); while(I2CCheckStatus(I2C_PORT) == STAT_COMM_PENDING); LowspeedWrite(I2C_PORT, 0, I2C_req_buf); } /* ******************************************************** * * ******************************************************** */ task main() { SetSensorLowspeed(I2C_PORT); while (TRUE) { ClearScreen(); TextOut(0, LCD_LINE1, "NXT Msg Board "); TextOut(0, LCD_LINE2, "Device Info: "); TextOut(0, LCD_LINE3, " "); TextOut(0, LCD_LINE4, "SW Ver: "); TextOut(0, LCD_LINE5, "Vendor: "); TextOut(0, LCD_LINE6, "Device: "); TextOut(0, LCD_LINE7, " "); //TextOut(0, LCD_LINE8, " Run"); //until (ButtonPressed(BTNRIGHT, TRUE)); //Wait(50); TextOut(48, LCD_LINE4, getDeviceInfo(I2C_MSG_BOARD, MBOD_SW_VERSION, 8)); TextOut(48, LCD_LINE5, getDeviceInfo(I2C_MSG_BOARD, MBOD_VENDOR_ID, 8)); TextOut(48, LCD_LINE6, getDeviceInfo(I2C_MSG_BOARD, MBOD_DEVICE_ID, 8)); Wait(3000); //TextOut(0, LCD_LINE8, "Next "); //until (ButtonPressed(BTNLEFT, TRUE)); //Wait(50); TextOut(0, LCD_LINE2, "Disply text: "); TextOut(0, LCD_LINE3, " "); TextOut(0, LCD_LINE4, " "); TextOut(0, LCD_LINE5, " "); TextOut(0, LCD_LINE6, " "); TextOut(0, LCD_LINE7, " "); while (TRUE) { //while(!isNextDisplay()) Wait(100); displayTextOnMsgBoard(DISPLAY_RED_FONT, strHBD); Wait(100); //while(!isNextDisplay()) Wait(100); //displayTextOnMsgBoard(DISPLAY_RED_FONT, strLego); //Wait(100); //while(!isNextDisplay()) Wait(100); //displayTextOnMsgBoard(DISPLAY_WHITE_FONT, strNXT); //Wait(100); //while(!isNextDisplay()) Wait(100); //displayCodeOnMsgBoard(CODE_9_TOUCH, // RGB_COLOR_GREEN, RGB_COLOR_BLACK); //Wait(100); //while(!isNextDisplay()) Wait(100); //displayCodeOnMsgBoard(CODE_1_GOGO, // RGB_COLOR_RED, RGB_COLOR_BLACK); //Wait(100); } } }