Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using GoBus;
- using Microsoft.SPOT.Hardware;
- using System.Threading;
- public class MyRgbLed : GoModule
- {
- // Fields
- private const int _frameLength = 0x12;
- private InterruptPort _irqPort;
- private AutoResetEvent _irqPortInterruptEvent;
- private Guid _moduleGuid;
- private byte[] _readFrameBuffer;
- private SPI _spi;
- private SPI.Configuration _spiConfig;
- private byte[] _writeFrameBuffer;
- // Methods
- public MyRgbLed()
- {
- this._moduleGuid = new Guid(new byte[] { 0x80, 0x39, 0xe8, 0x2b, 0x55, 0x58, 0xeb, 0x48, 0xab, 0x9e, 0x48, 0xd3, 0xfd, 0xae, 140, 0xee });
- this._irqPortInterruptEvent = new AutoResetEvent(false);
- this._writeFrameBuffer = new byte[0x12];
- this._readFrameBuffer = new byte[0x12];
- this._spiConfig = null;
- GoSocket[] compatibleSockets = base.GetSocketsByUniqueId(this._moduleGuid);
- if (compatibleSockets.Length == 0)
- {
- throw new Exception();
- }
- this.Initialize(compatibleSockets[0]);
- }
- public MyRgbLed(GoSocket socket)
- {
- this._moduleGuid = new Guid(new byte[] { 0x80, 0x39, 0xe8, 0x2b, 0x55, 0x58, 0xeb, 0x48, 0xab, 0x9e, 0x48, 0xd3, 0xfd, 0xae, 140, 0xee });
- this._irqPortInterruptEvent = new AutoResetEvent(false);
- this._writeFrameBuffer = new byte[0x12];
- this._readFrameBuffer = new byte[0x12];
- this._spiConfig = null;
- this.Initialize(socket);
- }
- private void _irqPort_OnInterrupt(uint data1, uint data2, DateTime time)
- {
- this._irqPortInterruptEvent.Set();
- }
- private void GetColor(out byte red, out byte green, out byte blue)
- {
- this._writeFrameBuffer[0] = 0x80;
- this._writeFrameBuffer[1] = 1;
- for (int i = 2; i < (this._writeFrameBuffer.Length - 1); i++)
- {
- this._writeFrameBuffer[i] = 0;
- }
- this._writeFrameBuffer[this._writeFrameBuffer.Length - 1] = CRC8.Compute8(this._writeFrameBuffer, 0, this._writeFrameBuffer.Length - 1);
- this._spi.Write(this._writeFrameBuffer);
- bool responseReceived = false;
- int numRetries = 4;
- int iRetry = 0;
- while (true)
- {
- if (this._irqPortInterruptEvent.WaitOne(3, false) || (iRetry >= (numRetries - 1)))
- {
- this._writeFrameBuffer[0] = 0;
- this._writeFrameBuffer[1] = 0;
- this._writeFrameBuffer[2] = 0;
- this._writeFrameBuffer[3] = 0;
- this._writeFrameBuffer[this._writeFrameBuffer.Length - 1] = CRC8.Compute8(this._writeFrameBuffer, 0, this._writeFrameBuffer.Length - 1);
- this._spi.WriteRead(this._writeFrameBuffer, this._readFrameBuffer);
- if (this._readFrameBuffer[2] == 3)
- {
- red = this._readFrameBuffer[3];
- green = this._readFrameBuffer[4];
- blue = this._readFrameBuffer[5];
- return;
- }
- if (iRetry >= (numRetries - 1))
- {
- red = 0;
- blue = 0;
- green = 0;
- return;
- }
- responseReceived = false;
- }
- else
- {
- this._writeFrameBuffer[0] = 0x80;
- this._writeFrameBuffer[1] = 1;
- this._writeFrameBuffer[this._writeFrameBuffer.Length - 1] = CRC8.Compute8(this._writeFrameBuffer, 0, this._writeFrameBuffer.Length - 1);
- this._spi.Write(this._writeFrameBuffer);
- }
- iRetry++;
- }
- }
- private void Initialize(GoSocket socket)
- {
- Cpu.Pin socketGpioPin;
- SPI.SPI_module socketSpiModule;
- Cpu.Pin socketSpiSlaveSelectPin;
- if (!base.BindSocket(socket, this._moduleGuid))
- {
- throw new ArgumentException();
- }
- socket.GetPhysicalResources(out socketGpioPin, out socketSpiModule, out socketSpiSlaveSelectPin);
- this._spiConfig = new SPI.Configuration(socketSpiSlaveSelectPin, false, 0, 0, false, false, 500, socketSpiModule);
- this._spi = new SPI(this._spiConfig);
- this._irqPort = new InterruptPort(socketGpioPin, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
- this._irqPort.OnInterrupt += new NativeEventHandler(this._irqPort_OnInterrupt);
- }
- public void SetColor(byte red, byte green, byte blue, double brightness = 0.33)
- {
- red = (byte)(red * brightness);
- green = (byte)(green * brightness);
- blue = (byte)(blue * brightness);
- int iRetry = 0;
- bool commandSuccess = false;
- while (!commandSuccess && (iRetry < 0x24))
- {
- byte verifyRed;
- byte verifyGreen;
- byte verifyBlue;
- this._writeFrameBuffer[0] = 0x80;
- this._writeFrameBuffer[1] = 2;
- this._writeFrameBuffer[2] = red;
- this._writeFrameBuffer[3] = green;
- this._writeFrameBuffer[4] = blue;
- this._writeFrameBuffer[this._writeFrameBuffer.Length - 1] = CRC8.Compute8(this._writeFrameBuffer, 0, this._writeFrameBuffer.Length - 1);
- this._spi.Write(this._writeFrameBuffer);
- this.GetColor(out verifyRed, out verifyGreen, out verifyBlue);
- if (((red == verifyRed) && (green == verifyGreen)) && (blue == verifyBlue))
- {
- break;
- }
- iRetry++;
- }
- }
- }
- internal static class CRC8
- {
- // Fields
- private static byte[] crcTab;
- // Methods
- public static byte Compute8(byte[] data, int index = 0, int count = -1)
- {
- if (data == null)
- {
- throw new ArgumentNullException();
- }
- if (count == -1)
- {
- count = data.Length - index;
- }
- if (count < 0)
- {
- throw new ArgumentOutOfRangeException();
- }
- if ((data.Length - index) < count)
- {
- throw new ArgumentException();
- }
- if (crcTab == null)
- {
- crcTab = GenerateTable(7);
- }
- byte crc = 0;
- int endIndex = index + count;
- while (index < endIndex)
- {
- crc = crcTab[crc ^ data[index]];
- index++;
- }
- return crc;
- }
- public static byte[] GenerateTable(byte polynomial)
- {
- byte[] tab = new byte[0x100];
- for (int i = 0; i < 0x100; i++)
- {
- int curr = i;
- for (int j = 0; j < 8; j++)
- {
- if ((curr & 0x80) != 0)
- {
- curr = (curr << 1) ^ polynomial;
- }
- else
- {
- curr = curr << 1;
- }
- }
- tab[i] = (byte)curr;
- }
- return tab;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment