Skip to content

Commit

Permalink
Hires a CodeMaid :P
Browse files Browse the repository at this point in the history
  • Loading branch information
walshie4 committed Sep 5, 2015
1 parent 47a6e2c commit 3a40f5a
Show file tree
Hide file tree
Showing 98 changed files with 5,785 additions and 4,890 deletions.
172 changes: 110 additions & 62 deletions Antumbra/Connector/ConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
using Antumbra.Glow.ExtensionFramework.Management;
using Antumbra.Glow.Observer.Colors;
using Antumbra.Glow.Observer.Connection;
using Antumbra.Glow.Observer.Colors;
using Antumbra.Glow.Observer.Logging;
using Antumbra.Glow.Observer.ToolbarNotifications;
using System;
using System.Collections.Generic;

namespace Antumbra.Glow.Connector {

/// <summary>
/// Manages dealing with connected Glow units via the SerialConnector class.
/// </summary>
public class ConnectionManager : ToolbarNotificationSource, Loggable, ConnectionEventSource, IDisposable, AntumbraColorObserver {
public delegate void NewToolbarNotif(int time, string title, string msg, int icon);
public event NewToolbarNotif NewToolbarNotifAvailEvent;
public delegate void NewLogMsgAvail(string title, string msg);
public event NewLogMsgAvail NewLogMsgAvailEvent;
public delegate void NewConnectionEventAvail(int devCount);
public event NewConnectionEventAvail NewConnectionEventAvailEvent;
public int GlowsFound { get; private set; }

#region Private Fields

private SerialConnector Connector;

private List<GlowDevice> Glows;

#endregion Private Fields

#region Public Constructors

/// <summary>
/// Constructor
/// </summary>
Expand All @@ -33,30 +33,35 @@ public ConnectionManager(int vid, int pid) {
Glows = new List<GlowDevice>();
}

/// <summary>
/// Update Glow device connections
/// </summary>
public void UpdateDeviceConnections() {
int len = this.Connector.UpdateDeviceList();
for(int i = 0; i < len; i += 1) {
GlowDevice device;
device.info = Connector.GetDeviceInfo(i);
device.id = i;
int err;
device.dev = Connector.OpenDevice(device.info, out err);
device.status = -1;
Log("Device " + i + " opened with response status " + err);
Glows.Add(device);
}
GlowsFound = Glows.Count;
if(NewConnectionEventAvailEvent != null) {
NewConnectionEventAvailEvent(GlowsFound);
}
}
#endregion Public Constructors

public void NewColorAvail(Color16Bit newColor, int id, long index) {
sendColor(newColor, id);
}
#region Public Delegates

public delegate void NewConnectionEventAvail(int devCount);

public delegate void NewLogMsgAvail(string title, string msg);

public delegate void NewToolbarNotif(int time, string title, string msg, int icon);

#endregion Public Delegates

#region Public Events

public event NewConnectionEventAvail NewConnectionEventAvailEvent;

public event NewLogMsgAvail NewLogMsgAvailEvent;

public event NewToolbarNotif NewToolbarNotifAvailEvent;

#endregion Public Events

#region Public Properties

public int GlowsFound { get; private set; }

#endregion Public Properties

#region Public Methods

/// <summary>
/// Attach a ToolbarNotificationObserver to this object
Expand All @@ -82,6 +87,24 @@ public void AttachObserver(ConnectionEventObserver observer) {
this.NewConnectionEventAvailEvent += observer.ConnectionUpdate;
}

public void Dispose() {
CloseAll();
FreeList();
}

/// <summary>
/// Log a message
/// </summary>
/// <param name="msg"></param>
public void Log(string msg) {
if(this.NewLogMsgAvailEvent != null)
NewLogMsgAvailEvent("Device Manager", msg);
}

public void NewColorAvail(Color16Bit newColor, int id, long index) {
sendColor(newColor, id);
}

/// <summary>
/// Send a Color16Bit to a specified physical Glow device
/// </summary>
Expand All @@ -105,17 +128,46 @@ public int sendColor(Antumbra.Glow.Observer.Colors.Color16Bit newColor, int id)
}

/// <summary>
/// Log a message
/// Update Glow device connections
/// </summary>
/// <param name="msg"></param>
public void Log(string msg) {
if(this.NewLogMsgAvailEvent != null)
NewLogMsgAvailEvent("Device Manager", msg);
public void UpdateDeviceConnections() {
int len = this.Connector.UpdateDeviceList();
for(int i = 0; i < len; i += 1) {
GlowDevice device;
device.info = Connector.GetDeviceInfo(i);
device.id = i;
int err;
device.dev = Connector.OpenDevice(device.info, out err);
device.status = -1;
Log("Device " + i + " opened with response status " + err);
Glows.Add(device);
}
GlowsFound = Glows.Count;
if(NewConnectionEventAvailEvent != null) {
NewConnectionEventAvailEvent(GlowsFound);
}
}

public void Dispose() {
CloseAll();
FreeList();
#endregion Public Methods

#region Private Methods

/// <summary>
/// Close all connections
/// </summary>
private void CloseAll() {
foreach(var active in this.Glows) {
IntPtr ptr = active.dev;
if(!ptr.Equals(IntPtr.Zero))//actually open?
this.Connector.CloseDevice(active.dev);
}
}

/// <summary>
/// Free the connections list
/// </summary>
private void FreeList() {
this.Connector.FreeList();
}

/// <summary>
Expand All @@ -141,45 +193,41 @@ private int sendColor(UInt16 r, UInt16 g, UInt16 b, int id) {
return status;
}

/// <summary>
/// Close all connections
/// </summary>
private void CloseAll() {
foreach(var active in this.Glows) {
IntPtr ptr = active.dev;
if(!ptr.Equals(IntPtr.Zero))//actually open?
this.Connector.CloseDevice(active.dev);
}
}
#endregion Private Methods

/// <summary>
/// Free the connections list
/// </summary>
private void FreeList() {
this.Connector.FreeList();
}
#region Private Structs

/// <summary>
/// Represents a physical Antumbra|Glow unit.
/// Holds the values needed to identify and send colors to it.
/// Represents a physical Antumbra|Glow unit. Holds the values needed to identify and send
/// colors to it.
/// </summary>
private struct GlowDevice {

#region Public Fields

/// <summary>
/// Device pointer
/// </summary>
public IntPtr dev;
/// <summary>
/// Info pointer
/// </summary>
public IntPtr info;

/// <summary>
/// The id of this device as given by the manager upon creation
/// </summary>
public int id;

/// <summary>
/// Info pointer
/// </summary>
public IntPtr info;

/// <summary>
/// Last know status of this device
/// </summary>
public int status;

#endregion Public Fields
}

#endregion Private Structs
}
}
89 changes: 66 additions & 23 deletions Antumbra/Connector/PreOutputProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Antumbra.Glow.Observer.Colors;
using Antumbra.Glow.Observer.Colors;
using Antumbra.Glow.Observer.Configuration;
using Antumbra.Glow.Settings;
using Antumbra.Glow.Observer.Logging;
using Antumbra.Glow.Settings;
using System;
using System.Collections.Generic;

namespace Antumbra.Glow.Connector {

public class PreOutputProcessor : ConfigurationObserver, AntumbraColorObserver, AntumbraColorSource, Loggable {
public delegate void NewLogMsg(String source, String msg);
public event NewLogMsg NewLogMsgAvail;
public delegate void NewColor(Color16Bit color, int id, long index);
public event NewColor NewColorAvailEvent;

#region Public Fields

public bool manualMode;

private Color16Bit Black;
#endregion Public Fields

#region Private Fields

private Dictionary<int, OutputSettings> AllDeviceSettings;
private Dictionary<int, long> OutputIndexes;

private Color16Bit Black;

private Dictionary<int, Color16Bit> Colors;

private Dictionary<int, long> OutputIndexes;

#endregion Private Fields

#region Public Constructors

public PreOutputProcessor() {
manualMode = false;
AttachObserver(LoggerHelper.GetInstance());
Expand All @@ -32,6 +40,34 @@ public PreOutputProcessor() {
Black.blue = 0;
}

#endregion Public Constructors

#region Public Delegates

public delegate void NewColor(Color16Bit color, int id, long index);

public delegate void NewLogMsg(String source, String msg);

#endregion Public Delegates

#region Public Events

public event NewColor NewColorAvailEvent;

public event NewLogMsg NewLogMsgAvail;

#endregion Public Events

#region Public Methods

public void AttachObserver(LogMsgObserver observer) {
NewLogMsgAvail += observer.NewLogMsgAvail;
}

public void AttachObserver(AntumbraColorObserver observer) {
NewColorAvailEvent += observer.NewColorAvail;
}

public void ConfigurationUpdate(Configurable config) {
if(config is DeviceSettings) {
DeviceSettings settings = (DeviceSettings)config;
Expand Down Expand Up @@ -106,33 +142,40 @@ public void NewColorAvail(Color16Bit newCol, int id, long index) {
AnnounceColor(newCol, id, index);
}

#endregion Public Methods

#region Private Methods

private void AnnounceColor(Color16Bit color, int id, long index) {
if(NewColorAvailEvent != null) {
Colors[id] = color;
NewColorAvailEvent(color, id, index);
}
}

public void AttachObserver(LogMsgObserver observer) {
NewLogMsgAvail += observer.NewLogMsgAvail;
}

public void AttachObserver(AntumbraColorObserver observer) {
NewColorAvailEvent += observer.NewColorAvail;
}

private void Log(String msg) {
if(NewLogMsgAvail != null) {
NewLogMsgAvail("PreOutputProcessor", msg);
}
}

#endregion Private Methods

#region Private Structs

private struct OutputSettings {

#region Public Fields

public double MaxBrightness;
public double newColorWeight;
public Int16 redBias, greenBias, blueBias;
public UInt16 whiteBalanceMin;
public bool weightingEnabled;
public double newColorWeight;
public UInt16 whiteBalanceMin;

#endregion Public Fields
}

#endregion Private Structs
}
}
Loading

0 comments on commit 3a40f5a

Please sign in to comment.