Toyoshi Yoshimoto / GP2Y0E03
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GP2Y0E03.cpp Source File

GP2Y0E03.cpp

00001 
00002 #include "GP2Y0E03.h"
00003 //
00004 GP2Y0E03::GP2Y0E03(I2C* ch)
00005 {
00006     i2cRef=ch;
00007     wdata[0]=0;
00008     wdata[1]=0;
00009     rdata[0]=0;
00010     rdata[1]=0;
00011 }
00012 
00013 int GP2Y0E03::writeCommand(char regadr, char dat)
00014 {
00015     status =0;
00016     wdata[0] = regadr;
00017     wdata[1] = dat;
00018     status = i2cRef->write(DMS_ADDR, wdata, 2, 0);
00019     return status;
00020 }
00021 
00022 int GP2Y0E03::readData(char regadr, char* dat)
00023 {
00024     status =0;
00025     wdata[0] = regadr;
00026     rdata[0] = 0;
00027     status = i2cRef->write(DMS_ADDR, wdata, 1, 0);
00028     if (status !=0) return status;
00029     status = i2cRef->read(DMS_ADDR, rdata, 1, 0);
00030     *dat = rdata[0];
00031     return status;
00032 }
00033 
00034 int GP2Y0E03::readDistance(int* dist)
00035 {
00036     status =0;
00037     wdata[0] = DMS_DIST;
00038     rdata[0] = 0;
00039     rdata[0] = 0;
00040     status = i2cRef->write(DMS_ADDR, wdata, 1, 0);
00041     if (status !=0) return (status + 100);
00042     status = i2cRef->read(DMS_ADDR, rdata, 2, 0);
00043     *dist = (rdata[0]*16 + rdata[1]) >> 5;
00044     return status;
00045 }