Temperature in iConvert

Post Reply
User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Temperature in iConvert

Post by Dutchman »

If you are not educated with SI units then there is a good way to understand the normal temperature range in degrees Celsius, to remember this short poem: "Zero is freezing, 10 is not, 20 is warm and 30 is hot."
About the weather in the USA I know that zero degrees is very cold and 100 degrees very hot. In order to know more accurately these temperatures, I have made the following functions with test program.
The conversion is done in two steps. First the temperature is converted to the Celsius scale, after which the conversion is made to one of the seven other temperature scales.
The required formulas have been found at https://en.wikipedia.org/wiki/Conversio ... emperature

Code: Select all

'Temperature conversion, by Dutchman, October 2018
' functions for iConvert
'
'===== Presets =====
OPTION BASE 1
'===== Constants ====
Tscale$="Temperature"

Tscales=8
Temperatures:
DATA "Celsius [°C]", "Delisle [°D]", "Fahrenheit [°F]", "Kelvin [K]", "Newton [°N]", "Rankine [°Ra]", "Réamur [°Re]", "Rømer [°Rø]"
DATA 1,5,3,2,6,4,7,8 ' unit-pointers
'
'===== Main ====
RESTORE TO Temperatures
GOSUB LoadArrays
'--- scan all conversions
Selp=1
FOR Selq=2 TO Tscales
  Invalue=40
  Outvalue=FromCelsius(Celsius(Invalue,measureval(selp)),measureval(selq))
  PRINT Invalue&Tstring$(selp)&" = "&outvalue&Tstring$(selq)
  '--- reverse conversion
  Invalue=outvalue
  Outvalue=FromCelsius(Celsius(Invalue,measureval(selq)),measureval(selp))
  PRINT "   "&Invalue&Tstring$(selq)&" = "&outvalue&Tstring$(selp)
NEXT Selq

END

'======  Functions and Subroutines ======
'
DEF FromCelsius(Value, UnitPointer)
'Converts Value on Unit-scale to Celsius-scale
'  to newvalue in scale of Unitpointer
ON UnitPointer GOTO 1,2,3,4,5,6,7,8
  1 RETURN Value 'Celsius
  2 RETURN Value+273.15 'Kelvin
  3 RETURN Value*9/5+32 'Fahrenheit
  4 RETURN (Value+273.15)*9/5 'Rankine
  5 RETURN (100-Value)*3/2 'Delisle
  6 RETURN Value*33/100 'Newton
  7 RETURN Value*4/5 'Réamur
  8 RETURN Value*21/40+7.5 'Rømer
END DEF
'
DEF Celsius(Value,UnitPointer)
'Converts Value in Unit to new value in Celsius
ON UnitPointer GOTO 1,2,3,4,5,6,7,8
  1 RETURN Value 'Celsius
  2 RETURN Value-273.15 'Kelvin
  3 RETURN (Value-32)*5/9 'Fahrenheit
  4 RETURN (Value-491.67)*5/9 'Rankine
  5 RETURN 100-Value*2/3 'Delisle
  6 RETURN Value*100/33 'Newton
  7 RETURN Value*5/4 'Réamur
  8 RETURN (Value-7.5)*40/21 'Rømer
END DEF
'
LoadArrays:
DIM measure$(Tscales)
DIM measureval(Tscales)
FOR i= 1 TO Tscales
READ Measure$(i)
NEXT i
FOR i= 1 TO Tscales
READ MeasureVal(i)
NEXT i
RETURN
'
DEF Tstring$(nr)
' string with or without °
s$=.measure$(nr)
IF INSTR(s$,"Kelvin")>0 THEN RETURN " "&s$ ELSE RETURN "° "&s$ 
END DEF
Screenshot 1.JPG
Screenshot 1.JPG (54.04 KiB) Viewed 3610 times
I have embedded the functions in iConvert 3.1.
The DATA line for values (for array 'measureval()') is now used for pointers to the correct formula.
The category is referred to as the variable Tscale$, so that it can be used to detect the correct conversion method.
The conversion itself is moved to the subroutine 'Convert' because then no complex IF ... ELSE constructions are required for multiple choices. The selected calculation can there simply be terminated with the command RETURN.
The changes are marked with a red background in the editor.

Code: Select all

/*
'r'
iConvert V3.1 with temperature category
''
by rbytes and Dutchman, October 2018
Converts measurements in many categories.
A collaborative international project.

'r'
V…-…
- Temperature category added by Dutchman
''
V3
- Currency category added by Dutchman
  DATA generated with "Update currencies.sb"
V2
- animation added to title
  (plays only once, at launch)
- measures added in most categories
- scientific notation is now used for
  results larger than 100,000,000 or
  smaller than .00000001
V1.1
- added Horsepower (Metric) to
  Power category
- eliminated >>> button;
  calculation is now automatic
  when list selections or
  input values are changed.
- code efficiency improved
*/
OPTION BASE 1
SET ORIENTATION LANDSCAPE
SET BUTTONS CUSTOM
SET TOOLBAR OFF
laun$=LAUNCHER$()
GET SCREEN SIZE sw,sh

' set to 1 to test iPhone layout on iPad
iostest=0
IF iostest THEN
  sw=568
  sh=320
  dev$=""
ENDIF
rw=sw/1024!rh=sh/740


' *** Note - IP address may change if router is rebooted ***
' run ipconfig on your PC to find its IP address.

URL$="192.170.61.43"   ' example of IP address
DIM h$(2)


' each line of a file sent to a PC print server must end with these characters:

cr$=CHR$(13)       ' carriage return character
lf$=CHR$(10)       ' line feed character


' interface object names

x$="guide"
y$="guide2"
n$="title"
o$="category"
p$="from"
q$="to"
pi$="from_in"
qi$="to_in"
qi2$="cover"


lf$=CHR$(10)
q$=CHR$(34)
cat=1          ' default category (area)
invalue=1

upshift=7

' correction for iPad display only
IF rw=1 then shift=10

' correction for non-iPad display only
IF rw<1 then shift2=75

fm$="##,###,###,###,###,###,###.#######"    ' formats the result with commas
fm2$="####################.#######"      ' formats the result without commas


' number of measures in each category
'r'
cats=14
''
DIM cat$(cats)
area=9
cook=9
dat=8
energ=12
leng=13
prefix=21
pow=12
press=13
spd=11
'r'
Tscales=8 ! Tscale$="Temperature"
''
timing=10
vol=12
wgt=10
toba=10


' include constant 'Valuta' and 'currencies:' data

{currencies.data}

GRAPHICS
GRAPHICS CLEAR .9,1,1


 ' animate title if gif animation file exists

A$="iconvert3.GIF"
IF FILE_EXISTS(A$) THEN
 SPRITE N$ LOAD A$
  SPRITE N$ AT sw/3-shift2,sh/60+3*shift scale 1*rh
  SPRITE N$ SHOW
ELSE
  DRAW IMAGE "iconvert.PNG" AT sw/2.8,sh/20 SCALE .8*rw
ENDIF
' read categories
RESTORE TO Categories

FOR t=1 TO cats
  READ cat$(t)
NEXT t

RESTORE TO area     ' default category
items=area
DIM measure$(items)
GOSUB setcategory


' create three lists

SET LISTS FONT SIZE 18*rw
LIST o$ TEXT cat$ AT sw*.1,260*rh SIZE 240*rw,310*rh
LIST o$ SELECT cat
LIST p$ TEXT measure$ AT sw*.385,260*rh SIZE 240*rw,310*rh
LIST p$ SELECT 1
LIST q$ TEXT measure$ AT sw*.672,260*rh SIZE 240*rw,310*rh
LIST q$ SELECT 1


' create fields and buttons

SET BUTTONS FONT SIZE 20*rw
DRAW COLOR 0,0,0
guide$="Pick from each list"&lf$&"Enter quantity here"
DRAW LINE sw*.26,168*rh TO sw*.392,168*rh
FILL COLOR 0,0,0
FILL CIRCLE sw*.39,168*rh SIZE 3*rw
FILL COLOR 1,1,1
FIELD x$ TEXT guide$ AT sw*.1,132*rh-upshift SIZE 250,60 RO ML
FIELD x$ FONT SIZE 18*rw
FIELD x$ BACK ALPHA 0
guide2$="All lists can be scrolled"
DRAW LINE sw*.096,618*rh TO sw*.38,618*rh
DRAW LINE sw*.63,618*rh TO sw*.91,618*rh
FIELD y$ TEXT guide2$ AT sw*.41,600*rh-upshift*.8 SIZE 250,60 RO ML
FIELD y$ FONT SIZE 18*rw
FIELD y$ BACK ALPHA 0
FIELD o$ TEXT "CATEGORY" AT sw*.115,210*rh SIZE 130,30 RO
FIELD o$ FONT SIZE 20*rw
FIELD o$ BACK ALPHA 0
FIELD p$ TEXT "FROM" AT sw*.405,210*rh SIZE 100,30 RO
FIELD p$ FONT SIZE 20*rw
FIELD p$ BACK ALPHA 0
FIELD q$ TEXT "TO" AT sw*.69,210*rh SIZE 70,30 RO
FIELD q$ FONT SIZE 20*rw
FIELD q$ BACK ALPHA 0
'BUTTON "calc" TEXT ">>>" AT sw*.62,150*rh SIZE 60*rw,30*rw
FILL ALPHA 0
BUTTON "copy" TEXT "COPY" AT 285*rw,650*rh SIZE 100*rw,50*rh
BUTTON "print" TEXT "PRINT" AT 405*rw,650*rh SIZE 100*rw,50*rh
BUTTON "save" TEXT "SAVE" AT 525*rw,650*rh SIZE 100*rw,50*rh
BUTTON "stop" TEXT "STOP" AT 645*rw,650*rh SIZE 100*rw,50*rh
FILL ALPHA 1
FIELD pi$ TEXT "1" AT sw*.400,150*rh SIZE 200*rw,30*rh
FIELD pi$ FONT SIZE 18*rw
FIELD qi$ TEXT "1" AT sw*.640,145*rh SIZE 280*rw,40*rh
FIELD qi$ FONT SIZE 22*rw
FIELD qi2$ TEXT "" AT sw*.640,145*rh SIZE 280*rw,40*rh RO
FIELD qi2$ BACK ALPHA 0
FILL COLOR .6,.8,.8
FILL RECT sw*.395,145*rh TO sw*.6,185*rh
FILL COLOR .8,.8,.8
FILL RECT sw*.635,140*rh TO sw*.92,190*rh
DRAW RECT sw*.1-1,258*rh TO sw*.1+240*rw+1,258*rh+312*rh
DRAW RECT sw*.386-2,258*rh TO sw*.386+240*rw,258*rh+312*rh
DRAW RECT sw*.673-2,258*rh TO sw*.673+240*rw,258*rh+312*rh

timer=time ()    ' set timer to stop title anim after one cycle


' main program loop

DO

' play once, then stop animation

IF time()-timer>1 AND time()-timer <2 and not noplay THEN
  IF FILE_EXISTS(A$) THEN SPRITE N$ PLAY
  noplay=1
ENDIF
IF time()-timer>6 AND time()-timer <7 THEN
  IF FILE_EXISTS(A$) THEN SPRITE N$ STOP
ENDIF


' check which category is selected

type=LIST_SELECTED(o$)


' choose and prepare a new category if necessary

IF type<>cat THEN
  cat=type
  changed=1
'r' address 95 added
  ON cat GOTO 10,20,25,30,40,50,60,70,80,90,95,100,110,120
''
  10 RESTORE TO area
     items=area
     GOTO skip
  20 RESTORE TO cooking
     items=cook
     GOTO skip
  25 RESTORE TO currencies
     items=curr
     GOTO skip
  30 RESTORE TO DATA
     items=dat
     GOTO skip
  40 RESTORE TO energy
     items=energ
     GOTO skip
  50 RESTORE TO length
     items=leng
     GOTO skip
  60 RESTORE TO prefixes
     items=prefix
     GOTO skip
  70 RESTORE TO power
     items=pow
     GOTO skip
     ' use the next two lines to show a category not yet ready
     ' gosub tobe  ' measure name
     ' goto skip
  80 RESTORE TO pressure
     items=press
     GOTO skip
  90 RESTORE TO speed ' speed
     items=SPD
     GOTO skip
  95 RESTORE TO Temperatures
     items=Tscales
     GOTO Skip
  100 RESTORE TO timing ' time
     items=timing
     GOTO skip
  110 RESTORE TO Volume
     items=vol
     GOTO skip
  120 RESTORE TO weight
     items=wgt
     GOTO skip
''
  skip:
  GOSUB setcategory
  LIST p$ TEXT measure$
  LIST p$ SELECT 1
  LIST q$ TEXT measure$
  LIST q$ SELECT 1
  FIELD pi$ TEXT "1"
ENDIF

IF LIST_SELECTED(p$)<>selp or LIST_SELECTED(q$)<>selq then changed=1


' calculate the conversion

if VAL(FIELD_TEXT$(pi$))<>invalue or changed then
  invalue=VAL(FIELD_TEXT$(pi$))
  selp=LIST_SELECTED(p$)
  selq=LIST_SELECTED(q$)
'r'
  GOSUB Convert ' calculate OutValue
''
  if outvalue<100000000 and outvalue>.000000001 then 
    out$=STR$(outvalue,fm$)
    out2$=STR$(outvalue,fm2$)
    format(out$)!out$=format.form$
    format(out2$)!out2$=format.form$
  else
    out$=STR$(outvalue)
  endif
  fsize=22*rw
  FIELD qi$ FONT SIZE fsize
  FIELD qi$ TEXT out$
  FIELD qi2$ TEXT ""
ENDIF

' copy the conversion info to clipboard

IF bp("copy") THEN
  GOSUB compile
  IF cp=0 THEN!CLIPBOARD CLEAR!cp=1!ENDIF
  CLIPBOARD WRITE convert$
  BEEP
ENDIF


' print the conversion info

IF bp("print") THEN
GOSUB compile
h$(1) = "content-type:text/html"   ' make header info
h$(2) = "content-length:" & LEN(convert$)

' print convert$ using Henko print technique
HTTP URL$ HEADER H$ POST convert$   ' send doc to the print server
BEEP
ENDIF


' save the conversion info to a file

IF bp("save") THEN
  GOSUB compile
  fname$="converted.txt"
  IF FILE_EXISTS(fname$) THEN FILE fname$ DELETE
  FILE fname$ WRITELINE convert$
  BEEP
ENDIF


' end the program

IF bp("stop") THEN
  IF laun$="desktop" THEN
    IF FILE_EXISTS("/launch") THEN
      RUN "/-Launch.sb"
    ELSE
      EXIT
    ENDIF
  ENDIF
  END
ENDIF
SLOWDOWN
UNTIL 0
END

'r'========== Subroutines and Functions =============
Convert:
'--- Convert via formula
'  temperature
IF Cat$(cat)=Tscale$ THEN
  Outvalue=FromCelsius(Celsius(Invalue,measureval(selp)),measureval(selq))
  RETURN
ENDIF
'--- Convert via ratio
  outvalue=invalue*1/(measureval(selq)/measureval(selp))
RETURN

' temperature conversion

DEF FromCelsius(Value,UnitPointer)
'Converts Value on 'Unit'-scale to Celsius-scale
'  to newvalue in scale of Unitpointer
ON UnitPointer GOTO 1,2,3,4,5,6,7,8
  1 RETURN Value 'Celsius
  2 RETURN Value+273.15 'Kelvin
  3 RETURN Value*9/5+32 'Fahrenheit
  4 RETURN (Value+273.15)*9/5 'Rankine
  5 RETURN (100-Value)*3/2 'Delisle
  6 RETURN Value*33/100 'Newton
  7 RETURN Value*4/5 'Réamur
  8 RETURN Value*21/40+7.5 'Rømer
END DEF
'
DEF Celsius(Value,UnitPointer)
'Converts Value in 'Unit' to new value in Celsius
ON UnitPointer GOTO 1,2,3,4,5,6,7,8
  1 RETURN Value 'Celsius
  2 RETURN Value-273.15 'Kelvin
  3 RETURN (Value-32)*5/9 'Fahrenheit
  4 RETURN (Value-491.67)*5/9 'Rankine
  5 RETURN 100-Value*2/3 'Delisle
  6 RETURN Value*100/33 'Newton
  7 RETURN Value*5/4 'Réamur
  8 RETURN (Value-7.5)*40/21 'Rømer
END DEF
''
' read the data for a category

setcategory:
DIM measure$(items)
FOR t=1 TO items
  READ measure$(t)
NEXT t
DIM measureval(items)
FOR t=1 TO items
  READ measureval(t)
NEXT t
RETURN


' prepare a string showing the conversion, for copying or saving to a file

compile:
  selp=LIST_SELECTED(p$)
  selq=LIST_SELECTED(q$)
  temp1$=measure$(selp)
  temp2$=measure$(selq)


  ' if a value is 1 or a fraction, change the name from plural to singular
  convin=val(FIELD_TEXT$(p$))
  temp1len=LEN(temp1$)
  IF convin=<1 AND RIGHT$(temp1$,1)="s" THEN temp1$=LEFT$(temp1$,temp1len-1)
  IF VAL(out2$)=<1 AND RIGHT$(temp2$,1)="s" THEN temp2$=LEFT$(temp2$,LEN(temp2$)-1)
  convert$&=FIELD_TEXT$(pi$)&" "&temp1$&" = "&FIELD_TEXT$(qi$)&" "&temp2$
  convert$&=cr$&lf$&FIELD_TEXT$(pi$)&" "&temp1$&" = "&out2$&" "&temp2$&cr$&lf$&cr$&lf$
RETURN


' this subroutine is used when adding a new category, until its data is complete.

tobe:
  RESTORE TO tba
  items=toba
  DIM measure$(items)
  GOSUB setcategory
RETURN


' shortcut for button press

DEF bp(a$) = BUTTON_PRESSED(a$)


' remove scientific notation before displaying result

DEF format(form$)
  WHILE LEFT$(form$,1)=" " OR LEFT$(form$,1)=","
    form$=RIGHT$(form$,LEN(form$)-1)
    ' trim leading spaces and separators
  ENDWHILE
  IF INSTR (form$, ".") THEN        ' if form$ has a decimal point
    IF NOT numpad.curr THEN
    ' trim trailing zeros
    WHILE RIGHT$(form$,1)="0"
      form$=LEFT$(form$,LEN(form$)-1)
    ENDWHILE
    IF RIGHT$(form$,1)="." THEN
      form$=LEFT$(form$,LEN(form$)-1)
    ENDIF
    ENDIF
  ENDIF
END DEF


/*
Conversion Data

The technique I use to calculate conversions is to choose a reference measure in the middle of the range and set its value to 1. All other measures are then assigned numbers representing the ratio of their value to the reference measure's value.
*/

Categories:

DATA "Area", "Cooking", "Currency "&CurrDate$ ,"Data", "Energy", "Length", "Prefixes", "Power", "Pressure", "Speed"
'r'
DATA Tscale$, "Time", "Volume", "Weight"
''
Area:
DATA "Acres", "Square Kilometers", "Hectares", "Square Meters", "Square Centimeters", "Square Miles", "Square Yards", "Square Feet", "Square Inches"
DATA 4046.85642, 1000000, 10000, 1, .0001, 2589988.110266, .8361273, .0929030, .000645159722

Cooking:
DATA "Centiliters", "Cups (CD)", "Fluid Ounces (CD)", "Liters", "Milliliters", "Pints (CD)", "Quarts (CD)", "Tablespoons", "Teaspoons"
DATA .35195,8,1,35.195079,.035195,20,40,.520421,.1734737

Data:
DATA "Bits", "Bytes", "Words", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes", "Petabytes"
DATA .000125, .001, .008, 1, 1000, 1000000, 1000000000, 1000000000000

Energy:
DATA "BTU", "Calories", "Cubic Mile of Oil", "Foot Pounds", "Horsepower Per Hr", "Joules", "KiloJoules", "KiloCalories", "Kilowatt Hours", "Kilotons", "MegaJoules", "Therms (US)"
DATA 251.99576, 1, 38334721315564331336, .32404825, 641615.559278, .23900573, 239.00573, 1000, 860420.6501033, 999999974320, 239005.7299999, 25210420.65043

Length:
DATA "Ångströms","Centimeters", "Fathoms", "Feet", "Furlongs (US)", "Inches", "Kilometers", "Meters","Mils", "Miles", "Millimeters", "Nanometers", "Yards"
DATA .00000001, 1, 182.88, 30.48, 20116.8, 2.54, 100000, 100, .00254, 160934.39999, .1, .0000001, 91.44

Power: '12 items
DATA "Watts","Kilowatts", "Megawatts", "Gigawatts", "Terawatts", "Horsepower (IT)", "Horsepower (Metric)", "Moosepower", "Calories per Hour", "BTU per Hour", "Foot Pounds per Hr", "Tons Refrigeration"
DATA 1,1000, 1000000, 1000000000, 1000000000000, 745.69987, 756.042476, 2438.4385749, 0.0011629, 2930710000, 3766160000, 3516.8

Prefixes:
DATA "yotta", "zetta", "exa", "peta", "tera", "giga", "mega", "kilo", "hecto", "deca", "none", "deci", "centi", "milli", "micro", "nano", "pico", "femto", "atto", "zepto", "yocto"
DATA 1000000000000000000000000, 1000000000000000000000, 1000000000000000000, 1000000000000000, 1000000000000, 1000000000, 1000000, 1000, 100, 10, 1, .1, .01, .001, .000001, .000000001, .000000000001, .000000000000001, .000000000000000001, .000000000000000000001, .000000000000000000000001

Pressure:
DATA "Bars", "Millibars", "Pascals", "HectoPascals", "KiloPascals", "MegaPascals", "Atmospheres", "Pounds per Sq. Foot", "Pounds per Sq. Inch", "Inches of Water", "Inches of Mercury", "Centimeters of Water", "Centimeters of Mercury"
DATA 1000, 1, .01, 1, 10, 10000, 1013.250099, .47880258, 68.947572, 2.490889, 33.864, .98066496, 13.332283459

Speed:
DATA "Feet per Second", "Feet per Minute", "Furlongs per Fortnight", "Inches per Second", "Kilometers per Hour", "Kilometers per Sec.", "Knots", "Miles per Hour", "Miles per Second", "Speed of Light", "Speed of Sound"
DATA 1.09728, .018288, .000598715, .09144, 1, 3600, 1.8519999, 1.609349, 5793.638399, 1079252848.794, 1225.0439999
'r'
Temperatures:
DATA "Celsius [°C]", "Delisle [°D]", "Fahrenheit [°F]", "Kelvin [K]", "Newton [°N]", "Rankine [°Ra]", "Réamur [°Re]", "Rømer [°Rø]"
DATA 1,5,3,2,6,4,7,8 ' unit-pointers
''
Timing:
DATA "Milliseconds", "Seconds", "Minutes", "Hours", "Days", "Weeks", "Fortnights", "Months", "Years", "Leap Years"
DATA .00001666666, .01666666, 1, 60, 1440, 10080, 20160, 43800, 525600, 527040

Volume:
DATA "Cubic Centimeters", "Cubic Feet", "Cubic Inches", "Cubic Yards", "Cups (CD)", "Fluid Ounces (CD)", "Gallons (CD)", "Gallons (US)", "Liters", "Milliliters", "Pints (CD)", "Quarts (CD)"
DATA 1,28316.84659,16.3871,764554,284.130625,28.4131,4546.08999,3785.411784,1000,1,568.26125,1136.5225

Weight:
DATA "Grams", "Kilograms", "Ounces", "Ounces (troy)", "Pounds", "Grains", "Tonnes (metric)", "Tons (US short)", "Long Tons (UK)", "Stones"
DATA .0022046226, 2.2046226, .0625, .0685714, 1, 10, 2204.62262184, 2000, 2240, 14


' this routine is used when adding a new category, until its data is entered.

TBA:
DATA "To Be Added","","","","","","","","",""
DATA 1,1,1,1,1,1,1,1,1,1
Screenshot 2.JPG
Screenshot 2.JPG (56.6 KiB) Viewed 3610 times

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Temperature in iConvert

Post by rbytes »

Excellent addition. Thanks, Ton

I have removed highlighting except for subroutines and functions. I extended the red highlighting to cover all of the subs and functions from previous versions.

Code: Select all

/*
iConvert V3.1
by rbytes and Dutchman, October 2018
Converts measurements in many categories.
A collaborative international project.

V3.1
- Temperature category added by Dutchman
V3
- Currency category added by Dutchman
  DATA generated with "Update currencies.sb"
V2
- animation added to title
  (plays only once, at launch)
- measures added in most categories
- scientific notation is now used for
  results larger than 100,000,000 or
  smaller than .00000001
V1.1
- added Horsepower (Metric) to
  Power category
- eliminated >>> button;
  calculation is now automatic
  when list selections or
  input values are changed.
- code efficiency improved
*/
OPTION BASE 1
SET ORIENTATION LANDSCAPE
SET BUTTONS CUSTOM
SET TOOLBAR OFF
laun$=LAUNCHER$()
GET SCREEN SIZE sw,sh

' set to 1 to test iPhone layout on iPad
iostest=0
IF iostest THEN
  sw=568
  sh=320
  dev$=""
ENDIF
rw=sw/1024!rh=sh/740


' *** Note - IP address may change if router is rebooted ***
' run ipconfig on your PC to find its IP address.

URL$="192.170.61.43"   ' example of IP address
DIM h$(2)


' each line of a file sent to a PC print server must end with these characters:

cr$=CHR$(13)       ' carriage return character
lf$=CHR$(10)       ' line feed character


' interface object names

x$="guide"
y$="guide2"
n$="title"
o$="category"
p$="from"
q$="to"
pi$="from_in"
qi$="to_in"
qi2$="cover"


lf$=CHR$(10)
q$=CHR$(34)
cat=1          ' default category (area)
invalue=1

upshift=7

' correction for iPad display only
IF rw=1 then shift=10

' correction for non-iPad display only
IF rw<1 then shift2=75

fm$="##,###,###,###,###,###,###.#######"    ' formats the result with commas
fm2$="####################.#######"      ' formats the result without commas


' number of measures in each category

cats=14
DIM cat$(cats)
area=9
cook=9
dat=8
energ=12
leng=13
prefix=21
pow=12
press=13
spd=11
Tscales=8 ! Tscale$="Temperature"
timing=10
vol=12
wgt=10
toba=10


' include constant 'Valuta' and 'currencies:' data

{currencies.data}

GRAPHICS
GRAPHICS CLEAR .9,1,1


 ' animate title if gif animation file exists

A$="iconvert3.GIF"
IF FILE_EXISTS(A$) THEN
 SPRITE N$ LOAD A$
  SPRITE N$ AT sw/3-shift2,sh/60+3*shift scale 1*rh
  SPRITE N$ SHOW
ELSE
  DRAW IMAGE "iconvert.PNG" AT sw/2.8,sh/20 SCALE .8*rw
ENDIF
' read categories
RESTORE TO Categories

FOR t=1 TO cats
  READ cat$(t)
NEXT t

RESTORE TO area     ' default category
items=area
DIM measure$(items)
GOSUB setcategory


' create three lists

SET LISTS FONT SIZE 18*rw
LIST o$ TEXT cat$ AT sw*.1,260*rh SIZE 240*rw,310*rh
LIST o$ SELECT cat
LIST p$ TEXT measure$ AT sw*.385,260*rh SIZE 240*rw,310*rh
LIST p$ SELECT 1
LIST q$ TEXT measure$ AT sw*.672,260*rh SIZE 240*rw,310*rh
LIST q$ SELECT 1


' create fields and buttons

SET BUTTONS FONT SIZE 20*rw
DRAW COLOR 0,0,0
guide$="Pick from each list"&lf$&"Enter quantity here"
DRAW LINE sw*.26,168*rh TO sw*.392,168*rh
FILL COLOR 0,0,0
FILL CIRCLE sw*.39,168*rh SIZE 3*rw
FILL COLOR 1,1,1
FIELD x$ TEXT guide$ AT sw*.1,132*rh-upshift SIZE 250,60 RO ML
FIELD x$ FONT SIZE 18*rw
FIELD x$ BACK ALPHA 0
guide2$="All lists can be scrolled"
DRAW LINE sw*.096,618*rh TO sw*.38,618*rh
DRAW LINE sw*.63,618*rh TO sw*.91,618*rh
FIELD y$ TEXT guide2$ AT sw*.41,600*rh-upshift*.8 SIZE 250,60 RO ML
FIELD y$ FONT SIZE 18*rw
FIELD y$ BACK ALPHA 0
FIELD o$ TEXT "CATEGORY" AT sw*.115,210*rh SIZE 130,30 RO
FIELD o$ FONT SIZE 20*rw
FIELD o$ BACK ALPHA 0
FIELD p$ TEXT "FROM" AT sw*.405,210*rh SIZE 100,30 RO
FIELD p$ FONT SIZE 20*rw
FIELD p$ BACK ALPHA 0
FIELD q$ TEXT "TO" AT sw*.69,210*rh SIZE 70,30 RO
FIELD q$ FONT SIZE 20*rw
FIELD q$ BACK ALPHA 0
'BUTTON "calc" TEXT ">>>" AT sw*.62,150*rh SIZE 60*rw,30*rw
FILL ALPHA 0
BUTTON "copy" TEXT "COPY" AT 285*rw,650*rh SIZE 100*rw,50*rh
BUTTON "print" TEXT "PRINT" AT 405*rw,650*rh SIZE 100*rw,50*rh
BUTTON "save" TEXT "SAVE" AT 525*rw,650*rh SIZE 100*rw,50*rh
BUTTON "stop" TEXT "STOP" AT 645*rw,650*rh SIZE 100*rw,50*rh
FILL ALPHA 1
FIELD pi$ TEXT "1" AT sw*.400,150*rh SIZE 200*rw,30*rh
FIELD pi$ FONT SIZE 18*rw
FIELD qi$ TEXT "1" AT sw*.640,145*rh SIZE 280*rw,40*rh
FIELD qi$ FONT SIZE 22*rw
FIELD qi2$ TEXT "" AT sw*.640,145*rh SIZE 280*rw,40*rh RO
FIELD qi2$ BACK ALPHA 0
FILL COLOR .6,.8,.8
FILL RECT sw*.395,145*rh TO sw*.6,185*rh
FILL COLOR .8,.8,.8
FILL RECT sw*.635,140*rh TO sw*.92,190*rh
DRAW RECT sw*.1-1,258*rh TO sw*.1+240*rw+1,258*rh+312*rh
DRAW RECT sw*.386-2,258*rh TO sw*.386+240*rw,258*rh+312*rh
DRAW RECT sw*.673-2,258*rh TO sw*.673+240*rw,258*rh+312*rh

timer=time ()    ' set timer to stop title anim after one cycle


' main program loop

DO

' play once, then stop animation

IF time()-timer>1 AND time()-timer <2 and not noplay THEN
  IF FILE_EXISTS(A$) THEN SPRITE N$ PLAY
  noplay=1
ENDIF
IF time()-timer>6 AND time()-timer <7 THEN
  IF FILE_EXISTS(A$) THEN SPRITE N$ STOP
ENDIF


' check which category is selected

type=LIST_SELECTED(o$)


' choose and prepare a new category if necessary

IF type<>cat THEN
  cat=type
  changed=1

  ON cat GOTO 10,20,25,30,40,50,60,70,80,90,95,100,110,120
  10 RESTORE TO area
     items=area
     GOTO skip
  20 RESTORE TO cooking
     items=cook
     GOTO skip
  25 RESTORE TO currencies
     items=curr
     GOTO skip
  30 RESTORE TO DATA
     items=dat
     GOTO skip
  40 RESTORE TO energy
     items=energ
     GOTO skip
  50 RESTORE TO length
     items=leng
     GOTO skip
  60 RESTORE TO prefixes
     items=prefix
     GOTO skip
  70 RESTORE TO power
     items=pow
     GOTO skip
     ' use the next two lines to show a category not yet ready
     ' gosub tobe  ' measure name
     ' goto skip
  80 RESTORE TO pressure
     items=press
     GOTO skip
  90 RESTORE TO speed ' speed
     items=SPD
     GOTO skip
  95 RESTORE TO Temperatures
     items=Tscales
     GOTO Skip
  100 RESTORE TO timing ' time
     items=timing
     GOTO skip
  110 RESTORE TO Volume
     items=vol
     GOTO skip
  120 RESTORE TO weight
     items=wgt
     GOTO skip
''
  skip:
  GOSUB setcategory
  LIST p$ TEXT measure$
  LIST p$ SELECT 1
  LIST q$ TEXT measure$
  LIST q$ SELECT 1
  FIELD pi$ TEXT "1"
ENDIF

IF LIST_SELECTED(p$)<>selp or LIST_SELECTED(q$)<>selq then changed=1


' calculate the conversion

if VAL(FIELD_TEXT$(pi$))<>invalue or changed then
  invalue=VAL(FIELD_TEXT$(pi$))
  selp=LIST_SELECTED(p$)
  selq=LIST_SELECTED(q$)
  GOSUB Convert ' calculate OutValue
  if outvalue<100000000 and outvalue>.000000001 then 
    out$=STR$(outvalue,fm$)
    out2$=STR$(outvalue,fm2$)
    format(out$)!out$=format.form$
    format(out2$)!out2$=format.form$
  else
    out$=STR$(outvalue)
  endif
  fsize=22*rw
  FIELD qi$ FONT SIZE fsize
  FIELD qi$ TEXT out$
  FIELD qi2$ TEXT ""
ENDIF

' copy the conversion info to clipboard

IF bp("copy") THEN
  GOSUB compile
  IF cp=0 THEN!CLIPBOARD CLEAR!cp=1!ENDIF
  CLIPBOARD WRITE convert$
  BEEP
ENDIF


' print the conversion info

IF bp("print") THEN
GOSUB compile
h$(1) = "content-type:text/html"   ' make header info
h$(2) = "content-length:" & LEN(convert$)

' print convert$ using Henko print technique
HTTP URL$ HEADER H$ POST convert$   ' send doc to the print server
BEEP
ENDIF


' save the conversion info to a file

IF bp("save") THEN
  GOSUB compile
  fname$="converted.txt"
  IF FILE_EXISTS(fname$) THEN FILE fname$ DELETE
  FILE fname$ WRITELINE convert$
  BEEP
ENDIF


' end the program

IF bp("stop") THEN
  IF laun$="desktop" THEN
    IF FILE_EXISTS("/launch") THEN
      RUN "/-Launch.sb"
    ELSE
      EXIT
    ENDIF
  ENDIF
  END
ENDIF
SLOWDOWN
UNTIL 0
END

'r'========== Subroutines and Functions =============
Convert:
'--- Convert via formula
'  temperature
IF Cat$(cat)=Tscale$ THEN
  Outvalue=FromCelsius(Celsius(Invalue,measureval(selp)),measureval(selq))
  RETURN
ENDIF
'--- Convert via ratio
  outvalue=invalue*1/(measureval(selq)/measureval(selp))
RETURN

' temperature conversion

DEF FromCelsius(Value,UnitPointer)
'Converts Value on 'Unit'-scale to Celsius-scale
'  to newvalue in scale of Unitpointer
ON UnitPointer GOTO 1,2,3,4,5,6,7,8
  1 RETURN Value 'Celsius
  2 RETURN Value+273.15 'Kelvin
  3 RETURN Value*9/5+32 'Fahrenheit
  4 RETURN (Value+273.15)*9/5 'Rankine
  5 RETURN (100-Value)*3/2 'Delisle
  6 RETURN Value*33/100 'Newton
  7 RETURN Value*4/5 'Réamur
  8 RETURN Value*21/40+7.5 'Rømer
END DEF
'
DEF Celsius(Value,UnitPointer)
'Converts Value in 'Unit' to new value in Celsius
ON UnitPointer GOTO 1,2,3,4,5,6,7,8
  1 RETURN Value 'Celsius
  2 RETURN Value-273.15 'Kelvin
  3 RETURN (Value-32)*5/9 'Fahrenheit
  4 RETURN (Value-491.67)*5/9 'Rankine
  5 RETURN 100-Value*2/3 'Delisle
  6 RETURN Value*100/33 'Newton
  7 RETURN Value*5/4 'Réamur
  8 RETURN (Value-7.5)*40/21 'Rømer
END DEF


' read the data for a category

setcategory:
DIM measure$(items)
FOR t=1 TO items
  READ measure$(t)
NEXT t
DIM measureval(items)
FOR t=1 TO items
  READ measureval(t)
NEXT t
RETURN


' prepare a string showing the conversion, for copying or saving to a file

compile:
  selp=LIST_SELECTED(p$)
  selq=LIST_SELECTED(q$)
  temp1$=measure$(selp)
  temp2$=measure$(selq)


  ' if a value is 1 or a fraction, change the name from plural to singular
  convin=val(FIELD_TEXT$(p$))
  temp1len=LEN(temp1$)
  IF convin=<1 AND RIGHT$(temp1$,1)="s" THEN temp1$=LEFT$(temp1$,temp1len-1)
  IF VAL(out2$)=<1 AND RIGHT$(temp2$,1)="s" THEN temp2$=LEFT$(temp2$,LEN(temp2$)-1)
  convert$&=FIELD_TEXT$(pi$)&" "&temp1$&" = "&FIELD_TEXT$(qi$)&" "&temp2$
  convert$&=cr$&lf$&FIELD_TEXT$(pi$)&" "&temp1$&" = "&out2$&" "&temp2$&cr$&lf$&cr$&lf$
RETURN


' this subroutine is used when adding a new category, until its data is complete.

tobe:
  RESTORE TO tba
  items=toba
  DIM measure$(items)
  GOSUB setcategory
RETURN


' shortcut for button press

DEF bp(a$) = BUTTON_PRESSED(a$)


' remove scientific notation before displaying result

DEF format(form$)
  WHILE LEFT$(form$,1)=" " OR LEFT$(form$,1)=","
    form$=RIGHT$(form$,LEN(form$)-1)
    ' trim leading spaces and separators
  ENDWHILE
  IF INSTR (form$, ".") THEN        ' if form$ has a decimal point
    IF NOT numpad.curr THEN
    ' trim trailing zeros
    WHILE RIGHT$(form$,1)="0"
      form$=LEFT$(form$,LEN(form$)-1)
    ENDWHILE
    IF RIGHT$(form$,1)="." THEN
      form$=LEFT$(form$,LEN(form$)-1)
    ENDIF
    ENDIF
  ENDIF
END DEF
''

/*
Conversion Data

The technique I use to calculate conversions is to choose a reference measure in the middle of the range and set its value to 1. All other measures are then assigned numbers representing the ratio of their value to the reference measure's value.
*/

Categories:

DATA "Area", "Cooking", "Currency "&CurrDate$ ,"Data", "Energy", "Length", "Prefixes", "Power", "Pressure", "Speed"
DATA Tscale$, "Time", "Volume", "Weight"

Area:
DATA "Acres", "Square Kilometers", "Hectares", "Square Meters", "Square Centimeters", "Square Miles", "Square Yards", "Square Feet", "Square Inches"
DATA 4046.85642, 1000000, 10000, 1, .0001, 2589988.110266, .8361273, .0929030, .000645159722

Cooking:
DATA "Centiliters", "Cups (CD)", "Fluid Ounces (CD)", "Liters", "Milliliters", "Pints (CD)", "Quarts (CD)", "Tablespoons", "Teaspoons"
DATA .35195,8,1,35.195079,.035195,20,40,.520421,.1734737

Data:
DATA "Bits", "Bytes", "Words", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes", "Petabytes"
DATA .000125, .001, .008, 1, 1000, 1000000, 1000000000, 1000000000000

Energy:
DATA "BTU", "Calories", "Cubic Mile of Oil", "Foot Pounds", "Horsepower Per Hr", "Joules", "KiloJoules", "KiloCalories", "Kilowatt Hours", "Kilotons", "MegaJoules", "Therms (US)"
DATA 251.99576, 1, 38334721315564331336, .32404825, 641615.559278, .23900573, 239.00573, 1000, 860420.6501033, 999999974320, 239005.7299999, 25210420.65043

Length:
DATA "Ångströms","Centimeters", "Fathoms", "Feet", "Furlongs (US)", "Inches", "Kilometers", "Meters","Mils", "Miles", "Millimeters", "Nanometers", "Yards"
DATA .00000001, 1, 182.88, 30.48, 20116.8, 2.54, 100000, 100, .00254, 160934.39999, .1, .0000001, 91.44

Power: '12 items
DATA "Watts","Kilowatts", "Megawatts", "Gigawatts", "Terawatts", "Horsepower (IT)", "Horsepower (Metric)", "Moosepower", "Calories per Hour", "BTU per Hour", "Foot Pounds per Hr", "Tons Refrigeration"
DATA 1,1000, 1000000, 1000000000, 1000000000000, 745.69987, 756.042476, 2438.4385749, 0.0011629, 2930710000, 3766160000, 3516.8

Prefixes:
DATA "yotta", "zetta", "exa", "peta", "tera", "giga", "mega", "kilo", "hecto", "deca", "none", "deci", "centi", "milli", "micro", "nano", "pico", "femto", "atto", "zepto", "yocto"
DATA 1000000000000000000000000, 1000000000000000000000, 1000000000000000000, 1000000000000000, 1000000000000, 1000000000, 1000000, 1000, 100, 10, 1, .1, .01, .001, .000001, .000000001, .000000000001, .000000000000001, .000000000000000001, .000000000000000000001, .000000000000000000000001

Pressure:
DATA "Bars", "Millibars", "Pascals", "HectoPascals", "KiloPascals", "MegaPascals", "Atmospheres", "Pounds per Sq. Foot", "Pounds per Sq. Inch", "Inches of Water", "Inches of Mercury", "Centimeters of Water", "Centimeters of Mercury"
DATA 1000, 1, .01, 1, 10, 10000, 1013.250099, .47880258, 68.947572, 2.490889, 33.864, .98066496, 13.332283459

Speed:
DATA "Feet per Second", "Feet per Minute", "Furlongs per Fortnight", "Inches per Second", "Kilometers per Hour", "Kilometers per Sec.", "Knots", "Miles per Hour", "Miles per Second", "Speed of Light", "Speed of Sound"
DATA 1.09728, .018288, .000598715, .09144, 1, 3600, 1.8519999, 1.609349, 5793.638399, 1079252848.794, 1225.0439999

Temperatures:
DATA "Celsius [°C]", "Delisle [°D]", "Fahrenheit [°F]", "Kelvin [K]", "Newton [°N]", "Rankine [°Ra]", "Réamur [°Re]", "Rømer [°Rø]"
DATA 1,5,3,2,6,4,7,8 ' unit-pointers

Timing:
DATA "Milliseconds", "Seconds", "Minutes", "Hours", "Days", "Weeks", "Fortnights", "Months", "Years", "Leap Years"
DATA .00001666666, .01666666, 1, 60, 1440, 10080, 20160, 43800, 525600, 527040

Volume:
DATA "Cubic Centimeters", "Cubic Feet", "Cubic Inches", "Cubic Yards", "Cups (CD)", "Fluid Ounces (CD)", "Gallons (CD)", "Gallons (US)", "Liters", "Milliliters", "Pints (CD)", "Quarts (CD)"
DATA 1,28316.84659,16.3871,764554,284.130625,28.4131,4546.08999,3785.411784,1000,1,568.26125,1136.5225

Weight:
DATA "Grams", "Kilograms", "Ounces", "Ounces (troy)", "Pounds", "Grains", "Tonnes (metric)", "Tons (US short)", "Long Tons (UK)", "Stones"
DATA .0022046226, 2.2046226, .0625, .0685714, 1, 10, 2204.62262184, 2000, 2240, 14


' this routine is used when adding a new category, until its data is entered.

TBA:
DATA "To Be Added","","","","","","","","",""
DATA 1,1,1,1,1,1,1,1,1,1

I took a look at some conversion apps, and found the following categories that we don't have yet.
Acceleration
Angle
Density
Flow Rate
Force
Fuel Economy
Torque

They would be easy to add, if anyone would find them useful.
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Temperature in iConvert

Post by Dutchman »

rbytes wrote:
Sat Oct 27, 2018 4:28 pm
… snip
I took a look at some conversion apps, and found the following categories that we don't have yet.
Acceleration
Angle
Density
Flow Rate
Force
Fuel Economy
Torque

They would be easy to add, if anyone would find them useful.
I am preparing another category: "Energy transition" e.g. from 'liter/year petrol' or 'm³/year natural gas' to watts. Then I can calculate which windmill I should order :lol:
Can you please update your code at viewtopic.php?f=20&t=2274
Last edited by Dutchman on Thu Jan 19, 2023 3:13 pm, edited 1 time in total.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Temperature in iConvert

Post by rbytes »

That one will be interesting. My son in BC (province to the west of us) bought a windmill. It puts out 12 volts at 500 watts, so can do a decent job of charging batteries when the wind is right.

I will add a Fuel Economy category when I get some time tomorrow.

The code is updated now at that link.
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Temperature in iConvert

Post by Dutchman »

rbytes wrote:
Sat Oct 27, 2018 6:39 pm

I will add a Fuel Economy category when I get some time tomorrow.
The code is updated now at that link.
"Fuel economy" will require handling inverse proportionality e.g. 'Liters/100km' versus 'Km/liter'.
You could use the imaginary part of the data to inform the conversion about that.

You could keep the link for future versions of iConvert and add a notice about the change after modifying the code.
To know the history can be useful. ;)

Post Reply