1 //
2 // Copyright (c) 2010-2018 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 using Antmicro.Renode.Exceptions;
8 using Antmicro.Renode.Peripherals.SPI;
9 using Antmicro.Renode.Utilities;
10 
11 namespace Antmicro.Renode.Peripherals.Sensors
12 {
13     public class MAX6682MUA : ISPIPeripheral
14     {
MAX6682MUA()15         public MAX6682MUA()
16         {
17             Reset();
18         }
19 
Reset()20         public void Reset()
21         {
22             isFirstByte = true;
23             currentReadOut = 0;
24         }
25 
FinishTransmission()26         public void FinishTransmission()
27         {
28             Reset();
29         }
30 
Transmit(byte data)31         public byte Transmit(byte data)
32         {
33             byte value = 0;
34             if(isFirstByte)
35             {
36                 currentReadOut = (uint)Temperature * 8;
37                 value = (byte)(currentReadOut >> 8);
38             }
39             else
40             {
41                 value = (byte)(currentReadOut & 0xFF);
42             }
43             isFirstByte = !isFirstByte;
44             return value;
45         }
46 
47         public decimal Temperature
48         {
49             get
50             {
51                 return temperature;
52             }
53             set
54             {
55                 if(MinTemperature > value || value > MaxTemperature)
56                 {
57                     throw new RecoverableException("The temperature value must be between {0} and {1}.".FormatWith(MinTemperature, MaxTemperature));
58                 }
59                 temperature = value;
60             }
61         }
62 
63         private decimal temperature;
64         private uint currentReadOut;
65         private bool isFirstByte;
66 
67         private const decimal MaxTemperature = 125;
68         private const decimal MinTemperature = -55;
69     }
70 }
71