Emulate native accelerometer.

5 replies [Last post]
p120ph37
User offline. Last seen 26 weeks 20 hours ago. Offline
Joined: 1 Feb 2011

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:

1
require('nativeremote.lua')

And here is the code for nativeremote.lua:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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

Replies

Matthew Pringle
User offline. Last seen 38 weeks 35 min ago. Offline
Joined: 23 Feb 2010

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

evs
User offline. Last seen 1 year 27 weeks ago. Offline
Joined: 22 Aug 2010

@ p120ph37

Thanks - very useful

evs

p120ph37
User offline. Last seen 26 weeks 20 hours ago. Offline
Joined: 1 Feb 2011

Hi Matt,

Yes, Corona Remote is quite helpful - thanks for putting it together for us!

zaphod8
User offline. Last seen 32 weeks 2 days ago. Offline
Joined: 20 Jan 2011

@ p120ph37

Thanks for making a great tool even better. Now I only have to comment out 1 line when I publish my app.

p120ph37
User offline. Last seen 26 weeks 20 hours ago. Offline
Joined: 1 Feb 2011

@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.

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.