This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Duster's profile picture

refresh when data changes

Started by Duster, 10 April 2016 - 09:24 AM
Duster #1
Posted 10 April 2016 - 11:24 AM
i have this code

while true do
term.clear()
term.setCursorPos(1,1)
print("Dust Os")
print("SmartStore 1.0")
x = 1
y = 1
  
m = peripheral.find("monitor")
m.setTextColor(colors.yellow)
 
function slow(text, speed) -- slow("Test Text!", 0.2)
  for x=1,string.len(text) do
    char = string.sub(text,x,x)
    m.write(char)
    sleep(speed)
  end
end
function down()
  x = 1
  y = y + 1
 
  m.setCursorPos(x,y)
end
 

d1 = peripheral.wrap("drive_6")
d2 = peripheral.wrap("drive_5")
m.clear()
m.setCursorPos(1,1)
m.setTextScale(3)
m.setBackgroundColor(colors.black)
m.setTextScale(.5)
m.setCursorBlink(true)
slow("Loading")
slow("...", .2)
m.clear()
m.setCursorPos(1,1)
slow("Status:")
down()
sleep(1)

d1Stat = {d1.isDiskPresent(), d1.getDiskLabel(), d1.hasData()}
d2Stat = {d2.isDiskPresent(), d1.getDiskLabel(), d1.hasData()}
slow("Disk 1")
down()
if d1Stat[1] then
  m.setTextColor(colors.green)
  slow("Disk Present")
  down()
  m.setTextColor(colors.yellow)
  if d1Stat[2] == nil then
    m.setTextColor(colors.red)
    slow("Label: Nil")
  else
    m.setTextColor(colors.yellow)
    slow("Label: " .. d1Stat[2])
  end
  down()
  if d1Stat[3] then
    m.setTextColor(colors.green)
    slow("Disk Has Data")
  else
    m.setTextColor(colors.red)
    slow("Disk Is Empty")
  end
 
  down()
else
  m.setTextColor(colors.red)
  slow("Disk Missing", .2)
  down()
  slow("Nil")
  down()
  slow("Nil")
  down()
end
down()
m.setTextColor(colors.yellow)
slow("Disk 2")
down()
if d2Stat[1] then
  m.setTextColor(colors.green)
  slow("Disk Present")
  down()
  m.setTextColor(colors.yellow)
  if d2Stat[2] == nil then
    m.setTextColor(colors.red)
    slow("Label: Nil")
  else
    m.setTextColor(colors.yellow)
    slow("Label: " .. d2Stat[2])
  end
  down()
  if d2Stat[3] then
    m.setTextColor(colors.green)
    slow("Disk Has Data")
  else
    m.setTextColor(colors.red)
    slow("Disk Is Empty")
  end
 
  down()
else
  m.setTextColor(colors.red)
  slow("Disk Missing")
  down()
  slow("Nil")
  down()
  slow("Nil")
end
sleep(100)
end
paste-bin: here

i was wondering if it could refresh from the to every time the data from d1Stat or d2Stat changed
Darth_Ben17 #2
Posted 10 April 2016 - 01:01 PM
One way to check if the data has changed would be to save the current data to a table, then check if it has changed each loop.

data = { a = 1, b = 2 }
saved = {}
while true do
  if not saved then
	saved = data
  end
  for i = 1, #saved, 1 do
	if saved[i] ~= data[i] then
	  -- Run Functions
	  saved = data -- or just saved = {}, the if statement at the start will ammend it
	  break
	end
  end
end
There is probably a better way to do it, but that's all i got.
EDIT: Not sure why the formatting on that stuffed up, oh well.
Edited on 10 April 2016 - 11:05 AM
Bomb Bloke #3
Posted 10 April 2016 - 03:10 PM
You want to listen for disk and disk_eject events, by the sounds of it.
Duster #4
Posted 11 April 2016 - 08:32 PM
You want to listen for disk and disk_eject events, by the sounds of it.
and how would i do that?
EveryOS #5
Posted 11 April 2016 - 09:02 PM
You want to listen for disk and disk_eject events, by the sounds of it.
and how would i do that?

eventTest = 'key'
e = {os.pullEvent(eventTest or nil)} --coroutine.yield() and os.pullEventRaw() should also work
term.write('Event was '..e[1])
if e[1] == 'disk' or e[1] == 'disk_ebject' then
  print('; Event side was'..e[2])
end
Edited on 11 April 2016 - 07:04 PM