public void SendDmxPacket(ICommand[] outputStates)
        {
            if (outputStates == null || this._statePacket == null || _serialPort == null)
            {
                return;
            }

            var channelValues = new byte[outputStates.Length];

            for (int index = 0; index < outputStates.Length; index++)
            {
                _8BitCommand command = outputStates[index] as _8BitCommand;
                if (command == null)
                {
                    // State reset
                    channelValues[index] = 0;
                    continue;
                }

                channelValues[index] = command.CommandValue;
            }

            if (!this._serialPort.IsOpen)
            {
                this._serialPort.Open();
            }

            this._statePacket[0] = 0;             // Start code
            Array.Copy(channelValues, 0, this._statePacket, 1, Math.Min(512, channelValues.Length));
            byte[] packet = this._dmxPacketMessage.Packet;
            if (packet != null)
            {
                this._serialPort.Write(packet, 0, packet.Length);
            }
        }
Example #2
0
        public void updateData(ICommand[] outputStates)
        {
            var channelValues = new byte[outputStates.Length];

            //Make sure that editing the output buffer is thread safe
            lock (buffer)
            {
                //copy the lighting commands to the DMX Buffer
                for (int i = 0; i < outputStates.Length; i++)
                {
                    _8BitCommand command = outputStates[i] as _8BitCommand;

                    //Reset the channel if the command is null
                    if (command == null)
                    {
                        // State reset
                        buffer[i + 1] = 0;
                        continue;
                    }

                    //Copy the new intensity value to the output buffer
                    buffer[i + 1] = command.CommandValue;
                }
            }
        }
Example #3
0
 public static ICommand Max(this _8BitCommand command, _8BitCommand commandOther)
 {
     if (command.CommandValue > commandOther.CommandValue)
     {
         return(command);
     }
     return(commandOther);
 }
Example #4
0
 public static ICommand Max(this _8BitCommand command, _8BitCommand commandOther)
 {
     if (command.CommandValue > commandOther.CommandValue)
     {
         return command;
     }
     return commandOther;
 }
Example #5
0
        protected override void _PreRender(CancellationTokenSource tokenSource = null)
        {
            _elementData = new EffectIntents();

            if (_data == null)
            {
                return;
            }

            ICommand command = null;

            switch (ValueType)
            {
            case CustomValueType._8Bit:
                command = new _8BitCommand(Value8Bit);
                break;

            case CustomValueType._16Bit:
                command = new _16BitCommand(Value16Bit);
                break;

            case CustomValueType._32Bit:
                command = new _32BitCommand(Value32Bit);
                break;

            case CustomValueType._64Bit:
                command = new _64BitCommand(Value64Bit);
                break;

            case CustomValueType.Color:
                command = new ColorCommand(ColorValue);
                break;

            case CustomValueType.String:
                command = new StringCommand(StringValue);
                break;
            }

            CommandValue value = new CommandValue(command);

            foreach (ElementNode node in TargetNodes)
            {
                foreach (var leafNode in node.GetLeafEnumerator())
                {
                    if (tokenSource != null && tokenSource.IsCancellationRequested)
                    {
                        return;
                    }

                    IIntent intent = new CommandIntent(value, TimeSpan);
                    _elementData.AddIntentForElement(leafNode.Element.Id, intent, TimeSpan.Zero);
                }
            }
        }
 public override void Handle(_8BitCommand obj)
 {
     if (CombinatorValue == null)
     {
         CombinatorValue = obj;
     }
     else
     {
         CombinatorValue = obj.Max((_8BitCommand)CombinatorValue);
     }
 }
Example #7
0
 public override void Handle(_8BitCommand obj)
 {
     if (CombinatorValue == null)
     {
         CombinatorValue = obj;
     }
     else
     {
         byte value1 = (CombinatorValue as _8BitCommand).CommandValue;
         byte value2 = obj.CommandValue;
         CombinatorValue = new _8BitCommand(Math.Max(value1, value2));
     }
 }
Example #8
0
 public override void Handle(ColorCommand obj)
 {
     if (CombinatorValue == null)
     {
         CombinatorValue = new _8BitCommand(RGBValue.GetGrayscaleLevel(obj.CommandValue));
     }
     else
     {
         byte value1 = (CombinatorValue as _8BitCommand).CommandValue;
         byte value2 = RGBValue.GetGrayscaleLevel(obj.CommandValue);
         CombinatorValue = new _8BitCommand(Math.Max(value1, value2));
     }
 }
Example #9
0
 public override void Handle(_8BitCommand obj)
 {
     if (CombinatorValue == null)
     {
         CombinatorValue = obj;
     }
     else
     {
         byte value1 = CombinatorValue.CommandValue;
         byte value2 = obj.CommandValue;
         CombinatorValue = new _8BitCommand((value1 + value2) >> 1);
     }
 }
Example #10
0
 public override void Handle(_16BitCommand obj)
 {
     if (CombinatorValue == null)
     {
         CombinatorValue = new _8BitCommand(obj.CommandValue);
     }
     else
     {
         byte value1 = CombinatorValue.CommandValue;
         byte value2 = (byte)obj.CommandValue;
         CombinatorValue = new _8BitCommand(Math.Max(value1, value2));
     }
 }
Example #11
0
 public override void Handle(_8BitCommand obj)
 {
     if (CombinatorValue == null)
     {
         CombinatorValue = new _16BitCommand(obj.CommandValue);
     }
     else
     {
         ushort value1 = CombinatorValue.CommandValue;
         ushort value2 = obj.CommandValue;
         CombinatorValue = new _16BitCommand(Math.Max(value1, value2));
     }
 }
 public override void Handle(_8BitCommand obj)
 {
     if (CombinatorValue == null)
     {
         CombinatorValue = new _32BitCommand(obj.CommandValue);
     }
     else
     {
         uint value1 = (CombinatorValue as _32BitCommand).CommandValue;
         uint value2 = obj.CommandValue;
         CombinatorValue = new _32BitCommand(Math.Max(value1, value2));
     }
 }
Example #13
0
        public static byte[] ToChannelValuesAsBytes(this ICommand[] outputStates)
        {
            if (outputStates == null)
            {
                return(new byte[0]);
            }

            var channelValues = new byte[outputStates.Length];

            for (int index = 0; index < outputStates.Length; index++)
            {
                _8BitCommand command = outputStates[index] as _8BitCommand;
                if (command == null)
                {
                    // State reset
                    channelValues[index] = 0;
                    continue;
                }

                channelValues[index] = command.CommandValue;
            }

            return(channelValues);
        }
Example #14
0
 // Handling intents as an evaluator.
 public override void Handle(IIntentState<RGBValue> obj)
 {
     byte byteLevel = (byte)(byte.MaxValue * obj.GetValue().Intensity);
     EvaluatorValue = new _8BitCommand(byteLevel);
 }
Example #15
0
 public override void Handle(IIntentState <IntensityValue> obj)
 {
     EvaluatorValue = new _8BitCommand((byte)(byte.MaxValue * obj.GetValue().Intensity));
 }
Example #16
0
        // Handling intents as an evaluator.
        public override void Handle(IIntentState <ColorValue> obj)
        {
            byte byteLevel = ColorValue.GetGrayscaleLevel(obj.GetValue().Color);

            EvaluatorValue = new _8BitCommand(byteLevel);
        }
Example #17
0
 public void Handle(_64BitCommand obj)
 {
     EvaluatorValue = new _8BitCommand(obj.CommandValue);
 }
Example #18
0
 public override void Handle(IIntentState <LightingValue> obj)
 {
     EvaluatorValue = new _8BitCommand(obj.GetValue().Intensity *byte.MaxValue);
 }
Example #19
0
 public override void Handle(_8BitCommand obj)
 {
     Command = new _8BitCommand(_reducer.Reduce(obj.CommandValue, ReductionPercentage));
 }
Example #20
0
 public override void Handle(_8BitCommand obj)
 {
     Value = (obj.CommandValue > 0) ? (short)1 : (short)0;
 }
Example #21
0
 public virtual void Handle(_8BitCommand obj)
 {
 }
Example #22
0
 // Additionally dispatching command intents and handling the command they wrap.
 public void Handle(_8BitCommand obj)
 {
     EvaluatorValue = obj;
 }
Example #23
0
 public override void Handle(IIntentState<LightingValue> obj)
 {
     EvaluatorValue = new _8BitCommand((byte)(byte.MaxValue * obj.GetValue().Intensity));
 }
Example #24
0
 public void Handle(ColorCommand obj)
 {
     EvaluatorValue = new _8BitCommand(ColorValue.GetGrayscaleLevel(obj.CommandValue));
 }
Example #25
0
 public override void Handle(IIntentState<PositionValue> obj)
 {
     EvaluatorValue = new _8BitCommand((byte) (byte.MaxValue*obj.GetValue().Position));
 }
Example #26
0
		public override void Handle(_8BitCommand obj)
		{
			Value = obj.CommandValue;
		}
Example #27
0
 public override void Handle(_8BitCommand obj)
 {
     Command = new _8BitCommand(_reducer.Reduce(obj.CommandValue, ReductionPercentage));
 }
Example #28
0
        public override void UpdateState(int chainIndex, ICommand[] outputStates)
        {
            if (_networkStream == null && !FakingIt())
            {
                bool success = OpenConnection();
                if (!success)
                {
                    Logging.Warn("BlinkyLinky: failed to connect to device, not updating the current state.");
                    return;
                }
            }

            // build up transmission packet
            byte[] data = new byte[1024];             // overkill, but it'll do
            int    totalPacketLength = 0;

            // protocol is:	4 bytes header
            //				1 byte command
            //
            // at the moment, only the 'Set Values' blinky-protocol command is supported.
            // Set Values:	1 byte for the stream (ie. 0-2, for which RS485 stream)
            //				2 bytes for the length of following data (high byte first) - ie. only the channel/value bytes
            //				2 bytes (repeated): channel, value.

            data[totalPacketLength++] = HEADER_1;
            data[totalPacketLength++] = HEADER_2;
            data[totalPacketLength++] = HEADER_3;
            data[totalPacketLength++] = HEADER_4;
            data[totalPacketLength++] = COMMAND_SET_VALUES;

            data[totalPacketLength++] = (byte)_data.Stream;
            int lengthPosH = totalPacketLength++;
            int lengthPosL = totalPacketLength++;

            bool changed = false;

            for (int i = 0; i < outputStates.Length; i++)
            {
                byte newValue = 0;

                if (outputStates[i] != null)
                {
                    _8BitCommand command = outputStates[i] as _8BitCommand;
                    if (command == null)
                    {
                        continue;
                    }
                    newValue         = command.CommandValue;
                    _nullCommands[i] = 0;
                }
                else
                {
                    // it was a null command. We should turn it off; however, to avoid some potentially nasty flickering,
                    // we will keep track of the null commands for this output, and ignore the first one. Any after that will
                    // actually be sent through.
                    if (_nullCommands[i] == 0)
                    {
                        _nullCommands[i] = 1;
                        newValue         = _lastValues[i];
                    }
                }

                if (_lastValues[i] != newValue)
                {
                    changed = true;
                    data[totalPacketLength++] = (byte)i;
                    data[totalPacketLength++] = newValue;
                    _lastValues[i]            = newValue;
                }
            }

            int totalData = totalPacketLength - lengthPosL - 1;

            data[lengthPosH] = (byte)((totalData >> 8) & 0xFF);
            data[lengthPosL] = (byte)(totalData & 0xFF);

            // don't bother writing anything if we haven't acutally *changed* any values...
            // (also, send at least a 'null' update command every 10 seconds. I think there's a bug in the micro
            // firmware; it doesn't seem to close network connections properly. Need to diagnose more, later.)
            if (changed || _timeoutStopwatch.ElapsedMilliseconds >= 10000)
            {
                try {
                    _timeoutStopwatch.Restart();
                    if (FakingIt())
                    {
                        System.Threading.Thread.Sleep(1);
                    }
                    else
                    {
                        _networkStream.Write(data, 0, totalPacketLength);
                        _networkStream.Flush();
                    }
                }
                catch (Exception ex) {
                    Logging.Warn(
                        "BlinkyLinky: failed to write data to device, this update state may be lost. Closing connection.", ex);
                    CloseConnection();
                }
            }
        }
Example #29
0
 public override void Handle(_8BitCommand obj)
 {
     Value = (obj.CommandValue > 0) ? (short)1 : (short)0;
 }
Example #30
0
        // Handling intents as an evaluator.
        public override void Handle(IIntentState <RGBValue> obj)
        {
            byte byteLevel = (byte)(byte.MaxValue * obj.GetValue().Intensity);

            EvaluatorValue = new _8BitCommand(byteLevel);
        }
Example #31
0
        protected override void _PreRender(CancellationTokenSource tokenSource = null)
        {
            _elementData = new EffectIntents();

            if (_data == null)
                return;

            ICommand command = null;

            switch (ValueType) {
                case CustomValueType._8Bit:
                    command = new _8BitCommand(Value8Bit);
                    break;
                case CustomValueType._16Bit:
                    command = new _16BitCommand(Value16Bit);
                    break;
                case CustomValueType._32Bit:
                    command = new _32BitCommand(Value32Bit);
                    break;
                case CustomValueType._64Bit:
                    command = new _64BitCommand(Value64Bit);
                    break;
                case CustomValueType.Color:
                    command = new ColorCommand(ColorValue);
                    break;
                case CustomValueType.String:
                    command = new StringCommand(StringValue);
                    break;
            }

            CommandValue value = new CommandValue(command);

            foreach (ElementNode node in TargetNodes)
            {
                foreach (var leafNode in node.GetLeafEnumerator())
                {
                    if (tokenSource != null && tokenSource.IsCancellationRequested)
                        return;

                    IIntent intent = new CommandIntent(value, TimeSpan);
                    _elementData.AddIntentForElement(leafNode.Element.Id, intent, TimeSpan.Zero);
                }

            }
        }
Example #32
0
 public override void Handle(_8BitCommand obj)
 {
     ByteValue = obj.CommandValue;
 }
Example #33
0
 virtual public void Handle(_8BitCommand obj)
 {
 }