keys = {
	{ "7", "8", "9", " *" ,"1/x","sin","asin","sinh"},
	{ "4", "5", "6", " -" ,"x^2", "cos","acos","cosh"},
	{ "1", "2", "3", " +","x^3","tan","atan","tanh"},
	{ "0", "+/-", " ." ,"  /","x^10","log","log10"," exp"},
       {"End","deg","rad"," =","  C","tan","  pi"," sqr"} }
black = Color.new(0, 0, 0)
white = Color.new(255,255,255)	
color = Color.new(255,255,255)
font = Font.load("system/arial.ttf") 
font:setPixelSizes(13,13) 
font1 = Font.load("system/arial.ttf") 
font1:setPixelSizes(15,20) 
font2 = Font.load("system/arial.ttf") 
font2:setPixelSizes(30,55) 
calculfond = Image.load("system/calculfond.png")
battery1=Image.load("bat/5.png")
battery2=Image.load("bat/3.png")
battery3=Image.load("bat/3.png")
battery4=Image.load("bat/2.png")
battery5=Image.load("bat/1.png")
System.currentDirectory("ms0:/MUSIC/")
fbrowser = {s = 1, fl = System.listDirectory("ms0:/MUSIC/"),ls = 1, sp = 1}
fbrowser.nf = table.getn(fbrowser.fl)

oldPad = Controls.read()
x = 1
y = 1
text = "0"
lastNumber = 0
deleteOnKey = true
lastOperation = ""
value = ""
number = 0
sccnt = 1
pseudo = System.nickName()
counter = Timer.new()
statut = "defaut"


while true do	

  screen:clear()
     pad = Controls.read()
     local time = os.date("%I:%M %p") 
    

	  if pad ~= oldPad then screen:blit(0,0,calculfond)

		if pad:left() then
			x = x - 1
			if x == 0 then
				x = 8
			end
		end
		if pad:right() then
			x = x + 1
			if x == 9 then
				x = 1
			end
		end
		if pad:up() then
			y = y - 1
			if y == 0 then
				y = 5
			end
		end
		if pad:down() then
			y = y + 1
			if y == 6 then
				y = 1
			end
               
		end
              if pad:cross() then
			value = keys[y][x]
			number = tonumber(text)
                     if value == "End" then
                     System.Quit() 
              end                           
			if value == "  C" then
				text = "0"
				lastNumber = 0
				lastOperation = ""
				deleteOnKey = true
			elseif value == "1/x" then
				text = tostring(1/number)
			elseif value == "asin" then
				text = tostring(math.asin(number))
			elseif value == "sinh" then
				text = tostring(math.sinh(number))
			elseif value == "acos" then
				text = tostring(math.acos(number))
			elseif value == "cosh" then
				text = tostring(math.cosh(number))
			elseif value == "atan" then
				text = tostring(math.atan(number))
			elseif value == "tanh" then
				text = tostring(math.tanh(number))
			elseif value == "sin" then
				text = tostring(math.sin(number))
			elseif value == "cos" then
				text = tostring(math.cos(number))
			elseif value == "tan" then
				text = tostring(math.tan(number))
			elseif value == "x^2" then
				text = tostring(number*number)
			elseif value == "x^3" then
				text = tostring(number^3)
			elseif value == "x^10" then
				text = tostring(number^10)
			elseif value == "log" then
				text = tostring(math.log(number))
			elseif value == "log10" then
				text = tostring(math.log10(number))
			elseif value == " exp" then
				text = tostring(math.exp(number))
			elseif value == "deg" then
				text = tostring(math.deg(number))
			elseif value == "rad" then
				text = tostring(math.rad(number))
			elseif value == "tan" then
				text = tostring(math.tan(number))
			elseif value == "log" then
				text = tostring(math.log(number))
			elseif value == "  pi" then
				text = tostring(math.pi)
			elseif value == " sqr" then
				text = tostring(math.sqrt(number))
			elseif value == "  /" or value == " *" or value == " -" or value == " +" or value == " ="  then
				if lastOperation == "  /" then
					text = tostring(lastNumber / number)
				elseif lastOperation == " *" then
					text = tostring(lastNumber * number)
				elseif lastOperation == " -" then
					text = tostring(lastNumber - number)
				elseif lastOperation == " +" then
					text = tostring(lastNumber + number)
				end
				number = tonumber(text)
				if value == " =" then
					text = tostring(number)
					lastOperation = ""
				else
					lastNumber = number
					lastOperation = value
				end
				deleteOnKey = true
			elseif value == "+/-" then
				text = tostring(-number)
			else
				if deleteOnKey then
					text = value
					deleteOnKey = false
				else
					text = text .. value
				end
			end                            
		end
     		
	end
 oldPad = pad     			

	yk = 0
	w = 58
	h = 36

   screen:blit(0,0,calculfond)
       currentTime = counter:time()


      if currentTime > 0 then
   screen:fontPrint(font2,90, 80,"Hello "..pseudo , black)
   screen:fontPrint(font,170, 12,"Calculmp3 v3 by mrafenne" ,white)
   end
      if currentTime > 3000 then
   screen:blit(0,0,calculfond)
   screen:fontPrint(font,170, 12,"Calculmp3" ,white)
   end

   screen:fontPrint(font1,10, 80, text, black)
       
       for yindex,line in ipairs(keys) do
		for xindex,key in ipairs(line) do
			x0 = xindex * w
			y0 = yindex * h
			
			if xindex == x and yindex == y then
				
				foreground = Color.new(0, 0, 0)
			else
				foreground = color
			end
			screen:fontPrint(font1,x0-35, y0 + 82, key, foreground)
                end
if System.powerGetBatteryLifePercent() >= 0 then batteryL = battery5 end
if System.powerGetBatteryLifePercent() >= 25 then batteryL = battery4 end
if System.powerGetBatteryLifePercent() >= 50 then batteryL = battery3 end
if System.powerGetBatteryLifePercent() >= 75 then batteryL = battery2 end
if System.powerGetBatteryLifePercent() >= 90 then batteryL = battery1 end
   screen:blit(465,3,batteryL)
   screen:fontPrint(font,5,12, ""..time.."",white)

if statut == "affiche" then
   screen:fillRect(0,18,Mp3me.percent()*5,1,white)
end
end
pad = Controls.read()
if pad:circle()  then
Mp3me.stop() 
System.currentDirectory("ms0:/MUSIC/")
Mp3me.load(fbrowser.fl[fbrowser.s].name)
Mp3me.play()
statut = "affiche" 
end
if pad:l()  then fbrowser.s = fbrowser.s - 1 end
if pad:r()  then fbrowser.s = fbrowser.s + 1 end
if pad:triangle()  then Mp3me.pause() end
if pad:square()  then Mp3me.stop() statut = "defaut" end
----
if Mp3me.vis(0) <= 85 then
screen:print(300,12,"-",white)
end
if Mp3me.vis(0) <= 80 then
screen:print(300,10,"-",white)
end
if Mp3me.vis(0) <= 75 then
screen:print(300,8,"-",white)
end
if Mp3me.vis(0) <= 70 then
screen:print(300,6,"-",white)
end
if Mp3me.vis(0) <= 65 then
screen:print(300,4,"-",white)
end
if Mp3me.vis(0) <= 60 then
screen:print(300,2,"-",white)
end
if Mp3me.vis(0) <= 55  then
screen:print(300,0,"-",white)
end
----
if Mp3me.vis(10) <= 85 then
screen:print(305,12,"-",white)
end
if Mp3me.vis(10) <= 80 then
screen:print(305,10,"-",white)
end
if Mp3me.vis(10) <= 75 then
screen:print(305,8,"-",white)
end
if Mp3me.vis(10) <= 70 then
screen:print(305,6,"-",white)
end
if Mp3me.vis(10) <= 65 then
screen:print(305,4,"-",white)
end
if Mp3me.vis(10) <= 60 then
screen:print(305,2,"-",white)
end
if Mp3me.vis(10) <= 55  then
screen:print(305,0,"-",white)
end
----
if Mp3me.vis(20) <= 85 then
screen:print(310,12,"-",white)
end
if Mp3me.vis(20) <= 80 then
screen:print(310,10,"-",white)
end
if Mp3me.vis(20) <= 75 then
screen:print(310,8,"-",white)
end
if Mp3me.vis(20) <= 70 then
screen:print(310,6,"-",white)
end
if Mp3me.vis(20) <= 65 then
screen:print(310,4,"-",white)
end
if Mp3me.vis(20) <= 60 then
screen:print(310,2,"-",white)
end
if Mp3me.vis(20) <= 55  then
screen:print(310,0,"-",white)
end
----
if Mp3me.vis(30) <= 85 then
screen:print(315,12,"-",white)
end
if Mp3me.vis(30) <= 80 then
screen:print(315,10,"-",white)
end
if Mp3me.vis(30) <= 75 then
screen:print(315,8,"-",white)
end
if Mp3me.vis(30) <= 70 then
screen:print(315,6,"-",white)
end
if Mp3me.vis(30) <= 65 then
screen:print(315,4,"-",white)
end
if Mp3me.vis(30) <= 60 then
screen:print(315,2,"-",white)
end
if Mp3me.vis(30) <= 55  then
screen:print(315,0,"-",white)
end
----
if Mp3me.vis(40) <= 85 then
screen:print(320,12,"-",white)
end
if Mp3me.vis(40) <= 80 then
screen:print(320,10,"-",white)
end
if Mp3me.vis(40) <= 75 then
screen:print(320,8,"-",white)
end
if Mp3me.vis(40) <= 70 then
screen:print(320,6,"-",white)
end
if Mp3me.vis(40) <= 65 then
screen:print(320,4,"-",white)
end
if Mp3me.vis(40) <= 60 then
screen:print(320,2,"-",white)
end
if Mp3me.vis(40) <= 55  then
screen:print(320,0,"-",white)
end
----
if Mp3me.vis(50) <= 85 then
screen:print(325,12,"-",white)
end
if Mp3me.vis(50) <= 80 then
screen:print(325,10,"-",white)
end
if Mp3me.vis(50) <= 75 then
screen:print(325,8,"-",white)
end
if Mp3me.vis(50) <= 70 then
screen:print(325,6,"-",white)
end
if Mp3me.vis(50) <= 65 then
screen:print(325,4,"-",white)
end
if Mp3me.vis(50) <= 60 then
screen:print(325,2,"-",white)
end
if Mp3me.vis(50) <= 55  then
screen:print(325,0,"-",white)
end
----
if Mp3me.vis(60) <= 85 then
screen:print(330,12,"-",white)
end
if Mp3me.vis(60) <= 80 then
screen:print(330,10,"-",white)
end
if Mp3me.vis(60) <= 75 then
screen:print(330,8,"-",white)
end
if Mp3me.vis(60) <= 70 then
screen:print(330,6,"-",white)
end
if Mp3me.vis(60) <= 65 then
screen:print(330,4,"-",white)
end
if Mp3me.vis(60) <= 60 then
screen:print(330,2,"-",white)
end
if Mp3me.vis(60) <= 55  then
screen:print(330,0,"-",white)
end
----
if Mp3me.vis(70) <= 85 then
screen:print(335,12,"-",white)
end
if Mp3me.vis(70) <= 80 then
screen:print(335,10,"-",white)
end
if Mp3me.vis(70) <= 75 then
screen:print(335,8,"-",white)
end
if Mp3me.vis(70) <= 70 then
screen:print(335,6,"-",white)
end
if Mp3me.vis(70) <= 65 then
screen:print(335,4,"-",white)
end
if Mp3me.vis(70) <= 60 then
screen:print(335,2,"-",white)
end
if Mp3me.vis(70) <= 55  then
screen:print(335,0,"-",white)
end
----
if Mp3me.vis(80) <= 85 then
screen:print(340,12,"-",white)
end
if Mp3me.vis(80) <= 80 then
screen:print(340,10,"-",white)
end
if Mp3me.vis(80) <= 75 then
screen:print(340,8,"-",white)
end
if Mp3me.vis(80) <= 70 then
screen:print(340,6,"-",white)
end
if Mp3me.vis(80) <= 65 then
screen:print(340,4,"-",white)
end
if Mp3me.vis(80) <= 60 then
screen:print(340,2,"-",white)
end
if Mp3me.vis(80) <= 55  then
screen:print(340,0,"-",white)
end

for i = ((fbrowser.ls-1)*fbrowser.sp)+1, fbrowser.ls*fbrowser.sp do
if fbrowser.nf >= i then
screen:fontPrint(font,5,((i-((fbrowser.ls-1)*fbrowser.sp))*50)-2,fbrowser.fl[i].name,black)
elseif fbrowser.nf < i then break end
end
if fbrowser.s > fbrowser.nf then fbrowser.s = fbrowser.nf elseif fbrowser.s < 1 then fbrowser.s = 1 end
if fbrowser.s > fbrowser.sp*fbrowser.ls then fbrowser.ls = fbrowser.ls + 1
elseif fbrowser.s < ((fbrowser.ls-1) * fbrowser.sp)+1 then fbrowser.ls = fbrowser.ls - 1 fbrowser.s = (fbrowser.ls)*fbrowser.sp end

if pad:select() then
screen:save("ms0:/PICTURE/"..sccnt..".png")
sccnt = sccnt +1 
end




screen.waitVblankStart()
screen.flip() 

end
