private void frequencyChangedHandler(Object sender, EventArgs e) { float multiplier = 1; double freq = 0; double?fVal; // get the frequency multiplier switch (fUnitsBox.Text) { case "Hz": multiplier = 1; break; case "KHz": multiplier = 1000; break; default: MessageBox.Show("Unknown frequency units: " + fUnitsBox.Text); return; } // Check if the frequency is a valid number fVal = Utils.getDoubleFromString(frequencyBox.Text); if (fVal == null) { frequencyBox.BackColor = Color.Salmon; return; } else { frequencyBox.BackColor = Color.Empty; } // Check if the frequency is between 1Hz and 1 MHz freq = fVal.Value * multiplier; if ((freq < 1) || (freq > 1000000)) { frequencyBox.BackColor = Color.Salmon; return; } else { frequencyBox.BackColor = Color.Empty; synchronousPwmFrequency = (int)freq; for (int i = 0; i < 8; i++) { channelControls[i].setNewFrequency((int)freq, fUnitsBox.Text); } } }
/// <summary> /// Recalculates and checks the dutycycle value /// </summary> private Boolean recalculateDutyCycle() { Boolean retVal = false; double? rawVal = Utils.getDoubleFromString(dutyCycleBox.Text); double maxDc = 0; double tmpVal = 0; if (rawVal != null) { if (rawVal.Value >= 0) { switch (dcUnitsBox.Text) { case "%": if (rawVal.Value <= 100) { tmpVal = ((1f / tmpFrequency) / 100f) * rawVal.Value; dutyCycleBox.BackColor = Color.Empty; retVal = true; } else { dutyCycleBox.BackColor = Color.Salmon; } break; case "us": tmpVal = rawVal.Value / 1000000; dutyCycleBox.BackColor = Color.Empty; break; case "ms": tmpVal = rawVal.Value / 1000; dutyCycleBox.BackColor = Color.Empty; break; default: throw new Exception("Unknown unit selected for dutycycle"); } //Check if the dutycycle doesn't exeed the frequency period time maxDc = 1f / tmpFrequency; if (tmpVal > maxDc) { dutyCycleBox.BackColor = Color.Salmon; retVal = false; } else { retVal = true; setTmpDc(tmpVal); updateSliderPosition(); } } else { dutyCycleBox.BackColor = Color.Salmon; } } else { dutyCycleBox.BackColor = Color.Salmon; } return(retVal); }