Advertisement
Guest User

Knob API

a guest
Feb 12th, 2013
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.81 KB | None | 0 0
  1.  
  2. -- This programm needs the following variables:
  3. -- Arrays: knob{}{}
  4. -- Other: m for peripheral acces ; deftextcolor for your default color; defbackcolor for your defaul background color
  5. -- Please do not use them in other parts of the programm
  6.  
  7.  
  8. -- Creates a new knob
  9. -- Name                          :== Name of your knob-Object (as String)
  10. -- xMin/xMax/yMin/YMax           :== Dimensins of your knob background (as Integer)
  11. -- textOn/textOff                :== The text that gets drawn on the display in the true/false states
  12. -- textcolorOn/textcolorOff      :== Color of the text that gets drawn on the monitor (as function colors.*)
  13. -- colortrue/colorfalse          :== Color of you knob background if the state of the knob is true or false (as function colors.*)
  14.  
  15. function newKnob(name, xMin, xMax, yMin, yMax, textOn, textOff, textcolorOn, textcolorOff, colortrue, colorfalse)
  16.    -- For each knob-Object create an array that saves all the important knob information
  17.    knob[name] = {};
  18.    -- State of the knob(ON or OFF) in boolean
  19.    knob[name]["active"] = false;
  20.    -- Data for drawing the knob box
  21.    knob[name]["xMin"] = xMin;
  22.    knob[name]["yMin"] = yMin;
  23.    knob[name]["xMax"] = xMax;
  24.    knob[name]["yMax"] = yMax;
  25.    -- Color if state is true
  26.    knob[name]["ctrue"] = colortrue;
  27.    -- Color if state is false
  28.    knob[name]["cfalse"] = colorfalse;
  29.    -- The actuall text that gets displayed
  30.    knob[name]["textOn"] = textOn;
  31.    knob[name]["textOff"] = textOff;
  32.    -- Color of the knob name on the monitor
  33.    knob[name]["tcOn"] = textcolorOn;
  34.    knob[name]["tcOff"] = textcolorOff;
  35.    knob[name]["isKnob"] = true;
  36. end
  37.  
  38. -- Quick and dirty version for creating a knob Object(lime/red background for states, white textcolor, same name in on and off)
  39. function newDefaultKnob(name, xMin, xMax, yMin, yMax)
  40.    knob[name] = {};
  41.    knob[name]["active"] = false;
  42.    knob[name]["xMin"] = xMin;
  43.    knob[name]["yMin"] = yMin;
  44.    knob[name]["xMax"] = xMax;
  45.    knob[name]["yMax"] = yMax;
  46.    knob[name]["ctrue"] = colors.lime;
  47.    knob[name]["cfalse"] = colors.red;
  48.    knob[name]["textOn"] = name;
  49.    knob[name]["textOff"] = name;
  50.    knob[name]["tcOn"] = colors.white;
  51.    knob[name]["tcOff"] = colors.white;
  52.    knob[name]["isKnob"] = true;
  53. end
  54.  
  55.  
  56. -- This knob can not be changed by touch events(you have to use the setState or toggleState functions below)
  57. function newDisplayKnob(name, xMin, xMax, yMin, yMax, textOn, textOff, textcolorOn, textcolorOff, colortrue, colorfalse)
  58.    knob[name] = {};
  59.    knob[name]["active"] = false;
  60.    knob[name]["xMin"] = xMin;
  61.    knob[name]["yMin"] = yMin;
  62.    knob[name]["xMax"] = xMax;
  63.    knob[name]["yMax"] = yMax;
  64.    knob[name]["ctrue"] = colortrue;
  65.    knob[name]["cfalse"] = colorfalse;
  66.    knob[name]["textOn"] = textOn;
  67.    knob[name]["textOff"] = textOff;
  68.    knob[name]["tcOn"] = textcolorOn;
  69.    knob[name]["tcOff"] = textcolorOff;
  70.    knob[name]["isKnob"] = false;
  71.  
  72. end
  73.  
  74. function newDefaultDisplayKnob(name, xMin, xMax, yMin, yMax)
  75.    knob[name] = {};
  76.    knob[name]["active"] = false;
  77.    knob[name]["xMin"] = xMin;
  78.    knob[name]["yMin"] = yMin;
  79.    knob[name]["xMax"] = xMax;
  80.    knob[name]["yMax"] = yMax;
  81.    knob[name]["ctrue"] = colors.lime;
  82.    knob[name]["cfalse"] = colors.red;
  83.    knob[name]["textOn"] = name;
  84.    knob[name]["textOff"] = name;
  85.    knob[name]["tcOn"] = colors.white;
  86.    knob[name]["tcOff"] = colors.white;
  87.    knob[name]["isKnob"] = false;
  88. end
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. -- Small function for creating a centered headline on your screen
  98. function header(text)
  99.    p, q = m.getSize();
  100.    m.setCursorPos((p-string.len(text)) / 2 + 1, 1);
  101.    m.write(text);
  102. end
  103.  
  104. -- Function that is responsible for changing the knob background color and calling a second function for actually drawing the button
  105. function display()
  106.    local currentColor;
  107.    for name,data in pairs(knob) do
  108.  
  109.       if data["active"] == true then
  110.  
  111.          currentColor = knob[name]["ctrue"];
  112.  
  113.       else
  114.  
  115.          currentColor = knob[name]["cfalse"];
  116.  
  117.       end
  118.  
  119.          draw(name, currentColor, data);
  120.  
  121.    end
  122.  
  123. end
  124.  
  125.  
  126. -- Function for drawing the knob on the display
  127. function draw(knobName, color, knobData)
  128.  
  129.    m.setBackgroundColor(color);
  130.    -- Finds the middle in the Y-Axis
  131.    local ypos = math.floor(( knobData["yMin"] + knobData["yMax"]) / 2);
  132.    -- Finds the middle in the X-Axis
  133.    local xpos;
  134.  
  135.    if knobData["active"] == true then
  136.       xpos = math.floor(( knobData["xMax"] - knobData["xMin"] - string.len(knobData["textOn"])) / 2) + 1;
  137.    else
  138.       xpos = math.floor(( knobData["xMax"] - knobData["xMin"] - string.len(knobData["textOff"])) / 2) + 1;
  139.    end
  140.    
  141.    for ytraverse = knobData["yMin"], knobData["yMax"] do
  142.       m.setCursorPos(knobData["xMin"], ytraverse);
  143.       -- If you are in the middle of your knob Y-Axis check for the middle of the X-Axis
  144.       -- Whitespaces(spaces) are being filled around your knob name
  145.       --
  146.       if ytraverse == ypos then
  147.  
  148.          -- If the state is true use textOn for positioning
  149.          if knobData["active"] == true then
  150.             for xtraverse = 0, knobData["xMax"] - knobData["xMin"] - string.len(knobData["textOn"]) + 1 do
  151.                if xtraverse == xpos then
  152.                   m.setTextColor(knobData["tcOn"]);
  153.                   m.write(knobData["textOn"]);
  154.                   m.setTextColor(deftextcolor);
  155.                else
  156.                   m.write(" ");
  157.                end
  158.             end
  159.  
  160.          -- If state is false use textOff for positioning  
  161.          else
  162.             for xtraverse = 0, knobData["xMax"] - knobData["xMin"] - string.len(knobData["textOff"]) + 1 do
  163.                if xtraverse == xpos then
  164.                   m.setTextColor(knobData["tcOff"]);
  165.                   m.write(knobData["textOff"]);
  166.                   m.setTextColor(deftextcolor);
  167.                else
  168.                   m.write(" ");
  169.                end
  170.             end
  171.          end
  172.  
  173.  
  174.       else
  175.          -- Whitespaces(spaces) are being filled around your knob name
  176.          for i = knobData["xMin"], knobData["xMax"] do
  177.             m.write(" ");
  178.          end
  179.       end
  180.    end
  181.    m.setBackgroundColor(defbackcolor);
  182. end
  183.      
  184.  
  185. -- 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
  186. function ckeckPos(x, y)
  187.    for name, data in pairs(knob) do
  188.       if y>=data["yMin"] and  y <= data["yMax"] then
  189.          if x>=data["xMin"] and x<= data["xMax"] then
  190.             if data["isKnob"] == true then
  191.                data["active"] = not data["active"];
  192.             end
  193.          end
  194.       end
  195.    end
  196. end
  197.      
  198.  
  199. -- Initializes your monitor and generates everything necessary for adding additional knobs
  200. -- You only have to run this once at the start of your programm
  201. -- side :== The side of you monitor (as String)
  202. -- textscale :== The size of the font (as Integer)
  203. -- textcolor :== The default color of your text (not that of the knob texts but of other parts  of your programm) (as function colors.*)
  204. -- backgroundcolor :== The background color of your monitor (as function color.*)
  205.  
  206. function startUp(side, textscale, textcolor, backgroundcolor)
  207. m = peripheral.wrap(side);
  208. m.setTextScale(textscale);
  209. m.setTextColor(textcolor);
  210. m.setBackgroundColor(backgroundcolor);
  211. defbackcolor = backgroundcolor;
  212. deftextcolor = textcolor;
  213. knob={};
  214. end
  215.  
  216.  
  217. -- Function that refreshes the monitor and cheacks for recent touch events
  218. -- Very important you have to loop this function for the monitor to work
  219. -- Also remeber that this function !!WAITS!! for a touch event
  220. --
  221. function run(headline)
  222.       -- Clears the monitor
  223.       m.clear();
  224.       -- Draws the headline
  225.       header(headline);
  226.       -- Draws the knobs
  227.       display();
  228.       -- Checks for touch events
  229.       local event, side, x, y = os.pullEvent("monitor_touch");
  230.       ckeckPos(x,y);
  231.       -- Short sleep time
  232.       sleep(.1);
  233. end
  234.  
  235.  
  236. -- Function for displaying inportant information on your computer(not your monitir)
  237. function help()
  238.  
  239.    print("startUp(side, textscale, textcolor, backgroundcolor)");
  240.  
  241.    print("newKnob(name, xMin, xMax, yMin, yMax, textOn, textOff, textcolorOn, textcolorOff, colortrue, colorfalse)");
  242.  
  243.    print("newDefaultKnob(name, xMin, xMax, yMin, yMax)");
  244.  
  245.    print("newDisplayKnob(name, xMin, xMax, yMin, yMax, textOn, textOff, textcolorOn, textcolorOff, colortrue, colorfalse)");
  246.  
  247.    print("newDisplayDefaultKnob(name, xMin, xMax, yMin, yMax)");
  248.  
  249.    print("run(headline) -- has to be repeated")
  250.  
  251.    print("getState(knobname) --returns boolean");
  252.  
  253.    print("getTextOn(knobname) --returns String");
  254.  
  255.    print("getTextOff(knobname) --returns String");
  256.  
  257.    print("setState(knobname, booleanstate)");
  258.  
  259.    print("toggleState(knobname)");
  260.  
  261.    print("setTextOn(knobname, text)");
  262.  
  263.    print("setTextOff(knobname, text)");
  264.  
  265.    
  266. end
  267.  
  268. -- Function for checking the boolean value of a knob-Object
  269. function getState(knobname)
  270.  
  271.    if knob[knobname]["active"] == true then
  272.       return true;
  273.    else
  274.       return false;
  275.  
  276.    end
  277.  
  278. end
  279.  
  280. -- Returns the text in true state as a String
  281. function getTextOn(knobname)
  282.  
  283.    return knob[knobname][textOn];
  284.  
  285. end
  286.  
  287. -- Returns the text in false state as a String
  288. function getTextOff(knobname)
  289.  
  290.    return knob[knobname][textOff]
  291.  
  292. end
  293.  
  294.  
  295. -- For setting states directly without a touch event
  296. function setState(knobname, booleanstate)
  297.  
  298.    knob[knobname]["active"] = booleanstate;
  299.  
  300. end
  301.  
  302. -- For toggleing states directly without a touch event
  303. function toggleState(knobname)
  304.  
  305.    knob[knobname]["active"] = not knob[knobname]["active"];
  306.  
  307. end
  308.  
  309. -- For setting the displayed text in the true state
  310. function setTextOn(knobname, text)
  311.  
  312.    knob[knobname]["textOn"] = text;
  313.  
  314. end
  315.  
  316. -- For setting the displayed text in the false state
  317. function setTextOff(knobname, text)
  318.  
  319.    knob[knobname]["textOff"] = text;
  320.  
  321. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement