Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This programm needs the following variables:
- -- Arrays: knob{}{}
- -- Other: m for peripheral acces ; deftextcolor for your default color; defbackcolor for your defaul background color
- -- Please do not use them in other parts of the programm
- -- Creates a new knob
- -- Name :== Name of your knob-Object (as String)
- -- xMin/xMax/yMin/YMax :== Dimensins of your knob background (as Integer)
- -- textOn/textOff :== The text that gets drawn on the display in the true/false states
- -- textcolorOn/textcolorOff :== Color of the text that gets drawn on the monitor (as function colors.*)
- -- colortrue/colorfalse :== Color of you knob background if the state of the knob is true or false (as function colors.*)
- function newKnob(name, xMin, xMax, yMin, yMax, textOn, textOff, textcolorOn, textcolorOff, colortrue, colorfalse)
- -- For each knob-Object create an array that saves all the important knob information
- knob[name] = {};
- -- State of the knob(ON or OFF) in boolean
- knob[name]["active"] = false;
- -- Data for drawing the knob box
- knob[name]["xMin"] = xMin;
- knob[name]["yMin"] = yMin;
- knob[name]["xMax"] = xMax;
- knob[name]["yMax"] = yMax;
- -- Color if state is true
- knob[name]["ctrue"] = colortrue;
- -- Color if state is false
- knob[name]["cfalse"] = colorfalse;
- -- The actuall text that gets displayed
- knob[name]["textOn"] = textOn;
- knob[name]["textOff"] = textOff;
- -- Color of the knob name on the monitor
- knob[name]["tcOn"] = textcolorOn;
- knob[name]["tcOff"] = textcolorOff;
- knob[name]["isKnob"] = true;
- end
- -- Quick and dirty version for creating a knob Object(lime/red background for states, white textcolor, same name in on and off)
- function newDefaultKnob(name, xMin, xMax, yMin, yMax)
- knob[name] = {};
- knob[name]["active"] = false;
- knob[name]["xMin"] = xMin;
- knob[name]["yMin"] = yMin;
- knob[name]["xMax"] = xMax;
- knob[name]["yMax"] = yMax;
- knob[name]["ctrue"] = colors.lime;
- knob[name]["cfalse"] = colors.red;
- knob[name]["textOn"] = name;
- knob[name]["textOff"] = name;
- knob[name]["tcOn"] = colors.white;
- knob[name]["tcOff"] = colors.white;
- knob[name]["isKnob"] = true;
- end
- -- This knob can not be changed by touch events(you have to use the setState or toggleState functions below)
- function newDisplayKnob(name, xMin, xMax, yMin, yMax, textOn, textOff, textcolorOn, textcolorOff, colortrue, colorfalse)
- knob[name] = {};
- knob[name]["active"] = false;
- knob[name]["xMin"] = xMin;
- knob[name]["yMin"] = yMin;
- knob[name]["xMax"] = xMax;
- knob[name]["yMax"] = yMax;
- knob[name]["ctrue"] = colortrue;
- knob[name]["cfalse"] = colorfalse;
- knob[name]["textOn"] = textOn;
- knob[name]["textOff"] = textOff;
- knob[name]["tcOn"] = textcolorOn;
- knob[name]["tcOff"] = textcolorOff;
- knob[name]["isKnob"] = false;
- end
- function newDefaultDisplayKnob(name, xMin, xMax, yMin, yMax)
- knob[name] = {};
- knob[name]["active"] = false;
- knob[name]["xMin"] = xMin;
- knob[name]["yMin"] = yMin;
- knob[name]["xMax"] = xMax;
- knob[name]["yMax"] = yMax;
- knob[name]["ctrue"] = colors.lime;
- knob[name]["cfalse"] = colors.red;
- knob[name]["textOn"] = name;
- knob[name]["textOff"] = name;
- knob[name]["tcOn"] = colors.white;
- knob[name]["tcOff"] = colors.white;
- knob[name]["isKnob"] = false;
- end
- -- Small function for creating a centered headline on your screen
- function header(text)
- p, q = m.getSize();
- m.setCursorPos((p-string.len(text)) / 2 + 1, 1);
- m.write(text);
- end
- -- Function that is responsible for changing the knob background color and calling a second function for actually drawing the button
- function display()
- local currentColor;
- for name,data in pairs(knob) do
- if data["active"] == true then
- currentColor = knob[name]["ctrue"];
- else
- currentColor = knob[name]["cfalse"];
- end
- draw(name, currentColor, data);
- end
- end
- -- Function for drawing the knob on the display
- function draw(knobName, color, knobData)
- m.setBackgroundColor(color);
- -- Finds the middle in the Y-Axis
- local ypos = math.floor(( knobData["yMin"] + knobData["yMax"]) / 2);
- -- Finds the middle in the X-Axis
- local xpos;
- if knobData["active"] == true then
- xpos = math.floor(( knobData["xMax"] - knobData["xMin"] - string.len(knobData["textOn"])) / 2) + 1;
- else
- xpos = math.floor(( knobData["xMax"] - knobData["xMin"] - string.len(knobData["textOff"])) / 2) + 1;
- end
- for ytraverse = knobData["yMin"], knobData["yMax"] do
- m.setCursorPos(knobData["xMin"], ytraverse);
- -- If you are in the middle of your knob Y-Axis check for the middle of the X-Axis
- -- Whitespaces(spaces) are being filled around your knob name
- --
- if ytraverse == ypos then
- -- If the state is true use textOn for positioning
- if knobData["active"] == true then
- for xtraverse = 0, knobData["xMax"] - knobData["xMin"] - string.len(knobData["textOn"]) + 1 do
- if xtraverse == xpos then
- m.setTextColor(knobData["tcOn"]);
- m.write(knobData["textOn"]);
- m.setTextColor(deftextcolor);
- else
- m.write(" ");
- end
- end
- -- If state is false use textOff for positioning
- else
- for xtraverse = 0, knobData["xMax"] - knobData["xMin"] - string.len(knobData["textOff"]) + 1 do
- if xtraverse == xpos then
- m.setTextColor(knobData["tcOff"]);
- m.write(knobData["textOff"]);
- m.setTextColor(deftextcolor);
- else
- m.write(" ");
- end
- end
- end
- else
- -- Whitespaces(spaces) are being filled around your knob name
- for i = knobData["xMin"], knobData["xMax"] do
- m.write(" ");
- end
- end
- end
- m.setBackgroundColor(defbackcolor);
- end
- -- Small function for comparing a given input with the specified areas in your knob-Object and then changing the state if the right conditions are met
- function ckeckPos(x, y)
- for name, data in pairs(knob) do
- if y>=data["yMin"] and y <= data["yMax"] then
- if x>=data["xMin"] and x<= data["xMax"] then
- if data["isKnob"] == true then
- data["active"] = not data["active"];
- end
- end
- end
- end
- end
- -- Initializes your monitor and generates everything necessary for adding additional knobs
- -- You only have to run this once at the start of your programm
- -- side :== The side of you monitor (as String)
- -- textscale :== The size of the font (as Integer)
- -- textcolor :== The default color of your text (not that of the knob texts but of other parts of your programm) (as function colors.*)
- -- backgroundcolor :== The background color of your monitor (as function color.*)
- function startUp(side, textscale, textcolor, backgroundcolor)
- m = peripheral.wrap(side);
- m.setTextScale(textscale);
- m.setTextColor(textcolor);
- m.setBackgroundColor(backgroundcolor);
- defbackcolor = backgroundcolor;
- deftextcolor = textcolor;
- knob={};
- end
- -- Function that refreshes the monitor and cheacks for recent touch events
- -- Very important you have to loop this function for the monitor to work
- -- Also remeber that this function !!WAITS!! for a touch event
- --
- function run(headline)
- -- Clears the monitor
- m.clear();
- -- Draws the headline
- header(headline);
- -- Draws the knobs
- display();
- -- Checks for touch events
- local event, side, x, y = os.pullEvent("monitor_touch");
- ckeckPos(x,y);
- -- Short sleep time
- sleep(.1);
- end
- -- Function for displaying inportant information on your computer(not your monitir)
- function help()
- print("startUp(side, textscale, textcolor, backgroundcolor)");
- print("newKnob(name, xMin, xMax, yMin, yMax, textOn, textOff, textcolorOn, textcolorOff, colortrue, colorfalse)");
- print("newDefaultKnob(name, xMin, xMax, yMin, yMax)");
- print("newDisplayKnob(name, xMin, xMax, yMin, yMax, textOn, textOff, textcolorOn, textcolorOff, colortrue, colorfalse)");
- print("newDisplayDefaultKnob(name, xMin, xMax, yMin, yMax)");
- print("run(headline) -- has to be repeated")
- print("getState(knobname) --returns boolean");
- print("getTextOn(knobname) --returns String");
- print("getTextOff(knobname) --returns String");
- print("setState(knobname, booleanstate)");
- print("toggleState(knobname)");
- print("setTextOn(knobname, text)");
- print("setTextOff(knobname, text)");
- end
- -- Function for checking the boolean value of a knob-Object
- function getState(knobname)
- if knob[knobname]["active"] == true then
- return true;
- else
- return false;
- end
- end
- -- Returns the text in true state as a String
- function getTextOn(knobname)
- return knob[knobname][textOn];
- end
- -- Returns the text in false state as a String
- function getTextOff(knobname)
- return knob[knobname][textOff]
- end
- -- For setting states directly without a touch event
- function setState(knobname, booleanstate)
- knob[knobname]["active"] = booleanstate;
- end
- -- For toggleing states directly without a touch event
- function toggleState(knobname)
- knob[knobname]["active"] = not knob[knobname]["active"];
- end
- -- For setting the displayed text in the true state
- function setTextOn(knobname, text)
- knob[knobname]["textOn"] = text;
- end
- -- For setting the displayed text in the false state
- function setTextOff(knobname, text)
- knob[knobname]["textOff"] = text;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement