-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceive.lua
More file actions
111 lines (87 loc) · 2.36 KB
/
receive.lua
File metadata and controls
111 lines (87 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
-- Cloned
-- Program to receive messages from computers/
-- turtles using flex.lua "send" function
-- <Flexico64@gmail.com>
--------------------------------------
-- |¯\|¯¯] /¯]|¯¯][¯¯]\\ //|¯¯]|¯\ --
-- | /| ] | [ | ] ][ \\// | ] | / --
-- | \|__] \_]|__][__] \/ |__]| \ --
--------------------------------------
local log_file = "log.txt"
local options_file = "flex_options.cfg"
os.loadAPI("flex.lua")
local modem_channel = 6464
if fs.exists(options_file) then
local file = fs.open("flex_options.cfg", "r")
local line = file.readLine()
while line ~= nil do
if string.find(line, "modem_channel=") == 1 then
modem_channel = tonumber( string.sub(
line, 15, string.len(line) ) )
break
end --if
line = file.readLine()
end --while
file.close()
end --if
local modem
local p = flex.getPeripheral("modem")
if #p > 0 then
modem = peripheral.wrap(p[1])
modem.open(modem_channel)
else
flex.printColors("Please attach a wireless"
.." or ender modem\n", colors.red)
sleep(2)
return
end --if/else
local monitor
p = flex.getPeripheral("monitor")
if #p > 0 then
monitor = peripheral.wrap(p[1])
term.redirect(monitor)
monitor.clear()
monitor.setCursorPos(1,1)
monitor.setTextScale(0.5)
end --if
local lcd_x,lcd_y = monitor.getSize()
local file, line
local filelist = {}
if fs.exists(log_file) then
file = fs.open(log_file, "r")
line = file.readLine()
while line ~= nil do
if line ~= "" or ( line == "" and
filelist[#filelist] ~= "" ) then
filelist[#filelist+1] = line
end --if
line = file.readLine()
end --while
file.close()
file = fs.open(log_file, "a")
else
-- Log file does not exist: make one!
file = fs.open(log_file, "w")
end --if/else
local x, y
y = math.max(1,#filelist-lcd_y)
for x=y, #filelist do
flex.printColors(filelist[x])
end --for
if filelist[#filelist] ~= "" then
file.writeLine("")
end --if
file.close()
term.setTextColor(colors.white)
print("Waiting for message on channel "
..tostring(modem_channel).."...")
while true do
local event, modemSide, senderChannel,
replyChannel, message, senderDistance =
os.pullEvent("modem_message")
file = fs.open(log_file, "a")
file.writeLine(message)
file.close()
flex.printColors(message)
sleep(0.01)
end --while