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

Pass new RedNet messages to scrolling board.

Started by CipherCrypt, 02 February 2016 - 09:20 PM
CipherCrypt #1
Posted 02 February 2016 - 10:20 PM
So, currently I am having a very minor issue in my programs that are designed to pass received RedNet messages to a scrolling text display. The issue is the fact that I can only pass ONE message to the scrolling board, once it receives a message from the receiver program it will not accept any further input, is there a way to fix this?

This is the program that receives RedNet messages and passes them to bUpdater
Spoiler

rednet.close("top")
rednet.open("top")
print("Waiting for messages to display...")
while true do
  id, message = rednet.receive()
  shell.run("bUpdater "..message)
  sleep(0.5)
end

This is the program that displays the received message from RedNet on a scrolling monitor.
Spoiler

monitor = peripheral.wrap("left")
monitor.clear()
monitor.setTextScale(4)
strtpos = 7
speed = 0.3

local input = {...}
wordCount = #input
i = 1
text = "-"

while i <= wordCount do
  text = (text.." "..input[i])
  sleep(0.1)
  i = i + 1
end

pos = strtpos
poslimit = 0 - string.len(text)

while pos >= poslimit do
  monitor.clear()
  monitor.setCursorPos(pos, 1)
  monitor.write(text)
  sleep(speed)
	if pos == poslimit then
	  pos = strtpos
	else
	  pos = pos -1
   end
end
Dragon53535 #2
Posted 03 February 2016 - 12:53 AM
I'm gonna say, just combine the programs, add the rednet.receive into the monitor program.

As for why it's not receiving again:


while pos >= poslimit do
  monitor.clear()
  monitor.setCursorPos(pos, 1)
  monitor.write(text)
  sleep(speed)
	    if pos == poslimit then
		  pos = strtpos
	    else
		  pos = pos -1
   end
end
This bit of code really isn't exiting. Since you're always running it. If pos at some point equals poslimit, you reset it, and the loop will never end.
CipherCrypt #3
Posted 03 February 2016 - 01:44 AM
I'm gonna say, just combine the programs, add the rednet.receive into the monitor program.

As for why it's not receiving again:


while pos >= poslimit do
  monitor.clear()
  monitor.setCursorPos(pos, 1)
  monitor.write(text)
  sleep(speed)
		if pos == poslimit then
		  pos = strtpos
		else
		  pos = pos -1
   end
end
This bit of code really isn't exiting. Since you're always running it. If pos at some point equals poslimit, you reset it, and the loop will never end.

How would I go about combining the two programs? And if I were to exit the loop in the code above wouldn't it stop scrolling the message after the first pass?

Thank you for your help.
KingofGamesYami #4
Posted 03 February 2016 - 02:30 AM
Make 1 loop that does both things. My statemachine API is helpful when doing that, forcing you to use a single loop.
CipherCrypt #5
Posted 03 February 2016 - 08:02 PM
So, I compacted my code in to one program and it will now accept new messages, however, each message only scrolls across the board once. I'm probably missing something very minor in my while loop but i'm not sure what it is. Any help would be greatly appreciated!
Spoiler

rednet.close("top")
rednet.open("top")
print("Waiting for messages to display...")
id, message = rednet.receive()

monitor = peripheral.wrap("left")
monitor.clear()
monitor.setTextScale(4)
strtpos = 7
speed = 0.3

text = ("- "..message)
newMessage = message

pos = strtpos
poslimit = 0 - string.len(text)

while pos >= poslimit do
  monitor.clear()
  monitor.setCursorPos(pos, 1)
  monitor.write(text)
  sleep(speed)
    if pos == poslimit then
	  id, message = rednet.receive()
	    if message ~= "" then
		    newMessage = message
		    text = ("- "..newMessage)
		    poslimit = 0 - string.len(text)
		    pos = strtpos
	    else
		    pos = strtpos
	    end
    else
	  pos = pos -1
   end
end
valithor #6
Posted 03 February 2016 - 10:45 PM
Right now your program is hanging on the rednet.receive that you have in the middle of your loop, where the computer stops what it is doing and waits for a new message.

One cool thing about rednet.receive is that you can pass it a optional argument, where it will only try to listen to a message for a certain amount of time. Using this you could do something like this:

(Only took the relevant part of your code)

local firstScroll = false --# will explain below

while pos >= poslimit do
  monitor.clear()
  monitor.setCursorPos(pos, 1)
  monitor.write(text)

  --# sleep(speed) --# remove this line

  if firstScroll then --# Only want to switch to using rednet.receive if we have not completely scrolled the other message first
	id, newMessage = rednet.receive(speed) --# using rednet.receive in this manner will make the computer still freeze for the amount of time but still listen for messages
  else
	sleep(speed)
  end

  if newMessage ~= nil and newMessage ~= "" then --# if the time runs out and no message was received newMessage and id would be nil
	message = newMessage
	text = ("- "..message)
	poslimit = 0 - string.len(text)
	pos = strtpos
	firstScroll = false --# new message so we need to set the variable back to false
  end

		if pos == poslimit then
		  pos = strtpos
		  firstScroll = true --# setting this to true since we have scrolled through once completely
		else
		  pos = pos -1
   end
end

All of the code in the if statement was taken from the if statement in your most recent post. The logic behind this is that since you are sleeping for a certain amount of time, you could instead just tell rednet.receive to listen for a message for that amount of time. Since the computer does basically the same thing that it does during rednet.receive as during sleep there will be no noticeable difference in that area. If no message is received newMessage will be nil, so the message will not change and it will continue as normal.

edit:

I noticed that in my example it would constantly switch the message it is displaying if the computer was being spammed rednet messages

The firstScroll variable is used to keep track of whether or not the message has been displayed at least once. If it has not then the variable will be false, and the program will use sleep. Once the message has been scrolled once the program will use rednet.receive, which will give it a chance to get a new message. If there is a new message then we set the firstScroll to false again since we need to let that message scroll as well.
Edited on 03 February 2016 - 09:58 PM