/* * Program that uses NRLink, reports IR signal * around you. */ #define PORT S1 const byte NRLinkID = 0x02; const byte NRLinkDataBytes = 0x40; const byte NRLinkCommandReg = 0x41; const byte NRLinkReadResult = 0x42; const byte NRLinkWriteData = 0x42; const byte NRLinkDefault = 0x44; const byte NRLinkFlush = 0x46; const byte NRLinkHighSpeed = 0x48; const byte NRLinkLongRange = 0x4C; const byte NRLinkShortRange = 0x53; const byte NRLinkSetADPAON = 0x4E; const byte NRLinkSETADPAOFF = 0x4F; const byte NRLinkTxUnassembled = 0x55; const byte NRLinkSelectRCX = 0x58; const byte NRLinkSelectTRAIN = 0x54; const byte NRLinkSelectPF = 0x50; void NRLink_Command(byte NRLinkCommand) { byte NRLinkMsg[]; ArrayBuild(NRLinkMsg, NRLinkID, NRLinkCommandReg, NRLinkCommand); byte nByteReady = 0; while (I2CStatus(PORT, nByteReady) == STAT_COMM_PENDING) { // Wait for I2C bus to be ready } // when the I2C bus is ready, send the message you built I2CWrite(PORT, 0, NRLinkMsg); } string i2cReadString(byte prt, byte adr, byte reg, byte cnt) { byte outbuf[32]; byte cmdbuf[33]; string temp = ""; ArrayBuild(cmdbuf, adr, reg); byte nByteReady = 0; while (I2CStatus(PORT, nByteReady) == STAT_COMM_PENDING) { // Wait for I2C bus to be ready } if(I2CBytes(prt, cmdbuf, cnt, outbuf)) { temp = ByteArrayToStr(outbuf); } return temp; } void ShowSensorInfo(byte prt) { ClearScreen(); TextOut(0, LCD_LINE1, i2cReadString(prt, NRLinkID, 0x00, 8)); TextOut(0, LCD_LINE2, i2cReadString(prt, NRLinkID, 0x08, 8)); TextOut(0, LCD_LINE3, i2cReadString(prt, NRLinkID, 0x10, 8)); } string format_bin ( int n ) { string s; int j; int b = 0x80; s = ""; for ( j = 0; j < 8; j++) { if ( n&b ) { s += NumToStr(1); } else { s += NumToStr(0); } b = b>>1; } return (s); } int NRLink_ReadPacket(byte prt, byte adr, byte & retval[]) { byte outbuf[32]; byte cmdbuf[32]; byte nByteReady; byte cnt, result; byte buf[32]; string s; /* * check if there was a buffer overflow. * if so, flush the buffer. */ ArrayBuild(cmdbuf, NRLinkID, 0x41); nByteReady = 0; while (I2CStatus(PORT, nByteReady) == STAT_COMM_PENDING) ; cnt = 1; if(I2CBytes(PORT, cmdbuf, cnt, buf)) { result = buf[0]; } if ( result != 0 ) { s = "IR signal found."; } else { s = " "; } TextOut(0, LCD_LINE7, s, false); s = "bin: "; s += format_bin(result); TextOut(0, LCD_LINE8, s, false); /* * read how many bytes are available to read */ ArrayBuild(cmdbuf, NRLinkID, 0x40); nByteReady = 0; while (I2CStatus(PORT, nByteReady) == STAT_COMM_PENDING) ; cnt = 1; if(I2CBytes(PORT, cmdbuf, cnt, buf)) { result = buf[0]; // number of bytes available to read. } if ( result == 0 ) return 0; cnt = result; /* * read all available bytes */ ArrayBuild(cmdbuf, NRLinkID, 0x42); nByteReady = 0; while (I2CStatus(PORT, nByteReady) == STAT_COMM_PENDING) ; if(I2CBytes(PORT, cmdbuf, cnt, retval)) { // retval = buf; return cnt; } else { return 0; } } task main() // main task: { string st; string msg; byte buf[32]; int result; string x; byte outbuf[]; byte cmdbuf[]; byte cnt, cnt2; int i; byte nByteReady; int loop_cnt; SetSensorLowspeed(PORT); ShowSensorInfo(PORT); TextOut(0, LCD_LINE4, "Press Orange btn"); until(ButtonPressed(BTNCENTER, true)); // TextOut(0, LCD_LINE4, "Initial NRLink."); NRLink_Command(NRLinkFlush); NRLink_Command(NRLinkDefault); NRLink_Command(NRLinkLongRange); NRLink_Command(NRLinkSelectPF); // TextOut(0, LCD_LINE5, "Commanding..."); loop_cnt = 0; while(true) { loop_cnt ++; cnt = NRLink_ReadPacket(PORT, NRLinkID, buf); st = "cnt: "; st += NumToStr(cnt); st += " "; st += "loop: "; st += NumToStr(loop_cnt); st += " "; TextOut(0, LCD_LINE5, st); /* st = " "; TextOut(0, LCD_LINE6, st, false); TextOut(0, LCD_LINE7, st, false); st = ""; if ( cnt <=4 ) { cnt2 = cnt; } else { cnt2 = 4; } st = "0-3:"; for (i =0; i < cnt2; i++) { result = buf[i]; st += NumToStr(result); st += " "; } TextOut(0, LCD_LINE6, st, false); if ( cnt > 4 ) { st = "4+:"; for (i =4; i < cnt; i++) { result = buf[i]; st += NumToStr(result); st += " "; } TextOut(0, LCD_LINE7, st, false); } if ( cnt > 5) { st = "RCX data: "; st += NumToStr(buf[5]); st += " "; // TextOut(0, LCD_LINE8, st, false); } */ Wait(1000); } }