PowerShell – turn on a Christmas tree (or device of choice)!   Leave a comment

This one’s about how to use a £5 USB LED Christmas Tree, a £30 k8055 experimimenters board, and a bit of code wrap of a C++ DLL with .Net c# code to make an Alert “light” work from PowerShell..

The commands are: tree-on & tree-off.

It gives our team a lovely visual clue when SharePoint is feeling off-colour. – We’re switching it other way around for Christmas (switching it off when there’s a problem!)

You know with ULS logs, there is so much information in there, and if you’ve read my article on how to retrieve the Critical Entries in an almost real time fashion, then you’re almost there…this goes really well with that previous script.

How:

For the k8055 board you’ll need to use the below code slap it in a Visual Studio of some description and compile it as a DLL.

I’ve not tried any open source compilers, but don’t see why they wouldn’t work…

It doesn’t work under 64 bit PowerShell, which I could do with some help to solve. 
I think it’s because the underlying C++ DLL is 32bit, but, I have been told that really shouldn’t make a difference…happy to learn if different – I’m a code by the seat of your pants kind of guy!

k8055 wrapper DLL Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;

namespace Dansk8055
{
public partial class Methods
{
// Device
private bool[] devs = { false, false, false, false };
private int crtDev = -1;

// Digital
private int[] dbtime = { 0, 2, 10, 1000 };
private enum enmSigTypeD { off, cycle, cup, cdn };
private enmSigTypeD sigTypeD = enmSigTypeD.off;

private int stateDI, stateDO, c1, c2;
private bool doChanged;

// Analogue
private enum enmAOSigType { off, ramp, saw, sine, i1, i1i, i2, i2i };
private enmAOSigType[] sigTypeA;
private int ai1, ai2, ao1, ao2;
private int ao1idx, ao2idx, ao1Delta, ao2Delta;
private bool ao1Changed, ao2Changed;

private int[] ramp, saw, sine;

// ============================================================
// API ========================================================
// ============================================================

// Following wraps K8055D.DLL
// (K8055D_C.DLL is required for .net DLL, as used in K8055_test_D)
// *** have not checked if K8055D_C.DLL can be used here as well ***
public sealed class api // DLL Wrapper
{
#region API Declares

[DllImport(“k8055d.dll”)]
public static extern int OpenDevice(int devNumber);

[DllImport(“k8055d.dll”)]
public static extern void CloseDevice();

[DllImport(“k8055d.dll”)]
public static extern int ReadAnalogChannel(int Channel);

[DllImport(“k8055d.dll”)]
public static extern void ReadAllAnalog(ref int Data1, ref int Data2);

[DllImport(“k8055d.dll”)]
public static extern void OutputAnalogChannel(int Channel, int Data);

[DllImport(“k8055d.dll”)]
public static extern void OutputAllAnalog(int Data1, int Data2);

[DllImport(“k8055d.dll”)]
public static extern void ClearAnalogChannel(int Channel);

[DllImport(“k8055d.dll”)]
public static extern void SetAllAnalog();

[DllImport(“k8055d.dll”)]
public static extern void ClearAllAnalog();

[DllImport(“k8055d.dll”)]
public static extern void SetAnalogChannel(int Channel);

[DllImport(“k8055d.dll”)]
public static extern void WriteAllDigital(int Data);

[DllImport(“k8055d.dll”)]
public static extern void ClearDigitalChannel(int Channel);

[DllImport(“k8055d.dll”)]
public static extern void ClearAllDigital();

[DllImport(“k8055d.dll”)]
public static extern void SetDigitalChannel(int Channel);

[DllImport(“k8055d.dll”)]
public static extern void SetAllDigital();

[DllImport(“k8055d.dll”)]
public static extern bool ReadDigitalChannel(int Channel);

[DllImport(“k8055d.dll”)]
public static extern int ReadAllDigital();

[DllImport(“k8055d.dll”)]
public static extern int ReadCounter(int CounterNr);

[DllImport(“k8055d.dll”)]
public static extern void ResetCounter(int CounterNr);

[DllImport(“k8055d.dll”)]
public static extern void SetCounterDebounceTime(int CounterNr, int DebounceTime);

[DllImport(“k8055d.dll”)]
public static extern void Version();

[DllImport(“k8055d.dll”)]
public static extern int SearchDevices();

[DllImport(“k8055d.dll”)]
public static extern int SetCurrentDevice(int lngCardAddress);

#endregion
}

// ============================================================
// DEVICE =====================================================
// ============================================================

private bool devForceCurrent(int devNumber)
{
// Set current device returning true/false
// (coded to deal with USB insert/removal)
api.CloseDevice();
if (api.SetCurrentDevice(devNumber) != devNumber)
if (api.OpenDevice(devNumber) != devNumber)
return false;

crtDev = devNumber;
return true;
}

private bool devSetCurrent(int devNumber)
{
// Set current device returning true/false
// (coded to deal with USB insert/removal)
if (devNumber == crtDev) return true; // Avoid redundant calls if poss
return devForceCurrent(devNumber);
}

private bool devExists(int devNumber)
{
return devForceCurrent(devNumber);
}

private int devCount()
{
// Relies on deviceExists to try to count devices

int count = 0;

if (devExists(0)) count++;
if (devExists(1)) count++;
if (devExists(2)) count++;
if (devExists(3)) count++;

return count;
}

// ============================================================

public int devOpen(int devNumber)
{
return api.OpenDevice(devNumber);
}

public void devClose()
{
api.CloseDevice();
}

private void devInit()
{
stateDI = stateDO = 0;

ai1 = ai2 = ao1 = ao2 = 0;
ao1Delta = ao2Delta = 1;
ao1idx = ao2idx = 0;
doChanged = ao1Changed = ao2Changed = false;
}

public void devSetDigitalChannel(int Channel)
{
api.SetDigitalChannel(Channel);
}

public void devSetAllDigital()
{
api.SetAllDigital();
}

public void devClearDigitalChannel(int Channel)
{
api.ClearDigitalChannel(Channel);
}

public void devClearAllDigital()
{
api.ClearAllDigital();
}

public void devSetAllanalog()
{
api.SetAllAnalog();
}

public void devClearAllAnalog()
{
api.ClearAllAnalog();
}
// ============================================================
// ============================================================
// ============================================================

public bool devConnectX(int devNumber)
{

if (crtDev > -1)
{
devClose();
}

crtDev = devOpen(devNumber);

devInit();
return true;
}

public bool devConnect(int devNumber)
{

if (devSetCurrent(devNumber) == false)
{
return false;
}
devInit();
return true;
}

}
}

 

Powershell:

Load remote assembly.

$dllFullPath = "\\<server>\scripts$\k8055\Dansk8055\Dansk8055\bin\Debug\dansk8055.dll"

$evidence = [System.Reflection.Assembly]::GetExecutingAssembly().Evidence

[System.Reflection.Assembly]::LoadFrom(“$dllFullPath”,$evidence) > $null

Function Global:Tree-On() {

# Relies on library DLL being called first by calling script.

$outt = New-Object dansk8055.Methods

$outt.devOpen(0) > $Null

$outt.devConnect(0) > $Null

$outt.devSetAllanalog()

$outt.devClose()

} # End Function.

Function Global:Tree-Off() {

# Relies on library DLL being called first by calling script.

$outt = New-Object dansk8055.Methods

$outt.devOpen(0) > $Null

$outt.devConnect(0) > $Null

$outt.devClearAllanalog()

$outt.devClose()

}# End Function.

Advertisements

Posted September 30, 2011 by stormwalker255 in SharePoint

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: