Interface library for the Devantech SRF02/SRF08 ultrasonic i2c rangers. Depends on I2cRtosDriver lib!
SRF02_IF.cpp@1:32c4dd194228, 2013-02-23 (annotated)
- Committer:
- humlet
- Date:
- Sat Feb 23 19:43:49 2013 +0000
- Revision:
- 1:32c4dd194228
- Parent:
- 0:2686e857daf6
Kein Bock mehr;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
humlet | 0:2686e857daf6 | 1 | #include "SRF02_IF.h" |
humlet | 0:2686e857daf6 | 2 | |
humlet | 0:2686e857daf6 | 3 | SRF02_IF::SRF02_IF(int adr, I2C& i2c) : m_adr(adr), m_i2c(i2c) |
humlet | 0:2686e857daf6 | 4 | {} |
humlet | 0:2686e857daf6 | 5 | |
humlet | 0:2686e857daf6 | 6 | int SRF02_IF::triggerRanging() |
humlet | 0:2686e857daf6 | 7 | { |
humlet | 0:2686e857daf6 | 8 | const char regNcmd[2]= {0x00,0x52}; |
humlet | 0:2686e857daf6 | 9 | return m_i2c.write(m_adr, regNcmd, 2); |
humlet | 0:2686e857daf6 | 10 | } |
humlet | 0:2686e857daf6 | 11 | |
humlet | 0:2686e857daf6 | 12 | int SRF02_IF::triggerPing() |
humlet | 0:2686e857daf6 | 13 | { |
humlet | 0:2686e857daf6 | 14 | const char regNcmd[2]= {0x00,0x5C}; |
humlet | 0:2686e857daf6 | 15 | return m_i2c.write(m_adr, regNcmd, 2); |
humlet | 0:2686e857daf6 | 16 | } |
humlet | 0:2686e857daf6 | 17 | |
humlet | 0:2686e857daf6 | 18 | int SRF02_IF::triggerEchoMeasurement() |
humlet | 0:2686e857daf6 | 19 | { |
humlet | 0:2686e857daf6 | 20 | const char regNcmd[2]= {0x00,0x58}; |
humlet | 0:2686e857daf6 | 21 | return m_i2c.write(m_adr, regNcmd, 2); |
humlet | 0:2686e857daf6 | 22 | } |
humlet | 0:2686e857daf6 | 23 | |
humlet | 1:32c4dd194228 | 24 | int SRF02_IF::readTransitTime_us(int* dbg) |
humlet | 0:2686e857daf6 | 25 | { |
humlet | 1:32c4dd194228 | 26 | static Timer tmr; |
humlet | 1:32c4dd194228 | 27 | tmr.reset(); |
humlet | 1:32c4dd194228 | 28 | tmr.start(); |
humlet | 0:2686e857daf6 | 29 | char chk[1]= {0xff}; |
humlet | 1:32c4dd194228 | 30 | int cnt=0; |
humlet | 1:32c4dd194228 | 31 | while((unsigned char)chk[0] == 0xff && tmr.read_ms()<100) { |
humlet | 1:32c4dd194228 | 32 | dbg[0]=m_i2c.write(m_adr, 0x00, 1); // evillive: check if necessary |
humlet | 1:32c4dd194228 | 33 | dbg[1]=tmr.read_us(); |
humlet | 1:32c4dd194228 | 34 | dbg[2]=m_i2c.read(m_adr, chk, 1); |
humlet | 1:32c4dd194228 | 35 | dbg[3]=tmr.read_us(); |
humlet | 1:32c4dd194228 | 36 | dbg[4]=(unsigned char)chk[0]; |
humlet | 1:32c4dd194228 | 37 | dbg[5]=++cnt; |
humlet | 0:2686e857daf6 | 38 | } |
humlet | 1:32c4dd194228 | 39 | /*int a=m_i2c.write(m_adr, 0x00, 1, 1); // evillive: check if necessary |
humlet | 1:32c4dd194228 | 40 | int eco; |
humlet | 0:2686e857daf6 | 41 | do { |
humlet | 0:2686e857daf6 | 42 | eco=m_i2c.read(1); |
humlet | 1:32c4dd194228 | 43 | pc.printf("%x ", eco); |
humlet | 0:2686e857daf6 | 44 | } while(eco == 0xff); |
humlet | 1:32c4dd194228 | 45 | pc.printf("\n");*/ |
humlet | 1:32c4dd194228 | 46 | |
humlet | 0:2686e857daf6 | 47 | const char reg[1] = {0x02}; |
humlet | 0:2686e857daf6 | 48 | char result[2]; |
humlet | 1:32c4dd194228 | 49 | m_i2c.write(m_adr, reg, 1); |
humlet | 0:2686e857daf6 | 50 | m_i2c.read(m_adr, result, 2); |
humlet | 0:2686e857daf6 | 51 | |
humlet | 0:2686e857daf6 | 52 | return (static_cast<int>(result[0])<<8)| static_cast<int>(result[1]); |
humlet | 0:2686e857daf6 | 53 | } |
humlet | 0:2686e857daf6 | 54 | |
humlet | 0:2686e857daf6 | 55 | void SRF02_IF::resetI2CAdress(int newAddress) |
humlet | 0:2686e857daf6 | 56 | { |
humlet | 0:2686e857daf6 | 57 | char regNcmd[2]; |
humlet | 0:2686e857daf6 | 58 | |
humlet | 0:2686e857daf6 | 59 | regNcmd[0]=0x00; |
humlet | 0:2686e857daf6 | 60 | |
humlet | 0:2686e857daf6 | 61 | regNcmd[1]=0xA0; |
humlet | 0:2686e857daf6 | 62 | m_i2c.write(m_adr,regNcmd,2); |
humlet | 0:2686e857daf6 | 63 | |
humlet | 0:2686e857daf6 | 64 | regNcmd[1]=0xAA; |
humlet | 0:2686e857daf6 | 65 | m_i2c.write(m_adr,regNcmd,2); |
humlet | 0:2686e857daf6 | 66 | |
humlet | 0:2686e857daf6 | 67 | regNcmd[1]=0xA5; |
humlet | 0:2686e857daf6 | 68 | m_i2c.write(m_adr,regNcmd,2); |
humlet | 0:2686e857daf6 | 69 | |
humlet | 0:2686e857daf6 | 70 | regNcmd[1]=newAddress; |
humlet | 0:2686e857daf6 | 71 | m_i2c.write(m_adr,regNcmd,2); |
humlet | 0:2686e857daf6 | 72 | } |
humlet | 0:2686e857daf6 | 73 | |
humlet | 0:2686e857daf6 | 74 |