enum PingUnit { MicroSeconds , Centimeters , Inches } /**
 * Sonar and ping utilities
 */ namespace sonar { param trig param echo param unit param maxCmDistance export function ping ( trig , echo , unit , maxCmDistance = 500 ) { pins . setPull ( trig , PinPullMode . PullNone ) ; pins . digitalWritePin ( trig , 0 ) ; control . waitMicros ( 2 ) ; pins . digitalWritePin ( trig , 1 ) ; control . waitMicros ( 10 ) ; pins . digitalWritePin ( trig , 0 ) ; const d = pins . pulseIn ( echo , PulseValue . High , maxCmDistance * 58 ) ; switch ( unit ) { case PingUnit . Centimeters : return Math . idiv ( d , 58 ) ; case PingUnit . Inches : return Math . idiv ( d , 148 ) ; default : return d ; } } }