Skip to content

Instantly share code, notes, and snippets.

@SamVanhoutte
Last active December 15, 2015 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamVanhoutte/9aa63f5be54c1e73212d to your computer and use it in GitHub Desktop.
Save SamVanhoutte/9aa63f5be54c1e73212d to your computer and use it in GitHub Desktop.
This class can be used to control the PiGlow LED on a Windows 10 IoT Core device. This has been tested on the Raspberry PI II against the build 10586. The controller is very easy to use and to instrument from a Universal Windows App.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;
namespace Codit.IoT.PIGlow
{
public class PiGlowController
{
private I2cDevice piGlow;
private const byte PIGLOW_I2C_ADDR = 0x54;
private const byte PIGLOW_ENABLE_OUTPUT = 0x00;
private const byte PIGLOW_ENABLE_LEDS = 0x13;
private const byte PIGLOW_ENABLE_BANK1 = 0x13;
private const byte PIGLOW_ENABLE_BANK2 = 0x14;
private const byte PIGLOW_ENABLE_BANK3 = 0x15;
private const byte PIGLOW_UPDATE = 0x16;
private const byte PIGLOW_ENABLE = 0xFF;
private const byte PIGLOW_FULL = 0xFF;
private const byte PIGLOW_OFF = 0x00;
private const byte LED_COLOUR_RED = 5;
private const byte LED_COLOUR_ORANGE = 4;
private const byte LED_COLOUR_YELLOW = 3;
private const byte LED_COLOUR_GREEN = 2;
private const byte LED_COLOUR_BLUE = 1;
private const byte LED_COLOUR_WHITE = 0;
// This 3D matrix is organized by color (row) and arm (column)
private byte[][] piGlowLeds = new byte[][]
{
new byte[] {0x0A, 0x0D, 0x0B }, // White leds
new byte[] {0x05, 0x0F, 0x0C }, // Blue leds
new byte[] {0x06, 0x04, 0x0E }, // Green leds
new byte[] {0x09, 0x03, 0x10 }, // Yellow leds
new byte[] {0x08, 0x02, 0x11 }, // Orange leds
new byte[] {0x07, 0x01, 0x12 } // Red leds
// Arm1, Arm2, Arm3
};
internal void Release()
{
piGlow.Dispose();
}
public PiGlowController()
{
}
public bool Initialized
{
get { return piGlow != null; }
}
public async Task<bool> InitializeAsync(bool enableOutput = true)
{
try
{
var settings = new I2cConnectionSettings(PIGLOW_I2C_ADDR);
settings.BusSpeed = I2cBusSpeed.FastMode; /* 400KHz bus speed */
string aqs = I2cDevice.GetDeviceSelector(); /* Get a selector string that will return all I2C controllers on the system */
var dis = await DeviceInformation.FindAllAsync(aqs); /* Find the I2C bus controller devices with our selector string */
piGlow = await I2cDevice.FromIdAsync(dis[0].Id, settings); /* Create an I2cDevice with our selected bus controller and I2C settings */
Debug.WriteLine($"Number of I2C devices: {dis.Count}");
if (piGlow == null)
{
System.Diagnostics.Debug.WriteLine($"Slave address {settings.SlaveAddress} on I2C Controller {dis[0].Id} is currently in use by " +
"another application. Please ensure that no other applications are using I2C.");
return false;
}
if (enableOutput)
{
// Enable output (turn on)
piGlow.Write(new byte[] { PIGLOW_ENABLE_OUTPUT, 0x01 });
// Enable each bank of LEDs (0-5 / 6-11 / 12-17)
piGlow.Write(new byte[] { PIGLOW_ENABLE_BANK1, 0xFF });
piGlow.Write(new byte[] { PIGLOW_ENABLE_BANK2, 0xFF });
piGlow.Write(new byte[] { PIGLOW_ENABLE_BANK3, 0xFF });
}
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"An error occurred with exception {ex.Message} :\r\n {ex.ToString()}");
throw;
}
}
public void TurnOff()
{
setLeds(piGlowLeds, PIGLOW_OFF);
applyChanges();
}
public void AllOn()
{
setLeds(piGlowLeds, PIGLOW_ENABLE);
applyChanges();
}
public void All(int brightness = 100)
{
setLeds(piGlowLeds, applyPercentage(brightness));
applyChanges();
}
public void SetArm(int arm, int brightness = 100)
{
var byteToSet = applyPercentage(brightness);
Debug.WriteLine($"Setting arm {arm} to byte {byteToSet}");
foreach (var color in piGlowLeds)
{
piGlow.Write(new byte[] { color[arm - 1], byteToSet });
}
applyChanges();
}
public void SetColor(int color, int brightness = 100)
{
var byteToSet = applyPercentage(brightness);
Debug.WriteLine($"Setting color {color} to byte {byteToSet}");
setLeds(piGlowLeds[color], byteToSet);
applyChanges();
}
public void AllBlue(int brightness = 100)
{
var byteToSet = applyPercentage(brightness);
// Set all leds
setLeds(piGlowLeds[LED_COLOUR_BLUE], byteToSet);
applyChanges();
}
public void AllWhite(int brightness = 100)
{
var byteToSet = applyPercentage(brightness);
// Set all leds
setLeds(piGlowLeds[LED_COLOUR_WHITE], byteToSet);
applyChanges();
}
public void AllGreen(int brightness = 100)
{
var byteToSet = applyPercentage(brightness);
// Set all leds
setLeds(piGlowLeds[LED_COLOUR_GREEN], byteToSet);
applyChanges();
}
public void AllYellow(int brightness = 100)
{
var byteToSet = applyPercentage(brightness);
// Set all leds
setLeds(piGlowLeds[LED_COLOUR_YELLOW], byteToSet);
applyChanges();
}
public void AllOrange(int brightness = 100)
{
var byteToSet = applyPercentage(brightness);
// Set all leds
setLeds(piGlowLeds[LED_COLOUR_ORANGE], byteToSet);
applyChanges();
}
public void AllRed(int brightness = 100)
{
var byteToSet = applyPercentage(brightness);
// Set all leds
setLeds(piGlowLeds[LED_COLOUR_RED], byteToSet);
applyChanges();
}
private void setLeds(byte[] addresses, byte byteToSet)
{
foreach (var b in addresses)
{
piGlow.Write(new byte[] { b, byteToSet });
}
}
private void setLeds(byte[][] addresses, byte byteToSet)
{
foreach (var ba in addresses)
{
setLeds(ba, byteToSet);
}
}
private void applyChanges()
{
piGlow.Write(new byte[] { PIGLOW_UPDATE, PIGLOW_ENABLE });
}
private byte applyPercentage(int percentage)
{
double converted = percentage * 2.55; // switch to range between 0 and 255
var result = (byte)((int)converted);
Debug.WriteLine($"Converted percentage {percentage} to byte {result}");
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment