Skip to content

Commit

Permalink
Add support for the second part of the orchestrator.
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Sep 27, 2016
1 parent 163c662 commit 9092843
Showing 11 changed files with 288 additions and 251 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace LightPi.OrchestratorFirmware.Devices
{
internal interface IOutputDevice
{
void Initialize();
void WriteState(byte[] buffer);
}
}
62 changes: 62 additions & 0 deletions Solution/LightPi.OrchestratorFirmware/Devices/MAX7311.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Diagnostics;
using Windows.Devices.I2c;

namespace LightPi.OrchestratorFirmware.Devices
{
internal sealed class MAX7311 : IOutputDevice
{
private const int OutputRegister1 = 2;
private const int ConfigurationRegister1 = 6;

private readonly byte[] _stateBuffer = { OutputRegister1, 255, 255 };

private readonly string _i2CBusId;
private readonly int _deviceAddress;
private readonly int _stateOffset;

private I2cDevice _i2CDevice;

public MAX7311(string i2CBusId, int deviceAddress, int stateOffset, byte[] initialState)
{
if (i2CBusId == null) throw new ArgumentNullException(nameof(i2CBusId));
if (initialState == null) throw new ArgumentNullException(nameof(initialState));

_stateBuffer[1] = initialState[0];
_stateBuffer[2] = initialState[1];

_i2CBusId = i2CBusId;
_deviceAddress = deviceAddress;
_stateOffset = stateOffset;
}

public void Initialize()
{
var settings = new I2cConnectionSettings(_deviceAddress)
{
BusSpeed = I2cBusSpeed.FastMode,
SharingMode = I2cSharingMode.Exclusive
};

_i2CDevice = I2cDevice.FromIdAsync(_i2CBusId, settings).AsTask().Result;

// Write the initial state to the output register. Then set the configuration to "output".
_i2CDevice.Write(_stateBuffer);

byte[] setConfigurationToOutput = { ConfigurationRegister1, 0, 0 };
_i2CDevice.Write(setConfigurationToOutput);

Debug.WriteLine($"Initialized MAX7311 with address {_deviceAddress}");
}

public void WriteState(byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));

_stateBuffer[1] = buffer[_stateOffset];
_stateBuffer[2] = buffer[_stateOffset + 1];

_i2CDevice.Write(_stateBuffer);
}
}
}
51 changes: 51 additions & 0 deletions Solution/LightPi.OrchestratorFirmware/Devices/PCF8574.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Diagnostics;
using Windows.Devices.I2c;

namespace LightPi.OrchestratorFirmware.Devices
{
internal sealed class PCF8574 : IOutputDevice
{
private readonly byte[] _stateBuffer = { 0 };

private readonly string _i2CBusId;
private readonly int _deviceAddress;
private readonly int _stateOffset;

private I2cDevice _i2CDevice;

public PCF8574(string i2CBusId, int deviceAddress, int stateOffset, byte initialState)
{
if (i2CBusId == null) throw new ArgumentNullException(nameof(i2CBusId));

_stateBuffer[0] = initialState;

_i2CBusId = i2CBusId;
_deviceAddress = deviceAddress;
_stateOffset = stateOffset;
}

public void Initialize()
{
var settings = new I2cConnectionSettings(_deviceAddress)
{
BusSpeed = I2cBusSpeed.FastMode,
SharingMode = I2cSharingMode.Exclusive
};

_i2CDevice = I2cDevice.FromIdAsync(_i2CBusId, settings).AsTask().Result;
_i2CDevice.Write(_stateBuffer);

Debug.WriteLine($"Initialized PCF8574 with address {_deviceAddress}");
}

public void WriteState(byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));

_stateBuffer[0] = buffer[_stateOffset];

_i2CDevice.Write(_stateBuffer);
}
}
}
Original file line number Diff line number Diff line change
@@ -11,8 +11,8 @@
<AssemblyName>LightPi.OrchestratorFirmware</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion>10.0.10586.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.10586.0</TargetPlatformMinVersion>
<TargetPlatformVersion>10.0.14393.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.14393.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<EnableDotNetNativeCompatibleProfile>true</EnableDotNetNativeCompatibleProfile>
<FileAlignment>512</FileAlignment>
@@ -99,11 +99,14 @@
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="MAX7311Wrapper.cs" />
<Compile Include="Devices\IOutputDevice.cs" />
<Compile Include="Devices\MAX7311.cs" />
<Compile Include="MoveBitMode.cs" />
<Compile Include="OrchestratorServer.cs" />
<Compile Include="PCF8574Wrapper.cs" />
<Compile Include="Devices\PCF8574.cs" />
<Compile Include="StartupTask.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StateConverter.cs" />
</ItemGroup>
<ItemGroup>
<AppxManifest Include="Package.appxmanifest">
@@ -121,17 +124,17 @@
<Content Include="Assets\StoreLogo.png" />
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
</ItemGroup>
<ItemGroup>
<SDKReference Include="WindowsIoT, Version=10.0.10586.0">
<Name>Windows IoT Extensions for the UWP</Name>
</SDKReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LightPi.Protocol\LightPi.Protocol.csproj">
<Project>{b77db4eb-5b81-4596-a5f1-07424306752a}</Project>
<Name>LightPi.Protocol</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<SDKReference Include="WindowsIoT, Version=10.0.14393.0">
<Name>Windows IoT Extensions for the UWP</Name>
</SDKReference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
56 changes: 0 additions & 56 deletions Solution/LightPi.OrchestratorFirmware/MAX7311Wrapper.cs

This file was deleted.

8 changes: 8 additions & 0 deletions Solution/LightPi.OrchestratorFirmware/MoveBitMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace LightPi.OrchestratorFirmware
{
internal enum MoveBitMode
{
Invert,
Default,
}
}
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ private void ProcessIncomingData(DatagramSocket sender, DatagramSocketMessageRec
_callback(package);
}

private byte[] ReadPackageFromMessage(DatagramSocketMessageReceivedEventArgs message)
private static byte[] ReadPackageFromMessage(DatagramSocketMessageReceivedEventArgs message)
{
using (var reader = message.GetDataReader())
{
50 changes: 0 additions & 50 deletions Solution/LightPi.OrchestratorFirmware/PCF8574Wrapper.cs

This file was deleted.

Loading

0 comments on commit 9092843

Please sign in to comment.