I’m beginning to mess around with OSC. I have no problems in implementing some basic stuff, but I’m eager to find out what there is to learn. Is there any code agnostic material I can read(or watch) to clue me into what might be possible and more importantly, what issues I’m going to face.
I can foresee I’ll need to learn how to handle multiple devices shouting at each other.
@bitmage Here it is. I don’t like it, but it seems to work. I need to look at this more to understand what’s really happening. I don’t like the way the 2 devices try to connect to each other. I need to create a better way.
It’s kind of hard writing code on 2 devices at the same time and keeping the code in sync.
Run this on both devices and drag your finger to move the circle on the other device.
viewer.mode=STANDARD
function setup()
--65491 max bytes to send
ip=1
msg=""
myIp = osc.start() -- address of this ipad
if myIp==nil then
msg="iPad is offline"
else
-- seperate my ip into ip1=xxx.xxx.xxx. ip2=xxx
ip1,ip2=string.match(myIp,"(%d+.%d+.%d+.)(%d+)")
print("My ip "..myIp)
getTheirIp=true
end
sx,sy=0,0
end
function draw()
background(0)
fill(255)
showText()
if getTheirIp then
getOtherIp()
end
ellipse(sx,sy,80)
end
function touched(t)
if t.state==CHANGED and not getTheirIp then
osc.host=otherIp
sendMsg("screenXY",t.x,t.y)
end
end
function getOtherIp()
if ip~=tonumber(ip2) then -- send message to other devices except myself
local temp=string.format("%s%s",ip1,ip)
osc.host=temp
msg="Looking for other devices IP "..temp
sendMsg("otherIp",myIp)
end
ip=ip+1
if ip>255 then
getTheirIp=false
msg="Other device is offline or not running, try restarting it"
end
end
function showText()
text(msg,WIDTH/2,HEIGHT/2)
end
function sendMsg(id,...)
osc.send(id,...)
end
function osc.listen(id,...) -- received messages
arg={...}
if id=="screenXY" then -- get screen x,y values from other device
sx=arg[1]
sy=arg[2]
end
if id=="gotIp" then
otherIp=arg[1]
getTheirIp=false
msg="Move your finger on the screen"
print("other ip",otherIp)
end
if id=="otherIp" then
getTheirIp=false -- got their ip, dont need keep trying
otherIp=arg[1] -- ip address of other device
print("Their ip",otherIp)
osc.host=otherIp
sendMsg("gotIp",myIp)
msg="Move your finger on the screen"
end
end