Advertisement
KSA_MissionCtrl

tempLog.ks

Feb 26th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. // https://github.com/KSP-KOS/KSLib/blob/master/library/lib_str_to_num.ks
  2. local num_lex is lexicon().
  3.  
  4. num_lex:add("0", 0).
  5. num_lex:add("1", 1).
  6. num_lex:add("2", 2).
  7. num_lex:add("3", 3).
  8. num_lex:add("4", 4).
  9. num_lex:add("5", 5).
  10. num_lex:add("6", 6).
  11. num_lex:add("7", 7).
  12. num_lex:add("8", 8).
  13. num_lex:add("9", 9).
  14.  
  15. function str_to_num {
  16. parameter s.
  17.  
  18. // Handle negative numbers
  19. if s:startswith("-") {
  20. return str_to_num(s:substring(1,s:length-1)) * -1.
  21. }
  22.  
  23. // Scientific Notation
  24. local e is s:find("e").
  25. if e <> -1 {
  26. local m is s:substring(e+1,1).
  27. if m <> "+" and m <> "-" { return "NaN". }
  28. local p is s:split("e" + m).
  29. if p:length <> 2 { return "NaN". }
  30. local p0 is str_to_num(p[0]).
  31. local p1 is str_to_num(p[1]).
  32. if p0 = "NaN" or p1 = "NaN" { return "NaN". }
  33. if m = "+" {
  34. return p0 * 10^p1.
  35. } else {
  36. return (p0 / 10^p1).
  37. }
  38. }
  39.  
  40. // Decimals
  41. if s:contains(".") {
  42. local p is s:split(".").
  43. if p:length <> 2 { return "NaN". }
  44. local p0 is str_to_num(p[0]).
  45. local p1 is str_to_num(p[1]).
  46. if p0 = "NaN" or p1 = "NaN" { return "NaN". }
  47. return p0 + (p1 / (10^p[1]:length)).
  48. }
  49.  
  50. // Integers (match on tokens, and bit-shift)
  51. local v is 0.
  52. for i IN s:split(""):sublist(1,s:length) {
  53. if num_lex:haskey(i) { set v to v + num_lex[i]. } else { return "NaN". }
  54. set v TO v * 10.
  55. }
  56. return v / 10.
  57.  
  58. }.
  59.  
  60. clearscreen.
  61. print "Gathering Temp Data...".
  62.  
  63. list files in fileList.
  64. for fil in fileList {
  65. if fil:name = "TempLogData.ks" { delete TempLogData. }.
  66. }.
  67.  
  68. list sensors in senselist.
  69. set thermometer to senselist[0].
  70. set high to str_to_num(thermometer:display:substring(0, thermometer:display:length-1)).
  71. set low to str_to_num(thermometer:display:substring(0, thermometer:display:length-1)).
  72.  
  73. on abort set mainloop to false.
  74.  
  75. set mainloop to true.
  76. until mainloop = false {
  77. set temp to str_to_num(thermometer:display:substring(0, thermometer:display:length-1)).
  78. log time:seconds + "," + (temp * 1.8 - 459.67) to TempLogData.
  79. if temp > high { set high to temp. }.
  80. if temp < low { set low to temp. }.
  81.  
  82. // wait for the ONLY set alarm to fire
  83. for alarm in addons:kac:alarms {
  84. if alarm:remaining <= 0 { set mainloop to false. }.
  85. }.
  86. wait 1.
  87. }.
  88.  
  89. print "high temp: " + (high * 1.8 - 459.67) + "F".
  90. print "low temp: " + (low * 1.8 - 459.67) + "F".
  91. print "avg temp: " + (((low + high) / 2) * 1.8 - 459.67) + "F".
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement