Emulate native accelerometer.

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]

Hi p120ph37

I will have a look at integrating your additions into the main remote.lua releases.

Thank you for adding something back to the project!

Hope it is helping you in your developments.

Matt [import]uid: 5354 topic_id: 7578 reply_id: 26898[/import]

@ p120ph37

Thanks - very useful
evs [import]uid: 8366 topic_id: 7578 reply_id: 27046[/import]

Hi Matt,

Yes, Corona Remote is quite helpful - thanks for putting it together for us! [import]uid: 32962 topic_id: 7578 reply_id: 27242[/import]

@ p120ph37

Thanks for making a great tool even better. Now I only have to comment out 1 line when I publish my app. [import]uid: 22878 topic_id: 7578 reply_id: 27372[/import]

@zaphod8

You should actually be able to publish the app even without commenting out the one line - the library will automatically detect that it is not running in the simulator and will do nothing.

If you are commenting out the one line because you do not want the additional lua code eating precious space in your .app bundle, keep in mind that even just having the .lua file in the same directory will get it scooped up by the build process and packed into the .app. In other words, if you want to entirely exclude it from the application when doing a build for distribution, you’ll need to remove the file itself from the build directory as well. [import]uid: 32962 topic_id: 7578 reply_id: 27744[/import]