Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adrian as5013 updates #329

Merged
merged 1 commit into from
May 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace Meadow.Foundation.Sensors.Hid
public partial class As5013
: SensorBase<AnalogJoystickPosition>, IAnalogJoystick
{
/// <summary>
/// Event if interrupt port is provided for interrupt pin
/// </summary>
public event EventHandler Interrupt;

/// <summary>
/// Default I2C bus speed
/// </summary>
Expand All @@ -28,21 +33,47 @@ public partial class As5013
/// </summary>
public bool IsVerticalInverted { get; set; }

/// <summary>
/// Swap horizonal and vertical
/// </summary>
public bool IsVerticalHorizonalSwapped { get; set; } = false;

/// <summary>
/// The joystick position
/// </summary>
public AnalogJoystickPosition? Position { get; private set; } = null;

/// <summary>
/// The digital joystick position
/// </summary>
public DigitalJoystickPosition? DigitalPosition
{
get
{
if(IsSampling == false)
{
Update();
}
return GetDigitalJoystickPosition();
}
}

readonly II2cPeripheral i2CPeripheral;

/// <summary>
/// Create a new As5013 object
/// </summary>
/// <param name="i2cBus">the I2C bus</param>
/// <param name="address">the device I2C address</param>
public As5013(II2cBus i2cBus, byte address = (byte)Addresses.Default)
/// <param name="interruptPort">port connected to the interrupt pin</param>
public As5013(II2cBus i2cBus, byte address = (byte)Addresses.Default, IDigitalInterruptPort interruptPort = null)
{
i2CPeripheral = new I2cPeripheral(i2cBus, address);

if(interruptPort != null)
{
interruptPort.Changed += (s, e) => Interrupt?.Invoke(s, EventArgs.Empty);
}
}

/// <summary>
Expand Down Expand Up @@ -164,6 +195,13 @@ void Update()
float newX = xValue / 128.0f * (IsHorizontalInverted ? -1 : 1);
float newY = yValue / 128.0f * (IsVerticalInverted ? -1 : 1);

if (IsVerticalHorizonalSwapped)
{
float temp = newX;
newX = newY;
newY = temp;
}

// capture history
var oldPosition = Position;
var newPosition = new AnalogJoystickPosition(newX, newY);
Expand All @@ -175,21 +213,30 @@ void Update()
base.RaiseEventsAndNotify(result);
}

void DisableInterrupt()
/// <summary>
/// Disable the interrupt pin
/// </summary>
public void DisableInterrupt()
{
var value = (byte)(i2CPeripheral.ReadRegister((byte)Register.JOYSTICK_CONTROL1) & 0x04);

i2CPeripheral.WriteRegister((byte)Register.JOYSTICK_CONTROL1, (byte)((byte)Command.JOYSTICK_CONTROL1_RESET_CMD | value));
}

void EnableInterrupt()
/// <summary>
/// enable the interrupt pin
/// </summary>
public void EnableInterrupt()
{
var value = (byte)(i2CPeripheral.ReadRegister((byte)Register.JOYSTICK_CONTROL1) | 0x04);

i2CPeripheral.WriteRegister((byte)Register.JOYSTICK_CONTROL1, (byte)((byte)Command.JOYSTICK_CONTROL1_RESET_CMD | value));
}

void SetDefaultConfiguration()
/// <summary>
/// Set the default configuration
/// </summary>
public void SetDefaultConfiguration()
{
i2CPeripheral.WriteRegister((byte)Register.JOYSTICK_CONTROL2, (byte)Command.JOYSTICK_CONTROL2_TEST_CMD);
i2CPeripheral.WriteRegister((byte)Register.JOYSTICK_AGC, (byte)Command.JOYSTICK_AGC_MAX_SENSITIVITY_CMD);
Expand All @@ -199,5 +246,48 @@ void SetDefaultConfiguration()

i2CPeripheral.WriteRegister((byte)Register.JOYSTICK_CONTROL1, (byte)((byte)Command.JOYSTICK_CONTROL1_RESET_CMD | value));
}

DigitalJoystickPosition GetDigitalJoystickPosition()
{
var h = Position.Value.Horizontal;
var v = Position.Value.Vertical;

var threshold = 0.5f;

if (h > threshold)
{ //Right
if (v > threshold)
{
return DigitalJoystickPosition.UpRight;
}
if (v < threshold)
{
return DigitalJoystickPosition.DownRight;
}
return DigitalJoystickPosition.Right;
}
else if (h < threshold)
{ //Left
if (v > threshold)
{
return DigitalJoystickPosition.UpLeft;
}
if (v < threshold)
{
return DigitalJoystickPosition.DownLeft;
}
return DigitalJoystickPosition.Left;
}
else if (v > threshold)
{ //Up
return DigitalJoystickPosition.Up;
}
else if (v < threshold)
{ //Down
return DigitalJoystickPosition.Down;
}

return DigitalJoystickPosition.Center;
}
}
}