unkwntech

RGB Led with Brightness control.

Apr 24th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.99 KB | None | 0 0
  1. using System;
  2. using GoBus;
  3. using Microsoft.SPOT.Hardware;
  4. using System.Threading;
  5.  
  6. public class MyRgbLed : GoModule
  7. {
  8.     // Fields
  9.     private const int _frameLength = 0x12;
  10.     private InterruptPort _irqPort;
  11.     private AutoResetEvent _irqPortInterruptEvent;
  12.     private Guid _moduleGuid;
  13.     private byte[] _readFrameBuffer;
  14.     private SPI _spi;
  15.     private SPI.Configuration _spiConfig;
  16.     private byte[] _writeFrameBuffer;
  17.  
  18.     // Methods
  19.     public MyRgbLed()
  20.     {
  21.         this._moduleGuid = new Guid(new byte[] { 0x80, 0x39, 0xe8, 0x2b, 0x55, 0x58, 0xeb, 0x48, 0xab, 0x9e, 0x48, 0xd3, 0xfd, 0xae, 140, 0xee });
  22.         this._irqPortInterruptEvent = new AutoResetEvent(false);
  23.         this._writeFrameBuffer = new byte[0x12];
  24.         this._readFrameBuffer = new byte[0x12];
  25.         this._spiConfig = null;
  26.         GoSocket[] compatibleSockets = base.GetSocketsByUniqueId(this._moduleGuid);
  27.         if (compatibleSockets.Length == 0)
  28.         {
  29.             throw new Exception();
  30.         }
  31.         this.Initialize(compatibleSockets[0]);
  32.     }
  33.  
  34.     public MyRgbLed(GoSocket socket)
  35.     {
  36.         this._moduleGuid = new Guid(new byte[] { 0x80, 0x39, 0xe8, 0x2b, 0x55, 0x58, 0xeb, 0x48, 0xab, 0x9e, 0x48, 0xd3, 0xfd, 0xae, 140, 0xee });
  37.         this._irqPortInterruptEvent = new AutoResetEvent(false);
  38.         this._writeFrameBuffer = new byte[0x12];
  39.         this._readFrameBuffer = new byte[0x12];
  40.         this._spiConfig = null;
  41.         this.Initialize(socket);
  42.     }
  43.  
  44.     private void _irqPort_OnInterrupt(uint data1, uint data2, DateTime time)
  45.     {
  46.         this._irqPortInterruptEvent.Set();
  47.     }
  48.  
  49.     private void GetColor(out byte red, out byte green, out byte blue)
  50.     {
  51.         this._writeFrameBuffer[0] = 0x80;
  52.         this._writeFrameBuffer[1] = 1;
  53.         for (int i = 2; i < (this._writeFrameBuffer.Length - 1); i++)
  54.         {
  55.             this._writeFrameBuffer[i] = 0;
  56.         }
  57.         this._writeFrameBuffer[this._writeFrameBuffer.Length - 1] = CRC8.Compute8(this._writeFrameBuffer, 0, this._writeFrameBuffer.Length - 1);
  58.         this._spi.Write(this._writeFrameBuffer);
  59.         bool responseReceived = false;
  60.         int numRetries = 4;
  61.         int iRetry = 0;
  62.         while (true)
  63.         {
  64.             if (this._irqPortInterruptEvent.WaitOne(3, false) || (iRetry >= (numRetries - 1)))
  65.             {
  66.                 this._writeFrameBuffer[0] = 0;
  67.                 this._writeFrameBuffer[1] = 0;
  68.                 this._writeFrameBuffer[2] = 0;
  69.                 this._writeFrameBuffer[3] = 0;
  70.                 this._writeFrameBuffer[this._writeFrameBuffer.Length - 1] = CRC8.Compute8(this._writeFrameBuffer, 0, this._writeFrameBuffer.Length - 1);
  71.                 this._spi.WriteRead(this._writeFrameBuffer, this._readFrameBuffer);
  72.                 if (this._readFrameBuffer[2] == 3)
  73.                 {
  74.                     red = this._readFrameBuffer[3];
  75.                     green = this._readFrameBuffer[4];
  76.                     blue = this._readFrameBuffer[5];
  77.                     return;
  78.                 }
  79.                 if (iRetry >= (numRetries - 1))
  80.                 {
  81.                     red = 0;
  82.                     blue = 0;
  83.                     green = 0;
  84.                     return;
  85.                 }
  86.                 responseReceived = false;
  87.             }
  88.             else
  89.             {
  90.                 this._writeFrameBuffer[0] = 0x80;
  91.                 this._writeFrameBuffer[1] = 1;
  92.                 this._writeFrameBuffer[this._writeFrameBuffer.Length - 1] = CRC8.Compute8(this._writeFrameBuffer, 0, this._writeFrameBuffer.Length - 1);
  93.                 this._spi.Write(this._writeFrameBuffer);
  94.             }
  95.             iRetry++;
  96.         }
  97.     }
  98.  
  99.     private void Initialize(GoSocket socket)
  100.     {
  101.         Cpu.Pin socketGpioPin;
  102.         SPI.SPI_module socketSpiModule;
  103.         Cpu.Pin socketSpiSlaveSelectPin;
  104.         if (!base.BindSocket(socket, this._moduleGuid))
  105.         {
  106.             throw new ArgumentException();
  107.         }
  108.         socket.GetPhysicalResources(out socketGpioPin, out socketSpiModule, out socketSpiSlaveSelectPin);
  109.         this._spiConfig = new SPI.Configuration(socketSpiSlaveSelectPin, false, 0, 0, false, false, 500, socketSpiModule);
  110.         this._spi = new SPI(this._spiConfig);
  111.         this._irqPort = new InterruptPort(socketGpioPin, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
  112.         this._irqPort.OnInterrupt += new NativeEventHandler(this._irqPort_OnInterrupt);
  113.     }
  114.  
  115.  
  116.     public void SetColor(byte red, byte green, byte blue, double brightness = 0.33)
  117.     {
  118.         red = (byte)(red * brightness);
  119.         green = (byte)(green * brightness);
  120.         blue = (byte)(blue * brightness);
  121.  
  122.         int iRetry = 0;
  123.         bool commandSuccess = false;
  124.         while (!commandSuccess && (iRetry < 0x24))
  125.         {
  126.             byte verifyRed;
  127.             byte verifyGreen;
  128.             byte verifyBlue;
  129.             this._writeFrameBuffer[0] = 0x80;
  130.             this._writeFrameBuffer[1] = 2;
  131.             this._writeFrameBuffer[2] = red;
  132.             this._writeFrameBuffer[3] = green;
  133.             this._writeFrameBuffer[4] = blue;
  134.             this._writeFrameBuffer[this._writeFrameBuffer.Length - 1] = CRC8.Compute8(this._writeFrameBuffer, 0, this._writeFrameBuffer.Length - 1);
  135.             this._spi.Write(this._writeFrameBuffer);
  136.             this.GetColor(out verifyRed, out verifyGreen, out verifyBlue);
  137.             if (((red == verifyRed) && (green == verifyGreen)) && (blue == verifyBlue))
  138.             {
  139.                 break;
  140.             }
  141.             iRetry++;
  142.         }
  143.     }
  144. }
  145.  
  146.  
  147. internal static class CRC8
  148. {
  149.     // Fields
  150.     private static byte[] crcTab;
  151.  
  152.     // Methods
  153.     public static byte Compute8(byte[] data, int index = 0, int count = -1)
  154.     {
  155.         if (data == null)
  156.         {
  157.             throw new ArgumentNullException();
  158.         }
  159.         if (count == -1)
  160.         {
  161.             count = data.Length - index;
  162.         }
  163.         if (count < 0)
  164.         {
  165.             throw new ArgumentOutOfRangeException();
  166.         }
  167.         if ((data.Length - index) < count)
  168.         {
  169.             throw new ArgumentException();
  170.         }
  171.         if (crcTab == null)
  172.         {
  173.             crcTab = GenerateTable(7);
  174.         }
  175.         byte crc = 0;
  176.         int endIndex = index + count;
  177.         while (index < endIndex)
  178.         {
  179.             crc = crcTab[crc ^ data[index]];
  180.             index++;
  181.         }
  182.         return crc;
  183.     }
  184.  
  185.     public static byte[] GenerateTable(byte polynomial)
  186.     {
  187.         byte[] tab = new byte[0x100];
  188.         for (int i = 0; i < 0x100; i++)
  189.         {
  190.             int curr = i;
  191.             for (int j = 0; j < 8; j++)
  192.             {
  193.                 if ((curr & 0x80) != 0)
  194.                 {
  195.                     curr = (curr << 1) ^ polynomial;
  196.                 }
  197.                 else
  198.                 {
  199.                     curr = curr << 1;
  200.                 }
  201.             }
  202.             tab[i] = (byte)curr;
  203.         }
  204.         return tab;
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment