Remarks

AnalogJoystick
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for AnalogJoystick

Code Example

AnalogJoystick joystick;

public MeadowApp()
{
    joystick = new AnalogJoystick(
        Device.CreateAnalogInputPort(Device.Pins.A01, 1, TimeSpan.FromMilliseconds(10), new Voltage(3.3)),
        Device.CreateAnalogInputPort(Device.Pins.A00, 1, TimeSpan.FromMilliseconds(10), new Voltage(3.3)),
        null);

    // assume that the stick is in the center when it starts up
    _ = joystick?.SetCenterPosition(); //fire and forget

    //==== Classic Events
    joystick.Updated += JoystickUpdated;

    //==== IObservable
    joystick.StartUpdating(TimeSpan.FromMilliseconds(20));
}

void JoystickUpdated(object sender, IChangeResult<AnalogJoystickPosition> e)
{
    Console.WriteLine($"Horizontal: {e.New.Horizontal:n2}, Vertical: {e.New.Vertical:n2}");
    Console.WriteLine($"Digital position: {joystick.DigitalPosition}");
}

Sample project(s) available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Hid.AnalogJoystick/AnalogJoystick_Fritzing.png"

Characteristic Locus
Inheritance System.Object ObservableBase<AnalogJoystickPosition> SensorBase<AnalogJoystickPosition> > AnalogJoystick
Implements IAnalogJoystick
Inherited Members SensorBase<AnalogJoystickPosition>.Updated SensorBase<AnalogJoystickPosition>.samplingLock SensorBase<AnalogJoystickPosition>.SamplingTokenSource SensorBase<AnalogJoystickPosition>.Conditions SensorBase<AnalogJoystickPosition>.IsSampling SensorBase<AnalogJoystickPosition>.UpdateInterval SensorBase<AnalogJoystickPosition>.RaiseEventsAndNotify(IChangeResult<AnalogJoystickPosition>) SensorBase<AnalogJoystickPosition>.Read() ObservableBase<AnalogJoystickPosition>.observers ObservableBase<AnalogJoystickPosition>.NotifyObservers(IChangeResult<AnalogJoystickPosition>) ObservableBase<AnalogJoystickPosition>.Subscribe(IObserver<IChangeResult<AnalogJoystickPosition>>) ObservableBase<AnalogJoystickPosition>.CreateObserver(Action<IChangeResult<AnalogJoystickPosition>>, Nullable<Predicate<IChangeResult<AnalogJoystickPosition>>>)
Namespace Meadow.Foundation.Sensors.Hid
Assembly Meadow.Foundation.dll

Syntax

public class AnalogJoystick : SensorBase<AnalogJoystickPosition>, IAnalogJoystick

Constructors

AnalogJoystick(IAnalogInputController, IPin, IPin, AnalogJoystick.JoystickCalibration)

Creates a 2-axis analog joystick

Declaration
public AnalogJoystick(IAnalogInputController device, IPin horizontalPin, IPin verticalPin, AnalogJoystick.JoystickCalibration calibration = null)

Parameters

Type Name Description
IAnalogInputController device

The IAnalogInputController to create the port on.

IPin horizontalPin
IPin verticalPin
AnalogJoystick.JoystickCalibration calibration

Calibration for the joystick.

AnalogJoystick(IAnalogInputController, IPin, IPin, AnalogJoystick.JoystickCalibration, Int32, TimeSpan)

Creates a 2-axis analog joystick

Declaration
public AnalogJoystick(IAnalogInputController device, IPin horizontalPin, IPin verticalPin, AnalogJoystick.JoystickCalibration calibration, int sampleCount, TimeSpan sampleInterval)

Parameters

Type Name Description
IAnalogInputController device

The IAnalogInputController to create the port on.

IPin horizontalPin
IPin verticalPin
AnalogJoystick.JoystickCalibration calibration

Calibration for the joystick.

System.Int32 sampleCount

How many samples to take during a given reading. These are automatically averaged to reduce noise.

TimeSpan sampleInterval

The time, in milliseconds, to wait in between samples during a reading.

AnalogJoystick(IAnalogInputPort, IAnalogInputPort, AnalogJoystick.JoystickCalibration)

Creates a 2-axis analog joystick

Declaration
public AnalogJoystick(IAnalogInputPort horizontalInputPort, IAnalogInputPort verticalInputPort, AnalogJoystick.JoystickCalibration calibration = null)

Parameters

Type Name Description
IAnalogInputPort horizontalInputPort
IAnalogInputPort verticalInputPort
AnalogJoystick.JoystickCalibration calibration

Fields

sampleCount

Number of samples used to calculate position

Declaration
protected int sampleCount

Field Value

Type Description
System.Int32

sampleIntervalMs

Interval between samples

Declaration
protected int sampleIntervalMs

Field Value

Type Description
System.Int32

Properties

Calibration

Callibration for 2-axis analog joystick

Declaration
public AnalogJoystick.JoystickCalibration Calibration { get; protected set; }

Property Value

Type Description
AnalogJoystick.JoystickCalibration

DigitalPosition

Digital position of joystick

Declaration
public DigitalJoystickPosition? DigitalPosition { get; }

Property Value

Type Description
System.Nullable<DigitalJoystickPosition>

HorizontalInputPort

Analog port connected to horizonal joystick pin

Declaration
protected IAnalogInputPort HorizontalInputPort { get; set; }

Property Value

Type Description
IAnalogInputPort

IsHorizontalInverted

Is the horizontal / x-axis inverted

Declaration
public bool IsHorizontalInverted { get; set; }

Property Value

Type Description
System.Boolean

IsVerticalInverted

Is the vertical / y-axis inverted

Declaration
public bool IsVerticalInverted { get; set; }

Property Value

Type Description
System.Boolean

Position

Current position of analog joystick

Declaration
public AnalogJoystickPosition? Position { get; protected set; }

Property Value

Type Description
System.Nullable<AnalogJoystickPosition>

VerticalInputPort

Analog port connected to vertical joystick pin

Declaration
protected IAnalogInputPort VerticalInputPort { get; set; }

Property Value

Type Description
IAnalogInputPort

Methods

GetDigitalJoystickPosition()

Translates an analog position into a digital position, taking into account the calibration information.

Declaration
protected DigitalJoystickPosition GetDigitalJoystickPosition()

Returns

Type Description
DigitalJoystickPosition

The digital joystick position as DigitalJoystickPosition

ReadSensor()

Convenience method to get the current temperature. For frequent reads, use StartSampling() and StopSampling() in conjunction with the SampleBuffer.

Declaration
protected override Task<AnalogJoystickPosition> ReadSensor()

Returns

Type Description
Task<AnalogJoystickPosition>

Overrides

Meadow.Foundation.SensorBase<AnalogJoystickPosition>.ReadSensor()

SetCenterPosition()

sets the current position as the center position and saves to the calibration.

Declaration
public Task SetCenterPosition()

Returns

Type Description
Task

StartUpdating(Nullable<TimeSpan>)

Starts continuously sampling the sensor.

This method also starts raising Changed events and IObservable subscribers getting notified. Use the readIntervalDuration parameter to specify how often events and notifications are raised/sent.

Declaration
public void StartUpdating(TimeSpan? updateInterval)

Parameters

Type Name Description
System.Nullable<TimeSpan> updateInterval

A TimeSpan that specifies how long to wait between readings. This value influences how often *Updated events are raised and IObservable consumers are notified. The default is 5 seconds.

StopUpdating()

Stops sampling the joystick position.

Declaration
public void StopUpdating()