注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

震动事件Android shake event with PhoneSensors(二)  

2014-09-06 12:39:03|  分类: B4A例程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
-------------------------------------------- 下面是代码模块Shake


'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
Dim tw As TextWriter
Dim recording As Boolean
Type AxisData (flipCount As Int, lastPeak As Float,lastValue As Float, timeStamp As Long)
Dim AxisX As AxisData
Dim MagnitudeThreshold As Float
Dim TimeThreshold As Int
MagnitudeThreshold = 11
TimeThreshold = 1500 'milliseconds
Dim CallBackActivity As String
End Sub
Sub StartRecording(Dir As String, FileName As String)
tw.Initialize(File.OpenOutput(Dir, FileName, False))
recording = True
End Sub
Sub EndRecording
tw.Close
recording = False
End Sub
Sub HandleSensorEvent(values() As Float)
If recording Then
tw.Write(values(0) & "," & values(1) & "," & values(2) & Chr(13) & Chr(10)) 'we are not using CRLF as we want the Windows end of line characters
Else
CalcAxis(values(0), AxisX)
End If
End Sub
Sub CalcAxis(v As Float, axis As AxisData)
Dim difference As Float
difference = v - axis.lastValue
axis.lastValue = v
If Abs(difference) > MagnitudeThreshold Then
If DateTime.Now - axis.timeStamp > TimeThreshold Then
axis.Initialize 'reset the data
End If
If axis.flipCount < 0 Then 
Log("Shake.CalcAxis - still waiting")
Return 'this will happen immediately after a "shake" event.
End If
If axis.lastPeak = 0 OR (axis.lastPeak < 0 AND difference > 0) OR (axis.lastPeak > 0 AND difference < 0) Then
axis.flipCount = axis.flipCount + 1
axis.lastPeak = difference
axis.timeStamp = DateTime.Now
If axis.flipCount = 2 Then
CallSub(CallBackActivity, "ShakeEvent")
'To avoid repetitive events:
axis.flipCount = -10
End If
Else
axis.timeStamp = DateTime.Now
End If
End If
End Sub
  评论这张
 
阅读(70)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017