OUYA Controller Library?

I would like to access the OUYA Controller from within Basic4Android.

Button presses should work. However, it doesn't appear that there is a mechanism for getting the left and right stick axis values. According to the OUYA docs, you need to override the activity's onGenericMotionEvent method.

Any suggestions...
 

barx

Well-Known Member
Licensed User
Longtime User
You would need to write a library
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please try the attached library. I currently don't have an OUYA console so I cannot test it.

You should add a reference to ouya library and write this code:
B4X:
Sub Globals
   Dim ou As Ouya
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ou.Initialize("ou")
End Sub

Sub ou_GenericMotion (Event As MotionEvent)
   Log(Event)
End Sub
Check the logs whether there are any messages printed.

You will also need to copy ouya-sdk.jar from the libs folder into the additional libraries folder.

Note that the library source code is attached. You can compile it with SLC tool.
 

Attachments

  • Ouya.zip
    146 KB · Views: 379
Upvote 0
I copied the jar and xml files to the Additional Libraries folder for B4A.
I also copied the ouya-odk.jar there.

I followed the posted suggestions for transferring the apk to the Ouya.
http://www.b4x.com/forum/basic4android-getting-started-tutorials/29881-ouya-specific-tweaks.html#post173560

I'm not connected to the Ouya by USB, so I modified your program slightly.

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim ou As Ouya
   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   Activity.LoadLayout("main")
   ou.Initialize("ou")
   Label1.Text = "I'm waiting..."
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   If KeyCode = 96 Then
     Label1.Text = Label1.Text & "O key pressed" & CRLF
    End If
   Return True
End Sub

Sub ou_OnGenericMotion(Event As MotionEvent)
   Label1.Text = Label1.Text & Event & CRLF
End Sub

I can capture key presses but I still can't capture the joysticks.

Suggestions?
 
Upvote 0

Tsunami007

New Member
Licensed User
Longtime User
Hi Guys, it's been a while since this thread was last updated . Im just wondering if anyone has managed to get the analogue sticks
working yet ? .I've tried Erels code and the code above with no luck. Also is there a KeyReleased event associated with the controller.

Thanks
 
Upvote 0

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Erel,

the Ouya Library works great with GOOGLE GLASS

look the log when touch the glasses


(MotionEvent) MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=648.0, y[0]=74.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=4343781, downTime=4343781, deviceId=1, source=0x100008 }

(MotionEvent) MotionEvent { action=ACTION_UP, id[0]=0, x[0]=400.0, y[0]=114.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=4344949, downTime=4343781, deviceId=1, source=0x100008 }

(MotionEvent) MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=658.0, y[0]=75.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=4343799, downTime=4343781, deviceId=1, source=0x100008 }
 
Upvote 0

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Erel,

2 Questions:

FIRST
====


In your example NOT WORK while not pass in this event:

Sub Activity_KeyPress (KeyCode As Int) As Boolean
ListView1.AddSingleLine (KeyCode)
End Sub


I mean, when you not invoke a activity (I think) its not work, after you touch, pass normally in ou_OnGenericMotion event.

You know any solution to work this only using the touch?


SECOND
=====


In EVENT (return of GenericMotion) Sub ou_GenericMotion (Event As MotionEvent)

I can get this, for example:
(MotionEvent) MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=491.0, y[0]=107.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=9158211, downTime=9158106, deviceId=1, source=0x100008 }

So, Can I way to get each separete part? Or only the ACTION?

I´m convert to string and then parsing string... Is too ugly
Dim strME As String = Event


Thank You
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Not sure that I understand the first question.

About the second:
You can call MotionEvent.GetAxisValue with the axis index as a parameter.

You can get the other values with JavaObject.

B4X:
Dim jo As JavaObject = Event
Log(jo.RunMethod("getDownTime", null))
The methods are listed here: http://developer.android.com/reference/android/view/MotionEvent.html
 
Upvote 0

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Not sure that I understand the first question.

About the second:
You can call MotionEvent.GetAxisValue with the axis index as a parameter.

You can get the other values with JavaObject.

B4X:
Dim jo As JavaObject = Event
Log(jo.RunMethod("getDownTime", null))
The methods are listed here: http://developer.android.com/reference/android/view/MotionEvent.html

Perfect!

Dim jo As JavaObject = Event
Log("JAVAOBJECT = " & jo.RunMethod("getAction", Null))
'1 = Down, 2 = Move , 0 = Up
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Look this example (attachment), If I start the app and ONLY move the fingers WITHOUT press any button, not fire the GENERICMOTION

But If I touch and put the code in a listview for example, the GENERICMOTION starting work.... Strange!!!
Why can't you use Activity_Touch event?
 
Upvote 0

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Erel, I resolve with this code, look:

In OUYA Event I get the values, Action and coordinates

Sub ou_GenericMotion (Event AsMotionEvent)
Dim jo As JavaObject = Event
Dim iAction As Int = jo.RunMethod("getAction", Null))
Dim iX As Int = event.GetAxisValue(0)
Dim iY As Int = event.GetAxisValue(1)
Activity_Touch (iAction, iX, iY) <------------ Look
End Sub


And In Activity_Touch I get this values to calculate RIGHT or LEFT direction of Google Glass

Sub Activity_Touch (Action As Int, x As Float, Y As Float)
Dim iHorizontal As Int = x
Dim strDirecao As String = ""

Select Action
Case Activity.ACTION_DOWN
lblTouch.Text = "DOWN " & Action & " / " & x & " / " & Y

gDPADXInicio = iHorizontal
gDPADXFim = 0

Case Activity.ACTION_MOVE
lblTouch.Text = "MOVE " & Action & " / " & x & " / " & Y
gDPADXFim = iHorizontal

Case Activity.ACTION_UP

Dim iPoints As Int = (gDPADXFim-gDPADXInicio)
If iPoints < 0 Then iPoints = iPoints * -1
strDirecao = " [" & iPoints & " points]"

If gDPADXInicio > gDPADXFim AND iPoints > gDistancia Then strDirecao = "<===" & (gDPADXInicio-gDPADXFim)
If gDPADXInicio < gDPADXFim AND iPoints > gDistancia Then strDirecao = "===>" & (gDPADXFim-gDPADXInicio)


lblTouch.Text = "UP " & Action & " / " & x & " / " & Y & " : " & strDirecao

Case Else
lblTouch.Text = "XXXX " & Action & " / " & x & " / " & Y
End Select

End Sub


Bu the ONLY problem now is while not pass in this line, the ou_GenericMotion not work... WHYYYYYYYYYYYYYYY?????? :-(
Sub Activity_KeyPress(KeyCode AsInt) AsBoolean

ListView1.AddSingleLine (KeyCode) <=== START the GenericMotion Event
Label1.text = Keycode <=== IF I change and put only this line, NOT START GenericMotion Event

End Sub

Curious not?

Questions:
* What´s the difference the ListView and Label in this situation?
* Why not automatically start without pass in this event first?

Another Problem:

After starting GenericMotion Event, the Activity_KeyPress STOP working

 

Attachments

  • 20140423_230340_931_x.jpg
    20140423_230340_931_x.jpg
    321.4 KB · Views: 265
  • GlassSensors.zip
    6.5 KB · Views: 279
Last edited:
Upvote 0
Top