@ -1,20 +1,30 @@
import React , { Component } from 'react' ;
import { ScrollView , StyleSheet , Tex t , TouchableOpacity , View } from 'react-native' ;
import { Alert , ScrollView , StyleSheet , TouchableOpacity , View } from 'react-native' ;
import Resistor from './resistor' ;
import Section from './section' ;
import ClearAll from './clearall' ;
import MyText from './mytext'
const styles = StyleSheet . create ( {
button : {
width : 100 ,
height : 30 ,
padding : 10 ,
backgroundColor : 'lightgray' ,
alignItems : 'center' ,
justifyContent : 'center' ,
margin : 3
function getUnit ( n ) {
if ( n >= 1000000 ) {
return { unit : ' MΩ' , mult : 1000000 } ;
} else if ( n >= 1000 ) {
return { unit : ' kΩ' , mult : 1000 } ;
} else if ( n >= 1 ) {
return { unit : ' Ω' , mult : 1 } ;
} else {
return { unit : ' mΩ' , mult : 0.001 } ;
}
} ) ;
}
const styles = {
table : {
borderBottomColor : 'lightgrey' ,
borderBottomWidth : 1 ,
padding : 3 ,
} ,
} ;
export default class Calc extends Component {
constructor ( props ) {
@ -24,42 +34,165 @@ export default class Calc extends Component {
render ( ) {
const { state } = this . props ;
const allValid = state . have . every ( ( x ) => x . valid ) ;
const nonEmpty = state . have . some ( ( x ) => x . value ) ;
const targetValid = state . target . valid ;
const targetNonEmpty = state . target . value ? true : false ;
const haveAllValid = state . have . every ( ( x ) => x . valid ) ;
const haveNonEmpty = state . have . some ( ( x ) => x . value ) ;
const precision = ( ) => {
if ( targetValid && targetNonEmpty ) {
const pieces = state . target . value . split ( '.' ) ;
if ( pieces [ 1 ] ) {
if ( pieces [ 1 ] . length > 20 ) {
return 20 ; // Max toFixed() allows
}
if ( pieces [ 1 ] . length > 2 ) {
return pieces [ 1 ] . length ;
}
}
}
return 2 ;
} ;
const targetRes = state . target . value * state . target . mult ;
const reciprocalSum = state . have
. filter ( ( x ) => x . value )
. reduce ( ( a , b ) => a + 1.0 / ( b . value * b . mult ) , 0.0 ) ;
. reduce ( ( a , b ) => a + 1.0 / ( b . value * b . mult ) , 0.0 ) ;
const sum = 1.0 / reciprocalSum ;
const percentError = Math . abs ( ( sum - targetRes ) / targetRes * 100 ) ;
const nextRes = 1.0 / ( 1.0 / targetRes - reciprocalSum ) ;
const printSum = ( ) => {
if ( haveAllValid && haveNonEmpty ) {
const { unit , mult } = getUnit ( sum ) ;
const val = sum / mult ;
return val . toFixed ( precision ( ) ) + unit ;
} else {
return '---' ;
}
} ;
const printPercentError = ( ) => {
if ( targetValid && targetNonEmpty && haveAllValid && haveNonEmpty ) {
// Check if the 'overall resistor value' matches the target
const { unit , mult } = getUnit ( targetRes ) ;
console . log ( ( targetRes / mult ) . toFixed ( precision ( ) ) + unit ) ;
if ( printSum ( ) === ( targetRes / mult ) . toFixed ( precision ( ) ) + unit ) {
return ( 0.0 ) . toFixed ( precision ( ) ) ;
}
return percentError . toFixed ( precision ( ) ) ;
} else {
return '---' ;
}
} ;
const printNextRes = ( ) => {
if ( targetValid && targetNonEmpty && haveAllValid && haveNonEmpty ) {
// Check if we are done because error is 0 or value's exact
if ( nextRes == Infinity || printPercentError ( ) == 0.0 ) {
return 'Done.' ;
} else if ( nextRes < 0 ) {
return 'N/A.' ;
}
const { unit , mult } = getUnit ( nextRes ) ;
const val = nextRes / mult ;
return val . toFixed ( precision ( ) ) + unit ;
} else {
return '---' ;
}
} ;
const printInfo = ( ) => {
if ( ! haveAllValid ) {
return 'A parallel resistor is not valid.' ;
} else if ( ! targetNonEmpty ) {
return 'No target resistor.' ;
} else if ( ! targetValid ) {
return 'Target resistor not valid.' ;
} else if ( ! haveNonEmpty ) {
return 'No resistors in parallel.' ;
} else if ( printNextRes ( ) === 'N/A.' ) {
return (
< MyText >
No solution exists . < MyText
style = { { color : '#3b6f73' } }
onPress = { ( ) => {
Alert . alert (
'No Solution Exists' ,
'This app recommends resistors to be added in parallel. It\'s impossible to increase resistance (to make it closer to the target) by adding another resistor in parallel.\n\nPlease remove the lowest value resistor if you wish to keep solving.' ,
[ { text : 'OK' } ]
) ;
} }
>
Why ?
< / M y T e x t >
< / M y T e x t >
) ;
} else if ( printNextRes ( ) === 'Done.' ) {
return 'Target achieved.' ;
} else {
return 'Add the next resistor.' ;
}
} ;
return (
< ScrollView >
< View style = { { padding : 20 } } >
< Section >
< Text >
< My Text>
Enter the exact resistance target :
< / T e x t >
< / M y T e x t >
< / S e c t i o n >
< Section >
< Resistor form = "target" / >
< / S e c t i o n >
< Section >
< Text >
Overall parallel resistor value :
{ allValid && nonEmpty ? ' ' + 1 / reciprocalSum : ' ???' }
< / T e x t >
< Text >
Error between this and the target : { 0 } %
< / T e x t >
< Text >
Next recommended resistor value : { 0 }
< / T e x t >
< View style = { { flexDirection : 'row' } } >
< View >
< MyText style = { styles . table } >
Overall parallel resistor value :
< / M y T e x t >
< MyText style = { styles . table } >
Error between this and target :
< / M y T e x t >
< MyText style = { styles . table } >
Next resistor value needed :
< / M y T e x t >
< / V i e w >
< View >
< MyText style = { styles . table } selectable = { true } >
{ printSum ( ) }
< / M y T e x t >
< MyText style = { styles . table } selectable = { true } >
{ printPercentError ( ) } %
< / M y T e x t >
< MyText style = { styles . table } selectable = { true } >
{ printNextRes ( ) }
< / M y T e x t >
< / V i e w >
< / V i e w >
< MyText style = { { padding : styles . table . padding } } >
{ printInfo ( ) }
< / M y T e x t >
< / S e c t i o n >
< Section >
< Text >
< My Text>
Enter the resistors you have in parallel :
< / T e x t >
< / M y T e x t >
< / S e c t i o n >
< Section >
{ state . have . map ( ( x , n ) =>
< Resistor form = "have" index = { n } key = { n } / >
) }
< / S e c t i o n >
< View style = { { alignItems : 'center' } } >
< ClearAll / >
< / V i e w >
< / V i e w >
< / S c r o l l V i e w >
) ;