Can anyone help me out with INA219?
Im trying to convert this driver to TinyCLR.
This is what i got s far:
class Program
{
static I2cDevice i2cDevice;
static int slaveAddress = 0x40;
static void Main()
{
var btn = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PE3);
btn.SetDriveMode(GpioPinDriveMode.Input);
btn.ValueChanged += Btn_ValueChanged;
I2cController i2cController = I2cController.FromName(SC20100.I2cBus.I2c1);
I2cConnectionSettings i2cConnectionSettings = new I2cConnectionSettings(slaveAddress);
i2cDevice = i2cController.GetDevice(i2cConnectionSettings);
Thread.Sleep(Timeout.Infinite);
}
public static void InaPointTo(Registers registerAddress)
{
byte[] inBuffer = new Byte[2];
inBuffer[0] = (byte)slaveAddress;
inBuffer[1] = (byte)registerAddress;
i2cDevice.Write(inBuffer);
Thread.Sleep(5);
Debug.WriteLine($"INA Write: {inBuffer[0]} - {inBuffer[1]}");
}
public static byte InaRead()
{
byte[] outBuffer = new byte[2];
i2cDevice.Read(outBuffer);
Debug.WriteLine($"INA Read: {outBuffer[0]} - {outBuffer[1]}");
return outBuffer[0];
}
private static void Btn_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
if (e.Edge == GpioPinEdge.RisingEdge)
{
Debug.WriteLine("Test INA219");
InaPointTo(Registers.Power);
InaRead();
}
}
public enum Registers
{
/// <summary>
/// All-register reset, settings for bus voltage range, PGA Gain, ADC resolution/averaging.
/// </summary>
Configuration = 0x00,
/// <summary>
/// Shunt voltage measurement data.
/// </summary>
ShuntVoltage = 0x01,
/// <summary>
/// Bus voltage measurement data.
/// </summary>
BusVoltage = 0x02,
/// <summary>
/// Power measurement data.
/// </summary>
Power = 0x03,
/// <summary>
/// Contains the value of the current flowing through the shunt resistor.
/// </summary>
Current = 0x04,
/// <summary>
/// Sets full-scale range and LSB of current and power measurements. Overall system calibration.
/// </summary>
Calibration = 0x05
}
}
This is the result, no matter which register i choose.
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Test INA219
INA Write: 64 - 3
INA Read: 57 - 159