Sunday, April 17, 2011

my first symbian application

and also my first Python script.


PyS60 is cool. You can create a script and run it on your Nokia. Simple. Then I learnt that there are tools like ensymble (from google code) to create a sis file to install and distribute the application.

I wanted a quick and simple unit converter. Although SI units are used by almost all countries in this world there are still people who use the awkward system of feet, psi, ounces etc. Thanks to the French and their metric system, the SI units are more readable and more easy to understand. I wrote this script for my Nokia to convert from various units. I also included old Maltese system of measurements.

To run on your nokia, just install PyS60 to your nokia - from https://garage.maemo.org/frs/?group_id=854&release_id=3264 - and copy the script to your memory card. I use Linx and obex to copy th files using bluetooth: which is much more handy.



import appuifw

convCat = [u"Length", u"Area", u"Volume", u"Weight", u"Temperature", u"Pressure"]


cont = 1
while cont:
# reset convCat
convUnits = []
convCalcToEsp = []
# get Category
convCatIndex = appuifw.popup_menu(convCat, u"Select a category")
if convCatIndex == 0:
# length, use m as 'Esperanto'.
convUnits = [ u"thou (th)", u"inches (in)", u"feet (ft)", u"yards (yd)", u"miles (mi)", u"knots (kt)", u"mm", u"cm", u"m", u"km", u"qasba", u"rod", u"fathom", u"league"]
convCalcToEsp = [0.0000254, 0.0254, 0.3048, 0.9144, 1609, 1852, 0.001, 0.01, 1, 1000, 2.096, 5.0292, 1.853184, 4828.032]
elif convCatIndex == 1:
# Area, use m2 as 'Esperanto'
convUnits = [ u"sq in", u"sq ft", u"sq yd", u"sq miles", u"sq knots", u"mm\u00B2", u"cm\u00B2", u"m\u00B2", u"km\u00B2", u"hectares", u"acres", u"tumoli", u"qasba kwadra"]
convCalcToEsp = [0.000645, 0.092903, 0.836127, 2589988, 3429904, 0.000001, 0.0001, 1, 1000000, 10000, 4046.87, 1124.13, 4.391]
elif convCatIndex == 2:
# Volume, use m3 as 'Esperanto'
convUnits = [ u"cu in", u"cu ft", u"cu yd", u"UK gal", u"UK quart", u"UK pint", u"fl oz", u"US gal", u"US quart", u"US pint", u"mm\u00B3", u"cm\u00B3", u"m\u00B3", u"litre (l)", u"centilitre (cl)", u"millilitre (ml)", u"cup", u"tablespoon", u"teaspoon", u"Oil barrel"]
convCalcToEsp = [ 0.000016387, 0.028316847, 0.764554858, 0.0045461, 0.0011365, 0.0005682, 2.841306E-05, 0.0037856, 0.0009464, 0.0004732, 0.000000001, 0.000001, 1, 0.001, 0.00001, 0.000001, 0.000236588, 0.000014784, 0.000004929, 0.15898 ]
elif convCatIndex == 3:
# Weight, use kg as 'Esperanto'
convUnits = [ u"grain (gr)", u"drachm (drc)", u"ounce (oz)", u"pound (lb)", u"stone (st)", u"UK hundredweight (cwt)", u"ton", u"mg", u"g", u"kg", u"tonne", u"ratal", u"wizna", u"qantar"]
convCalcToEsp = [ 0.00006579891, 0.001771845, 0.028349523, 0.45359237, 6.350293, 50.802345, 1016.0469, 0.000001, 0.001, 1, 1000, 0.7938, 3.969, 79.38]
elif convCatIndex == 4:
# Temperature, use C as 'Esperanto'
convUnits = [ u"F", u"C", u"K"]
convCalcToEsp = [ 1, 1, 1]
elif convCatIndex == 5:
# Pressure, Pa as 'Esperanto'
convUnits = [ u"psi", u"bar", u"Pascal (N/m\u00B2)"]
convCalcToEsp = [ 6894, 100000, 1]

else:
# 'back was pressed
cont=0

# check if there's a valid category
if len(convUnits) > 0 :
# check that if I made a programming mistake in arrays.
if len(convUnits) == len(convCalcToEsp):
# ok, hence calc.
convFromIndex = appuifw.popup_menu(convUnits, u"Convert from:")
if convFromIndex >= 0 :
convToIndex = appuifw.popup_menu(convUnits, u"Convert " + convUnits[convFromIndex] + " to:")
if (convFromIndex >=0) and (convToIndex >=0):
conVal = appuifw.query(u"Convert " + convUnits[convFromIndex] + " to " + convUnits[convToIndex], "float")
if convCatIndex == 4:
if (convFromIndex != convToIndex):
if (convFromIndex == 0):
# convert from F to C or K
resVal = (conVal -32) * 5/9
if (convToIndex == 2):
# convert C to K
resVal = resVal + 273.15
elif (convToIndex == 0):
# convert to F
C = conVal
if (convFromIndex == 2):
# convert K to C first
C = C - 273.15
# convert C to F
resVal = (C * 9/5) + 32
# now F is not select, hence conv from C to K or vice versa
else:
if convFromIndex == 1:
resVal = conVal + 273.15
else:
resVal = conVal - 273.15
else:
# no conversion required.
resVal=conVal
else:
# if not temperature, then use the following equation.
# appuifw.note(u"calc: "+ str(conVal) + " x " + str(convCalcToEsp[convFromIndex]) + " / " + str(convCalcToEsp[convToIndex]), "info")
# dummy = appuifw.query(u"calc: "+ str(conVal) + " x " + str(convCalcToEsp[convFromIndex]) + " / " + str(convCalcToEsp[convToIndex]), "query")
resVal = conVal * convCalcToEsp[convFromIndex] / convCalcToEsp[convToIndex]
# output
cont = appuifw.query(u" " + str(conVal) + " " + convUnits[convFromIndex] + " = " + str(resVal) + " " + convUnits[convToIndex] + "\n\nConvert another?", "query")
else:
appuifw.note(u"Internal error (" + str(len(convUnits)) + "/" + str(len(convCalcToEsp)) + ")", "error")



Here's the script - then a sis may follow!

No comments: