Here is my modified version of remote.lua which emulates the native accelerometer.
This means that *any* app which was written for the Corona accelerometer API will work with Corona Remote with the inclusion of just one line in main.lua:
[lua]require(‘nativeremote.lua’)[/lua]
And here is the code for nativeremote.lua:
[lua]module( …, package.seeall )
require( ‘socket’ )
– Initial Values
local tcpServer = false
local tcpServerStatus = ‘asynchronous’
local errorCount = 60
local connectionKey = ‘CORONAREMOTE1.2’
local connectionResponseKey = ‘CORONAREMOTE1.2CONNECTED’
local port = ‘8080’
local remoteIP = false
– Async Callback
local function asynchronousConnection( event )
if ( event.isError ) then
errorCount = 60
else
errorCount = 0
tcpClientMessage = {}
local function helper( line ) table.insert( tcpClientMessage , line ) return ‘’ end
helper( ( event.response:gsub( ‘(.-)*’ , helper ) ) )
Runtime:dispatchEvent( {
name = ‘accelerometer’,
xGravity = tonumber( tcpClientMessage[1] ),
yGravity = tonumber( tcpClientMessage[2] ),
zGravity = tonumber( tcpClientMessage[3] ),
xInstant = tonumber( tcpClientMessage[4] ),
yInstant = tonumber( tcpClientMessage[5] ),
zInstant = tonumber( tcpClientMessage[6] ),
isShake = (tcpClientMessage[7] == ‘1’ and true) or false,
} )
Runtime:dispatchEvent( {
name = ‘heading’,
magnetic = tonumber( tcpClientMessage[8] ),
geographic = tonumber( tcpClientMessage[8] ),
} )
if remoteIP and tcpServerStatus == “asynchronous” then
network.request( “http://”…remoteIP…":8080" , “GET” , asynchronousConnection )
end
end
end
– Autostart
if system.getInfo( “environment” ) == “simulator” then
Runtime:addEventListener( “enterFrame” , function()
if tcpServerStatus == “asynchronous” then
errorCount = errorCount + 1
if errorCount >= 60 then
errorCount = 0
tcpServerStatus = “tcp”
remoteIP = false
tcpServer = socket.tcp()
if tcpServer then
tcpServer:setoption( “reuseaddr” , true )
local res = tcpServer:bind( “*” , port )
if not res then tcpServer = nil else
local res = tcpServer:listen( 0 )
if not res then tcpServer = nil end
end
end
end
elseif tcpServerStatus == “tcp” then
tcpServer:settimeout( 0 )
local tcpClient = tcpServer:accept()
if tcpClient ~= nil then
local tcpClientMessage = tcpClient:receive(’*l’)
if ( tcpClientMessage ~= nil ) then
if tcpClientMessage == connectionKey then
local communication = connectionResponseKey
tcpClient:send( communication … “\n” )
remoteIP = tcpClient:getpeername()
if remoteIP and tcpServerStatus == “tcp” then
errorCount = 0
tcpServerStatus = “asynchronous”
tcpClient:close(); tcpServer:close()
network.request( “http://”…remoteIP…":8080" , “GET” , asynchronousConnection )
end
end
end
tcpClient:close()
end
end
end )
end[/lua] [import]uid: 32962 topic_id: 7578 reply_id: 307578[/import]