--codeExampleStart 1 ----------------------------------------------------------- -- setupI2C() - sets up the specified port to handle an I2C sensor function setupI2C(port) nxt.InputSetType(port,2) nxt.InputSetDir(port,1,1) nxt.InputSetState(port,1,1) nxt.I2CInitPins(port) end -- Now put the standard I2C messages into the nxt table nxt.I2Cversion = { [2] = string.char( 0x02, 0x00 ), [4] = string.char( 0x04, 0x00 ) } nxt.I2Cproduct = { [2] = string.char( 0x02, 0x08 ), [4] = string.char( 0x04, 0x08 ) } nxt.I2Ctype = { [2] = string.char( 0x02, 0x10 ), [4] = string.char( 0x04, 0x10 ) } nxt.I2Cdata = { [2] = string.char( 0x02, 0x42 ), [4] = string.char( 0x04, 0x42 ) } -- waitI2C() - sits in a tight loop until the I2C system goes idle function waitI2C( port ) while( 0 ~= nxt.I2CGetStatus( port ) ) do end end -- Put it all together in a function that prints out a report of which -- sensor is connected to a port function checkI2C(port, address) setupI2C(port) nxt.I2CSendData( port, nxt.I2Cversion[address], 8 ) waitI2C( port ) print( "Version -> " .. nxt.I2CRecvData( port, 8 ) ) nxt.I2CSendData( port, nxt.I2Cproduct[address], 8 ) waitI2C( port ) print( "Product ID -> " .. nxt.I2CRecvData( port, 8 ) ) nxt.I2CSendData( port, nxt.I2Ctype[address], 8 ) waitI2C( port ) print( "SensorType -> " .. nxt.I2CRecvData( port, 8 ) ) end --codeExampleEnd 1 --codeExampleStart n ----------------------------------------------------------- --codeExampleEnd n