For Sharp GP2Y0E03(optical distance measurement sensor).
Embed:
(wiki syntax)
Show/hide line numbers
MjGP2Y0E03.cpp
00001 #include "MjGP2Y0E03.h" 00002 00003 #define IMAGE_SENSOR_DATA_NUMBER (220) 00004 00005 namespace matsujirushi { 00006 00007 MjGP2Y0E03::MjGP2Y0E03(I2C* i2c, uint8_t address) 00008 { 00009 this->i2c = i2c; 00010 this->address = address; 00011 } 00012 00013 uint16_t MjGP2Y0E03::getDistance() 00014 { 00015 uint8_t data[2]; 00016 00017 read(0x5e, data, sizeof (data)); // Distance 00018 00019 return ((uint16_t)data[0] << 4) + (data[1] & 0x0f); 00020 } 00021 00022 bool MjGP2Y0E03::getImageSensorData(uint32_t *data, size_t dataSize, size_t *dataSizeActual) 00023 { 00024 if (dataSize < sizeof (uint32_t) * 220) 00025 { 00026 if (dataSizeActual != NULL) 00027 { 00028 *dataSizeActual = sizeof (uint32_t) * 220; 00029 } 00030 return false; 00031 } 00032 00033 // write(0xef, 0x00); // Bank Select = Bank0 00034 // write(0xec, 0xff); // Clock Select = manual clock 00035 // wait_ms(80); 00036 00037 write(0x03, 0x00); // Hold Bit = Hold 00038 wait_ms(80); 00039 00040 write(0x4c, 0x10); // SRAM Access = Access SRAM 00041 wait_ms(80); 00042 00043 uint8_t dataL[IMAGE_SENSOR_DATA_NUMBER]; 00044 write(0x90, 0x10); // Read out Image Sensor Data = Low Level 00045 read(0x00, dataL, sizeof (dataL)); 00046 00047 uint8_t dataM[IMAGE_SENSOR_DATA_NUMBER]; 00048 write(0x90, 0x11); // Read out Image Sensor Data = Middle Level 00049 read(0x00, dataM, sizeof (dataM)); 00050 00051 uint8_t dataH[IMAGE_SENSOR_DATA_NUMBER]; 00052 write(0x90, 0x12); // Read out Image Sensor Data = High Level 00053 read(0x00, dataH, sizeof (dataH)); 00054 00055 write(0x90, 0x00); // Read out Image Sensor Data = Disable 00056 write(0x03, 0x01); // Hold Bit = Device enable normally 00057 00058 for (int i = 0; i < IMAGE_SENSOR_DATA_NUMBER; i++) 00059 { 00060 *data++ = dataH[i] * 65536 + dataM[i] * 256 + dataL[i]; 00061 } 00062 if (dataSizeActual != NULL) 00063 { 00064 *dataSizeActual = sizeof (uint32_t) * 220; 00065 } 00066 00067 return true; 00068 } 00069 00070 void MjGP2Y0E03::read(uint8_t regAddress, uint8_t *data, size_t dataSize) 00071 { 00072 i2c->write(address, (char*)®Address, 1, true); 00073 i2c->read(address, (char*)data, dataSize); 00074 } 00075 00076 void MjGP2Y0E03::write(uint8_t regAddress, uint8_t data) 00077 { 00078 char buffer[] = { regAddress, data, }; 00079 i2c->write(address, buffer, sizeof (buffer)); 00080 } 00081 00082 } // namespace matsujirushi 00083
Generated on Tue Jul 12 2022 15:12:06 by 1.7.2