-
Notifications
You must be signed in to change notification settings - Fork 47
/
LinearConversion.tsp
91 lines (85 loc) · 3.33 KB
/
LinearConversion.tsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-- calculate the two constant m and b of the linear conversion, apply them as Math operation and set unit
-- Y = m.X+b
function GetX1Y1X2Y2_custom()
displayunit = display.input.string("The unit of Y: ('Pa' or ...)", display.SFORMAT_ANY)
displayunit = string.sub(displayunit, 1, 3)
X1 = display.input.number("Enter measured value X1:", display.NFORMAT_PREFIX, 0.004)
Y1 = display.input.number("Y1 "..displayunit.." related to X1 "..X1..":" , display.NFORMAT_PREFIX, 0)
repeat
X2 = display.input.number("Enter measured value X2:", display.NFORMAT_PREFIX, 0.020)
if X1 == X2 then
warningprompt = display.prompt(display.BUTTONS_NONE, "X2 must be different from X1, try again.")
delay(2)
display.delete(warningprompt)
end
until X1 != X2
Y2 = display.input.number("Y2 "..displayunit.." related to X2 "..X2..":" , display.NFORMAT_PREFIX, 100)
return X1,Y1,X2,Y2,displayunit
end
function GetX1Y1X2Y2_VtoA()
displayunit = "AM" -- M: calculated value, avoid confusion with a real "A" measurement
X1 = 0
Y1 = 0
repeat
X2 = display.input.number("Resistor value (Ohm):", display.NFORMAT_PREFIX, 1)
if X2 <= 0 then
warningprompt = display.prompt(display.BUTTONS_NONE, "The resistor must be > 0 Ohm, try again.")
delay(2)
display.delete(warningprompt)
end
until X2 > 0
Y2 = 1
return X1,Y1,X2,Y2,displayunit
end
function GetX1Y1X2Y2_0to100percent()
displayunit = "%"
X1 = 0
Y1 = 0
repeat
X2 = display.input.number("Value representing 100%:", display.NFORMAT_PREFIX)
if X2 == 0 then
warningprompt = display.prompt(display.BUTTONS_NONE, "The value must be different from 0, try again.")
delay(2)
display.delete(warningprompt)
end
until X2 != 0
Y2 = 100
return X1,Y1,X2,Y2,displayunit
end
function SetLinearConversion(X1,Y1,X2,Y2,displayunit)
-- prcoess input and set m, b and unit
m = (Y2-Y1) / (X2-X1)
dmm.measure.math.format = dmm.MATH_MXB
dmm.measure.math.mxb.mfactor = m
dmm.measure.math.mxb.bfactor = Y1 - (m * X1)
dmm.measure.math.enable = dmm.ON
displayunit = string.sub(displayunit, 1, 2) -- more than 2 not supported
buffer.unit(buffer.UNIT_CUSTOM1, displayunit)
defbuffer1.clear() --try not to mix units in one buffer
--dmm.digitize.math.format = dmm.MATH_MXB --maybe added later
--dmm.digitize.math.mxb.mfactor = m
--dmm.digitize.math.mxb.bfactor = Y1 - (m * X1)
--dmm.digitize.math.enable = dmm.ON
end
-- get the user input
optionID = display.input.option("Linear Conversions:","Custom", "Volt -> Amp, R?", "0-X -> 0-100%","1 -> 1, 7 digit" )
-- process based on type of conversion
if optionID == display.BUTTON_OPTION1 then
X1,Y1,X2,Y2,displayunit = GetX1Y1X2Y2_custom()
SetLinearConversion(X1,Y1,X2,Y2,displayunit)
elseif optionID == display.BUTTON_OPTION2 then
X1,Y1,X2,Y2,displayunit = GetX1Y1X2Y2_VtoA()
SetLinearConversion(X1,Y1,X2,Y2,displayunit)
elseif optionID == display.BUTTON_OPTION3 then
X1,Y1,X2,Y2,displayunit = GetX1Y1X2Y2_0to100percent()
SetLinearConversion(X1,Y1,X2,Y2,displayunit)
elseif optionID == display.BUTTON_OPTION4 then
displayunit=tostring(dmm.measure.unit)
displayunit = string.sub(displayunit, 10, 10)
SetLinearConversion(0,0,10,10,displayunit)
elseif optionID == display.BUTTON_CANCEL or optionID == nil then
buffer.unit(buffer.UNIT_CUSTOM1, "X")
warningprompt = display.prompt(display.BUTTONS_NONE, "OK, linear is not your thing now ;)")
delay(3)
display.delete(warningprompt)
end