Page 1 of 1

deviceorientation events?

Posted: Mon Oct 31, 2016 12:02 am
by idle
I've been trying to get deviceorientation events working but can't figure it out
like what is "window.addEventListener("deviceorientation",p_fn)" expecting for the second parameter, an address, variable or a string?

Code: Select all

Procedure OrientHandlerFunc(*event) 
  Protected alpha.d,beta.d,gamma.d 
  !v_alpha = p_event.alpha; 
  !v_beta = p_event.beta;
  !v_gamma = p_event.gamma 
  
  Debug "alpha "  + StrD(alpha,2) 
  Debug "beta " + StrD(beta,2) 
  Debug "gamma " + StrD(gamma,2) 
 
EndProcedure   
   
; 
 Procedure IsDeviceOrientationEvent(*fn) 
   Protected result,alpha.d,beta.d,gamma.d 
   !if (window.DeviceOrientationEvent) {
		!v_result = 1 ;
		!window.addEventListener("deviceorientation",p_fn);
 	!}
	ProcedureReturn result 
 EndProcedure       

OpenWindow(0, 10, 10, 200, 50, "Desktop example",#PB_Window_Background)

ExamineDesktops()

Debug "Desktop dimension: " + DesktopWidth(0) + " x " + DesktopHeight(0)

Procedure TimerEvent()
  Debug "Mouse position: x:" + DesktopMouseX() + " y:" + DesktopMouseY()
EndProcedure

x =IsDeviceOrientationEvent(@orienthandlerfunc()) 
Debug x 

AddWindowTimer(0, 0, 1000) ; Add a timer every second (1000 ms)
BindEvent(#PB_Event_Timer, @TimerEvent())

Re: deviceorientation events?

Posted: Mon Oct 31, 2016 12:36 am
by falsam
This code could help you?

Code: Select all

Enumeration
  #mainForm
  
  #ax 
  #ay
  #az
  
  #alpha
  #beta
  #gamma
  
EndEnumeration

;Declare motion(event)
Declare onExit()

OpenWindow(#mainForm, 0, 0, 0, 0, "", #PB_Window_Background)

TextGadget(-1, 10, 20, 300, 22, "<h2>Device Accelerometer</h2>")
TextGadget(-1, 50, 70, 50, 100, "x")
StringGadget(#ax, 100, 70, 200, 22, "0")

TextGadget(-1, 50, 100, 50, 100, "y")
StringGadget(#ay, 100, 100, 200, 22, "0")

TextGadget(-1, 50, 130, 50, 100, "z")
StringGadget(#az, 100, 130, 200, 22, "0")

TextGadget(-1, 10, 170, 300, 22, "<h2>Device Orientation</h2>")
TextGadget(-1, 50, 220, 50, 100, "alpha")
StringGadget(#alpha, 100, 220, 200, 22, "0")

TextGadget(-1, 50, 250, 50, 100, "beta")
StringGadget(#beta, 100, 250, 200, 22, "0")

TextGadget(-1, 50, 280, 50, 100, "gamma")
StringGadget(#gamma, 100, 280, 200, 22, "0")

!function motion(event) {
!  var v_ax = event.accelerationIncludingGravity.x
!  var v_ay = event.accelerationIncludingGravity.y
!  var v_az = event.accelerationIncludingGravity.z
   SetGadgetText(#ax, StrF(ax))
   SetGadgetText(#ay, StrF(ay))
   SetGadgetText(#az, StrF(az))
   
!}  

!function orientation(event){
!  var v_alpha = event.alpha;
!  var v_beta  = event.beta;
!  var v_gamma = event.gamma;
   SetGadgetText(#alpha, StrF(alpha))
   SetGadgetText(#beta, StrF(beta))
   SetGadgetText(#gamma, StrF(gamma))
!}

!function go() {

!	if(window.DeviceMotionEvent){
!		window.addEventListener("devicemotion", motion, false);
!	}
  
!	if(window.DeviceOrientationEvent){
!		window.addEventListener("deviceorientation", orientation, false);
!	}

!}

!go()

Re: deviceorientation events?

Posted: Mon Oct 31, 2016 5:34 am
by idle
Thanks, that helps a lot.
I'm still trying to work out how to syntactically mash it together or should I just stick to js!

Code: Select all

Enumeration
  #mainForm
 
  #ax 
  #ay
  #az
EndEnumeration

Declare motion()
Declare onExit()

OpenWindow(#mainForm, 0, 0, 0, 0, "", #PB_Window_Background)

TextGadget(-1, 10, 20, 300, 22, "<h2>Device Accelerometer</h2>")

TextGadget(-1, 50, 70, 50, 22, "x")
StringGadget(#ax, 100, 70, 200, 22, "0")

TextGadget(-1, 50, 100, 50, 22, "y")
StringGadget(#ay, 100, 100, 200, 22, "0")

TextGadget(-1, 50, 130, 50, 22, "z")
StringGadget(#az, 100, 130, 200, 22, "0")

Structure vec3 
  x.f
  y.f
  z.f
EndStructure 

Procedure GetEventAccelerationIncludingGravity(*m.vec3) 
  !p_m._x = event.accelerationIncludingGravity.x
  !p_m._y = event.accelerationIncludingGravity.y
  !p_m._z = event.accelerationIncludingGravity.z
EndProcedure   
  
Procedure OnMotion()
   Protected m.vec3 
   GetEventAccelerationIncludingGravity(@m) 
   SetGadgetText(#ax, StrF(m\x))
   SetGadgetText(#ay, StrF(m\y))
   SetGadgetText(#az, StrF(m\z))
EndProcedure    

Procedure SetDeviceMotionEventListner(*func) 
  Protected result 
  !if(window.DeviceMotionEvent) {
  !window.addEventListener("devicemotion",p_func, false);
  !v_result =1 
  !}
  ProcedureReturn result 
EndProcedure   

SetDeviceMotionEventListner(@OnMotion())