@@ -38,11 +38,8 @@ unsigned int SRF08::getDistance()
3838 static const uint8_t kFirstEchoHighByte = 0x02 ;
3939 static const uint8_t kNumberOfBytesToRequest = 2 ;
4040
41- mRuntime .i2cBeginTransmission (mAddress );
42- mRuntime .i2cWrite (kRangingCommandRegister );
43- mRuntime .i2cWrite (kRangingInCm );
44- mRuntime .i2cEndTransmission ();
45- mRuntime .delayMillis (mPingDelay );
41+ measureDistance ();
42+
4643 mRuntime .i2cBeginTransmission (mAddress );
4744 mRuntime .i2cWrite (kFirstEchoHighByte );
4845 mRuntime .i2cEndTransmission ();
@@ -116,11 +113,7 @@ uint8_t SRF08::getLightReading()
116113 static const uint8_t kNumberOfBytesToRequest = 1 ;
117114
118115 // Start a ranging
119- mRuntime .i2cBeginTransmission (mAddress );
120- mRuntime .i2cWrite (kRangingCommandRegister );
121- mRuntime .i2cWrite (kRangingInCm );
122- mRuntime .i2cEndTransmission ();
123- mRuntime .delayMillis (mPingDelay );
116+ measureDistance ();
124117
125118 // Get only the light reading byte
126119 mRuntime .i2cBeginTransmission (mAddress );
@@ -166,3 +159,12 @@ uint8_t SRF08::changeAddress(uint8_t newAddress)
166159
167160 return mAddress ;
168161}
162+
163+ void SRF08::measureDistance ()
164+ {
165+ mRuntime .i2cBeginTransmission (mAddress );
166+ mRuntime .i2cWrite (kRangingCommandRegister );
167+ mRuntime .i2cWrite (kRangingInCm );
168+ mRuntime .i2cEndTransmission ();
169+ mRuntime .delayMillis (mPingDelay );
170+ }
0 commit comments