For Sharp GP2Y0E03(optical distance measurement sensor).
#include "mbed.h" #include "MjGP2Y0E03.h" using namespace matsujirushi; Serial pc(USBTX, USBRX); I2C i2c(dp5, dp27); MjGP2Y0E03 distanceSensor(&i2c, 0x80); int main() { pc.baud(115200); i2c.frequency(400000); for(;;) { uint16_t distance = distanceSensor.getDistance(); pc.printf("%4d [", distance); int i; for (i = 0; i < distance / 50; i++) { pc.putc('*'); } for (; i < 4096 / 50; i++) { pc.putc(' '); } pc.puts("]\r\n"); wait_ms(50); } }
Revision 1:1832cde75561, committed 2014-11-26
- Comitter:
- matsujirushi
- Date:
- Wed Nov 26 12:00:04 2014 +0000
- Parent:
- 0:33c4a1de6547
- Commit message:
- Add getImageSensorData() function.
Changed in this revision
MjGP2Y0E03.cpp | Show annotated file Show diff for this revision Revisions of this file |
MjGP2Y0E03.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 33c4a1de6547 -r 1832cde75561 MjGP2Y0E03.cpp --- a/MjGP2Y0E03.cpp Mon Nov 24 07:51:53 2014 +0000 +++ b/MjGP2Y0E03.cpp Wed Nov 26 12:00:04 2014 +0000 @@ -1,5 +1,7 @@ #include "MjGP2Y0E03.h" +#define IMAGE_SENSOR_DATA_NUMBER (220) + namespace matsujirushi { MjGP2Y0E03::MjGP2Y0E03(I2C* i2c, uint8_t address) @@ -10,13 +12,71 @@ uint16_t MjGP2Y0E03::getDistance() { - char regAddress = 0x5e; - i2c->write(address, ®Address, 1, true); + uint8_t data[2]; + + read(0x5e, data, sizeof (data)); // Distance + + return ((uint16_t)data[0] << 4) + (data[1] & 0x0f); +} + +bool MjGP2Y0E03::getImageSensorData(uint32_t *data, size_t dataSize, size_t *dataSizeActual) +{ + if (dataSize < sizeof (uint32_t) * 220) + { + if (dataSizeActual != NULL) + { + *dataSizeActual = sizeof (uint32_t) * 220; + } + return false; + } + +// write(0xef, 0x00); // Bank Select = Bank0 +// write(0xec, 0xff); // Clock Select = manual clock +// wait_ms(80); + + write(0x03, 0x00); // Hold Bit = Hold + wait_ms(80); + + write(0x4c, 0x10); // SRAM Access = Access SRAM + wait_ms(80); + + uint8_t dataL[IMAGE_SENSOR_DATA_NUMBER]; + write(0x90, 0x10); // Read out Image Sensor Data = Low Level + read(0x00, dataL, sizeof (dataL)); - char data[2]; - i2c->read(address, data, sizeof (data)); + uint8_t dataM[IMAGE_SENSOR_DATA_NUMBER]; + write(0x90, 0x11); // Read out Image Sensor Data = Middle Level + read(0x00, dataM, sizeof (dataM)); + + uint8_t dataH[IMAGE_SENSOR_DATA_NUMBER]; + write(0x90, 0x12); // Read out Image Sensor Data = High Level + read(0x00, dataH, sizeof (dataH)); + + write(0x90, 0x00); // Read out Image Sensor Data = Disable + write(0x03, 0x01); // Hold Bit = Device enable normally - return ((uint16_t)data[0] << 4) + (data[1] & 0x0f); + for (int i = 0; i < IMAGE_SENSOR_DATA_NUMBER; i++) + { + *data++ = dataH[i] * 65536 + dataM[i] * 256 + dataL[i]; + } + if (dataSizeActual != NULL) + { + *dataSizeActual = sizeof (uint32_t) * 220; + } + + return true; +} + +void MjGP2Y0E03::read(uint8_t regAddress, uint8_t *data, size_t dataSize) +{ + i2c->write(address, (char*)®Address, 1, true); + i2c->read(address, (char*)data, dataSize); +} + +void MjGP2Y0E03::write(uint8_t regAddress, uint8_t data) +{ + char buffer[] = { regAddress, data, }; + i2c->write(address, buffer, sizeof (buffer)); } } // namespace matsujirushi
diff -r 33c4a1de6547 -r 1832cde75561 MjGP2Y0E03.h --- a/MjGP2Y0E03.h Mon Nov 24 07:51:53 2014 +0000 +++ b/MjGP2Y0E03.h Wed Nov 26 12:00:04 2014 +0000 @@ -10,11 +10,15 @@ public: MjGP2Y0E03(I2C* i2c, uint8_t address); uint16_t getDistance(); + bool getImageSensorData(uint32_t *data, size_t dataSize, size_t *dataSizeActual); private: I2C *i2c; uint8_t address; + void read(uint8_t regAddress, uint8_t *data, size_t dataSize); + void write(uint8_t regAddress, uint8_t data); + }; } // namespace matsujirushi