Advertisement
ssccsscc

graph of average temp

Aug 15th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. local gstatus='off'
  2. local timei=20
  3. local data=0
  4. local pos=1
  5. local time=0
  6. local maxdata=100
  7. local array = {}
  8. array[1]=0
  9. local temp=0
  10. local h=0
  11. local act=1
  12. array[1]=0
  13. array[2]=0
  14.  
  15. local function gclear()
  16.     for o=1, 610 do
  17.         array[o]=0
  18.     end
  19.     pos=1
  20.     maxdata=100
  21. end
  22.  
  23. local function updatetimep()
  24.     timei=timei+5
  25. end
  26.  
  27. local function updatetimem()
  28.     if time<5 then
  29.     else
  30.         timei=timei-5
  31.     end
  32. end
  33.  
  34. local function file_exists(name)
  35.     local f=io.open(name,"r")
  36.     if f~=nil then io.close(f) return 1 else return 0 end
  37. end
  38.  
  39. local function image()
  40.     if gstatus=='off' then
  41.         return
  42.     end
  43.     if tpt.set_pause() == 1 then
  44.         for i=1, tonumber(pos) do
  45.             if i==1 then
  46.                 data2=100-(tonumber(array[i])*100)/maxdata
  47.                 data1=100-(tonumber(array[i])*100)/maxdata
  48.             elseif i==2 then
  49.                 data2=100-(tonumber(array[i])*100)/maxdata
  50.                 data1=100-(tonumber(array[i-1])*100)/maxdata
  51.             else
  52.                 data2=100-(tonumber(array[i-1])*100)/maxdata
  53.                 data1=100-(tonumber(array[i-2])*100)/maxdata
  54.             end
  55.             graphics.drawLine(i, data1, i, data2, 192, 192, 192)
  56.         end
  57.         graphics.drawLine(1, 101, 610, 101, 255, 9, 9)
  58.         return
  59.     end
  60.     if timei~=timeold then
  61.         time=0
  62.         timeold=timei
  63.     end
  64.     data=0
  65.     h=0
  66.     tpt.start_getPartIndex()
  67.     while tpt.next_getPartIndex() do
  68.         h=h+1
  69.         local index = tpt.getPartIndex()
  70.         prop=tpt.get_property("temp",index)
  71.         data=math.ceil(data+prop)-274
  72.     end
  73.     if h>0 then
  74.         data=math.ceil(data/h)
  75.     end
  76.     time=time+1
  77.     if time==timei then
  78.         time=0
  79.         if data>maxdata then
  80.             maxdata=data+1
  81.         end
  82.         if data<0 then
  83.             data=0
  84.         else
  85.             array[tonumber(pos)]=tonumber(data)
  86.             pos=pos+1
  87.             if pos==610 then
  88.                 pos=1
  89.                 maxdata=100
  90.             end
  91.         end
  92.     end
  93.     for i=1, tonumber(pos) do
  94.         if i==1 then
  95.             data2=100-(tonumber(array[i])*100)/maxdata
  96.             data1=100-(tonumber(array[i])*100)/maxdata
  97.         elseif i==2 then
  98.             data2=100-(tonumber(array[i])*100)/maxdata
  99.             data1=100-(tonumber(array[i-1])*100)/maxdata
  100.         else
  101.             data2=100-(tonumber(array[i-1])*100)/maxdata
  102.             data1=100-(tonumber(array[i-2])*100)/maxdata
  103.         end
  104.         graphics.drawLine(i, data1, i, data2, 192, 192, 192)
  105.     end
  106.     graphics.drawLine(1, 101, 610, 101, 255, 9, 9)
  107.     timeold=timei
  108. end
  109. tpt.register_step(image)
  110. local bmpfile = {}
  111. for a=1, 100 do
  112.     bmpfile[a] = {}
  113.     for b=1, 610 do
  114.         bmpfile[a][b]='ffffff'
  115.     end
  116. end
  117.  
  118. function string.fromhex(str)
  119.     local x = {}
  120.     for y in str:gmatch('(..)') do
  121.         x[#x+1] = string.char( tonumber(y, 16) )
  122.     end
  123.     return table.concat( x )
  124. end
  125.  
  126. local function gwrite()
  127.     --write graph to image
  128.     bmpfile = {}
  129.     for a=1, 100 do
  130.         bmpfile[a] = {}
  131.         for b=1, 610 do
  132.             bmpfile[a][b]='FFFFFF'
  133.         end
  134.         bmpfile[a][611]='FFFF'
  135.     end
  136.     for g=1, 610 do
  137.         if not array[g] then
  138.             array[g]=0
  139.         end
  140.         if array[g]==0 then
  141.             array[g]=1
  142.         end
  143.         h=math.ceil(100-(tonumber(array[g])*100)/maxdata)
  144.         bmpfile[101-h][g]='FF008C'
  145.         if g==1 then
  146.             bmpfile[101-h][g]='FF008C'
  147.         else
  148.             h1=math.ceil(100-(tonumber(array[g])*100)/maxdata)
  149.             h2=math.ceil(100-(tonumber(array[g-1])*100)/maxdata)
  150.             if h1~=h2 then
  151.                 if h1>h2 then
  152.                     for k=h2, h1 do
  153.                         bmpfile[101-k][g]='FF008C'
  154.                     end
  155.                 else
  156.                     for k=h1, h2 do
  157.                         bmpfile[101-k][g]='FF008C'
  158.                     end
  159.                 end
  160.             end
  161.         end
  162.     end
  163.     local n=0
  164.     repeat
  165.         n=n+1
  166.         arg='graph'..n..'.bmp'
  167.     until file_exists(arg)==0
  168.     local out = assert(io.open(arg, "a"))
  169.     imagestart='424DD6CB02000000000036000000280000006202000064000000010018000000000000000000C40E0000C40E00000000000000000000'
  170.     out:write(string.fromhex(imagestart))
  171.     assert(out:close())
  172.     local out = assert(io.open(arg, "a"))
  173.     for a=1,100 do
  174.         for b=1,611 do
  175.             out:write(string.fromhex(tostring(bmpfile[a][b])))
  176.         end
  177.     end
  178.     assert(out:close())
  179. end
  180.  
  181. local function button(x,y,w,h,c,r,g,b,action)
  182.     graphics.drawRect(x, y, w, h, r, g, b)
  183.     k=string.len(c)*4+string.len(c)-1
  184.     graphics.drawText(x+(w-k)/2, y+(h-7)/2, c)
  185.     if (tpt.mousex>=x) and (tpt.mousex<=x+w) and (tpt.mousey>=y) and (tpt.mousey<=y+h) then
  186.         graphics.fillRect(x, y, w, h, r, g, b,50)
  187.     end
  188.     function click(mousex, mousey, button, event)
  189.         if (mousex>=x) and (mousex<=x+w) and (mousey>=y) and (mousey<=y+h) and (button==1) then
  190.             if event==3 then
  191.                 graphics.fillRect(x, y, w, h, r, g, b)
  192.                 p=1
  193.             end
  194.             if (event==2) and (p==1) then
  195.                 p=0
  196.                 action()
  197.             end
  198.         end
  199.     end
  200.     tpt.register_mouseclick(click)
  201. end
  202.  
  203. local function hud()
  204.     if gstatus=='on' then
  205.         button(550,3,50,12,'Clear',255,255,100,gclear)
  206.         button(550,18,50,12,'Write',155,255,255,gwrite)
  207.         button(550,33,12,12,'+',255,155,255,updatetimep)
  208.         button(588,33,12,12,'-',155,155,255,updatetimem)
  209.         graphics.drawText(570, 35, timei)
  210.     end
  211. end
  212. tpt.register_step(hud)
  213.  
  214. local function toggle(a,b,c,d)
  215.     if (b==103) and (c==256) and (d==1) then
  216.         if gstatus=='off' then
  217.             gstatus='on'
  218.         else
  219.             gstatus='off'
  220.         end
  221.     end
  222.     if (b==103) and (c==256) and (d==2) then
  223.         ren.grid(0)
  224.     end
  225. end
  226. tpt.register_keypress(toggle)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement