-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from BojanDolic/develop_res_freq_calculator
Resonant frequency calculator
- Loading branch information
Showing
13 changed files
with
777 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'dart:math' as math; | ||
|
||
class Calculator { | ||
double calculateMMC(double capacitance, int seriesCap, int parallelCap) { | ||
double? _capacitance = 0.0; | ||
|
||
try { | ||
var _cap = (capacitance * parallelCap) / seriesCap; | ||
_capacitance = double.parse(_cap.toStringAsFixed(7)); | ||
} catch (e) { | ||
_capacitance = 0.0; | ||
} | ||
return _capacitance; | ||
} | ||
|
||
double calculateResFrequency(double inductance, double capacitance) { | ||
double resFreqResult = 0.0; | ||
|
||
var sqrtCap = math.sqrt(capacitance); | ||
var sqrtInd = math.sqrt(inductance); | ||
|
||
var frequency = 1 / (2 * math.pi * sqrtInd * sqrtCap); | ||
|
||
/*try { | ||
resFreqResult = double.parse(frequency.toStringAsFixed(2)); | ||
} catch (e) { | ||
resFreqResult = 0.0; | ||
}*/ | ||
return frequency; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
import 'package:coiler_app/calculator/calculator.dart'; | ||
import 'package:coiler_app/util/constants.dart'; | ||
import 'package:coiler_app/util/conversion.dart'; | ||
import 'package:coiler_app/util/list_constants.dart'; | ||
import 'package:coiler_app/widgets/border_container.dart'; | ||
import 'package:coiler_app/widgets/input_field_dropdown.dart'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class ResonantFrequencyScreen extends StatefulWidget { | ||
const ResonantFrequencyScreen({Key? key}) : super(key: key); | ||
|
||
static String id = "/calculators/resfreq"; | ||
|
||
@override | ||
_ResonantFrequencyScreenState createState() => | ||
_ResonantFrequencyScreenState(); | ||
} | ||
|
||
class _ResonantFrequencyScreenState extends State<ResonantFrequencyScreen> { | ||
final _formKey = GlobalKey<FormState>(); | ||
|
||
String inductance = ""; | ||
String capacitance = ""; | ||
String frequency = ""; | ||
|
||
Units inductanceUnit = Units.MICRO; | ||
Units capacitanceUnit = Units.MICRO; | ||
Units frequencyUnit = Units.KILO; | ||
|
||
TextEditingController inductanceController = TextEditingController(); | ||
TextEditingController capacitanceController = TextEditingController(); | ||
|
||
void calculateFrequency() { | ||
if (!_formKey.currentState!.validate()) { | ||
return; | ||
} | ||
var inductanceTemp = double.tryParse(inductance); | ||
var capacitanceTemp = double.tryParse(capacitance); | ||
|
||
if (inductanceTemp != 0 && capacitanceTemp != 0) { | ||
var inductance = Converter() | ||
.convertUnits(inductanceTemp, inductanceUnit, Units.DEFAULT); | ||
var capacitance = Converter() | ||
.convertUnits(capacitanceTemp, capacitanceUnit, Units.DEFAULT); | ||
|
||
var frequencyTemp = | ||
Calculator().calculateResFrequency(inductance, capacitance); | ||
var frequency = | ||
Converter().convertUnits(frequencyTemp, Units.DEFAULT, frequencyUnit); | ||
|
||
setState(() { | ||
this.frequency = frequency.toStringAsFixed(4); | ||
}); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
backgroundColor: Colors.white, | ||
body: SafeArea( | ||
child: Padding( | ||
padding: const EdgeInsets.symmetric( | ||
horizontal: 12, | ||
vertical: 8, | ||
), | ||
child: SingleChildScrollView( | ||
child: Form( | ||
autovalidateMode: AutovalidateMode.always, | ||
key: _formKey, | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.stretch, | ||
children: [ | ||
Align( | ||
alignment: Alignment.center, | ||
child: BorderContainer( | ||
elevated: true, | ||
child: Column( | ||
children: [ | ||
Image.asset( | ||
"assets/lc_circuit.png", | ||
height: 200, | ||
), | ||
Text( | ||
"Calculate resonant frequency of your LC circuit by entering values below.", | ||
style: normalTextStyleOpenSans14, | ||
textAlign: TextAlign.center, | ||
) | ||
], | ||
), | ||
), | ||
), | ||
SizedBox( | ||
height: 10, | ||
), | ||
BorderContainer( | ||
child: Row( | ||
mainAxisSize: MainAxisSize.max, | ||
mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
children: [ | ||
Text("Frequency: $frequency"), | ||
Container( | ||
padding: EdgeInsets.symmetric( | ||
horizontal: 9, | ||
), | ||
decoration: BoxDecoration( | ||
color: lightBlueColor, | ||
borderRadius: BorderRadius.circular(9), | ||
), | ||
child: DropdownButton<Units>( | ||
value: frequencyUnit, | ||
items: frequencyDropDownList, | ||
borderRadius: BorderRadius.circular(9), | ||
underline: Container(), | ||
onChanged: (value) { | ||
setState(() { | ||
frequencyUnit = value!; | ||
calculateFrequency(); | ||
}); | ||
}), | ||
), | ||
], | ||
), | ||
), | ||
SizedBox( | ||
height: 20, | ||
), | ||
Padding( | ||
padding: const EdgeInsets.symmetric( | ||
vertical: 9, | ||
), | ||
child: InputFieldDropDown( | ||
hintText: "Enter inductance", | ||
labelText: "Inductance", | ||
controller: inductanceController, | ||
onTextChanged: (text) { | ||
setState(() { | ||
inductance = text; | ||
calculateFrequency(); | ||
}); | ||
}, | ||
validator: (text) { | ||
if (text == null || text.isEmpty) { | ||
return "Input invalid"; | ||
} else { | ||
return null; | ||
} | ||
}, | ||
dropDownValue: inductanceUnit, | ||
onDropDownChanged: (value) { | ||
setState(() { | ||
inductanceUnit = value!; | ||
calculateFrequency(); | ||
}); | ||
}, | ||
dropDownList: inductanceDropDownList), | ||
), | ||
Padding( | ||
padding: const EdgeInsets.symmetric( | ||
vertical: 9, | ||
), | ||
child: InputFieldDropDown( | ||
hintText: "Enter capacitance", | ||
labelText: "Capacitance", | ||
controller: capacitanceController, | ||
onTextChanged: (text) { | ||
setState(() { | ||
capacitance = text; | ||
calculateFrequency(); | ||
}); | ||
}, | ||
validator: (text) {}, | ||
dropDownValue: capacitanceUnit, | ||
onDropDownChanged: (value) { | ||
setState(() { | ||
capacitanceUnit = value!; | ||
calculateFrequency(); | ||
}); | ||
}, | ||
dropDownList: capacitanceDropDownList), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import 'package:coiler_app/util/constants.dart'; | ||
|
||
class Converter { | ||
static const Map<Units, double> unitsMap = { | ||
Units.DEFAULT: 1.0, | ||
Units.MILI: 0.001, | ||
Units.MICRO: 0.000001, | ||
Units.NANO: 0.000000001, | ||
Units.PICO: 0.000000000001, | ||
Units.KILO: 1000, | ||
Units.MEGA: 1000000, | ||
Units.GIGA: 1000000000, | ||
}; | ||
|
||
double getCapMultiplier(Units unit) => unitsMap[unit]!; | ||
|
||
/// Function used to convert different units | ||
/// [value] value to be converted | ||
/// [from] from which unit we need to convert | ||
/// [to] unit which we will convert to | ||
/// | ||
/// Example: | ||
/// | ||
/// ```dart | ||
/// result = convertUnits(22, Units.MICRO, Units.NANO) -> 22000.0 | ||
/// result = convertUnits(2, Units.DEFAULT, Units.MICRO) -> 2000000.0 | ||
/// result = convertUnits(13.4, Units.PICO, Units.NANO) -> 0.0134 | ||
/// ``` | ||
/// | ||
double convertUnits(dynamic value, Units from, Units to) { | ||
double result = 0.0; | ||
try { | ||
var multiplier = getCapMultiplier(from) / getCapMultiplier(to); | ||
double tempResult = value * multiplier; | ||
result = tempResult; //double.parse(tempResult.toStringAsFixed(7)); | ||
} catch (e) { | ||
print(e); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import 'package:coiler_app/util/constants.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
const capacitanceDropDownList = [ | ||
DropdownMenuItem( | ||
child: Text("F"), | ||
value: Units.DEFAULT, | ||
), | ||
DropdownMenuItem( | ||
child: Text("uF"), | ||
value: Units.MICRO, | ||
), | ||
DropdownMenuItem( | ||
child: Text("nF"), | ||
value: Units.NANO, | ||
), | ||
DropdownMenuItem( | ||
child: Text("pF"), | ||
value: Units.PICO, | ||
), | ||
]; | ||
|
||
const inductanceDropDownList = [ | ||
DropdownMenuItem( | ||
child: Text("H"), | ||
value: Units.DEFAULT, | ||
), | ||
DropdownMenuItem( | ||
child: Text("mH"), | ||
value: Units.MILI, | ||
), | ||
DropdownMenuItem( | ||
child: Text("uH"), | ||
value: Units.MICRO, | ||
), | ||
DropdownMenuItem( | ||
child: Text("nH"), | ||
value: Units.NANO, | ||
), | ||
]; | ||
|
||
const frequencyDropDownList = [ | ||
DropdownMenuItem( | ||
child: Text("Hz"), | ||
value: Units.DEFAULT, | ||
), | ||
DropdownMenuItem( | ||
child: Text("kHz"), | ||
value: Units.KILO, | ||
), | ||
DropdownMenuItem( | ||
child: Text("MHz"), | ||
value: Units.MEGA, | ||
), | ||
DropdownMenuItem( | ||
child: Text("GHz"), | ||
value: Units.GIGA, | ||
), | ||
]; |
Oops, something went wrong.