10. .NET Gadgeteer ハンズオン資料
2.2.3 USB クライアントモジュールの接続
ボードにもよりますが、メインボードを PC と接続するには USB クライアントモジュールが必要です。
USB クライアントが必要なメインモジュールの例
FEZ Spider, FEZ Hydra, FEZ Cerberus など
USB クライアントが不要なメインモジュールの例
FEZ Cerbuino NET, FEZ Cobra II など
USB クライアントが不要なメインモジュールは USB mini コネクタを持っているのですぐに分かります。
10
13. .NET Gadgeteer ハンズオン資料
3. 以下のコードを記述します。
using Microsoft.SPOT;
using GT = Gadgeteer;
namespace DebugLedTest
{
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
var timer = new GT.Timer(1000);
timer.Tick += timer_Tick;
timer.Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window during
debugging.
Debug.Print("Program Started");
}
void timer_Tick(GT.Timer timer)
{
PulseDebugLED();
}
}
}
4. PC とボード(USB クライアントモジュール)とを USB ケーブルで接続します。
5. Visual Studio で F5 キーを押す、または [開始] ボタンを押します。
6. 1000 ミリ秒 (= 1 秒) ごとにメインボード上の LED が点滅します。また Visual Studio の [出力] タブにメッセー
ジが表示されます。
13
14. .NET Gadgeteer ハンズオン資料
3.2 ハンズオン:メインボード上の LED を操作する
このプログラムの目的:メインボード上の LED はデバッグ用途以外にも利用できることを理解します。
このハンズオンでは以下のボードとモジュールを使用します。
任意のメインボード
メインボード上の LED はデバッグ用途の DebugPulseLED メソッド以外にも利用できます。例えばハードウェアの
初期化終了や不定期な処理の実行を確認する用途などに利用できます。
1. 任意のメインボードで新しいプロジェクトを新規作成します。プロジェクト名はここでは “BoardLedDemo” とし
ます。
2. “Program.cs” を開いて以下のコードを記述します。
14
15. .NET Gadgeteer ハンズオン資料
using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
namespace BoardLedDemo
{
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
var timer = new GT.Timer(1000);
timer.Tick += timer_Tick;
timer.Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window during
debugging.
Debug.Print("Program Started");
}
private bool _isLightOn = false;
void timer_Tick(GT.Timer timer)
{
_isLightOn = !_isLightOn;
Mainboard.SetDebugLED(_isLightOn);
}
}
}
15
18. .NET Gadgeteer ハンズオン資料
4.3 ハンズオン:タイマーでポーリング
このプログラムの目的:タイマーで定期的にポーリングする方法を理解します。
このハンズオンではメインボードの他に以下のモジュールを使用します。
LED7R ・・・6 個の LED が円形に等間隔に並び、中央にもう 1 個の LED が置かれたモジュール
最初にタイマーでポーリングして処理を繰り返すアプリケーションを作ってみます。
なお本書ですでに実施した、メインボード上のデバッグ用 LED を点滅させるコードも実際にはタイマーでポーリン
グする方法で繰り返し処理を実現していました。
1. Visual Studio で新しいプロジェクトを作成します。プロジェクト名はここでは “LedBlinkTimer” とします。メイ
ンボードは本書では Spider を使用します。モジュールは以下のように配置します。
18
19. .NET Gadgeteer ハンズオン資料
2. “Program.cs” を開いて、以下のコードを記述します。
using Microsoft.SPOT;
using GT = Gadgeteer;
namespace LedBlinkTimer
{
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
var timer = new GT.Timer(1000);
timer.Tick += timer_Tick;
timer.Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window
during debugging.
Debug.Print("Program Started");
}
private bool _isLightOn = false;
void timer_Tick(GT.Timer timer)
{
_isLightOn = !_isLightOn;
if (_isLightOn)
led7r.TurnLightOn(7);
else
led7r.TurnLightOff(7);
}
}
}
3. デバイスを接続して実行します。LED7R の中央の LED が 1 秒間隔で点滅します。
19
22. .NET Gadgeteer ハンズオン資料
2.
“Program.cs” を開いて、以下のコードを記述します。
using System.Threading;
using Microsoft.SPOT;
namespace LedBlinkThread
{
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
new Thread(Blink).Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window
during debugging.
Debug.Print("Program Started");
}
private void Blink()
{
bool isLightOn = false;
while (true)
{
isLightOn = !isLightOn;
if (isLightOn)
led7r.TurnLightOn(7);
else
led7r.TurnLightOff(7);
Thread.Sleep(1000);
}
}
}
}
22
26. .NET Gadgeteer ハンズオン資料
2. “Program.cs” を開いて、以下のコードを記述します。
using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
namespace JoystickLed
{
public partial class Program
{
private const double MinRadius = 0.5;
private const double Tan60 = 1.7320508075;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
var timer = new GT.Timer(100);
timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
timer.Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window
during debugging.
Debug.Print("Program Started");
}
void timer_Tick(GT.Timer timer)
{
var pos = joystick.GetPosition();
double x = pos.X;
double y = pos.Y;
TurnLightOn(x, y);
Debug.Print("x=" + x.ToString() + ", y=" + y.ToString());
}
26
27. .NET Gadgeteer ハンズオン資料
void TurnLightOn(double x, double y)
{
if (x * x + y * y < MinRadius * MinRadius)
{
led7r.TurnLightOn(7, true);
}
else
{
if (x == 0)
led7r.TurnLightOn(y > 0 ? 1 : 4, true);
else if (x > 0 && y >= 0)
led7r.TurnLightOn(y / x <= Tan60 ? 2 : 1,
else if (x < 0 && y >= 0)
led7r.TurnLightOn(y / -x > Tan60 ? 1 : 6,
else if (x < 0 && y < 0)
led7r.TurnLightOn(y / x <= Tan60 ? 5 : 4,
else /* x > 0 && y < 0 */
led7r.TurnLightOn(-y / x > Tan60 ? 4 : 3,
}
}
true);
true);
true);
true);
}
}
3. デバイスを接続して実行します。ジョイスティックの位置に応じて LED7R モジュールの周囲の LED が点灯しま
す。
27
28. .NET Gadgeteer ハンズオン資料
ジョイスティックモジュールは GetPosition メソッドで位置情報を返します。x 方向、y 方向とも -1.0~1.0 の値を
返します。ジョイスティックはイベントを発行しないためタイマーでポーリングします。今回は 100 ミリ秒のタイマーに
しましたが用途に応じて間隔を調整するといいでしょう。
LED7R モジュールは 12 時の方向の LED が 1 番で、時計回りに 2 番, 3 番, ・・・ と 6 番まで並んでいます。中
央が 7 番 LED です。今回は 2, 3, 5, 6 番 LED を点灯する条件として tan 60 (=ルート 3) が分かればいいだけな
ので、毎回三角関数で計算することはせずに tan 60 を定数で持ちます。
ジョイスティックモジュールはボタンの役割も持っています。クリックして確かめてみてください。
ボタンクリックについては JoystickPressed イベントおよび JoystickReleased イベントがありますので、余裕のある
方は ボタンが押された時の処理を追加してみてください。
例えば、ProgramStarted メソッドは以下のように変更してみます。網掛けの部分が追加するコードです。
void ProgramStarted()
{
joystick.JoystickPressed += joystick_JoystickPressed;
joystick.JoystickReleased += joystick_JoystickReleased;
var timer = new GT.Timer(100);
timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
timer.Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window
during debugging.
Debug.Print("Program Started");
}
28
31. .NET Gadgeteer ハンズオン資料
2. “Program.cs” を開いて、以下のコードを記述します。
using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
namespace LightSensorLed
{
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
var timer = new GT.Timer(100);
timer.Tick += timer_Tick;
timer.Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window during
debugging.
Debug.Print("Program Started");
}
void timer_Tick(GT.Timer timer)
{
var light = lightSensor.ReadLightSensorPercentage();
SetLight(light < 50);
}
private void SetLight(bool lightOn)
{
if (lightOn)
led7r.TurnLightOn(7);
else
led7r.TurnLightOff(7);
}
}
}
31
32. .NET Gadgeteer ハンズオン資料
3. デバイスを接続して実行します。周囲が暗くなると LED が点灯します。動作確認の際は、照度センサーを
手で覆ったり室内照明に向けたりして動作確認してください。
ReadLightSensorPercengate メソッドは、(センサーで取得できる) もっとも明るい状態の状態で 100 を、もっ
とも暗い状態で 0 を返します。読み取ったデータに応じて LED を点けたり消したりする様子が分かると思い
ます。
これを応用すると、暗くなると自動的に点灯する照明を実現することができます。ハンズオンでは小さな LED
なので照明の代わりにはなりませんが、出力を変えることでより明るい照明を点けられます。
32
34. .NET Gadgeteer ハンズオン資料
1. Visual Studio で新しいプロジェクトを作成します。プロジェクト名はここでは “AnimationLed” とします。メイン
ボードは本書では Spider を使用します。モジュールは以下のように配置します。
2.
“Program.cs” を開いて、以下のコードを記述します。
using
using
using
using
System.Threading;
Microsoft.SPOT;
GT = Gadgeteer;
GTM = Gadgeteer.Modules;
namespace AnimationLed
{
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
new Thread(ReadSense).Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window during
debugging.
Debug.Print("Program Started");
}
34
35. .NET Gadgeteer ハンズオン資料
private void ReadSense()
{
var th = new Thread(AnimateLed);
th.Start();
th.Suspend();
var animated = false;
while (true)
{
var light = lightSensor.ReadLightSensorPercentage();
if (light < 50 && !animated)
{
animated = true;
th.Resume();
}
else if (light >= 50 && animated)
{
th.Suspend();
animated = false;
for (int i = 1; i <= 6; i++)
{
led7r.TurnLightOff(i);
}
}
Thread.Sleep(100);
}
}
private void AnimateLed()
{
while (true)
{
led7r.Animate(100, true, true, true);
led7r.Animate(100, true, false, false);
}
}
}
}
35
38. .NET Gadgeteer ハンズオン資料
1. Visual Studio で新しいプロジェクトを作成します。プロジェクト名はここでは “SensorToCharDisp” とします。
メインボードは本書では Spider を使用します。モジュールは以下のように配置します。
2. “Program.cs” を開いて、以下のコードを記述します。
using
using
using
using
System.Threading;
Microsoft.SPOT;
GT = Gadgeteer;
GTM = Gadgeteer.Modules;
namespace SensorToCharDisp
{
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
private void ProgramStarted()
{
char_Display.SetBacklight(true);
new Thread(ReadSense).Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window
during debugging.
Debug.Print("Program Started");
}
38
39. .NET Gadgeteer ハンズオン資料
private void ReadSense()
{
var th = new Thread(AnimateLed);
th.Start();
th.Suspend();
var animated = false;
while (true)
{
var light = lightSensor.ReadLightSensorPercentage();
var threshold = potentiometer.ReadPotentiometerPercentage() * 100;
DisplayValues(light, threshold);
if (light < threshold && !animated)
{
animated = true;
th.Resume();
}
else if (light >= threshold && animated)
{
th.Suspend();
animated = false;
for (int i = 1; i <= 6; i++)
{
led7r.TurnLightOff(i);
}
}
Thread.Sleep(100);
}
}
39
43. .NET Gadgeteer ハンズオン資料
2. “Program.cs” を開いて、以下のコードを記述します。
using
using
using
using
Microsoft.SPOT;
GT = Gadgeteer;
GTM = Gadgeteer.Modules;
Gadgeteer.Modules.GHIElectronics;
namespace MonitorCamera
{
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
camera.PictureCaptured += camera_PictureCaptured;
var timer = new GT.Timer(200);
timer.Tick += timer_Tick;
timer.Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window
during debugging.
Debug.Print("Program Started");
}
void timer_Tick(GT.Timer timer)
{
if (camera.CameraReady)
camera.TakePicture();
}
void camera_PictureCaptured(Camera sender, GT.Picture picture)
{
display_T35.SimpleGraphics.DisplayImage(picture, 0, 0);
}
}
}
43
47. .NET Gadgeteer ハンズオン資料
2. “Program.cs” を開いて、以下のコードを記述します。
using
using
using
using
using
using
using
System.Threading;
Microsoft.SPOT;
Microsoft.SPOT.Presentation.Media;
Microsoft.SPOT.Presentation.Shapes;
GT = Gadgeteer;
GTM = Gadgeteer.Modules;
Gadgeteer.Modules.GHIElectronics;
namespace MonitorCamera2
{
public partial class Program
{
private Bitmap _cameraBitmap;
private Rectangle _rectangle;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
InitCamera();
InitWindow();
camera.StartStreamingBitmaps(_cameraBitmap);
// Use Debug.Print to show messages in Visual Studio's "Output" window
during debugging.
Debug.Print("Program Started");
}
47
50. .NET Gadgeteer ハンズオン資料
メインボードは Spider のみ使用可能です。WiFi モジュールが乗った Corba II では多少コードを変更することで
同様のデバイスが作れます。有線 LAN が乗ったメインボードや有線 LAN モジュール を使用してもいいでしょう。
1. Visual Studio で新しいプロジェクトを作成します。プロジェクト名はここでは “WeatherStation” とします。メイ
ンボードは本書では Spider を使用します。モジュールは以下のように配置します。一つ前のハンズオンを試
した方は、モジュールをつなぎ替える必要はありません。
50
51. .NET Gadgeteer ハンズオン資料
2. “Program.cs” を開いて、以下のコードを記述します。
using
using
using
using
using
using
using
using
using
System;
System.Threading;
Microsoft.SPOT;
Gadgeteer.Networking;
GT = Gadgeteer;
GTM = Gadgeteer.Modules;
Gadgeteer.Modules.Seeed;
GHINET = GHI.Premium.Net;
System.Text;
namespace WeatherStation
{
public partial class Program
{
private WebEvent _barometerEvent;
private double _pressure;
private double _temperature;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
var timer = new GT.Timer(5000);
timer.Tick += timer_Tick;
timer.Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window
during debugging.
Debug.Print("Program Started");
}
51