Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: TFMini_i2c.cpp
- Revision:
- 0:aa77e47dd2c4
diff -r 000000000000 -r aa77e47dd2c4 TFMini_i2c.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TFMini_i2c.cpp Thu Aug 22 09:59:10 2019 +0000 @@ -0,0 +1,60 @@ +#include "TFMini_i2c.h" + +TFMini_i2c::TFMini_i2c( I2C& i2c, Serial& pc ): i2c(i2c), pc(pc) +{ + i2c_commands = 1; +} + +TFMini_i2c::~TFMini_i2c() {} + +bool TFMini_i2c::update() +{ + //send 0x0 to TFMini module and receive back 7 Bytes data + if( i2c.write(TFMINI_I2C_ADDRESS, &i2c_commands, 1 ) == 0) { + if(i2c.read(TFMINI_I2C_ADDRESS, bufferS, 7 ) == 0 ) { + + // assign the data + frame.dist_mm = static_cast<uint16_t>(read16(bufferS, DIST)); + frame.quality = static_cast<uint16_t>(read16(bufferS, STRENGTH)); + frame.mode = static_cast<uint8_t>(read8(bufferS, MODE)); + return true; + + } else { + printf("Error: reading I2C\r\n"); + return false; + } + } else { + pc.printf("Error: writing I2C\r\n"); + return false; + } + +} + + +// Methods to return the sensordata from datastructure +// Simple frame +uint16_t TFMini_i2c::dist() +{ + return frame.dist_mm; +} + +uint16_t TFMini_i2c::quality() +{ + return frame.quality; +} + + +uint8_t TFMini_i2c::read8(char *buffer, const unsigned int& idx) +{ + return uint8_t( buffer[idx] ); +} + +uint16_t TFMini_i2c::read16(char *buffer, const unsigned int& idx) +{ + return uint16_t( read8( buffer, idx ) | (read8(buffer, idx + 1 ) << 8 ) ); +} + +uint32_t TFMini_i2c::read32(char *buffer, const unsigned int& idx) +{ + return uint32_t( (buffer[idx] << 0) | (buffer[idx + 1] << 8) | (buffer[idx+2] << 16) | (buffer[idx+3] << 24) ); +}