-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathI2cDummyDevice.cs
76 lines (64 loc) · 2.19 KB
/
I2cDummyDevice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Device;
using System.Device.Gpio;
using System.Device.I2c;
using System.Globalization;
using System.IO;
using System.Text;
namespace Board.Tests
{
internal class I2cDummyDevice : I2cDevice
{
private bool _disposed;
public I2cDummyDevice(I2cConnectionSettings settings)
{
ConnectionSettings = settings;
_disposed = false;
}
public override I2cConnectionSettings ConnectionSettings { get; }
public override byte ReadByte()
{
if (_disposed)
{
throw new ObjectDisposedException("This dummy instance is disposed");
}
if (ConnectionSettings.DeviceAddress != 0x55 && ConnectionSettings.DeviceAddress != 0x52)
{
throw new IOException("Nothing at this address");
}
return 0xFF;
}
public override void Read(Span<byte> buffer)
{
throw new IOException("No answer from device");
}
public override void WriteByte(byte value)
{
throw new IOException("No answer from device");
}
public override void Write(ReadOnlySpan<byte> buffer)
{
throw new IOException("No answer from device");
}
public override void WriteRead(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer)
{
throw new IOException("No answer from device");
}
protected override void Dispose(bool disposing)
{
_disposed = true;
base.Dispose(disposing);
}
public override ComponentInformation QueryComponentInformation()
{
var self = new ComponentInformation(this, "Dummy I2C Device");
self.Properties["BusNo"] = ConnectionSettings.BusId.ToString(CultureInfo.InvariantCulture);
self.Properties["DeviceAddress"] = $"0x{ConnectionSettings.DeviceAddress:x2}";
return self;
}
}
}