private void newToolStripMenuItem_Click(object sender, EventArgs e) { _code = new IrCode(); _fileName = null; RefreshForm(); }
/* * public static ushort[] ConvertIrCodeToPronto(IrCode irCode) * { * CodeType codeType; * Int64 value; * * if (Decode(irCode, out codeType, out value)) * return EncodePronto(codeType, value); * else * return null; * } */ /// <summary> /// Converts the ir code into Pronto raw format. /// </summary> /// <param name="irCode">The ir code to convert.</param> /// <returns>Pronto data (raw format).</returns> public static ushort[] ConvertIrCodeToProntoRaw(IrCode irCode) { List <ushort> prontoData = new List <ushort>(); double carrier; ushort prontoCarrier; CodeType codeType; int irCodeCarrier = IrCode.CarrierFrequencyDefault; switch (irCode.Carrier) { case IrCode.CarrierFrequencyDCMode: codeType = CodeType.RawUnmodulated; irCodeCarrier = IrCode.CarrierFrequencyDefault; break; case IrCode.CarrierFrequencyUnknown: codeType = CodeType.RawOscillated; irCodeCarrier = IrCode.CarrierFrequencyDefault; break; default: codeType = CodeType.RawOscillated; irCodeCarrier = irCode.Carrier; break; } prontoCarrier = ConvertToProntoCarrier(irCodeCarrier); carrier = prontoCarrier * ProntoClock; for (int index = 0; index < irCode.TimingData.Length; index++) { int duration = Math.Abs(irCode.TimingData[index]); prontoData.Add((ushort)Math.Round(duration / carrier)); } if (prontoData.Count % 2 != 0) { prontoData.Add(SignalFree); } ushort burstPairs = (ushort)(prontoData.Count / 2); prontoData.Insert(0, (ushort)codeType); // Pronto Code Type prontoData.Insert(1, prontoCarrier); // IR Frequency prontoData.Insert(2, burstPairs); // First Burst Pairs prontoData.Insert(3, 0x0000); // Repeat Burst Pairs return(prontoData.ToArray()); }
private static byte[] DataPacket(IrCode code) { if (code.TimingData.Length == 0) { return(null); } // Construct data bytes into "packet" ... List <byte> packet = new List <byte>(); for (int index = 0; index < code.TimingData.Length; index++) { double time = code.TimingData[index]; byte duration = (byte)Math.Abs(Math.Round(time / 50)); bool pulse = (time > 0); while (duration > 0x7F) { packet.Add((byte)(pulse ? 0xFF : 0x7F)); duration -= 0x7F; } packet.Add((byte)(pulse ? 0x80 | duration : duration)); } // Insert byte count markers into packet data bytes ... int subpackets = (int)Math.Ceiling(packet.Count / (double)4); byte[] output = new byte[packet.Count + subpackets + 1]; int outputPos = 0; for (int packetPos = 0; packetPos < packet.Count;) { byte copyCount = (byte)(packet.Count - packetPos < 4 ? packet.Count - packetPos : 0x04); output[outputPos++] = (byte)(0x80 | copyCount); for (int index = 0; index < copyCount; index++) { output[outputPos++] = packet[packetPos++]; } } output[outputPos] = 0x80; return(output); }
/// <summary> /// Creates an IrCode object from Pronto format file bytes. /// </summary> /// <param name="data">IR file bytes.</param> /// <returns>New IrCode object.</returns> private static IrCode FromProntoData(byte[] data) { string code = Encoding.ASCII.GetString(data); string[] stringData = code.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); ushort[] prontoData = new ushort[stringData.Length]; for (int i = 0; i < stringData.Length; i++) { prontoData[i] = ushort.Parse(stringData[i], NumberStyles.HexNumber); } IrCode newCode = Pronto.ConvertProntoDataToIrCode(prontoData); if (newCode != null) { newCode.FinalizeData(); } // Seems some old files have excessively long delays in them .. this might fix that problem ... return(newCode); }
/// <summary> /// Creates an IrCode object from old IR file bytes. /// </summary> /// <param name="data">IR file bytes.</param> /// <returns>New IrCode object.</returns> private static IrCode FromOldData(byte[] data) { List <int> timingData = new List <int>(); int len = 0; for (int index = 0; index < data.Length; index++) { byte curByte = data[index]; if ((curByte & 0x80) != 0) { len += (curByte & 0x7F); } else { len -= curByte; } if ((curByte & 0x7F) != 0x7F) { timingData.Add(len * 50); len = 0; } } if (len != 0) { timingData.Add(len * 50); } IrCode newCode = new IrCode(timingData.ToArray()); newCode.FinalizeData(); // Seems some old files have excessively long delays in them .. this might fix that problem ... return(newCode); }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(openFileDialog.InitialDirectory)) { openFileDialog.InitialDirectory = Common.FolderIRCommands; } if (openFileDialog.ShowDialog(this) != DialogResult.OK) { return; } using (FileStream file = File.OpenRead(openFileDialog.FileName)) { if (file.Length == 0) { MessageBox.Show(this, "The selected file is empty", "Empty file", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } byte[] fileData = new byte[file.Length]; file.Read(fileData, 0, (int)file.Length); IrCode newCode = IrCode.FromByteArray(fileData); if (newCode == null) { MessageBox.Show(this, "Not a valid IR code file", "Bad file", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } _code = newCode; } _fileName = openFileDialog.FileName; RefreshForm(); }
/* public static ushort[] ConvertIrCodeToPronto(IrCode irCode) { CodeType codeType; Int64 value; if (Decode(irCode, out codeType, out value)) return EncodePronto(codeType, value); else return null; } */ /// <summary> /// Converts the ir code into Pronto raw format. /// </summary> /// <param name="irCode">The ir code to convert.</param> /// <returns>Pronto data (raw format).</returns> public static ushort[] ConvertIrCodeToProntoRaw(IrCode irCode) { List<ushort> prontoData = new List<ushort>(); double carrier; ushort prontoCarrier; CodeType codeType; int irCodeCarrier = IrCode.CarrierFrequencyDefault; switch (irCode.Carrier) { case IrCode.CarrierFrequencyDCMode: codeType = CodeType.RawUnmodulated; irCodeCarrier = IrCode.CarrierFrequencyDefault; break; case IrCode.CarrierFrequencyUnknown: codeType = CodeType.RawOscillated; irCodeCarrier = IrCode.CarrierFrequencyDefault; break; default: codeType = CodeType.RawOscillated; irCodeCarrier = irCode.Carrier; break; } prontoCarrier = ConvertToProntoCarrier(irCodeCarrier); carrier = prontoCarrier * ProntoClock; for (int index = 0; index < irCode.TimingData.Length; index++) { int duration = Math.Abs(irCode.TimingData[index]); prontoData.Add((ushort) Math.Round(duration / carrier)); } if (prontoData.Count % 2 != 0) prontoData.Add(SignalFree); ushort burstPairs = (ushort) (prontoData.Count / 2); prontoData.Insert(0, (ushort) codeType); // Pronto Code Type prontoData.Insert(1, prontoCarrier); // IR Frequency prontoData.Insert(2, burstPairs); // First Burst Pairs prontoData.Insert(3, 0x0000); // Repeat Burst Pairs return prontoData.ToArray(); }
/// <summary> /// Creates an IrCode object from old IR file bytes. /// </summary> /// <param name="data">IR file bytes.</param> /// <returns>New IrCode object.</returns> private static IrCode FromOldData(byte[] data) { List<int> timingData = new List<int>(); int len = 0; for (int index = 0; index < data.Length; index++) { byte curByte = data[index]; if ((curByte & 0x80) != 0) len += (curByte & 0x7F); else len -= curByte; if ((curByte & 0x7F) != 0x7F) { timingData.Add(len * 50); len = 0; } } if (len != 0) timingData.Add(len * 50); IrCode newCode = new IrCode(timingData.ToArray()); newCode.FinalizeData(); // Seems some old files have excessively long delays in them .. this might fix that problem ... return newCode; }
private void ReceivedMessage(IrssMessage received) { IrssLog.Debug("Received Message \"{0}\"", received.Type); try { switch (received.Type) { case MessageType.RegisterClient: if ((received.Flags & MessageFlags.Success) == MessageFlags.Success) { _irServerInfo = IRServerInfo.FromBytes(received.GetDataAsBytes()); _registered = true; string message = "Connected"; IrssLog.Info(message); Invoke(new UpdateWindowDel(UpdateWindow), new string[] { message }); } else if ((received.Flags & MessageFlags.Failure) == MessageFlags.Failure) { _registered = false; string message = "Failed to connect"; IrssLog.Warn(message); Invoke(new UpdateWindowDel(UpdateWindow), new string[] { message }); } return; case MessageType.BlastIR: if ((received.Flags & MessageFlags.Success) == MessageFlags.Success) { string message = "Blast successful"; IrssLog.Info(message); Invoke(new UpdateWindowDel(UpdateWindow), new string[] { message }); } else if ((received.Flags & MessageFlags.Failure) == MessageFlags.Failure) { string message = "Failed to blast IR command"; IrssLog.Error(message); Invoke(new UpdateWindowDel(UpdateWindow), new string[] { message }); } break; case MessageType.LearnIR: if ((received.Flags & MessageFlags.Success) == MessageFlags.Success) { byte[] dataBytes = received.GetDataAsBytes(); _code = IrCode.FromByteArray(dataBytes); _fileName = null; string message = "Learned IR Successfully"; IrssLog.Info(message); Invoke(new UpdateWindowDel(UpdateWindow), new string[] { message }); RefreshForm(); } else if ((received.Flags & MessageFlags.Failure) == MessageFlags.Failure) { string message = "Failed to learn IR command"; IrssLog.Warn(message); Invoke(new UpdateWindowDel(UpdateWindow), new string[] { message }); } else if ((received.Flags & MessageFlags.Timeout) == MessageFlags.Timeout) { string message = "Learn IR command timed-out"; IrssLog.Warn(message); Invoke(new UpdateWindowDel(UpdateWindow), new string[] { message }); } break; case MessageType.ServerShutdown: _registered = false; Invoke(new UpdateWindowDel(UpdateWindow), new string[] { "Server shut down" }); return; case MessageType.Error: IrssLog.Error("Error from server: " + received.GetDataAsString()); return; } } catch (Exception ex) { IrssLog.Error(ex); MessageBox.Show(this, ex.Message, "IR File Tool Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }