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

QueueThread Dynamic and Singleton #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1,038 changes: 1,038 additions & 0 deletions .vs/config/applicationhost.config

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions MonoBrickFirmware/MonoBrickFirmware.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<ItemGroup />
<ItemGroup>
<Compile Include="Native\Libc.cs" />
<Compile Include="Tools\EventNode.cs" />
<Compile Include="Tools\QueueThread.cs" />
<Compile Include="Display\Console.cs" />
<Compile Include="Display\Basics.cs" />
Expand Down Expand Up @@ -172,11 +173,5 @@
<LogicalName>font.profont_7</LogicalName>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Folder Include="FileSystem\" />
<Folder Include="Settings\" />
<Folder Include="FirmwareUpdate\" />
<Folder Include="Device\" />
<Folder Include="Display\Dialogs\UserInput\" />
</ItemGroup>
<ItemGroup />
</Project>
51 changes: 25 additions & 26 deletions MonoBrickFirmware/Sensors/EV3ColorSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,30 @@
namespace MonoBrickFirmware.Sensors
{
public class EV3ColorSensor : UartSensor{

/// <summary>
/// Initializes a new instance of the NXTColorSensor class in color mode
/// </summary>
public EV3ColorSensor (SensorPort port) : this(port, ColorMode.Color)
{

}

/// <summary>
/// Initializes a new instance of the NXTColorSensor class.
/// </summary>
/// <param name="mode">Mode.</param>
public EV3ColorSensor (SensorPort port, ColorMode mode) : base(port)
{
base.Initialise(base.uartMode);
Mode = mode;

}

/// <summary>
/// Gets or sets the color mode.
/// </summary>
/// <value>The color mode.</value>
public ColorMode Mode {
/// <summary>
/// Initializes a new instance of the EV3ColorSensor class in color mode
/// </summary>
/// <param name="pollInteval">refresh rate for pollThread</param>
public EV3ColorSensor(SensorPort port, int pollInterval = 50) : this(port, ColorMode.Color, pollInterval)
{
}

/// <summary>
/// Initializes a new instance of the EV3ColorSensor class.
/// </summary>
/// <param name="mode">Mode.</param>
/// <param name="pollInteval">refresh rate for pollThread</param>
public EV3ColorSensor(SensorPort port, ColorMode mode, int pollInterval = 50) : base(port, pollInterval)
{
base.Initialise(base.uartMode);
Mode = mode;
}

/// <summary>
/// Gets or sets the color mode.
/// </summary>
/// <value>The color mode.</value>
public ColorMode Mode {
get{return SensorModeToColorMode(base.uartMode);}
set{
base.SetMode(ColorModeToSensorMode(value));
Expand Down Expand Up @@ -80,7 +79,7 @@ public int ReadRaw ()
/// <summary>
/// Read the intensity of the reflected or ambient light in percent. In color mode the color index is returned
/// </summary>
public int Read()
public override int Read()
{
int value = 0;
switch (Mode)
Expand Down
49 changes: 24 additions & 25 deletions MonoBrickFirmware/Sensors/EV3GyroSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,29 @@ public enum GyroMode {
/// Class for the EV3 Gyro sensor
/// </summary>
public class EV3GyroSensor :UartSensor{

/// <summary>
/// Initializes a new instance of the Gyro sensor.
/// </summary>
public EV3GyroSensor (SensorPort port) : this(port, GyroMode.Angle)
{

}

/// <summary>
/// Initializes a new instance of the Gyro sensor.
/// </summary>
/// <param name="mode">Mode.</param>
public EV3GyroSensor (SensorPort port, GyroMode mode) : base(port)
{
base.Initialise(base.uartMode);
Mode = mode;
}

/// <summary>
/// Gets or sets the Gyro mode.
/// </summary>
/// <value>The mode.</value>
public GyroMode Mode {

/// <summary>
/// Initializes a new instance of the Gyro sensor.
/// </summary>
public EV3GyroSensor(SensorPort port, int pollInterval = 50) : this(port, GyroMode.Angle, pollInterval)
{
}

/// <summary>
/// Initializes a new instance of the Gyro sensor.
/// </summary>
/// <param name="mode">Mode.</param>
public EV3GyroSensor(SensorPort port, GyroMode mode, int pollInterval = 50) : base(port, pollInterval)
{
base.Initialise(base.uartMode);
Mode = mode;
}

/// <summary>
/// Gets or sets the Gyro mode.
/// </summary>
/// <value>The mode.</value>
public GyroMode Mode {
get{return (GyroMode) base.uartMode;}
set{SetMode((UARTMode) value);}
}
Expand Down Expand Up @@ -106,7 +105,7 @@ public int RotationCount ()
/// <summary>
/// Read the gyro sensor value. The returned value depends on the mode.
/// </summary>
public int Read ()
public override int Read ()
{
if (Mode == GyroMode.Angle) {
return BitConverter.ToInt16(ReadBytes(2),0)%360;
Expand Down
41 changes: 20 additions & 21 deletions MonoBrickFirmware/Sensors/EV3IRSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,25 @@ public class BeaconLocation{
/// </summary>
public class EV3IRSensor : UartSensor{

/// <summary>
/// Initializes a new instance of the <see cref="MonoBrickFirmware.Sensors.EV3IRSensor"/> class.
/// </summary>
/// <param name="port">Port.</param>
public EV3IRSensor (SensorPort port) : this(port, IRMode.Proximity)
{

}

/// <summary>
/// Initializes a new instance of the <see cref="MonoBrickFirmware.Sensors.EV3IRSensor"/> class.
/// </summary>
/// <param name="port">Senosr port.</param>
/// <param name="mode">IR Mode.</param>
public EV3IRSensor (SensorPort port, IRMode mode) : base(port)
{
base.Initialise(base.uartMode);
Mode = mode;
Channel = IRChannel.One;
}
/// <summary>
/// Initializes a new instance of the <see cref="MonoBrickFirmware.Sensors.EV3IRSensor"/> class.
/// </summary>
/// <param name="port">Port.</param>
public EV3IRSensor(SensorPort port, int pollInterval = 50) : this(port, IRMode.Proximity, pollInterval)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MonoBrickFirmware.Sensors.EV3IRSensor"/> class.
/// </summary>
/// <param name="port">Senosr port.</param>
/// <param name="mode">IR Mode.</param>
public EV3IRSensor(SensorPort port, IRMode mode, int pollInterval = 50) : base(port, pollInterval)
{
base.Initialise(base.uartMode);
Mode = mode;
Channel = IRChannel.One;
}

/// <summary>
/// Gets or sets the IR mode.
Expand Down Expand Up @@ -125,7 +124,7 @@ public override string ReadAsString ()
/// Read the sensor value. The returned value depends on the mode. Distance in proximity mode.
/// Remote command number in remote mode. Beacon location in seek mode.
/// </summary>
public int Read(){
public override int Read(){
int value = 0;
switch ((IRMode)base.uartMode)
{
Expand Down
47 changes: 23 additions & 24 deletions MonoBrickFirmware/Sensors/EV3UltraSonicSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@ namespace MonoBrickFirmware.Sensors
/// Class for the EV3 ultrasonic sensor
/// </summary>
public class EV3UltrasonicSensor : UartSensor{
/// <summary>
/// Initializes a new instance of the EV3 Ultrasonic Sensor.
/// </summary>
public EV3UltrasonicSensor (SensorPort port) : this(port, UltraSonicMode.Centimeter)
{

}

/// <summary>
/// Initializes a new instance of the EV3 Ultrasonic Sensor.
/// </summary>
/// <param name="mode">Mode.</param>
public EV3UltrasonicSensor (SensorPort port, UltraSonicMode mode) : base(port)
{
base.Initialise(base.uartMode);
Mode = mode;
}

/// <summary>
/// Gets or sets the Gyro mode.
/// </summary>
/// <value>The mode.</value>
public UltraSonicMode Mode {
/// <summary>
/// Initializes a new instance of the EV3 Ultrasonic Sensor.
/// </summary>
public EV3UltrasonicSensor(SensorPort port, int pollInterval = 50) : this(port, UltraSonicMode.Centimeter, pollInterval)
{
}

/// <summary>
/// Initializes a new instance of the EV3 Ultrasonic Sensor.
/// </summary>
/// <param name="mode">Mode.</param>
public EV3UltrasonicSensor(SensorPort port, UltraSonicMode mode, int pollInterval = 50) : base(port, pollInterval)
{
base.Initialise(base.uartMode);
Mode = mode;
}

/// <summary>
/// Gets or sets the Gyro mode.
/// </summary>
/// <value>The mode.</value>
public UltraSonicMode Mode {
get{return (UltraSonicMode) base.uartMode;}
set{SetMode((UARTMode) value);}
}
Expand Down Expand Up @@ -59,7 +58,7 @@ public override string ReadAsString ()
/// <summary>
/// Read the sensor value. Result depends on the mode
/// </summary>
public int Read ()
public override int Read ()
{
if (Mode == UltraSonicMode.Listen)
{
Expand Down
5 changes: 5 additions & 0 deletions MonoBrickFirmware/Sensors/SensorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ public override string ReadAsString ()
{
return "";
}

public override int Read()
{
return 0;
}

/// <summary>
/// Gets the name of the sensor.
Expand Down
63 changes: 52 additions & 11 deletions MonoBrickFirmware/Sensors/UARTSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using MonoBrickFirmware.Native;
using MonoBrickFirmware.Tools;
using System.Threading;
using System.ComponentModel;

namespace MonoBrickFirmware.Sensors
{
Expand Down Expand Up @@ -51,17 +53,56 @@ public abstract class UartSensor:ISensor
protected const int NumberOfSensorPorts = SensorManager.NumberOfSensorPorts;
protected SensorPort port;
protected UARTMode uartMode{get; private set;}

public UartSensor (SensorPort port)
{
this.port = port;
uartMemory = SensorManager.Instance.UartMemory;
UartDevice = SensorManager.Instance.UartDevice;
}

public abstract string ReadAsString ();

public abstract void SelectNextMode();

private int pollTime = 50;
private EventWaitHandle stopPolling = new ManualResetEvent(false);
private QueueThread queue = QueueThread.Instance;
private Thread pollThread = null;
public UartSensor(SensorPort port, int pollTime = 50)
{
this.port = port;
this.pollTime = pollTime;
uartMemory = SensorManager.Instance.UartMemory;
UartDevice = SensorManager.Instance.UartDevice;
pollThread = new Thread(sensorPollThread);
pollThread.Start();
}

/// <summary>
/// Stop polling this instance
/// </summary>
public void Kill()
{
stopPolling.Set();
pollThread.Join();
}


/// <summary>
/// thread that checks the sensor' state, and raises the propertyChanged event in queue when it is changed.
/// </summary>
private void sensorPollThread()
{
Thread.CurrentThread.IsBackground = true;
int lastState = Read();
while (!stopPolling.WaitOne(pollTime))
{
int currenState = Read();
if (currenState != lastState)
{
queue.Enqueue(propertyChangedEvent, this, new PropertyChangedEventArgs(GetSensorName()));
lastState = currenState;
}
}
}

public event PropertyChangedEventHandler propertyChangedEvent;

public abstract string ReadAsString();

public abstract int Read();

public abstract void SelectNextMode();

public abstract string GetSensorName();

Expand Down
26 changes: 26 additions & 0 deletions MonoBrickFirmware/Tools/EventNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MonoBrickFirmware.Tools
{
public class EventNode
{
private Delegate EventToRaise;
private Object[] Parameters;

public EventNode(Delegate Event, Object[] Parameters)
{
this.EventToRaise = Event;
this.Parameters = Parameters;
}

public Delegate eventToRaise {
get { return this.EventToRaise; }
}
public Object[] parameters {
get { return this.Parameters; }
}
}
}
Loading