/* ************************************************************ * TP-BT-Master.nxc * CH, Chen(Taiwan) * 2012.4.25 * ************************************************************ */ #define BT_CHANNEL 1 #define SLAVE_DEVICE "CH_NXT_3" #define SLAVE_PROGRAM "TP-BT-Slave.rxe" #define TOUCH_PANEL 0x04 #define TOUCH_STATUS 0x42 #define TOUCH_X 0x42 #define TOUCH_Y 0x43 #define TOUCH_BUTTON 0x44 #define GESTURE_DATA 0x4D #define GESTURE_SIZE 0x4D #define GESTURE_X 0x4E #define GESTURE_Y 0x4F #define TOUCH_PANEL_PORT S3 struct vectorCoordinate { int x; int y; }; /* ************************************************************ * Build_Connection() function * 1. Search index of slave device name in bluetooth contact list * 2. Call SysCommExecuteFunction(CommExecuteFunctionType cefArgs) * to connect with slave device * ************************************************************ */ #define MUST_WAIT_5_SECONDS 5 bool build_Connection(byte BtChannel, const string BtDevname) { string msg; bool isOK = FALSE; short idx = 0, cmpLen = strlen(BtDevname); ClearScreen(); TextOut(0, LCD_LINE1, "Build Connection"); TextOut(0, LCD_LINE2, StrCat(" with ", BtDevname)); // Search index of Slave device in BT contact list TextOut(0, LCD_LINE4, "Searching Slave "); for(short xii=0; xii0; xjj--) { NumOut(42, LCD_LINE5, xjj); PlayTone(440,100); Wait(1000); } NumOut(42, LCD_LINE5, 0); PlayTone(440,1000); Wait(1000); isOK = TRUE; break; } else { TextOut(0, LCD_LINE6, "Re-try: "); NumOut(48, LCD_LINE6, xii); TextOut(0, LCD_LINE7, "Result: "); NumOut(48, LCD_LINE7, cefArgs.Result); } } if(!isOK) { TextOut(0, LCD_LINE5, " Failed!! "); TextOut(0, LCD_LINE8, " Enter to Stop "); until(ButtonPressed(BTNCENTER, TRUE)); Stop(TRUE); } return isOK; } /* ************************************************************ * RemoteLanuch_Slave() function * 1. Check if the bluetooth connection with slave is existed ? * 2. If not, then call Build_Connection() function to build. * 3. If the connection is built, then remote start program * running on slave device. * ************************************************************ */ void remoteStart_Slave(byte BtChannel, const string BtDevname, const string SlaveProgram) { bool isOK = TRUE; if(!(BluetoothStatus(BtChannel) == NO_ERR)) isOK = build_Connection(BtChannel, BtDevname); if(isOK) { ClearScreen(); TextOut(0, LCD_LINE1, "Remote Start "); TextOut(6, LCD_LINE2, SlaveProgram); RemoteStartProgram(BtChannel,SlaveProgram); while(BluetoothStatus(BT_CHANNEL)== STAT_COMM_PENDING); } } /* ************************************************** * flush_TPGestureData () * ************************************************** */ void flush_TPGestureData() { byte nBytesready; byte I2C_req_buf[]; ArrayBuild(I2C_req_buf, TOUCH_PANEL, GESTURE_DATA); byte I2C_get_buf[]; ArrayInit(I2C_get_buf,0,15); while(TRUE) { while(LowspeedCheckStatus(TOUCH_PANEL_PORT)==STAT_COMM_PENDING); LowspeedWrite(TOUCH_PANEL_PORT,15, I2C_req_buf); while(LowspeedStatus(TOUCH_PANEL_PORT,nBytesready)==STAT_COMM_PENDING); LowspeedRead(TOUCH_PANEL_PORT,nBytesready, I2C_get_buf); if(I2C_get_buf[0] == 0) break; Wait(10); } } /* ************************************************** * int get_TPButton() * ************************************************** */ int get_TPButton() { byte nBytesready; byte I2C_req_buf[]; ArrayBuild(I2C_req_buf, TOUCH_PANEL, TOUCH_BUTTON); byte I2C_get_buf[]; ArrayInit(I2C_get_buf,0,1); while(LowspeedCheckStatus(TOUCH_PANEL_PORT)==STAT_COMM_PENDING); LowspeedWrite(TOUCH_PANEL_PORT, 1, I2C_req_buf); while(LowspeedStatus(TOUCH_PANEL_PORT, nBytesready)==STAT_COMM_PENDING); LowspeedRead(TOUCH_PANEL_PORT, nBytesready, I2C_get_buf); short buttonNo=0; if(I2C_get_buf[0] > 0) { for(int bidx=0; bidx<8; bidx++) { if((0x01 << bidx) & I2C_get_buf[0]) { buttonNo = bidx+1; break; } } } return buttonNo; } /* ************************************************** * int get_TPGestureData(int maxPoints, int &xPos[], int &yPos[]) * Read Gesture data * Parm: maxPoints - max points of this reading * xPos,yPos - array of reading points * (Needs to init these two arrays before calling this function) * Return: Total points of this reading * Registers info: * (1) How many gesture data in rolling buffer (max 110 points): 0x4D * (2) Rolling buffer of gesture data(coordinate of each point): 0x4E, 0x4F * ************************************************** */ #define I2C_READ_BUFFER_SIZE 15 #define MAX_GESTURE_POINTS 110 int get_TPGestureData(int &xPos[], int &yPos[]) { int totalPoints=0; bool isContinueRaed=TRUE; byte nBytesready; byte I2C_req_buf[]; ArrayBuild(I2C_req_buf, TOUCH_PANEL, GESTURE_DATA); byte I2C_get_buf[]; while(isContinueRaed) { while(LowspeedCheckStatus(TOUCH_PANEL_PORT)==STAT_COMM_PENDING); LowspeedWrite(TOUCH_PANEL_PORT, I2C_READ_BUFFER_SIZE, I2C_req_buf); while(LowspeedStatus(TOUCH_PANEL_PORT,nBytesready)==STAT_COMM_PENDING); ArrayInit(I2C_get_buf, 0, I2C_READ_BUFFER_SIZE); LowspeedRead(TOUCH_PANEL_PORT, nBytesready, I2C_get_buf); if(I2C_get_buf[0] == 0) break; else { for(int bidx=1; bidx<=(I2C_READ_BUFFER_SIZE-1)/2; bidx++) { // Filter null points(0,0) if((I2C_get_buf[2*bidx-1] >=6) && (I2C_get_buf[2*bidx] >=8)) { xPos[totalPoints] = I2C_get_buf[2*bidx-1]; yPos[totalPoints] = I2C_get_buf[2*bidx]; totalPoints++; if(totalPoints >= MAX_GESTURE_POINTS) { totalPoints = MAX_GESTURE_POINTS; isContinueRaed = FALSE; break; } } } } } return totalPoints; } /* ************************************************** * get_TPVector () * ************************************************** */ #define X_DIFFERENCE_ALLOWANCE 5 #define Y_DIFFERENCE_ALLOWANCE 3 vectorCoordinate get_TPVector() { int totalPoints, xPos[], yPos[]; vectorCoordinate tpVector; tpVector.x = tpVector.y = 0; //flush_TPGestureData(); ArrayInit(xPos, 0, MAX_GESTURE_POINTS); ArrayInit(yPos, 0, MAX_GESTURE_POINTS); totalPoints = get_TPGestureData(xPos, yPos); if(totalPoints >= 2) { tpVector.x = xPos[(totalPoints-1)] - xPos[0]; tpVector.y = yPos[(totalPoints-1)] - yPos[0]; if(abs(tpVector.x) <= X_DIFFERENCE_ALLOWANCE) tpVector.x = 0; if(abs(tpVector.y) <= Y_DIFFERENCE_ALLOWANCE) tpVector.y = 0; } return tpVector; } /* ************************************************************ * get_Command function * Return: Command for sending to slave * "N" - No data being detected * "Bn" - Which button is pressed * "Vxxxxyyy" - Value of vector of stylus moving * ************************************************************ */ string get_Command() { vectorCoordinate tpVector; int buttonNo; string rtnCommand = "N", strVecX = "", strVecY = ""; buttonNo = get_TPButton(); if((buttonNo >= 1) && (buttonNo <= 8)) { rtnCommand = StrCat("B",NumToStr(buttonNo)); //if(buttonNo>=5) flush_TPGestureData(); } else { tpVector = get_TPVector(); if(!((tpVector.x == 0) && (tpVector.y == 0))) { strVecX = StrCat(" ",NumToStr(tpVector.x)); strVecX = SubStr(strVecX, StrLen(strVecX)-4, 4); strVecY = StrCat(" ",NumToStr(tpVector.y)); strVecY = SubStr(strVecY, StrLen(strVecY)-3, 3); rtnCommand = StrCat("V", strVecX, strVecY); } } return rtnCommand; } /* ************************************************************ * Show_Command function * ************************************************************ */ string show_Command(string remoteCommand) { ClearScreen(); TextOut(0, LCD_LINE1, "TouchPnl Command"); TextOut(0, LCD_LINE2, "Ready to Send: "); TextOut(0, LCD_LINE4, remoteCommand); if(SubStr(remoteCommand,0,1) == "B") { TextOut( 0, LCD_LINE5, "Button( ) "); TextOut(48, LCD_LINE5, SubStr(remoteCommand,1,1)); } else if(SubStr(remoteCommand,0,1) == "V") { TextOut( 0, LCD_LINE5, "Vector( , )"); TextOut(42, LCD_LINE5, SubStr(remoteCommand,1,4)); TextOut(72, LCD_LINE5, SubStr(remoteCommand,5,3)); } else if(SubStr(remoteCommand,0,1) == "N") TextOut(0, LCD_LINE5, "No Action "); } /* ************************************************************ * Show_Command function * ************************************************************ */ void send_Command(string remoteCommand) { SendRemoteString(BT_CHANNEL, MAILBOX1, remoteCommand); while(BluetoothStatus(BT_CHANNEL)== STAT_COMM_PENDING); } /* ************************************************************ * main task * ************************************************************ */ #define WAIT_FOR_NEXT_READING 1000 #define BUTTON_NO_OF_EXIT "B5" task main() { string remoteCommand; SetSensorLowspeed(TOUCH_PANEL_PORT); remoteStart_Slave(BT_CHANNEL, SLAVE_DEVICE, SLAVE_PROGRAM); while(TRUE) { remoteCommand = get_Command(); show_Command(remoteCommand); send_Command(remoteCommand); TextOut(0, LCD_LINE8," Exit"); Wait(WAIT_FOR_NEXT_READING); if(ButtonPressed(BTNRIGHT, TRUE) || (remoteCommand == BUTTON_NO_OF_EXIT)) { RemoteStopProgram(BT_CHANNEL); while(BluetoothStatus(BT_CHANNEL)== STAT_COMM_PENDING); Stop(TRUE); } } }