Windows IoT:创客的世界地图
本帖最后由 ∮仗剑ノ天涯 于 2016-1-18 21:57 编辑
在这个工程中,你将使用基于 Raspberry Pi 2 的 ***链接停止解析***创造一个根据 Web API 调用响应来使 LED 灯闪烁的工程。
准备工作:
[list=1]
[*]Raspberry Pi 2
[*]560欧姆的电阻 x1
[*]面包板 x1
[*]LED灯 x1
[*]公/母杜邦线若干
[*]源代码(***链接停止解析***)
[/list]
硬件设置:
把 Raspberry Pi 2 连接到面包板和其他组件,如下图所示:
***附件停止解析***
注意:LED 的引脚已经由之前***链接停止解析***中的 GPIO 5 变为了GPIO 18。
代码:
MainPage.cs
你可以从 ***链接停止解析*** 下载代码初始工程,我们将会引导你使用这些额外的代码,在和 Web 服务通信以及在地图上得到你的标记时,需要这些代码。关于更多地图的信息,***链接停止解析***。
打开”Lesson_201\StartSolution\Lesson_201.sln”,然后再打开”MainPage.xaml.cs”文件。
在这个解决方案中,我们已经罗列了一些方法来让你入门。如果你想跳过这部分,你可以在”Lesson_201\FullSolution\Lesson_201.sln”下找到包含所有代码的解决方案。
在 MainPage 类的开头添加这些代码。其定义了你将会在 Pi2 上使用哪些引脚来控制 LED 和一个 InternetLed 类的引用,你将会在稍后添加这个引用。
[mw_shl_code=csharp,true]public sealed partial class MainPage : Page
{
// which GPIO pin do we want to use to control the LED light
const int GPIOToUse = 18;
// The class which wraps our LED.
InternetLed internetLed;
public MainPage()[/mw_shl_code]
现在在 OnNavigatedTo 方法中添加代码,这将会:
1.创建一个新的 InternetLed 对象;
2.同过调用 Web API 来在 Maker Map 上添加我们的标记;
3.初始化对象;
4.通过调用 Web API 来得到我们的延时值;
5.通过返回的延迟值循环100次,在每次循环中闪烁一次 LED;
如果你不想在地图上增加标记,移除”MakePinWebAPICall();”
[mw_shl_code=csharp,true]// This method will be called by the application framework when the page is first loaded.
protected override async void OnNavigatedTo(NavigationEventArgs navArgs)
{
Debug.WriteLine("MainPage::OnNavigatedTo");
MakePinWebAPICall();
try
{
// Create a new InternetLed object
internetLed = new InternetLed(GPIOToUse);
// Initialize it for use
internetLed.InitalizeLed();
// Now have it make the web API call and get the led blink delay
int blinkDelay = await internetLed.GetBlinkDelayFromWeb();
for (int i = 0; i < 100; i++)
{
internetLed.Blink();
await Task.Delay(blinkDelay);
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}[/mw_shl_code]
InternetLed.cs
现在你将会添加一个新的类文件,实际上在这个类文件当中将会执行大多数任务。
在主菜单上选择 Project -> Add Class...;
”Add New Item”对话框将会弹出,并默认为 Visual C# 类;
把文件命名为”InternetLed.cs”,然后点击”Add”;
这个类文件将会被创建并打开。
更新 using's
你将需要在文件的开头更新 using's 部分,以便代码可以引用 GPIO 设备、Web 接口和系统诊断。
[mw_shl_code=csharp,true]using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using Windows.Devices.Gpio;
namespace Lesson_201
{[/mw_shl_code]
类成员
在类括号内增加下列的代码,其将会在不久后被代码引用。
[mw_shl_code=csharp,true]class InternetLed
{
// GPIO controller code
private GpioController gpio;
private GpioPin LedControlGPIOPin;
private int LedControlPin;
// only used if we don't get a response from the webapi call.
private const int DefaultBlinkDelay = 1000;
// An enumeration to store the state of the led
public enum eLedState { Off, On };
private eLedState _LedState; [/mw_shl_code]
类构造函数
现在,添加类构造函数代码,其将会存储引脚的值,这些值将会被用于控制 LED。
[mw_shl_code=csharp,true]public InternetLed(int ledControlPin)
{
Debug.WriteLine("InternetLed::New InternetLed");
// Store the selected GPIO control pin id for use when we initialize the GPIO
LedControlPin = ledControlPin;
}[/mw_shl_code]
初始化成员
添加初始化代码,这一部分将负责建立和 Pi2 GPIO 控制器的通信。
[mw_shl_code=csharp,true]public void InitalizeLed()
{
Debug.WriteLine("InternetLed::InitalizeLed");
// Now setup the LedControlPin
gpio = GpioController.GetDefault();
LedControlGPIOPin = gpio.OpenPin(LedControlPin);
LedControlGPIOPin.SetDriveMode(GpioPinDriveMode.Output);
// Get the current pin value
GpioPinValue startingValue = LedControlGPIOPin.Read();
_LedState = (startingValue == GpioPinValue.Low) ? eLedState.On : eLedState.Off;
}[/mw_shl_code]
LedState 属性
这一部分将会把表示 LED 状态(开或者关)的交互信息送入中央位置。
[mw_shl_code=csharp,true]// A public property for interacting with the LED from code.
public eLedState LedState
{
get { return _LedState; }
set
{
Debug.WriteLine("InternetLed::LedState::set " + value.ToString());
if (LedControlGPIOPin != null)
{
GpioPinValue newValue = (value == eLedState.On ? GpioPinValue.High : GpioPinValue.Low);
LedControlGPIOPin.Write(newValue);
_LedState = value;
}
}
} [/mw_shl_code]
Blink 方法
添加一个改变 LED 状态的方法。
[mw_shl_code=csharp,true]// Change the state of the led from on to off or off to on
public void Blink()
{
if (LedState == eLedState.On)
{
LedState = eLedState.Off;
}
else
{
LedState = eLedState.On;
}
} [/mw_shl_code]
GetBlinkDelayFromWeb 方法
这里将是这个类的主体工作完成的地方。
通过调用 HttpClient 开始,然后执行它。接下来输出返回的字符串到 Debug 通道,这样你就可以看到它的值了。
接下来,我们来确定延时的值是多少并返回它。
[mw_shl_code=csharp,true]// Let us know what the returned string was
Debug.WriteLine(String.Format("Response string: [{0}]", responseString));
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
int delay;
if (!int.TryParse(responseString, out delay))
{
delay = DefaultBlinkDelay;
}
// return the blink delay
return delay;
}
}
}[/mw_shl_code]
实战一下试试
一旦输入所有的代码,你可以创建一个解决方案并在你的 Pi2 上运行这些代码。如果需要的话,你可以查看这些指令是如何在 Pi2 上连接和运行的。
打开 Visual Studio 的输出标签。如果需要的话,在主菜单上使用 Debug -> Window -> Output。
现在你可以查看 Debug.WriteLine 命令的输出了。
找到以”Response string:”开头的消息,紧接它们的是来自 Web API 的响应,其被”[]”中括号包围。
[mw_shl_code=csharp,true]MainPage::OnNavigatedTo
InternetLed::New InternetLed
InternetLed::InitalizeLed
InternetLed::MakeWebApiCall
Response string: [100]
InternetLed::LedState::set Off[/mw_shl_code]
闪烁 LED
这个时候你的 LED 应该大约每秒钟闪烁一次,这由从 Web API 返回的时间字符串的值决定。
标签: Windows
为防止不可控的内容风险,本站已关闭新用户注册,新贴的发表及评论;
你现在看到的内容只是互联网用户曾经发表的言论快照,仅用于老用户留存纪念,且仅与科技行业相关,全部内容不代表本站观点及立场;
本站重新开放前已针对包括用户隐私、版权保护、信息安全、国家政策在内的各种互联网法律法规要求,执行了隐患内容的自查、屏蔽和删除;
本站目前所属个人主体,未有任何盈利安排与计划,且与原WFUN.COM所属公司不存在任何关联关系;
如果本帖内容或者相关资源侵犯到您的合法权益,或者您认为存在问题,那么请您务必点此举报或投诉!