/// <summary> /// Creates a new <see cref="DataCell"/> from specified parameters. /// </summary> /// <param name="parent">The reference to parent <see cref="DataFrame"/> of this <see cref="DataCell"/>.</param> /// <param name="configurationCell">The <see cref="ConfigurationCell"/> associated with this <see cref="DataCell"/>.</param> /// <param name="addEmptyValues">If <c>true</c>, adds empty values for each defined configuration cell definition.</param> public DataCell(DataFrame parent, ConfigurationCell configurationCell, bool addEmptyValues) : this(parent, configurationCell) { if (!addEmptyValues) { return; } // Define needed phasor values foreach (IPhasorDefinition phasorDefinition in configurationCell.PhasorDefinitions) { PhasorValues.Add(new PhasorValue(this, phasorDefinition)); } // Define a frequency and df/dt FrequencyValue = new FrequencyValue(this, configurationCell.FrequencyDefinition); // Define any analog values foreach (IAnalogDefinition analogDefinition in configurationCell.AnalogDefinitions) { AnalogValues.Add(new AnalogValue(this, analogDefinition)); } // Define any digital values foreach (IDigitalDefinition digitalDefinition in configurationCell.DigitalDefinitions) { DigitalValues.Add(new DigitalValue(this, digitalDefinition)); } }
/// <summary> /// Creates a new <see cref="DataCell"/> from specified parameters. /// </summary> /// <param name="parent">The reference to parent <see cref="DataFrame"/> of this <see cref="DataCell"/>.</param> /// <param name="configurationCell">The <see cref="ConfigurationCell"/> associated with this <see cref="DataCell"/>.</param> /// <param name="addEmptyValues">If <c>true</c>, adds empty values for each defined configuration cell definition.</param> public DataCell(DataFrame parent, ConfigurationCell configurationCell, bool addEmptyValues) : this(parent, configurationCell) { if (addEmptyValues) { int x; // Define needed phasor values for (x = 0; x < configurationCell.PhasorDefinitions.Count; x++) { PhasorValues.Add(new PhasorValue(this, configurationCell.PhasorDefinitions[x])); } // Define a frequency and df/dt FrequencyValue = new FrequencyValue(this, configurationCell.FrequencyDefinition); // Define any analog values for (x = 0; x < configurationCell.AnalogDefinitions.Count; x++) { AnalogValues.Add(new AnalogValue(this, configurationCell.AnalogDefinitions[x])); } // Define any digital values for (x = 0; x < configurationCell.DigitalDefinitions.Count; x++) { DigitalValues.Add(new DigitalValue(this, configurationCell.DigitalDefinitions[x])); } } }
/// <summary> /// Parses the information contained in the IO sample bytes reading the value of each configured /// DIO and ADC. /// </summary> private void ParseRawIOSample() { int dataIndex = 3; // Obtain the digital mask. // Available digital IOs in 802.15.4 DigitalHSBMask = ioSamplePayload[1] & 0x01; // 0 0 0 0 0 0 0 1 DigitalLSBMask = ioSamplePayload[2] & 0xFF; // 1 1 1 1 1 1 1 1 // Combine the masks. DigitalMask = (DigitalHSBMask << 8) + DigitalLSBMask; // Obtain the analog mask. // Available analog IOs in 802.15.4 AnalogMask = ((ioSamplePayload[1] << 8) + (ioSamplePayload[2] & 0xFF)) & 0x7E00; // 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 // Read the digital values (if any). There are 9 possible digital lines in 802.15.4 protocol. The // digital mask indicates if there is any digital line enabled to read its value. If 0, no digital // values are received. if (DigitalMask > 0) { // Obtain the digital values. digitalHSBValues = ioSamplePayload[3] & 0x7F; digitalLSBValues = ioSamplePayload[4] & 0xFF; // Combine the values. digitalValues = (digitalHSBValues << 8) + digitalLSBValues; for (int i = 0; i < 16; i++) { if (!ByteUtils.IsBitEnabled(DigitalMask, i)) { continue; } if (ByteUtils.IsBitEnabled(digitalValues, i)) { DigitalValues.Add(IOLine.UNKNOWN.GetDIO(i), IOValue.HIGH); } else { DigitalValues.Add(IOLine.UNKNOWN.GetDIO(i), IOValue.LOW); } } // Increase the data index to read the analog values. dataIndex += 2; } // Read the analog values (if any). There are 6 possible analog lines. The analog mask indicates // if there is any analog line enabled to read its value. If 0, no analog values are received. int adcIndex = 9; while ((ioSamplePayload.Length - dataIndex) > 1 && adcIndex < 16) { if (!ByteUtils.IsBitEnabled(AnalogMask, adcIndex)) { adcIndex += 1; continue; } // 802.15.4 protocol does not provide power supply value, so get just the ADC data. AnalogValues.Add(IOLine.UNKNOWN.GetDIO(adcIndex - 9), ((ioSamplePayload[dataIndex] & 0xFF) << 8) + (ioSamplePayload[dataIndex + 1] & 0xFF)); // Increase the data index to read the next analog values. dataIndex += 2; adcIndex += 1; } }
/// <summary> /// Gets the analog value of the specified <paramref name="ioLine"/>. /// </summary> /// <example>To verify if this sample contains an analog value for the specified /// <paramref name="ioLine"/>, use the method <see cref="HasAnalogValue(IOLine)"/>. /// <c>if (ioSample.HasAnalogValue(IOLine.DIO0_AD0)) { /// var value = ioSample.GetAnalogValue(IOLine.DIO0_AD0); /// ... /// } else { /// ... /// } /// }</c></example> /// <param name="ioLine">The IO line to get its analog value.</param> /// <returns>The analog value of the given IO line.</returns> /// <seealso cref="AnalogValues"/> /// <seealso cref="HasAnalogValue"/> /// <seealso cref="HasAnalogValues"/> /// <seealso cref="IOLine"/> public int GetAnalogValue(IOLine ioLine) { if (!AnalogValues.ContainsKey(ioLine)) { return(int.MaxValue); } return(AnalogValues[ioLine]); }
/// <summary> /// Parses the information contained in the IO sample bytes reading the value of each configured /// DIO and ADC. /// </summary> private void ParseIOSample() { int dataIndex = 4; // Obtain the digital masks. // Available digital IOs DigitalHSBMask = ioSamplePayload[1] & 0x7F; // 0 1 1 1 1 1 1 1 DigitalLSBMask = ioSamplePayload[2] & 0xFF; // 1 1 1 1 1 1 1 1 // Combine the masks. DigitalMask = (DigitalHSBMask << 8) + DigitalLSBMask; // Obtain the analog mask. // Available analog IOs AnalogMask = ioSamplePayload[3] & 0xBF; // 1 0 1 1 1 1 1 1 // Read the digital values (if any). There are 16 possible digital lines. The digital mask indicates // if there is any digital line enabled to read its value. If 0, no digital values are received. if (DigitalMask > 0) { // Obtain the digital values. digitalHSBValues = ioSamplePayload[4] & 0x7F; digitalLSBValues = ioSamplePayload[5] & 0xFF; // Combine the values. digitalValues = (digitalHSBValues << 8) + digitalLSBValues; for (int i = 0; i < 16; i++) { if (!ByteUtils.IsBitEnabled(DigitalMask, i)) { continue; } if (ByteUtils.IsBitEnabled(digitalValues, i)) { DigitalValues.Add(IOLine.UNKNOWN.GetDIO(i), IOValue.HIGH); } else { DigitalValues.Add(IOLine.UNKNOWN.GetDIO(i), IOValue.LOW); } } // Increase the data index to read the analog values. dataIndex += 2; } // Read the analog values (if any). There are 6 possible analog lines. The analog mask indicates // if there is any analog line enabled to read its value. If 0, no analog values are received. int adcIndex = 0; while ((ioSamplePayload.Length - dataIndex) > 1 && adcIndex < 8) { if (!ByteUtils.IsBitEnabled(AnalogMask, adcIndex)) { adcIndex += 1; continue; } // When analog index is 7, it means that the analog value corresponds to the power // supply voltage, therefore this value should be stored in a different value. if (adcIndex == 7) { powerSupplyVoltage = ((ioSamplePayload[dataIndex] & 0xFF) << 8) + (ioSamplePayload[dataIndex + 1] & 0xFF); } else { AnalogValues.Add(IOLine.UNKNOWN.GetDIO(adcIndex), ((ioSamplePayload[dataIndex] & 0xFF) << 8) + (ioSamplePayload[dataIndex + 1] & 0xFF)); } // Increase the data index to read the next analog values. dataIndex += 2; adcIndex += 1; } }
/// <summary> /// Indicates whether or not the specified <paramref name="ioLine"/> has an analog value. /// </summary> /// <param name="ioLine">The IO line to check if has an analog value.</param> /// <returns><c>true</c> if the specified <paramref name="ioLine"/> has an analog value, /// <c>false</c> otherwise.</returns> /// <seealso cref="GetAnalogValue"/> /// <seealso cref="AnalogValues"/> /// <seealso cref="HasAnalogValues"/> /// <seealso cref="IOLine"/> public bool HasAnalogValue(IOLine ioLine) { return(AnalogValues.ContainsKey(ioLine)); }