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

Reading OpenPeripheral chat messages ($$Hello World!)

Started by Robinlemon, 20 August 2015 - 09:12 PM
Robinlemon #1
Posted 20 August 2015 - 11:12 PM
Trying to pull the chat event and run it through a few conditionals….

The event is called 'glasses_chat_command' from what I can tell, but this only returns the event name and the side the peripheral is on.

For Example


while true do
   local event, side = os.pullEvent("glasses_chat_command")
   print("Event: "..event.." Side: "..side)
end

Would print:

Event: glasses_chat_command Side: top

;(

CODE:

glass = peripheral.wrap("top")
cell = peripheral.wrap("bottom")

colors = {
WHITE = 0xFFFFFF,
BLACK = 0x000000,
LIGHTGRAY = 0xC0C0C0,
DARKGRAY = 0x808080,
RED = 0xFF0000,
ORANGE = 0xFF7F00,
YELLOW = 0xFFFF00,
LIME = 0x7FFF00,
DARKGREEN = 0x008000,
LIGHTBLUE = 0x38B0DE,
DARKBLUE = 0x0000FF,
PURPLE = 0x800080
}

WIDTH = 200
HEIGHT = 25				
HOFFSET = 5	  
VOFFSET = 3  
TITLE = "POWER"
SCREEN = 490

TOGGLE = true

BOX_ALPHA = 0.2
GRADIENT_ALPHA1 = 0.2
GRADIENT_ALPHA2 = 0.2
  
ALPHA_OVERIDE = true
ALPHA_OVERIDE_INT = 0.8

function draw(power)
   if TOGGLE then  
	  if ALPHA_OVERIDE then
		 BOX_ALPHA = ALPHA_OVERIDE_INT
		 GRADIENT_ALPHA1 = ALPHA_OVERIDE_INT
		 GRADIENT_ALPHA2 = ALPHA_OVERIDE_INT
	  end
  
	  glass.addBox(HOFFSET, VOFFSET, HOFFSET+WIDTH, VOFFSET+HEIGHT, 0xFFFFFF, BOX_ALPHA) -- x,y, x,y, colorHex, alpha							
	  glass.addGradientBox(HOFFSET+5, VOFFSET+5, HOFFSET+WIDTH-10, VOFFSET+HEIGHT-10 ,colors.RED, GRADIENT_ALPHA1, colors.ORANGE, GRADIENT_ALPHA2, 2)
	  glass.addText((WIDTH/2)-string.len(TITLE), (HEIGHT+VOFFSET)/2, TITLE, colors.LIGHTBLUE)
  
   end
end

function send(side, power)
   if rednet.isOpen(side) == false then
	  rednet.open(side)
   end

   rednet.broadcast(power)
end

function chatCommands()
   local event, message = os.pullEvent("glasses_chat_command")
  
   print(event)
   print(message)
	
   if message == "sync" then
	  glass.sync()
   elseif message == "toggle" then
	  TOGGLE = not TOGGLE
   end
end

while true do
  glass.clear()
  -------------------------
  power = cell.getEnergyStored()
  draw(power)

  send("left", power)
  chatCommands()
  -------------------------		  
  glass.sync()
  sleep(0.1)
end
Edited on 20 August 2015 - 09:18 PM
Bomb Bloke #2
Posted 20 August 2015 - 11:35 PM
The event is called 'glasses_chat_command' from what I can tell, but this only returns the event name and the side the peripheral is on.

For Example


while true do
   local event, side = os.pullEvent("glasses_chat_command")
   print("Event: "..event.." Side: "..side)
end

Would print:

Event: glasses_chat_command Side: top

;(

Silly question, but have you tried assigning to more than just two variables?

while true do
	local event = {os.pullEvent("glasses_chat_command")}
	for i = 1, #event do print(event[i]) end
	print()
end
Robinlemon #3
Posted 20 August 2015 - 11:36 PM
The event is called 'glasses_chat_command' from what I can tell, but this only returns the event name and the side the peripheral is on.

For Example


while true do
   local event, side = os.pullEvent("glasses_chat_command")
   print("Event: "..event.." Side: "..side)
end

Would print:

Event: glasses_chat_command Side: top

;(

Silly question, but have you tried assigning to more than just two variables?

while true do
	local event = {os.pullEvent("glasses_chat_command")}
	for i = 1, #event do print(event[i]) end
	print()
end

Yep

local event, side, a, b, c, d = os.pullEvent("glasses_chat_command")

EDIT: nevermind seems Im an idiot it returns event, side, name, UUID, message

However I still do have a problem, it waits for a chat message, and wont update the code until it gets one, how can i bypass this so its as fast as assigning a variable


event, side, name, UUID, message = getTheChat
print("Chicken")

I made a timer to skip the event process


os.startTimer(3)
event, etc1, etc2,...... = os.pullEvent()

if event == "glasses_chat_command" then
**stuff**
end
Edited on 20 August 2015 - 10:05 PM
HPWebcamAble #4
Posted 21 August 2015 - 12:07 AM
In your 'send' function, you check that the modem is on every time the function is called
Why not just do 'rednet.open(side)' at the top of your code?
Its extremely unlikely that it would be closed in the middle of your program

But thats a little offtopic…
However I still do have a problem, it waits for a chat message, and wont update the code until it gets one, how can i bypass this so its as fast as assigning a variable
Well, 0.1 seconds is a very short time. Do you really need to have it update that quickly? I'd make it at least 1 second.

You'd need to do something like this:

--# Open modem

--# Define variables here
local lastTimerID = os.startTimer( 1 ) --# Starts the first timer, saves its ID

--# Define functions here

while true do
  local event = { os.pullEvent() } --# Notice there is not filter - it gets all events. Also, it puts the result in a table, instead of separate variables

  if event[1] == "glasses_chat_command" then
	--# In this case, event[2] is the side, event[3] is the name, ect
  elseif event[1] == "timer" and event[2] == lastTimerID then
	--# The timer ended, update the dipslay

	lastTimerID = os.startTimer( 1 ) --# Start a new 1 second timer
  end
  --# Additional events you are looking for get added on like that

end
Edited on 20 August 2015 - 10:07 PM
Robinlemon #5
Posted 21 August 2015 - 12:31 AM
Yeah I figured it out, but thanks for the help but I came across a new Issue! Great -_-/> I don't even know if you can do this, but Id like to have a header in my console, so it will print out stuff but it will always have Hello World at the top
HPWebcamAble #6
Posted 21 August 2015 - 12:44 AM
I don't even know if you can do this, but Id like to have a header in my console, so it will print out stuff but it will always have Hello World at the top

Of the computer? Sure, but you might need to modify your program slightly.

This is how I do it at least:

Store the console lines in a table (Like line 1 is 'Starting program..' and line 2 is 'Opening modem', things like that)
Every time a new line is added,
Clear the screen, and reprint everything.

--# Clearing the screen
term.clear()
term.setCursorPos(1,1) --# Thats important
To make sure you only print the entries that will fit on the screen, print the last 'h' entries in the table, where 'h' is the height of the screen
Then print the heading.

Hopefully you follow me here :P/>
(You can get the basic idea by just reading whats in bold :)/> )
Edited on 20 August 2015 - 10:45 PM