-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathSeesawEeprom.cs
79 lines (68 loc) · 3.27 KB
/
SeesawEeprom.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
77
78
79
// 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.Device.I2c;
namespace Iot.Device.Seesaw
{
/// <summary>
/// The Seesaw EEPROM class.
/// </summary>
public partial class Seesaw : IDisposable
{
/// <summary>
/// Write a byte to the EEProm area on the Seesaw module.
/// </summary>
/// <param name="eepromAddress">The point in the EEProm area to write the byte.</param>
/// <param name="value">The value to write into the EEProm area.</param>
public void WriteEEPromByte(byte eepromAddress, byte value)
{
WriteEEProm(eepromAddress, new byte[] { value });
}
/// <summary>
/// Write a byte array to the EEProm area on the Seesaw module.
/// </summary>
/// <param name="eepromAddress">The point in the EEProm area to start writing the data.</param>
/// <param name="data">The bytes to be written into the EEProm area.</param>
public void WriteEEProm(byte eepromAddress, byte[] data)
{
if (!HasModule(SeesawModule.Eeprom))
{
throw new InvalidOperationException($"The hardware on I2C Bus {I2cDevice.ConnectionSettings.BusId}, Address 0x{I2cDevice.ConnectionSettings.DeviceAddress:X2} does not support Adafruit SeeSaw EEPROM functionality");
}
Write(SeesawModule.Eeprom, (SeesawFunction)eepromAddress, data);
}
/// <summary>
/// Read a byte from the EEProm area on the Seesaw module.
/// </summary>
/// <param name="eepromAddress">The point in the EEProm area to start reading the data.</param>
/// <returns>The data byte read from the EEProm area.</returns>
public byte ReadEEPromByte(byte eepromAddress)
{
if (!HasModule(SeesawModule.Eeprom))
{
throw new InvalidOperationException($"The hardware on I2C Bus {I2cDevice.ConnectionSettings.BusId}, Address 0x{I2cDevice.ConnectionSettings.DeviceAddress:X2} does not support Adafruit SeeSaw EEPROM functionality");
}
return ReadByte(SeesawModule.Eeprom, (SeesawFunction)eepromAddress);
}
/// <summary>
/// Change the I2C address that the Seesaw board listens on. Note that this will reset communications
/// with the host device and dispose the current I2cDevice.
/// </summary>
/// <param name="i2cAddress">The new I2C address to be used.</param>
public void SetI2cAddress(byte i2cAddress)
{
I2cConnectionSettings oldSsettings = I2cDevice.ConnectionSettings;
if (i2cAddress != GetI2cAddress())
{
WriteEEPromByte((byte)SeesawFunction.EepromI2cAddr, i2cAddress);
I2cDevice.Dispose();
Initialize(I2cDevice.Create(new I2cConnectionSettings(oldSsettings.BusId, i2cAddress)));
}
}
/// <summary>
/// Read the address configured to be used as the I2C address.
/// </summary>
/// <returns>The data byte representing the I2C address.</returns>
private byte GetI2cAddress() => ReadEEPromByte((byte)SeesawFunction.EepromI2cAddr);
}
}