Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
mopet01

Button API

Question

Hello everybody :)

Well, for the first let me tell you, i'm a bloody beginner in OC and lua, so i hope to get some help here ^_^

I'm trying to modify a code for my world but when i do, i just get errors :(

So, please, can anybody help me to get this work....

The "buttonAPI" is the part in the background and the "aufzu" is the working part where the functions are told.

 

buttonApi.lua

local API = {}
local button={}

local component = require("component")
local colors = require("colors")
local term = require("term")
local mon = component.gpu
local w, h = mon.getResolution()
local Green = 0x00AA00
local Red = 0xAA0000
local Black = 0x000000

buttonStatus = nil

function API.clear()
  mon.setBackground(Black)
  mon.fill(1, 1, w, h, " ")
end

function API.clearTable()
  button = {}
  API.clear()
end
               
function API.setTable(name, func, xmin, xmax, ymin, ymax)
  button[name] = {}
  button[name]["func"] = func
  button[name]["active"] = false
  button[name]["xmin"] = xmin
  button[name]["ymin"] = ymin
  button[name]["xmax"] = xmax
  button[name]["ymax"] = ymax
end

function API.fill(text, color, bData)
  local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  local xspot = math.floor((bData["xmax"] + bData["xmin"] - string.len(text)) /2)+1
  local oldColor = mon.setBackground(color)
  mon.fill(bData["xmin"], bData["ymin"], (bData["xmax"]-bData["xmin"]+1), (bData["ymax"]-bData["ymin"]+1), " ")
  mon.set(xspot, yspot, text)
  mon.setBackground(oldColor)
end
     
function API.screen()
  local currColor
  for name,data in pairs(button) do
    local on = data["active"]
    if on == true then currColor = Green else currColor = Red end
    API.fill(name, currColor, data)
  end
end

function API.toggleButton(name)
  button[name]["active"] = not button[name]["active"]
  buttonStatus = button[name]["active"]
  API.screen()
end     

function API.flash(name,length)
  API.toggleButton(name)
  API.screen()
  os.sleep(length)
  API.toggleButton(name)
  API.screen()
end
                                             
function API.checkxy(x, y)
  for name, data in pairs(button) do
    if y>=data["ymin"] and  y <= data["ymax"] then
      if x>=data["xmin"] and x<= data["xmax"] then
        data["func"]()
          return true
      end
    end
  end
  return false
end
     
function API.heading(text)
  w, h = mon.getResolution()
  term.setCursor((w-string.len(text))/2+1, 1)
  term.write(text)
end
     
function API.label(w, h, text)
  term.setCursor(w, h)
  term.write(text)
end

return API

 

aufzu.lua:

API = require("buttonAPI")
local event = require("event")
local computer = require("computer")
local term = require("term")
local component = require("component")
local gpu = component.gpu

local rs = component.redstone
local colors = require("colors")
local side = require("sides")

function API.fillTable()
  API.setTable("Tor", tor, 10,20,3,5)  
  API.setTable("Bahn", bahn, 22,32,3,5)
  API.setTable("Raus", test3, 10,20,8,10)
  API.setTable("Rein", test4, 22,32,8,10)
  API.screen()
end

function getClick()
  local _, _, x, y = event.pull(1,touch)
  if x == nil or y == nil then
    local h, w = gpu.getResolution()
    gpu.set(h, w, ".")
    gpu.set(h, w, " ")
  else 
    API.checkxy(x,y)
  end
end

function tor()
  API.flash("Flash",0.01)
  rs.setBundledOutput(sides.back, colors.lime, 200)
  os.sleep(16)
  rs.setBundledOutput(sides.back, colors.lime, 0)
end

function bahn()
  API.toggleButton("Toggle")
  if buttonStatus == true then
  rs.setBundledOutput(sides.back, colors.red, 0)
    else
     rs.setBundledOutput(sides.back, colors.red, 200)
  end
end

function test3()
  API.flash("Flash")
  term.setCursor(1,25)
  term.write("Free Memory: "..computer.freeMemory().." bytes")
end

function test4()
  computer.shutdown(true)
end

term.setCursorBlink(false)
gpu.setResolution(80, 25)
API.clear()
API.fillTable()
API.heading("Was soll getan werden?")

while true do
  getClick()
end

 

When i click on the, for example, "tor", i just get errors and that is all i get :(

I also tried to give a redstone signal via lua, but here i also get an error, i don't know why.

Computer is a Tier 3 and the restone card inside is a Tier 2

 

Thank you in advice :)

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Thank you for your fast reply.

I changed the code into your sugest, but there are still the same errors, also the "in function getclick" ...

 

Maybe it is easier if i tell, what i would need...

There should be 4 buttons, is not nessesary that these toggles, and they should send a redstone signal into a bundled cable.

Maybe there is an easier way for a code for this :)

Link to post
Share on other sites
  • 0

Hi, I found your problem

 

aufzu.lua:

API = require("buttonAPI")
local event = require("event")
local computer = require("computer")
local term = require("term")
local component = require("component")
local gpu = component.gpu

local rs = component.redstone
local colors = require("colors")
local side = require("sides")

function API.fillTable()
  API.setTable("Tor", tor, 10,20,3,5)     --You set then name of button 1 to "Tor"
  API.setTable("Bahn", bahn, 22,32,3,5)   --You set button 2 to "Bahn"
  API.setTable("Raus", test3, 10,20,8,10) --and so on...
  API.setTable("Rein", test4, 22,32,8,10)
  API.screen()
end

function getClick()
  local _, _, x, y = event.pull(1,touch) --EDIT: No this can be without quotes.
  if x == nil or y == nil then
    local h, w = gpu.getResolution()
    gpu.set(h, w, ".")
    gpu.set(h, w, " ")
  else 
    API.checkxy(x,y)
  end
end

function tor()
  API.flash("Flash",0.01)  --The problem lies here the API is trying to flash a button named "Flash" 
                           --<< on the original demo this button was named "Flash" FIX: chang thisto "Tor"
  rs.setBundledOutput(sides.back, colors.lime, 200)
  os.sleep(16)
  rs.setBundledOutput(sides.back, colors.lime, 0)
end

function bahn()
  API.toggleButton("Toggle") --Same as above
  if buttonStatus == true then
  rs.setBundledOutput(sides.back, colors.red, 0)
    else
     rs.setBundledOutput(sides.back, colors.red, 200)
  end
end

function test3()
  API.flash("Flash") --and same here as well
  term.setCursor(1,25)
  term.write("Free Memory: "..computer.freeMemory().." bytes")
end

function test4()
  computer.shutdown(true)
end

term.setCursorBlink(false)
gpu.setResolution(80, 25)
API.clear()
API.fillTable()
API.heading("Was soll getan werden?")

while true do
  getClick()
end

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.