1 //
2 // Copyright (c) 2010-2021 Antmicro
3 //
4 //  This file is licensed under the MIT License.
5 //  Full license text is available in 'licenses/MIT.txt'.
6 //
7 
8 using Antmicro.Renode.Logging;
9 using Antmicro.Renode.Utilities;
10 using Antmicro.Renode.Peripherals.I2C;
11 
12 namespace Antmicro.Renode.Peripherals.Mocks
13 {
14     public class EchoI2CDevice : II2CPeripheral
15     {
EchoI2CDevice()16         public EchoI2CDevice()
17         {
18             Reset();
19         }
20 
Write(byte[] data)21         public void Write(byte[] data)
22         {
23             this.Log(LogLevel.Debug, "Written {0} bytes of data: {1}", data.Length, Misc.PrettyPrintCollectionHex(data));
24             buffer = data;
25         }
26 
Read(int count = 1)27         public byte[] Read(int count = 1)
28         {
29             this.Log(LogLevel.Debug, "Reading {0} bytes", count);
30             var result = new byte[count];
31             for(var i = 0; i < result.Length; i++)
32             {
33                 result[i] = (i < buffer.Length)
34                     ? buffer[i]
35                     : (byte)0;
36             }
37 
38             return result;
39         }
40 
FinishTransmission()41         public void FinishTransmission()
42         {
43             this.Log(LogLevel.Debug, "Finishing transmission");
44         }
45 
Reset()46         public void Reset()
47         {
48             buffer = new byte[0];
49         }
50 
51         private byte[] buffer;
52     }
53 }
54