■ RGB LED 모듈을 사용하는 방법을 보여준다.
▶ 부품 내역
1 2 3 4 5 6 7 |
──────────────── 구분 모델 수량 비고 ─────── ── ── ── RGB LED Module 1 ──────────────── |
[회로 구성도]
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<Page x:Class="TestApplication.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="using:TestApplication" FontFamily="나눔고딕코딩" FontSize="16" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Ellipse x:Name="ledEllipse" Width="100" Height="100" Margin="10" Fill="LightGray" Stroke="White" /> <TextBlock x:Name="delayTextBlock" Margin="10" TextAlignment="Center" FontSize="24" Text="500ms" /> <Slider x:Name="delaySlider" Margin="10" Width="200" Maximum="1000" LargeChange="100" SmallChange="10" StepFrequency="10" Value="500" /> <TextBlock x:Name="messageTextBlock" Margin="10 50 10 10" TextAlignment="Center" FontSize="24" Text="GPIO 초기화 대기중..." /> </StackPanel> </Grid> </Page> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
using System; using System.Collections.Generic; using System.Diagnostics; using Windows.Devices.Gpio; using Windows.Security.ExchangeActiveSyncProvisioning; using Windows.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Media; namespace TestApplication { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration ////////////////////////////////////////////////////////////////////////////////////////// Public #region 장치 모델 - DeviceModel /// <summary> /// 장치 모델 /// </summary> public enum DeviceModel { /// <summary> /// 라즈베리 파이 /// </summary> RaspberryPi, /// <summary> /// 미노우 보드 맥스 /// </summary> MinnowBoardMax, /// <summary> /// 드래곤 보드 410 /// </summary> DragonBoard410, /// <summary> /// 알 수 없슴 /// </summary> Unknown }; #endregion #region LED 상태 - LEDStatus /// <summary> /// LED 상태 /// </summary> public enum LEDStatus { /// <summary> /// 빨강색 /// </summary> Red, /// <summary> /// 녹색 /// </summary> Green, /// <summary> /// 파랑색 /// </summary> Blue }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 빨강색 LED 핀 번호 /// </summary> private const int RED_LED_PIN_NUMBER = 5; /// <summary> /// 녹색 LED 핀 번호 /// </summary> private const int GREEN_LED_PIN_NUMBER = 13; /// <summary> /// 파랑색 LED 핀 번호 /// </summary> private const int BLUE_LED_PIN_NUMBER = 6; /// <summary> /// 빨강색 브러시 /// </summary> private SolidColorBrush redBrush = null; /// <summary> /// 녹색 브러시 /// </summary> private SolidColorBrush greenBrush = null; /// <summary> /// 파랑색 브러시 /// </summary> private SolidColorBrush blueBrush = null; /// <summary> /// LED 상태 /// </summary> private LEDStatus ledStatus; /// <summary> /// 빨강색 GPIO 핀 /// </summary> private GpioPin redGpioPin; /// <summary> /// 녹색 GPIO 핀 /// </summary> private GpioPin greenGpioPin; /// <summary> /// 파랑색 GPIO 핀 /// </summary> private GpioPin blueGpioPin; /// <summary> /// 타이머 /// </summary> private DispatcherTimer timer; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.redBrush = new SolidColorBrush(Colors.Red ); this.greenBrush = new SolidColorBrush(Colors.Green); this.blueBrush = new SolidColorBrush(Colors.Blue ); Loaded += Page_Loaded; this.delaySlider.ValueChanged += delaySlider_ValueChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 페이지 로드시 처리하기 - Page_Loaded(sender, e) /// <summary> /// 페이지 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Page_Loaded(object sender, RoutedEventArgs e) { GpioController gpioController = GpioController.GetDefault(); if(gpioController == null) { this.messageTextBlock.Text = "이 장치에 GPIO 컨트롤러가 없습니다.."; return; } DeviceModel deviceModel = GetDeviceModel(); if(deviceModel == DeviceModel.RaspberryPi) { this.redGpioPin = gpioController.OpenPin(RED_LED_PIN_NUMBER ); this.greenGpioPin = gpioController.OpenPin(GREEN_LED_PIN_NUMBER); this.blueGpioPin = gpioController.OpenPin(BLUE_LED_PIN_NUMBER ); } else { List<GpioPin> gpioPinList = new List<GpioPin>(3); for(int i = 0; i < gpioController.PinCount; i++) { switch(deviceModel) { case DeviceModel.DragonBoard410 : { if(i == 21 || i == 120) { continue; } break; } } GpioPin gpioPin; GpioOpenStatus gpioOpenStatus; if(gpioController.TryOpenPin(i, GpioSharingMode.Exclusive, out gpioPin, out gpioOpenStatus)) { gpioPinList.Add(gpioPin); if(gpioPinList.Count == 3) { break; } } } if(gpioPinList.Count != 3) { this.messageTextBlock.Text = "3개의 이용 가능한 핀들을 찾을 수 없습니다. 이 샘플은 3개의 GPIO 핀들이 필요합니다."; return; } this.redGpioPin = gpioPinList[0]; this.greenGpioPin = gpioPinList[1]; this.blueGpioPin = gpioPinList[2]; } this.redGpioPin.Write(GpioPinValue.High); this.redGpioPin.SetDriveMode(GpioPinDriveMode.Output); this.greenGpioPin.Write(GpioPinValue.High); this.greenGpioPin.SetDriveMode(GpioPinDriveMode.Output); this.blueGpioPin.Write(GpioPinValue.High); this.blueGpioPin.SetDriveMode(GpioPinDriveMode.Output); this.messageTextBlock.Text = string.Format ( "빨강색 핀 = {0}, 녹색 핀 = {1}, 파랑색 핀 = {2}", this.redGpioPin.PinNumber, this.greenGpioPin.PinNumber, this.blueGpioPin.PinNumber ); this.timer = new DispatcherTimer(); this.timer.Interval = TimeSpan.FromMilliseconds(500); this.timer.Tick += timer_Tick; this.timer.Start(); } #endregion #region 타이머 틱 처리하기 - timer_Tick(sender, e) /// <summary> /// 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void timer_Tick(object sender, object e) { FlipLED(); } #endregion #region 지연 슬라이더 값 변경시 처리하기 - delaySlider_ValueChanged(sender, e) /// <summary> /// 지연 슬라이더 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void delaySlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { if(this.timer == null) { return; } if(e.NewValue == this.delaySlider.Minimum) { this.delayTextBlock.Text = "Stopped"; this.timer.Stop(); TurnOffLED(); } else { this.delayTextBlock.Text = e.NewValue + "ms"; this.timer.Interval = TimeSpan.FromMilliseconds(e.NewValue); this.timer.Start(); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 장치 모델 구하기 - GetDeviceModel() /// <summary> /// 장치 모델 구하기 /// </summary> /// <returns>장치 모델</returns> private DeviceModel GetDeviceModel() { EasClientDeviceInformation easClientDeviceInformation = new EasClientDeviceInformation(); if(easClientDeviceInformation.SystemProductName.IndexOf("Raspberry", StringComparison.OrdinalIgnoreCase) >= 0) { return DeviceModel.RaspberryPi; } else if(easClientDeviceInformation.SystemProductName.IndexOf("MinnowBoard", StringComparison.OrdinalIgnoreCase) >= 0) { return DeviceModel.MinnowBoardMax; } else if(easClientDeviceInformation.SystemProductName == "SBC") { return DeviceModel.DragonBoard410; } else { return DeviceModel.Unknown; } } #endregion #region LED 깜박이기 - FlipLED() /// <summary> /// LED 깜박이기 /// </summary> private void FlipLED() { Debug.Assert(this.redGpioPin != null && this.blueGpioPin != null && this.greenGpioPin != null); switch(this.ledStatus) { case LEDStatus.Red : { this.redGpioPin.Write (GpioPinValue.High); this.blueGpioPin.Write (GpioPinValue.Low ); this.greenGpioPin.Write(GpioPinValue.Low ); this.ledEllipse.Fill = this.redBrush; this.ledStatus = LEDStatus.Green; // 다음 상태를 설정한다. break; } case LEDStatus.Green : { this.redGpioPin.Write (GpioPinValue.Low ); this.greenGpioPin.Write(GpioPinValue.High); this.blueGpioPin.Write (GpioPinValue.Low ); this.ledEllipse.Fill = this.greenBrush; this.ledStatus = LEDStatus.Blue; // 다음 상태를 설정한다. break; } case LEDStatus.Blue : { this.redGpioPin.Write (GpioPinValue.Low ); this.greenGpioPin.Write(GpioPinValue.Low ); this.blueGpioPin.Write (GpioPinValue.High); this.ledEllipse.Fill = this.blueBrush; this.ledStatus = LEDStatus.Red; // 다음 상태를 설정한다. break; } } } #endregion #region LED 끄기 - TurnOffLED() /// <summary> /// LED 끄기 /// </summary> private void TurnOffLED() { this.redGpioPin.Write (GpioPinValue.Low); this.greenGpioPin.Write(GpioPinValue.Low); this.blueGpioPin.Write (GpioPinValue.Low); } #endregion } } |