Last active
July 19, 2021 21:06
-
-
Save asvdvl/539faa42ab23e91d8dde92c8cb7c10b1 to your computer and use it in GitHub Desktop.
Snipets for opencomputers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--This file is parts of different codes for different tasks | |
local cmp = require("component") | |
--get information from gt_batterybuffer | |
local current, total = 0, 0 | |
local bb = cmp.proxy(--[[bbaddr]]) | |
local current = current + string.gsub(bb.getSensorInformation()[3], "([^0-9]+)", "") | |
local total = total + string.gsub(bb.getSensorInformation()[4], "([^0-9]+)", "") | |
--get information from multiple gt_batterybuffer | |
--init | |
local current, total = 0, 0 | |
--main | |
current, total = 0, 0 | |
for bbaddr in pairs(cmp.list("gt_batterybuffer")) do | |
local bb = cmp.proxy(bbaddr) | |
current = current + string.gsub(bb.getSensorInformation()[3], "([^0-9]+)", "") | |
total = total + string.gsub(bb.getSensorInformation()[4], "([^0-9]+)", "") | |
end | |
--get a percentage of the computer's remaining energy | |
local persent = computer.energy() / computer.maxEnergy() * 100 | |
--check file list on exist | |
local filesTable = { | |
"/a", | |
"/b", | |
"/c" | |
} | |
local exists = {} | |
for _, file in pairs(filesTable) do | |
if fs.exists(file) then | |
table.insert(exists, file) | |
end | |
end | |
--detect error and print message | |
local success, message = fs.remove(file) | |
if not success then | |
io.stderr:write("Cannot delete this file: "..file.." by reason: "..message) | |
end | |
--write once and close | |
io.open(filePath, "w"):write(""):close() | |
--read all file | |
io.open(filePath, "r"):read("*a") | |
--dump file into chatbox(run via lua cmd) | |
handler = io.open("/home/1", "r") | |
for value in handler:lines("l") do component.chat_box.say(value) end | |
--mikro direction library for drone | |
local drone = component.proxy(component.list("drone")()) | |
local direction = 1 --1 - 4 | |
local function turnRight() | |
direction = direction + 1 | |
if direction > 4 then | |
direction = 1 | |
end | |
end | |
local function turnLeft() | |
direction = direction - 1 | |
if direction < 1 then | |
direction = 4 | |
end | |
end | |
local function forward(step) | |
if direction == 1 then | |
drone.move(step, 0, 0) | |
elseif direction == 2 then | |
drone.move(0, 0, step) | |
elseif direction == 3 then | |
drone.move(-step, 0, 0) | |
elseif direction == 4 then | |
drone.move(0, 0, -step) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment