This commit is contained in:
Gvidas Juknevičius 2024-08-18 02:08:11 +03:00
parent 1cfca1d5b8
commit 6f61e9549b
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
6 changed files with 243 additions and 54 deletions

View File

@ -1,31 +0,0 @@
SRC_DIR="./src"
BUILD_DIR="./build"
OUTPUT_FILE="$BUILD_DIR/startup.lua"
BOOT_FILE="$SRC_DIR/boot.lua"
MAIN_FILE="$SRC_DIR/main.lua"
mkdir -p "$BUILD_DIR"
> "$OUTPUT_FILE"
if [ -e "$BOOT_FILE" ]; then
echo "-- File: boot.lua" >> "$OUTPUT_FILE"
cat "$BOOT_FILE" >> "$OUTPUT_FILE"
echo >> "$OUTPUT_FILE"
fi
for file in "$SRC_DIR"/*.lua; do
if [ -e "$file" ] && [ "$(basename "$file")" != "boot.lua" ] && [ "$(basename "$file")" != "main.lua" ]; then
filename=$(basename "$file")
echo "-- File: $filename" >> "$OUTPUT_FILE"
cat "$file" >> "$OUTPUT_FILE"
echo >> "$OUTPUT_FILE"
fi
done
if [ -e "$MAIN_FILE" ]; then
echo "-- File: main.lua" >> "$OUTPUT_FILE"
cat "$MAIN_FILE" >> "$OUTPUT_FILE"
echo >> "$OUTPUT_FILE"
fi

182
keypadOS.lua Normal file
View File

@ -0,0 +1,182 @@
-- FILE: src/boot.lua
-- keycardOS "bootloader", has no access to basalt
-- intended for checking for updates, and automatically updating basalt if it is missing
LAST_USED = os.time()
local function checkForUpdate()
current_time = os.time()
difference = math.abs(current_time - LAST_USED)
print("Checking for update... difference: " .. tostring(difference))
--its been considerable time since the keypad was interacted with
--therefore it's time to force an update down the users throat (microsoft moment)
-- if difference > 4 then
if difference > .5 then
--logic for updating
end
end
function updateChecker()
while true do
checkForUpdate()
sleep(1)
end
end
local function getBasalt()
if fs.exists("basalt.lua") then
mPrint("Basalt found!")
else
mPrint("Download basalt...")
basalt_code = http.get("https://raw.githubusercontent.com/Pyroxenium/Basalt/master/docs/versions/latest.lua").readAll()
mPrint("Install basalt...")
local file = fs.open("basalt.lua", "w")
file.write(basalt_code)
file.close()
mPrint("Rebooting...")
os.reboot()
end
end
KEYPADOS_VERSION = "3.0"
MONITOR = peripheral.find("monitor")
local function boot()
mon_reset(0.5);
mPrint("keypadOS v" .. KEYPADOS_VERSION)
mon_reset(1);
end
boot()
local files = {}
local globalRequire = require -- Store default require reference
local require = function(path) -- Will return saved file or attempt default lua require
return files[path] or globalRequire(path)
end
files["boot.lua"] = function(...)
local function boot()
mon_reset(0.5);
mPrint("keypadOS v" .. KEYPADOS_VERSION)
mon_reset(1);
end
return {
boot
}
end
-- FILE: src/init.lua
local basalt = require("basalt")
local monitor = peripheral.find("monitor")
local drive = peripheral.find("drive")
local main = basalt.addMonitor()
main:setMonitor(monitor)
btnX = 1
btnY = 3
pin = ""
-- FILE: src/config.lua
correctPin = "42169"
-- FILE: src/unlock_door.lua
function unlockDoor()
if drive.isDiskPresent() then
if drive.getDiskLabel() == correctPin then
pin = correctPin
drive:ejectDisk()
end
end
basalt.debug("test")
if pin == correctPin then
enterButton:setBackground(colors.green)
pinLabel:setText("Welcome")
redstone.setOutput("front", true)
if drive.isDiskPresent() then
if drive.getDiskLabel() == nil then
drive.setDiskLabel(correctPin)
pinLabel:setText("Crd set")
drive:ejectDisk()
end
end
else
pinLabel:setText("Wrong")
enterButton:setBackground(colors.red)
end
local thread = main:addThread()
:start(resetEverything)
end
function resetEverything()
sleep(2)
pin = ""
pinLabel:setText("")
redstone.setOutput("front", false)
enterButton:setBackground(colors.blue)
end
function addToPin(i)
if #pin >= 5 then
return
end
pin = pin .. tostring(i)
pinLabel:setText(pin)
end
-- FILE: src/utils.lua
MONITOR_Y = 1
function mPrint(text)
MONITOR.setCursorPos(1,y)
MONITOR.write(text)
MONITOR_Y = MONITOR_Y + 1
end
function mon_reset(scale)
MONITOR.clear()
MONITOR.setTextScale(scale)
end
-- FILE: src/main.lua
pinLabel = main:addLabel()
:setText("")
:setFontSize(1)
enterButton = main:addButton()
:setText(">>>>")
:setBackground(colors.blue)
:setPosition(6,3)
:setSize(1.5,3.2)
:onClick(unlockDoor)
for i = 1, 9 do
local button = main:addButton()
:setPosition(btnX,btnY)
:setText(tostring(i))
:setSize(2,1)
:onClick(
function()
addToPin(i)
end)
btnX = btnX + 2
if btnX >= 6 then
btnY = btnY + 1
btnX = 1
end
end
parallel.waitForAll(basalt.autoUpdate, updateChecker)

8
pyproject.toml Normal file
View File

@ -0,0 +1,8 @@
[tool.pylint.'MESSAGES CONTROL']
disable = [
"unnecessary-semicolon",
"trailing-newlines",
"missing-docstring"
]

View File

@ -7,40 +7,23 @@ local function checkForUpdate()
current_time = os.time()
difference = math.abs(current_time - LAST_USED)
print("Checking for update... difference: " .. tostring(difference))
--its been considerable time since the keypad was interacted with, therefore it's time to force an update down the users throat (microsoft moment)
--its been considerable time since the keypad was interacted with
--therefore it's time to force an update down the users throat (microsoft moment)
-- if difference > 4 then
if difference > .5 then
--logic for updating
end
end
function updateChecker()
while true do
checkForUpdate()
sleep(1)
end
end
local function boot()
KEYPADOS_VERSION = "3.0"
monitor = peripheral.find("monitor")
monitor.clear()
monitor.setTextScale(0.5)
y = 1
function mPrint(text)
monitor.setCursorPos(1,y)
monitor.write(text)
y = y + 1
end
mPrint("keypadOS v" .. KEYPADOS_VERSION)
local function getBasalt()
if fs.exists("basalt.lua") then
mPrint("Basalt found!")
else
@ -53,10 +36,15 @@ local function boot()
mPrint("Rebooting...")
os.reboot()
end
end
monitor.setTextScale(1)
monitor.clear()
KEYPADOS_VERSION = "3.0"
MONITOR = peripheral.find("monitor")
local function boot()
mon_reset(0.5);
mPrint("keypadOS v" .. KEYPADOS_VERSION)
mon_reset(1);
end
boot()

11
src/utils.lua Normal file
View File

@ -0,0 +1,11 @@
MONITOR_Y = 1
function mPrint(text)
MONITOR.setCursorPos(1,y)
MONITOR.write(text)
MONITOR_Y = MONITOR_Y + 1
end
function mon_reset(scale)
MONITOR.clear()
MONITOR.setTextScale(scale)
end

31
x.py Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/python
OUTPUT="keypadOS.lua";
FILES= [
"src/boot.lua",
"src/init.lua",
"src/config.lua",
"src/unlock_door.lua",
"src/utils.lua",
"src/main.lua",
]
def read_file(p: str) -> str:
buf = "";
with open(p, "r", encoding="utf-8") as f:
buf = f"\n-- FILE: {p}\n\n" + f.read();
return buf;
def main():
buf = "";
for file in FILES:
print(f"=== FILE: {file}");
buf += read_file(file);
with open(OUTPUT, "w", encoding="utf-8") as f:
f.write(buf);
print("DONE");
if __name__ == "__main__":
main();