■ CHIRP 라이브러리를 사용하는 방법을 보여준다.
▶ MainWindow.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 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 |
<Window x:Class="TestProject.MainWindow" 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" mc:Ignorable="d" Width="600" Height="450" Title="CHIRP 라이브러리 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="19"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Button x:Name="startButton" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" Width="100" Height="30" Content="시작하기" Click="startButton_Click" /> <Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" Content="상태" /> <TextBlock x:Name="stateTextBlock" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" FontWeight="Bold" Foreground="Red" Text="Not Created" /> <Label Grid.Row="2" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" Content="송신 메시지" /> <TextBox x:Name="sendMessageTextBox" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="5" Height="25" BorderThickness="1" BorderBrush="Black" VerticalContentAlignment="Center" Text="테스트 문자열" /> <Button x:Name="sendButton" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" Width="100" Height="30" Click="sendButton_Click" IsEnabled="False" Content="송신하기" /> <Label Grid.Row="4" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" Content="수신 데이터" /> <TextBox x:Name="receiveMessageTextBox" Grid.Row="4" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="5" Height="25" BorderThickness="1" BorderBrush="Black" VerticalContentAlignment="Center" IsEnabled="False" /> <Label Grid.Row="5" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" Content="버전" /> <TextBlock x:Name="versionTextBlock" Grid.Row="5" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" Foreground="Blue" Text="" /> <Label Grid.Row="6" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" Content="정보" /> <TextBlock x:Name="informationTextBlock" Grid.Row="6" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" Foreground="Blue" TextWrapping="Wrap" Text="" /> </Grid> </Window> |
▶ MainWindow.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 |
using System; using System.ComponentModel; using System.Text; using System.Windows; using System.Windows.Media; using ChirpConnectNet; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 키 /// </summary> private const string KEY = "[여기에 본인의 키를 설정해 주시기 바랍니다.]"; /// <summary> /// 시크릿 /// </summary> private const string SECRET = "[여기에 본인의 시크릿을 설정해 주시기 바랍니다.]"; /// <summary> /// CHIRP 연결 /// </summary> private ChirpConnect connect; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); #region 연결을 설정한다. try { this.connect = new ChirpConnect(KEY, SECRET, null); } catch(Exception exception) { this.startButton.IsEnabled = false; this.sendButton.IsEnabled = false; this.sendMessageTextBox.IsEnabled = false; ShowErrorMessage(exception.Message); return; } #endregion #region 이벤트를 설정한다. Closing += Window_Closing; this.connect.OnStateChanged += connect_OnStateChanged; this.connect.OnReceived += connect_OnReceived; this.connect.OnUnsupportedCaptureDevice += connect_OnUnsupportedCaptureDevice; this.connect.OnUnsupportedRendererDevice += connect_OnUnsupportedRendererDevice; #endregion Application.Current.Dispatcher.BeginInvoke(new Action(() => { this.versionTextBlock.Text = this.connect.Version; this.informationTextBlock.Text = this.connect.Info; this.stateTextBlock.Text = this.connect.State.ToString(); })); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 닫을 경우 처리하기 - Window_Closing(sender, e) /// <summary> /// 윈도우 닫을 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Closing(object sender, CancelEventArgs e) { try { if(this.connect.State != SDKState.Stopped) { this.connect.Stop(); } this.connect.Dispose(); } catch(Exception exception) { ShowErrorMessage(exception.Message); } } #endregion #region 연결 상태 변경시 처리하기 - connect_OnStateChanged(previousState, newState) /// <summary> /// 연결 상태 변경시 처리하기 /// </summary> /// <param name="previousState">이전 상태</param> /// <param name="newState">신규 상태</param> private void connect_OnStateChanged(SDKState previousState, SDKState newState) { Brush color = Brushes.Blue; if(newState == SDKState.Sending || newState == SDKState.Receiving) { color = Brushes.LightGreen; } else if(newState == SDKState.Running) { color = Brushes.Black; } else if(newState == SDKState.Stopped) { color = Brushes.Red; } Application.Current.Dispatcher.BeginInvoke(new Action(() => { this.stateTextBlock.Foreground = color; this.stateTextBlock.Text = newState.ToString(); })); } #endregion #region 연결 수신시 처리하기 - connect_OnReceived(messageByteArray, channel) /// <summary> /// 연결 수신시 처리하기 /// </summary> /// <param name="messageByteArray">메시지 바이트 배열</param> /// <param name="channel">채널</param> private void connect_OnReceived(byte[] messageByteArray, uint channel) { if(messageByteArray.Length > 0) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { string message = Encoding.UTF8.GetString(messageByteArray); this.receiveMessageTextBox.Text = message; })); } else { Application.Current.Dispatcher.BeginInvoke(new Action(() => { this.receiveMessageTextBox.Text = "메시지를 해석할 수 없습니다. 볼륨을 높여 주시기 바랍니다."; })); } } #endregion #region 연결 캡처 장치 미지원시 처리하기 - connect_OnUnsupportedCaptureDevice() /// <summary> /// 연결 캡처 장치 미지원시 처리하기 /// </summary> private void connect_OnUnsupportedCaptureDevice() { Application.Current.Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show ( this, "선택된 캡처 장치가 지원되지 않습니다. 다른 장치를 선택해 주시기 바랍니다.", "확인", MessageBoxButton.OK, MessageBoxImage.Information ); })); } #endregion #region 연결 렌더러 장치 미지원시 처리하기 - connect_OnUnsupportedRendererDevice() /// <summary> /// 연결 렌더러 장치 미지원시 처리하기 /// </summary> private void connect_OnUnsupportedRendererDevice() { Application.Current.Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show ( this, "선택된 렌더러 장치가 지원되지 않습니다. 다른 장치를 선택해 주시기 바랍니다.", "확인", MessageBoxButton.OK, MessageBoxImage.Information ); })); } #endregion #region 시작하기 버튼 클릭시 처리하기 - startButton_Click(sender, e) /// <summary> /// 시작하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void startButton_Click(object sender, RoutedEventArgs e) { if(this.startButton.Content.ToString() == "시작하기") { ChirpError chirpError = this.connect.Start(AudioMode.PlayingListening); if(chirpError != null) { ShowErrorMessage("시작시 에러 : " + chirpError.ToString()); return; } this.sendButton.IsEnabled = true; this.startButton.Content = "중단하기"; } else if(this.startButton.Content.ToString() == "중단하기") { ChirpError chirpError = this.connect.Stop(); if(chirpError != null) { ShowErrorMessage("중단시 에러 : " + chirpError); return; } this.sendButton.IsEnabled = false; this.startButton.Content = "시작하기"; } } #endregion #region 송신하기 버튼 클릭시 처리하기 - sendButton_Click(sender, e) /// <summary> /// 송신하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void sendButton_Click(object sender, RoutedEventArgs e) { if(string.IsNullOrEmpty(this.sendMessageTextBox.Text)) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show ( this, "송신 메시지 항목을 입력해 주시기 바랍니다.", "확인", MessageBoxButton.OK, MessageBoxImage.Information ); })); } else { string message = this.sendMessageTextBox.Text; byte[] byteArray = Encoding.UTF8.GetBytes(message); ChirpError chirpError = this.connect.Send(byteArray); if(chirpError != null) { ShowErrorMessage("송신시 에러 : " + chirpError.ToString()); } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 에러 메시지 보여주기 - ShowErrorMessage(message) /// <summary> /// 에러 메시지 보여주기 /// </summary> /// <param name="message">메시지</param> private void ShowErrorMessage(string message) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { this.stateTextBlock.Foreground = Brushes.Red; this.stateTextBlock.Text = message; })); } #endregion } } |
※ 32비트 버전 컴파일만 가능하다.