Skip to content

Commit

Permalink
Windows .NET library added
Browse files Browse the repository at this point in the history
  • Loading branch information
todbot committed Dec 19, 2012
1 parent 3913069 commit fb32235
Show file tree
Hide file tree
Showing 16 changed files with 444 additions and 0 deletions.
122 changes: 122 additions & 0 deletions windows/Blink1Lib/Blink1Lib/Blink1.cs
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace Blink1Lib
{
public class Blink1
{
static void Main(string[] args)
{
}

/// <summary>
/// Library for communicating with blink(1) USB RGB LEDs.
/// On instantiation, enumerates the USB and records any plugged in blink(1) devices.
/// </summary>
public Blink1()
{
enumerate();
}

/// <summary>
/// Open blink(1) device for use. If multiple blink(1)s, opens the first device.
/// </summary>
/// <returns>true if device was opened, false if no device available.</returns>
public Boolean open()
{
dev = blink1_open();
if (dev == null) return false;
return true;
}

/// <summary>
/// Open a particular blink(1) device, if there are several
/// </summary>
/// <param name="i">integer id (0,1,2...) of device to open</param>
/// <returns>true if device was opened, false otherwise</returns>
public Boolean openById(int i)
{
dev = blink1_openById(i);
if (dev == null) return false;
return true;
}

/// <summary>
/// Close an open blink(1) device.
/// </summary>
public void close()
{
blink1_close(dev);
}

/// <summary>
/// Set blink(1) to RGB color immediately.
/// </summary>
/// <param name="r">red component</param>
/// <param name="g">green component</param>
/// <param name="b">blue component</param>
public void setRGB(int r, int g, int b)
{
blink1_setRGB(dev, r, g, b);
}

/// <summary>
/// Fade to an RGB color over a period of milliseconds.
/// </summary>
/// <param name="millis">milliseconds fade time</param>
/// <param name="r">red component</param>
/// <param name="g">green component</param>
/// <param name="b">blue component</param>
public void fadeToRGB(int millis, int r, int g, int b)
{
blink1_fadeToRGB(dev, millis, r, g, b);
}

/// <summary>
/// FIXME: make more like Blink1.m enumerate
/// </summary>
/// <returns></returns>
public int enumerate()
{
return blink1_enumerate();
}

public string getCachedSerial(int i)
{
//Marshal.PtrToStringAuto
IntPtr wcstr = blink1_getCachedSerial(i);
string result = Marshal.PtrToStringAuto(wcstr);
return result;
}

// see: http://msdn.microsoft.com/en-us/magazine/cc164123.aspx#S7
private const string DllName = "blink1-lib.dll";
private System.IntPtr dev;

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern System.IntPtr blink1_open();

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern System.IntPtr blink1_openById(int i);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void blink1_close( System.IntPtr dev );

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern System.IntPtr blink1_setRGB(System.IntPtr dev, int r, int g, int b);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern System.IntPtr blink1_fadeToRGB(System.IntPtr dev, int millis, int r, int g, int b);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int blink1_enumerate();

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr blink1_getCachedSerial(int i);

}
}
58 changes: 58 additions & 0 deletions windows/Blink1Lib/Blink1Lib/Blink1Lib.csproj
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4FE2AE76-BA05-44BC-A04E-5CAF5D7A7D2F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Blink1</RootNamespace>
<AssemblyName>Blink1</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Blink1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="blink1-lib.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions windows/Blink1Lib/Blink1Lib/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Blink1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Blink1")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("b714d0d3-f611-4ee9-8c75-c5adf344dd40")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Binary file added windows/Blink1Lib/Blink1Lib/blink1-lib.dll
Binary file not shown.
29 changes: 29 additions & 0 deletions windows/Blink1Lib/Blink1LibDemo.sln
@@ -0,0 +1,29 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 2012 for Windows Desktop
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blink1LibDemo", "Blink1LibDemo\Blink1LibDemo.csproj", "{F47758A5-F3F8-4B24-AD5A-333E30E5D249}"
ProjectSection(ProjectDependencies) = postProject
{4FE2AE76-BA05-44BC-A04E-5CAF5D7A7D2F} = {4FE2AE76-BA05-44BC-A04E-5CAF5D7A7D2F}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blink1Lib", "Blink1Lib\Blink1Lib.csproj", "{4FE2AE76-BA05-44BC-A04E-5CAF5D7A7D2F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F47758A5-F3F8-4B24-AD5A-333E30E5D249}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F47758A5-F3F8-4B24-AD5A-333E30E5D249}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F47758A5-F3F8-4B24-AD5A-333E30E5D249}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F47758A5-F3F8-4B24-AD5A-333E30E5D249}.Release|Any CPU.Build.0 = Release|Any CPU
{4FE2AE76-BA05-44BC-A04E-5CAF5D7A7D2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4FE2AE76-BA05-44BC-A04E-5CAF5D7A7D2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4FE2AE76-BA05-44BC-A04E-5CAF5D7A7D2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4FE2AE76-BA05-44BC-A04E-5CAF5D7A7D2F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions windows/Blink1Lib/Blink1LibDemo/App.config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
67 changes: 67 additions & 0 deletions windows/Blink1Lib/Blink1LibDemo/Blink1LibDemo.csproj
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F47758A5-F3F8-4B24-AD5A-333E30E5D249}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Blink1LibDemo</RootNamespace>
<AssemblyName>Blink1LibDemo</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Blink1Lib\Blink1Lib.csproj">
<Project>{4fe2ae76-ba05-44bc-a04e-5caf5d7a7d2f}</Project>
<Name>Blink1Lib</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
51 changes: 51 additions & 0 deletions windows/Blink1Lib/Blink1LibDemo/Program.cs
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

using Blink1Lib;

namespace Blink1LibDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("------------Blink1LibDemo Begin-------------");
Blink1 blink1 = new Blink1();

int count = blink1.enumerate();
Console.WriteLine("detected " + count + " blink(1 devices");

if (count != 0)
{
string serialnum = blink1.getCachedSerial(0);
Console.WriteLine("blink(1) serial number: " + serialnum);
}

blink1.open();

Console.WriteLine("setting white");
blink1.setRGB(255, 255, 255);
Thread.Sleep(1000);

Console.WriteLine("fading cyan");
blink1.fadeToRGB(500, 0, 255, 255);
Thread.Sleep(1000);

Console.WriteLine("fading red");
blink1.fadeToRGB(500, 255, 0, 0);
Thread.Sleep(1000);

Console.WriteLine("fading black");
blink1.fadeToRGB(1000, 0, 0, 0);

Console.WriteLine("------------Blink1LibDemo End --------------");
Thread.Sleep(3000);

}

}
}

0 comments on commit fb32235

Please sign in to comment.