11. P/Invokeを使った
長~~~いコード(1)
• バイブレータを制御するNLed APIを
使うのでP/Invokeの定義を行います
Public Class Led
<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function NLedGetDeviceInfo(ByVal h As UInteger, _
ByRef pOutput As NLED_COUNT_INFO) As Boolean
End Function
<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function NLedSetDevice(ByVal h As UInteger, _
ByRef pOutput As NLED_SETTINGS_INFO) As Boolean
End Function
Private Const NLED_COUNT_INFO_ID As UInteger = 0
Private Const NLED_SETTINGS_INFO_ID As UInteger = 2
12. P/Invokeを使った
長~~~いコード(2)
• さきほど定義したNled APIに
渡す構造体を定義します
Private Structure NLED_SETTINGS_INFO
Public LedNum As UInteger
Public OffOnBlink As Integer
Public TotalCycleTime As Long
Public OnTime As Long
Public OffTime As Long
Public MetaCycleOn As Integer
Public MetaCycleOff As Integer
End Structure
Private Structure NLED_COUNT_INFO
Public cLeds As UInteger
End Structure
13. P/Invokeを使った
長~~~いコード (3)
Public Shared Function GetLedCount() As Integer
Dim info As New NLED_COUNT_INFO()
Dim LEDCount As Integer = 0
If NLedGetDeviceInfo(NLED_COUNT_INFO_ID, info) Then
LEDCount = CInt(info.cLeds)
End If
Return LEDCount
End Function
Public Shared Sub SetLedStatus(ByVal ledID As Integer, _
ByVal status As Integer)
Dim info As New NLED_SETTINGS_INFO()
With info
.LedNum = System.Convert.ToUInt32(ledID)
.OffOnBlink = System.Convert.ToUInt16(status)
End With
NLedSetDevice(NLED_SETTINGS_INFO_ID, info)
End Sub
End Class
14. P/Invokeを使った
長~~~いコード (4)
• バイブレートさせるための定義が終わったので、
ようやくバイブをさせることが出来ます
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button2.Click
' バイブレーション開始
LedVibrate.SetLedStatus(1, 1)
' 2秒間鳴動させた後、バイブを停止する
System.Threading.Thread.Sleep(2000)
LedVibrate.SetLedStatus(1, 0)
End Sub
16. OpenNETCFを使った
短くシンプルなコード
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
' バイブレーション開始
Dim led As New OpenNETCF.WindowsCE.Notification.Led()
led.SetLedStatus(1, OpenNETCF.WindowsCE.Notification.Led.LedState.On)
' 2秒間鳴動させた後、バイブを停止する
System.Threading.Thread.Sleep(2000)
led.SetLedStatus(1, OpenNETCF.WindowsCE.Notification.Led.LedState.Off)
End Sub
• これだけ。
21. LEDの点灯・消灯
• OpenNETCF.WindowsCE.Notification
名前空間
• Led クラス
– LEDの点灯を行ったり、LEDの情報を取得します
‘ ID2のLEDを点灯させます
Dim led As New OpenNETCF.WindowsCE.Notification.Led()
led.SetLedStatus(2, OpenNETCF.WindowsCE.Notification.Led.LedState.On)
– バイブレータに振られているIDを指定することで、
このLEDクラスでもバイブさせる事が出来ます