first commit
This commit is contained in:
commit
a5a0434432
1126 changed files with 439481 additions and 0 deletions
40
Software/CSharp/GrovePi/Sensors/UltrasonicRangerSensor.cs
Normal file
40
Software/CSharp/GrovePi/Sensors/UltrasonicRangerSensor.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using GrovePi.Common;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IUltrasonicRangerSensor
|
||||
{
|
||||
int MeasureInCentimeters();
|
||||
}
|
||||
|
||||
internal class UltrasonicRangerSensor : IUltrasonicRangerSensor
|
||||
{
|
||||
private const byte CommandAddress = 7;
|
||||
private readonly GrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal UltrasonicRangerSensor(GrovePi device, Pin pin)
|
||||
{
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
|
||||
public int MeasureInCentimeters()
|
||||
{
|
||||
var buffer = new byte[4] {CommandAddress, (byte) _pin, Constants.Unused, Constants.Unused};
|
||||
var result = _device.DirectAccess.WritePartial(buffer);
|
||||
if (result.Status != Windows.Devices.I2c.I2cTransferStatus.FullTransfer)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
Delay.Milliseconds(50);
|
||||
buffer = new byte[3];
|
||||
result = _device.DirectAccess.ReadPartial(buffer);
|
||||
if (result.Status != Windows.Devices.I2c.I2cTransferStatus.FullTransfer)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return buffer[1]*256 + buffer[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue