3/26/16 12:25 am JJ
Revision 0:08b7f391566c, committed 2016-03-26
- Comitter:
- j_j205
- Date:
- Sat Mar 26 05:26:10 2016 +0000
- Commit message:
- 3/26/16 12:25 am JJ
Changed in this revision
ShortRangeSensor.cpp | Show annotated file Show diff for this revision Revisions of this file |
ShortRangeSensor.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 08b7f391566c ShortRangeSensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ShortRangeSensor.cpp Sat Mar 26 05:26:10 2016 +0000 @@ -0,0 +1,54 @@ +#include "ShortRangeSensor.h" + +ShortRangeSensor::ShortRangeSensor(PinName sda, PinName scl) : i2c(sda, scl) +{ + //disables ECE + i2c.write(ADDR, SYSRANGE__RANGE_CHECK_ENABLES, 3); +} + +//returns range in mm +int ShortRangeSensor::getRange() +{ + char data; + + i2c.write(ADDR, SYSRANGE__START_SAMPLE, 3); + wait_ms(20); + i2c.write(ADDR, RESULT__RANGE_VAL, 2); + i2c.read(ADDR, &data, 1); + + return data; +} + +int ShortRangeSensor::getStatus() +{ + char data; + + i2c.write(ADDR, RESULT__RANGE_STATUS, 2); + i2c.read(ADDR, &data, 1); + + return data; +} + +int ShortRangeSensor::getPPOffset() +{ + char data; + + i2c.write(ADDR, SYSRANGE__PART_TO_PART_RANGE_OFFSET, 2); + i2c.read(ADDR, &data, 1); + + return data; +} + +void ShortRangeSensor::setPPOffset(int offset) +{ + if(offset < -128 || offset > 128) + return; + + char cmd[3]; + + cmd[0] = SYSRANGE__PART_TO_PART_RANGE_OFFSET[0]; + cmd[1] = SYSRANGE__PART_TO_PART_RANGE_OFFSET[1]; + cmd[2] = offset; + + i2c.write(ADDR, cmd, 3); +} \ No newline at end of file
diff -r 000000000000 -r 08b7f391566c ShortRangeSensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ShortRangeSensor.h Sat Mar 26 05:26:10 2016 +0000 @@ -0,0 +1,32 @@ +#ifndef SHORTRANGESENSOR_H +#define SHORTRANGESENSOR_H + +#include "mbed.h" + +//constants for i2c communication +const int ADDR = 0x52; +const char SYSRANGE__START_SAMPLE[] = {0x00,0x18,0x01}; +const char RESULT__RANGE_VAL[] = {0x00,0x62}; +const char RESULT__RANGE_STATUS[] = {0x00,0x4D}; +const char SYSRANGE__RANGE_CHECK_ENABLES[] = {0x00,0x2D,0x10}; +const char SYSRANGE__PART_TO_PART_RANGE_OFFSET_INIT[] = {0x00,0x24,0x00}; //once correct offset is found, chage 0x00 to correct offset +const char SYSRANGE__PART_TO_PART_RANGE_OFFSET[] = {0x00,0x24}; + +class ShortRangeSensor +{ + public: + ShortRangeSensor(PinName sda, PinName scl); + //returns range in mm + int getRange(); + //returns value of status register + int getStatus(); + //returns value of part to part range offset register + int getPPOffset(); + //sets value of part to part range offset register, -128 to 128 + void setPPOffset(int offset); + + private: + I2C i2c; +}; + +#endif \ No newline at end of file