Remote display

I don’t know if this will be useful to anyone, but I had fun writing it. This was stripped from my other programs. One that sends projects from one iPad to another and my TRON like game that’s played on 2 iPads communicating with each other.

The purpose of this program is to allow you to send debug info to another device on the same router. You can run your program on one device and send print statements or variable values to another device. You can run your code in fullscreen and see the info print on the other device. This program does simple print statements, but could probably be expanded to display more complex info as text on the screen. The text display could be scrolled if a lot of information is shown.

To see how this works, load this code on 2 devices on the same router. On the primary device, uncomment the 2 sections to demo sending information to the secondary. When you run both programs, they will get their IP address and the IP address of the other device. On the primary, move your finger around the screen. You’ll see the x,y values printing on the secondary device. Also, you see a draw count of a variable print on the secondary. This can be added to other code to print information about the running program.

Like I said, I don’t know if this is useful, but I had fun writing it.


viewer.mode=STANDARD

function setup() 
    b,e,cnt=0,0,0
    sh=remoteDisplay()
end

function draw()
    background(0)
    
    --[[
    cnt=cnt+1
    if cnt%60==0 then
        sh:sendMessage("draw count  "..cnt)
    end
    --]]
    
    sh:draw()
end

function touched(t)
    --[[
    if t.state==BEGAN then
        b=b+1
        sh:sendMessage("BEGAN count "..b)
    end
    if t.state==CHANGED then
        sh:sendMessage("x= "..t.x.."   y= "..t.y)
    end
    if t.state==ENDED then
        e=e+1
        sh:sendMessage("ENDED count "..e)
    end
    --]]
end




remoteDisplay=class()

function remoteDisplay:init()
    self.socket=require("socket")
    self.theirIp="0.0.0.0"  
    self:getMyIp()
    self:setMySocket()
    self:setTheirSocket()
    self:getTheirIp()
end

function remoteDisplay:draw()
    self:receiveTheirMessage()
    fill(255)  
    text("My IP "..self.myIp,WIDTH/2-100,HEIGHT-25)
    text("Their IP "..self.theirIp,WIDTH/2+100,HEIGHT-25)
end

function remoteDisplay:setMySocket()    
    self.server=self.socket.udp()
    self.server:setsockname(self.myIp,5544)
    self.server:settimeout(0)
end

function remoteDisplay:setTheirSocket()
    self.client=self.socket.udp()
    self.client:settimeout(0)
end

function remoteDisplay:getMyIp()
    self.server=self.socket.udp()
    self.server:setpeername("1.1.1.1",80)
    self.myIp,self.myPort=self.server:getsockname()
    self.ip1,self.ip2=string.match(self.myIp,"(%d+.%d+.%d+.)(%d+)")
end

function remoteDisplay:getTheirIp()
    self.client=self.socket.udp()
    self.client:settimeout(0)
    -- send a message to everyone on this network except to myself
    for z=1,255 do
        if z~=tonumber(self.ip2) then
            self.client:setpeername(self.ip1..z,5544)
            self.client:send("get their ip")
        end
    end
end

function remoteDisplay:receiveTheirMessage()
    local data,msg,port=self.server:receivefrom()
    if data~=nil then
        self.theirIp=msg       
        if data=="get their ip" then
            self.client=self.socket.udp()
            self.client:settimeout(0)
            self.client:setpeername(msg,5544)
            self.client:send("got their ip"..self.myIp)            
        elseif string.sub(data,1,12)=="got their ip" then
            self.theirIp=msg
        else
            print(data)
        end
    end
end

function remoteDisplay:sendMessage(tempStr)
    self.client = self.socket.udp()
    self.client:setpeername(self.theirIp,5544)
    self.client:settimeout(0)
    self.client:send(tempStr)
end

i’ll try to find time to try this. looks interesting.

Took longer than I thought, but now I’m getting into this. How did you figure out how to do it? Did you find some examples to study? Thanks!

@RonJeffries I think the original code was more trial and error just to get something simple to work. I then expanded on it to create other programs. I switched to OSC which seemed easier to use. Here’s a link to one of those OSC programs. Scroll to the bottom of the discussion. That code allows you to move your finger on one iPad to move a circle around the screen on another iPad. I was reading your latest Robot article were you were talking about using sockets. I was going to send you some links, but you responded here first.

https://codea.io/talk/discussion/12940/reading-recommendations

That’s rather nice, isn’t it? I will have to think whether I can use it. Certainly for proof of concept, I can. Since I can’t reply, server will have to send back. Have to think whether/how to make that work, maybe some coroutine trick …

Thanks much!