Skip to content

Commit

Permalink
Merge pull request #2 from BojanDolic/develop_res_freq_calculator
Browse files Browse the repository at this point in the history
Resonant frequency calculator
  • Loading branch information
BojanDolic authored Mar 1, 2022
2 parents e4abf1f + bdfbadb commit 44388b1
Show file tree
Hide file tree
Showing 13 changed files with 777 additions and 2 deletions.
Binary file added assets/lc_circuit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions lib/calculator/calculator.dart
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;
}
}
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:coiler_app/screens/calculators/capacitor_screen.dart';
import 'package:coiler_app/screens/calculators/resonant_freq_screen.dart';
import 'package:coiler_app/screens/calculators_screen.dart';
import 'package:coiler_app/screens/information_screen.dart';
import 'package:coiler_app/screens/main_screen.dart';
Expand All @@ -19,6 +20,8 @@ class MyApp extends StatelessWidget {
MainScreen.id: (context) => const MainScreen(),
CalculatorsScreen.id: (context) => const CalculatorsScreen(),
CapacitorScreen.id: (context) => const CapacitorScreen(),
ResonantFrequencyScreen.id: (context) =>
const ResonantFrequencyScreen(),
InformationScreen.id: (context) => const InformationScreen(),
},
initialRoute: MainScreen.id,
Expand Down
191 changes: 191 additions & 0 deletions lib/screens/calculators/resonant_freq_screen.dart
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),
),
],
),
),
),
),
),
);
}
}
5 changes: 3 additions & 2 deletions lib/screens/calculators_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:coiler_app/screens/calculators/capacitor_screen.dart';
import 'package:coiler_app/screens/calculators/resonant_freq_screen.dart';
import 'package:coiler_app/util/constants.dart';
import 'package:flutter/material.dart';

Expand All @@ -13,7 +14,7 @@ class CalculatorsScreen extends StatelessWidget {
body: SafeArea(
child: Column(
children: [
Center(
const Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
Expand Down Expand Up @@ -48,7 +49,7 @@ class CalculatorsScreen extends StatelessWidget {
height: 26,
),
onTap: () {
//TODO Navigate
Navigator.pushNamed(context, ResonantFrequencyScreen.id);
},
),
CalculatorItem(
Expand Down
18 changes: 18 additions & 0 deletions lib/util/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ const lightCategoryTextStyle = TextStyle(
fontFamily: "OpenSans",
color: Colors.black54,
);

const normalTextStyleOpenSans14 = TextStyle(
fontFamily: "OpenSans",
fontWeight: FontWeight.normal,
fontSize: 15,
);

const biggerTextStyleOpenSans15 = TextStyle(
fontFamily: "OpenSans",
fontWeight: FontWeight.w500,
fontSize: 16,
);

const lightBlueColor = Color(0xffe1efff);

enum Units { DEFAULT, MILI, MICRO, NANO, PICO, KILO, MEGA, GIGA }

const voltageUnitText = "V";
41 changes: 41 additions & 0 deletions lib/util/conversion.dart
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;
}
}
59 changes: 59 additions & 0 deletions lib/util/list_constants.dart
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,
),
];
Loading

0 comments on commit 44388b1

Please sign in to comment.