Today we’ll talk about the Preview Release of TinyCLR-OS
5 Likes
To learn more and download TinyCLR OS:
[url]https://www.ghielectronics.com/community/forum/topic?id=24044[/url]
TinyCLR code for Sparkfun Studio Spectrum Shield with Strip LEDs.
using System;
using System.Diagnostics;
using System.Threading;
using GHIElectronics.TinyCLR.Pins;
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Adc;
namespace SpectrumShield
{
public class Program
{
static LedStripLPD8806 mLedStrip;
const int LED_INTENSITY = 2;
static Random mRandom = new Random();
const int NUM_LEDS = 56;
static GpioPin ResetPin;
static GpioPin Strobe;
static AdcChannel Ain;
static double[] Frequencies_One = new double[7];
public static void Main()
{
var Adc = AdcController.GetDefault();
Ain = Adc.OpenChannel(FEZPandaIII.AnalogInput.A0);
var gpio = GpioController.GetDefault(); /* Get the default GPIO controller on the system */
Strobe = gpio.OpenPin(FEZPandaIII.Gpio.D4);
Strobe.Write(GHIElectronics.TinyCLR.Devices.Gpio.GpioPinValue.High);
Strobe.SetDriveMode(GHIElectronics.TinyCLR.Devices.Gpio.GpioPinDriveMode.Output);
ResetPin = gpio.OpenPin(FEZPandaIII.Gpio.D5);
ResetPin.Write(GpioPinValue.High);
ResetPin.SetDriveMode(GHIElectronics.TinyCLR.Devices.Gpio.GpioPinDriveMode.Output);
Thread.Sleep(5000);
//Initialize Spectrum Analyzers
Strobe.Write(GHIElectronics.TinyCLR.Devices.Gpio.GpioPinValue.Low);
Thread.Sleep(1);// delay(1);
ResetPin.Write(GHIElectronics.TinyCLR.Devices.Gpio.GpioPinValue.High);
Thread.Sleep(1);//delay(1);
Strobe.Write(GHIElectronics.TinyCLR.Devices.Gpio.GpioPinValue.High);
Thread.Sleep(1);//delay(1);
Strobe.Write(GpioPinValue.Low);
Thread.Sleep(1);//delay(1);
ResetPin.Write(GpioPinValue.Low);
mLedStrip = new LedStripLPD8806(NUM_LEDS);
Debug.WriteLine("starting...");
while (true)
{
Read_Frequencies();
Graph_Frequencies();
}
}
static void Read_Frequencies()
{
//Read frequencies for each band
for (int i = 0; i < 7; i++)
{
Frequencies_One[i] = Ain.ReadRatio();// analogRead(DC_One);
Strobe.Write(GpioPinValue.High);
Strobe.Write(GpioPinValue.Low);
}
}
static void Graph_Frequencies()
{
for (int i = 0; i < 7; i++)
{
switch ((int)(7 * Frequencies_One[i]))
{
case 1:
mLedStrip.Set(0 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
break;
case 2:
mLedStrip.Set(0 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(1 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
break;
case 3:
mLedStrip.Set(0 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(1 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(2 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
break;
case 4:
mLedStrip.Set(0 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(1 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(2 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(3 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(4 + (8 * i), LED_INTENSITY/2, LED_INTENSITY/2, 0);//YELLOW
break;
case 5:
mLedStrip.Set(0 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(1 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(2 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(3 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(4 + (8 * i), LED_INTENSITY/2, LED_INTENSITY/2, 0);//YELLOW
mLedStrip.Set(5 + (8 * i), LED_INTENSITY/2, LED_INTENSITY/2, 0);//YELLOW
break;
case 6:
mLedStrip.Set(0 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(1 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(2 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(3 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(4 + (8 * i), LED_INTENSITY/2, LED_INTENSITY/2, 0);//YELLOW
mLedStrip.Set(5 + (8 * i), LED_INTENSITY/2, LED_INTENSITY/2, 0);//YELLOW
mLedStrip.Set(6 + (8 * i), LED_INTENSITY, 0, 0);//RED
break;
case 7:
mLedStrip.Set(0 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(1 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(2 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(3 + (8 * i), 0, LED_INTENSITY, 0);//GREEN
mLedStrip.Set(4 + (8 * i), LED_INTENSITY/2, LED_INTENSITY/2, 0);//YELLOW
mLedStrip.Set(5 + (8 * i), LED_INTENSITY/2, LED_INTENSITY/2, 0);//YELLOW
mLedStrip.Set(6 + (8 * i), LED_INTENSITY, 0, 0);//RED
mLedStrip.Set(7 + (8 * i), LED_INTENSITY, 0, 0);//RED
break;
default:
mLedStrip.Set(0, 0, 0);
break;
}
}
mLedStrip.Set(0, 0, 0);
}
static void FlashTest(object o)
{
for (int test = 0; test < 3; test++)
{
int r = test == 0 ? 32 : 0;
int g = test == 1 ? 32 : 0;
int b = test == 2 ? 32 : 0;
mLedStrip.Set(r, g, b);
Thread.Sleep(250);
}
mLedStrip.TurnOff();
}
}
}
LedStripLPD8806 Class:
using GHIElectronics.TinyCLR.Pins;
using GHIElectronics.TinyCLR.Devices.Spi;
public class LedStripLPD8806
{
/// <summary>this LPD8806 wants every byte to have 0x80 on, for some reason</summary>
private const byte MASK = 0x80;
/// <summary>AND'ed in to get the real color component out</summary>
private const byte UNMASK = 0x7F;
/// <summary>number of LEDs in our strip</summary>
private readonly int mNumLeds;
/// <summary>number of bytes used for the strip (mNumLeds * 3)</summary>
private readonly int mLedByteCount;
/// <summary>LED data plus unlatch bytes</summary>
private byte[] mData;
/// <summary>true if the user doesn't want to push data to the strip yet</summary>
private bool mUpdating;
/// <summary>the SPI object for the strip itself</summary>
private SpiDevice mLedStrip;
public LedStripLPD8806(int numLeds)
{
mLedStrip = SpiDevice.FromId("SPI2", new SpiConnectionSettings(FEZPandaIII.Gpio.A1)
{ ClockFrequency = 2000000, Mode = SpiMode.Mode0, DataBitLength = 8 });
int latchBytes = ((numLeds + 63) / 64) * 3;
mLedByteCount = numLeds * 3;
mData = new byte[mLedByteCount + latchBytes];
mNumLeds = numLeds;
// start with all the LEDs off
for (int i = 0; i < mLedByteCount; i++)
{
mData[i] = MASK;
}
// give the strip an inital poke of the latch bytes (no idea
// why this is needed)
mLedStrip.Write(new byte[latchBytes]);
// push the initial values (all off) to the strip
SendUpdate();
}
/// <summary>
/// pushes the colors to the strip, if we're not doing an update
/// </summary>
public void SendUpdate()
{
if (!mUpdating)
mLedStrip.Write(mData);
}
/// <summary>
/// turns off a particular LED
/// </summary>
/// <param name="index">which LED (0-based) to turn off</param>
public void TurnOff(int index)
{
int i = index * 3;
mData[i + 0] = MASK;
mData[i + 1] = MASK;
mData[i + 2] = MASK;
SendUpdate();
}
/// <summary>
/// Turns off the entire LED strip
/// </summary>
public void TurnOff()
{
for (int i = 0; i < mLedByteCount; i += 3)
{
mData[i + 0] = MASK;
mData[i + 1] = MASK;
mData[i + 2] = MASK;
}
SendUpdate();
}
/// <summary>
/// gets or sets a particular LED RGB color value
/// </summary>
/// <param name="index">the LED in question</param>
/// <returns>the RGB (not BRG) colors in the lower 24 bits</returns>
public int this[int index]
{
get
{
int i = index * 3;
int g = (mData[i + 0] & UNMASK);
int r = (mData[i + 1] & UNMASK);
int b = (mData[i + 2] & UNMASK);
// NB: returning RGB, not BRG
int color = r << 16 | g << 8 | b << 0;
return color;
}
set
{
Set(index, value >> 16, value >> 8, value);
}
}
/// <summary>
/// sets a particular LED to a color (0-127 for each color)
/// </summary>
/// <param name="index">the LED to set</param>
/// <param name="red">the red value for the LED</param>
/// <param name="green">the green value for the LED</param>
/// <param name="blue">the blue value for the LED</param>
public void Set(int index, int red, int green, int blue)
{
int i = index * 3;
mData[i + 0] = (byte)(MASK | green);
mData[i + 1] = (byte)(MASK | red);
mData[i + 2] = (byte)(MASK | blue);
SendUpdate();
}
/// <summary>
/// sets all LEDs in the strip to a particular color
/// </summary>
/// <param name="red">the red value for the LED</param>
/// <param name="green">the green value for the LED</param>
/// <param name="blue">the blue value for the LED</param>
public void Set(int red, int green, int blue)
{
for (int i = 0; i < mLedByteCount; i += 3)
{
mData[i + 0] = (byte)(MASK | green);
mData[i + 1] = (byte)(MASK | red);
mData[i + 2] = (byte)(MASK | blue);
}
SendUpdate();
}
}
Very cool. I love that it is all based on NEW stuff - VS2017. Not 4 year old versions. Thanks!
2 Likes
I got it working last night with a G80TH
2 Likes
@ Greg Norris -
I have searched but I could not find anything about where to source the Seeed Studio Spectrum Shield.
Do you have a part number or other info?
Thank You!
@ cyberh0me -
sparkfun spectrum
That is what I thought but I was not sure.
Thanks
1 Like
@ willgeorge -
cyberh0me is correct it is the Spark Fun Shield he posted the link too.