/* Program ID : NumPad-T01.nxc Create date : 11-10-2010 Author : CH, Chen (Taiwan) Description : Multi-Key Touched detection of Mindsensors Numeric Pad I2C Register Layout : Register Read Write --------- --------------------------- --------------------- 0x00 bit0 ~ bit7 key touch detect 0x01 bit0 ~ bit3 key touch detect */ #define DEVICE_PORT S1 //Connect sensor to this port #define DEVICE_ID 0xB4 #define KEY_VALUE 0x00 #define CHECK_INTERVAL 4000 /* ******************************************************** * Set sensor value * ******************************************************** */ void SetSensorValue (const byte & Group[]) { byte I2C_req_buf[]; ArrayBuild(I2C_req_buf, DEVICE_ID, Group); while(LowspeedCheckStatus(DEVICE_PORT) == STAT_COMM_PENDING); LowspeedWrite(DEVICE_PORT, 0, I2C_req_buf); while(LowspeedCheckStatus(DEVICE_PORT) == STAT_COMM_PENDING); } /* ******************************************************** * Initialize Numeric Pad * ******************************************************** */ void NumPad_Init() { byte Group1[] = {0x41, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F};//2 byte Group2[] = {0x4A, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F};//3 byte Group3[] = {0x52, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F};//4 byte Group4[] = {0x5C, 0x0b, 0x20, 0x0C};//5 byte Group5[] = {0x2B, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0xFF, 0x02};//1 byte Group6[] = {0x7B, 0x0B}; byte Group7[] = {0x7D, 0x9C, 0x65, 0x8C};//6 SetSensorValue(Group1); SetSensorValue(Group2); SetSensorValue(Group3); SetSensorValue(Group4); SetSensorValue(Group5); SetSensorValue(Group6); SetSensorValue(Group7); } /* ******************************************************** * Adjust bit seqence bit6 & bit7 of byte 1 * and bit2 & bit3 of byte 2 * ******************************************************** */ int AdjustBitSeq(byte &I2C_get_buf[]) { byte Byte1_b6 = (I2C_get_buf[0] & 0x80) >> 1; //get bit 7 then right shift byte Byte1_b7 = (I2C_get_buf[0] & 0x40) << 1; //get bit 6 then left shift byte Byte1 = (I2C_get_buf[0] & 0x3F) + Byte1_b6 + Byte1_b7; byte Byte2_b2 = (I2C_get_buf[1] & 0x08) >> 1; //get bit 3 then right shift byte Byte2_b3 = (I2C_get_buf[1] & 0x04) << 1; //get bit 2 then left shift byte Byte2 = (I2C_get_buf[1] & 0x03) + Byte2_b2 + Byte2_b3; return ((Byte1 + (Byte2 << 8)) & 0x0FFF); } /* ******************************************************** * Get sensor value * ******************************************************** */ int GetSensor () { byte I2C_req_buf[]; ArrayBuild(I2C_req_buf, DEVICE_ID, KEY_VALUE); byte I2C_get_buf[]; ArrayInit(I2C_get_buf, 0, 2); byte nbytes; char xstatus; int xii,xvalue=0; while(LowspeedCheckStatus(DEVICE_PORT) == STAT_COMM_PENDING); LowspeedWrite(DEVICE_PORT, 2, I2C_req_buf); while(LowspeedStatus(DEVICE_PORT, nbytes) == STAT_COMM_PENDING); xstatus = LowspeedRead(DEVICE_PORT, nbytes, I2C_get_buf); if (xstatus == NO_ERR) return (AdjustBitSeq(I2C_get_buf)); else return 0; } /* ******************************************************** * Display key touched on screen * 18 42 66 * 40 [11] [7] [3] * 32 [10] [6] [2] * 24 [ 9] [5] [1] * 16 [ 8] [4] [0] * ******************************************************** */ void Do_Disp_rtn(int keyStatus) { string keyList = "#9630852*741"; string xkeyChar; int xii, xjj, xmask=0x0001; //Scan key touched status and map key character for displaying for (xii=0; xii<12; xii++) { if ((keyStatus & xmask) == 0) xkeyChar = " " ; else xkeyChar = SubStr(keyList,xii,1); TextOut(66-(xii/4)*24, 16+(xii%4)*8, xkeyChar); xmask <<= 1; } } /* ******************************************************** * Clear screen * ******************************************************** */ void Do_ClearScreen_rtn() { // "1234567890123456" TextOut(0, LCD_LINE3, " [ ] [ ] [ ] " ); //3,7,11->18,42,66 TextOut(0, LCD_LINE4, " [ ] [ ] [ ] " ); TextOut(0, LCD_LINE5, " [ ] [ ] [ ] " ); TextOut(0, LCD_LINE6, " [ ] [ ] [ ] " ); } /* ******************************************************* * I2C sensor initialization * ******************************************************* */ void I2CSensor_Init() { SetSensorLowspeed(DEVICE_PORT); Wait(100); } /* ******************************************************** * main() * ******************************************************** */ task main() { int keyStatus, last_keyStatus; long currTick; I2CSensor_Init(); NumPad_Init(); ClearScreen(); TextOut(0, LCD_LINE1, "<>" ); TextOut(0, LCD_LINE3, " [ ] [ ] [ ] " ); //3,7,11->18,42,66 TextOut(0, LCD_LINE4, " [ ] [ ] [ ] " ); TextOut(0, LCD_LINE5, " [ ] [ ] [ ] " ); TextOut(0, LCD_LINE6, " [ ] [ ] [ ] " ); TextOut(0, LCD_LINE8, " Quit" ); currTick = CurrentTick(); do { keyStatus = GetSensor(); if (keyStatus <> last_keyStatus) { Do_ClearScreen_rtn(); Do_Disp_rtn(keyStatus); last_keyStatus = keyStatus; currTick = CurrentTick(); //Reset time counting } else if ((CurrentTick() - currTick) > CHECK_INTERVAL) { Do_ClearScreen_rtn(); currTick = CurrentTick(); //Reset time counting } } until (ButtonPressed(BTNRIGHT, TRUE)); }