Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://github.com/KSP-KOS/KSLib/blob/master/library/lib_str_to_num.ks
- local num_lex is lexicon().
- num_lex:add("0", 0).
- num_lex:add("1", 1).
- num_lex:add("2", 2).
- num_lex:add("3", 3).
- num_lex:add("4", 4).
- num_lex:add("5", 5).
- num_lex:add("6", 6).
- num_lex:add("7", 7).
- num_lex:add("8", 8).
- num_lex:add("9", 9).
- function str_to_num {
- parameter s.
- // Handle negative numbers
- if s:startswith("-") {
- return str_to_num(s:substring(1,s:length-1)) * -1.
- }
- // Scientific Notation
- local e is s:find("e").
- if e <> -1 {
- local m is s:substring(e+1,1).
- if m <> "+" and m <> "-" { return "NaN". }
- local p is s:split("e" + m).
- if p:length <> 2 { return "NaN". }
- local p0 is str_to_num(p[0]).
- local p1 is str_to_num(p[1]).
- if p0 = "NaN" or p1 = "NaN" { return "NaN". }
- if m = "+" {
- return p0 * 10^p1.
- } else {
- return (p0 / 10^p1).
- }
- }
- // Decimals
- if s:contains(".") {
- local p is s:split(".").
- if p:length <> 2 { return "NaN". }
- local p0 is str_to_num(p[0]).
- local p1 is str_to_num(p[1]).
- if p0 = "NaN" or p1 = "NaN" { return "NaN". }
- return p0 + (p1 / (10^p[1]:length)).
- }
- // Integers (match on tokens, and bit-shift)
- local v is 0.
- for i IN s:split(""):sublist(1,s:length) {
- if num_lex:haskey(i) { set v to v + num_lex[i]. } else { return "NaN". }
- set v TO v * 10.
- }
- return v / 10.
- }.
- clearscreen.
- print "Gathering Temp Data...".
- list files in fileList.
- for fil in fileList {
- if fil:name = "TempLogData.ks" { delete TempLogData. }.
- }.
- list sensors in senselist.
- set thermometer to senselist[0].
- set high to str_to_num(thermometer:display:substring(0, thermometer:display:length-1)).
- set low to str_to_num(thermometer:display:substring(0, thermometer:display:length-1)).
- on abort set mainloop to false.
- set mainloop to true.
- until mainloop = false {
- set temp to str_to_num(thermometer:display:substring(0, thermometer:display:length-1)).
- log time:seconds + "," + (temp * 1.8 - 459.67) to TempLogData.
- if temp > high { set high to temp. }.
- if temp < low { set low to temp. }.
- // wait for the ONLY set alarm to fire
- for alarm in addons:kac:alarms {
- if alarm:remaining <= 0 { set mainloop to false. }.
- }.
- wait 1.
- }.
- print "high temp: " + (high * 1.8 - 459.67) + "F".
- print "low temp: " + (low * 1.8 - 459.67) + "F".
- print "avg temp: " + (((low + high) / 2) * 1.8 - 459.67) + "F".
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement