<div class="se-component se-text se-l-default" id="SE-26bd43dc-20c7-4100-b2aa-5358c90388f0" style="margin: 0px; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-size: medium; line-height: inherit; font-family: se-nanumgothic, &quot;\\B098눔고딕&quot;, nanumgothic, sans-serif, Meiryo; vertical-align: baseline; position: relative; color: #3d4444; background-color: #ffffff;">
   <div class="se-component-content" style="margin: 0px auto; padding: 0px 40px; border: 0px; font: inherit; vertical-align: baseline; max-width: 100%;">
      <div class="se-section se-section-text se-l-default" style="margin: 0px; padding: 0px; border: 0px; font: inherit; vertical-align: baseline;">
         <div class="se-module se-module-text" style="margin: 0px; padding: 0px; border: 0px; font: inherit; vertical-align: baseline;">
            <div class="hgroup" style="margin: 50px auto 60px; padding: 0px 0px 20px; max-width: 860px; border-bottom: 1px solid rgb(235, 235, 235);">
               <h1 style="font-size: 1.6875em; margin: 0px 0px 9px; padding: 0px; line-height: 1.4444em;">Processing에서 시리얼통신으로 받은 데이터를 그래프로 표현하기</h1>
               <div class="post-meta" style="margin: 0px; padding: 0px; font-size: 0.8125em; color: #808080;"><span class="author">PinkWink</span>&nbsp;<span class="date">2013. 10. 23. 18:39</span></div>
               </div>
            <div class="entry-content" style="margin: 0px; padding: 0px; height: auto !important;">
               <div class="contents_style" style="margin: 0px; padding: 0px; height: auto !important;">
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">요즘 제가 MATLAB을 시작으로, Arduino, Python 등에서 시리얼 통신으로 데이터를 핸들링하는 아주 기초적인 글을 몇개 다뤘는데요. 이번에는 Processing이라고 하는 툴을 최근에 소개했었는데[<a href="http://pinkwink.kr/585" target="_blank" class="tx-link" style="color: #666666;">바로가기</a>] 그 Processing으에 관련된 글을 하나 더 다룰려고 합니다. 가지고 놀다보니 Processing도 많은 장점들이 있더라구요. 그것도 정말 쉽게 잘 쓸수 있구요. Processing에 관련된 기초적인 이야기는 공식홈페이지[<a href="http://www.processing.org/" target="_blank" class="tx-link" style="color: #666666;">바로가기</a>]만 가도 엄청 많으니 패스하구요.&nbsp;이번글의 목적은 Processing에서 시리얼 통신을 통해 들어온 데이터를 그래프로 표현하는 것입니다. 거기에 살짝 양념을 쳐서 GUI를 살짝 흉내도 내도록 하구 있구요. &nbsp;아무튼 뭐 그렇습니다.^^.</p>
                  <p class="polaroid" style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: center; clear: none; float: none;"><span class="imageblock" style="display: inline-block; height: auto; max-width: 100%;"><a href="https://pinkwink.kr/attachment/cfile27.uf@23704B34526780A617C86C.zip" style="color: #666666;" target="_blank"><img src="https://t1.daumcdn.net/tistory_admin/assets/blog/tistory-eff3d50340d1f7c29c7b8ddb6a26e5d491adc1df/blogs/image/extension/zip.gif?_version_=tistory-eff3d50340d1f7c29c7b8ddb6a26e5d491adc1df" alt="" style="border: none; vertical-align: middle; max-width: 100%; height: auto; margin-top: 36px;" />&nbsp;monitorNTARSv1.zip</a></span></p>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">일단 Processing에서 기본 지원하는 배포본인데요. 어차피 소스코드까지 열람이 가능하니까 큰 상관은 없을듯 합니다.</p>
                  <p class="polaroid" style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: center; clear: none; float: none;"><span class="imageblock" style="display: inline-block; width: 515px; height: auto; max-width: 100%;"><img src="https://t1.daumcdn.net/cfile/tistory/221B5434526780A304" width="515" filename="002.jpg" filemime="image/jpeg" style="border: none; vertical-align: top; max-width: 100%; height: auto; margin-top: 36px;" /></span></p>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">받은 압축화일을 풀고나면 있는 저 응용프로그램을 실행하시면 됩니다.</p>
                  <p class="polaroid" style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: center; clear: none; float: none;"><span class="imageblock" style="display: inline-block; width: 662px; height: auto; max-width: 100%;"><img src="https://t1.daumcdn.net/cfile/tistory/2333BD34526780A330" width="662" filename="003.jpg" filemime="image/jpeg" style="border: none; vertical-align: top; max-width: 100%; height: auto; margin-top: 36px;" /></span></p>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">바로 저기 있는 AVAILABLEPORTS 때문에 controlP5라는 라이브러리를 사용했습니다. 해당 라이브러리는 제가 이전글에서 언급[<a href="http://pinkwink.kr/585" target="_blank" class="tx-link" style="color: #666666;">바로가기</a>]했던 GUI 라이브러리입니다. 위에 표시된 부분은 Drop down list라고 하는데요. 살짝 코드를 한번 보죠.</p>
                  <div style="margin: 0px; padding: 0.2em 0.6em; color: #000000; font-family: &quot;Noto Sans KR&quot;, sans-serif; font-size: 16px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow: auto; width: auto; border-style: solid; border-color: gray; border-image: initial; border-width: 0.1em 0.1em 0.1em 0.8em;">
                     <pre style="overflow: auto; margin-top: 0px; margin-bottom: 0px; padding: 0px; word-break: break-all; white-space: pre-wrap; overflow-wrap: break-word; line-height: 16.25px;"><span style="color: #000080; font-weight: bold;">import</span> controlP5.*;
ControlP5 cp5;
DropdownList l;
<span style="color: #000080; font-weight: bold;">void</span> setup() {
  ... ... ...
  cp5 = <span style="color: #000080; font-weight: bold;">new</span> ControlP5(<span style="color: #000080; font-weight: bold;">this</span>);
  l = cp5.<span style="color: #ff0000;">addDropdownList</span>(<span style="color: #0000ff;">"AvailablePorts"</span>);
  ... ... ...
  comPortList(l);
}
<span style="color: #000080; font-weight: bold;">void</span> comPortList(DropdownList ddl) {
  availablePort = arsPort.<span style="color: #ff0000;">list</span>();
  ddl.<span style="color: #ff0000;">setPosition</span>(<span style="color: #0000ff;">10</span>, <span style="color: #0000ff;">70</span>);
  ddl.<span style="color: #ff0000;">setSize</span>(<span style="color: #0000ff;">80</span>, <span style="color: #0000ff;">60</span>);
  ddl.<span style="color: #ff0000;">setItemHeight</span>(<span style="color: #0000ff;">15</span>);
  ddl.<span style="color: #ff0000;">setBarHeight</span>(<span style="color: #0000ff;">15</span>);
  ddl.<span style="color: #ff0000;">setColorBackground</span>(ColorOfListBoxBackground);
  ddl.<span style="color: #ff0000;">setColorActive</span>(ColorOfListBoxActive);
  ddl.<span style="color: #ff0000;">setColorForeground</span>(ColorOfListBoxForeground);
  ddl.<span style="color: #ff0000;">setValue</span>(<span style="color: #0000ff;">0</span>);
  ddl.<span style="color: #ff0000;">captionLabel</span>().<span style="color: #ff0000;">toUpperCase</span>(<span style="color: #000080; font-weight: bold;">true</span>);
  ddl.<span style="color: #ff0000;">captionLabel</span>().<span style="color: #ff0000;">set</span>(<span style="color: #0000ff;">"AvailablePorts"</span>);
  ddl.<span style="color: #ff0000;">captionLabel</span>().<span style="color: #ff0000;">setColor</span>(ColorOfListBoxSetColor);  
  ddl.<span style="color: #ff0000;">captionLabel</span>().<span style="color: #ff0000;">style</span>().<span style="color: #ff0000;">marginTop</span> = <span style="color: #0000ff;">3</span>;
  ddl.<span style="color: #ff0000;">valueLabel</span>().<span style="color: #ff0000;">style</span>().<span style="color: #ff0000;">marginTop</span> = <span style="color: #0000ff;">3</span>; 
  <span style="color: #000080; font-weight: bold;">for</span> (<span style="color: #000080; font-weight: bold;">int</span> i=<span style="color: #0000ff;">0</span>; i&lt;availablePort.<span style="color: #ff0000;">length</span>; i++) {
    l.<span style="color: #ff0000;">addItem</span>(availablePort[i], i);
  }
}
<span style="color: #000080; font-weight: bold;">void</span> controlEvent(ControlEvent theEvent) {
  <span style="color: #000080; font-weight: bold;">if</span> (theEvent.<span style="color: #ff0000;">isGroup</span>() &amp;amp;&amp;amp; theEvent.<span style="color: #ff0000;">name</span>().<span style="color: #ff0000;">equals</span>(<span style="color: #0000ff;">"AvailablePorts"</span>)){
    <span style="color: #000080; font-weight: bold;">int</span> connectPortNo = (<span style="color: #000080; font-weight: bold;">int</span>)theEvent.<span style="color: #ff0000;">group</span>().<span style="color: #ff0000;">getValue</span>();
    connectPort = availablePort[connectPortNo];
  }
  <span style="color: #000080; font-weight: bold;">if</span> (!arsConnection) { 
    arsPort = <span style="color: #000080; font-weight: bold;">new</span> Serial(<span style="color: #000080; font-weight: bold;">this</span>, connectPort, <span style="color: #0000ff;">115200</span>); 
    arsConnection = <span style="color: #000080; font-weight: bold;">true</span>;
  }
  delay(<span style="color: #0000ff;">100</span>);  
  arsPort.<span style="color: #ff0000;">write</span>(<span style="color: #0000ff;">"&lt;CAH&gt;"</span>);    
}
</pre>
                     </div>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">위 코드가 controlP5에서 드롭다운 리스트를 다룬 부분인데요. controlP5를 import하구요. cp5로 선언하고, 드롭다운 리스트 변수를 l로 두었습니다. 그리고 setup()함수에서 실제 다루고 있는 부분만 void setup()내에서 두었습니다. 본 함수를 이야기하기 전에 전 드롭다운리스트에서 하고싶었던건 그저 현재 가용한 시리얼 포트의 리스트를 나열하고 싶었던 거에요. 그래서 comPortList()함수를 확인해보면, 15번행에서 시리얼 통신 라이브러리에서 제공하는 list()함수를 사용해서 가용한 포트를 받고, 그걸 addItem으로 리스트에 제공하는 부분입니다. 그 후에 controlEvent()함수에서 이벤트를 핸들링하고 있구요. 최근 시리얼 통신을 다루던 제 글은 다 그렇지만 뭔가 실습할 대상이 있어야하니 전 그걸 NT-ARSv1[<a href="http://pinkwink.kr/425" target="_blank" class="tx-link" style="color: #666666;">바로가기</a>]으로 잡은거에요. 그래서 그에 맞는 설정을 43번행에서 하고 있는 겁니다. 그리고 드롭다운 리스트에서 포트가 선택되면 ARS한테 각도를 넘겨 달라는 &lt;CAH&gt; 명령을 전송하는거죠.</p>
                  <p class="polaroid" style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: center; clear: none; float: none;"><span class="imageblock" style="display: inline-block; width: 419px; height: auto; max-width: 100%;"><img src="https://t1.daumcdn.net/cfile/tistory/24741D34526780A345" width="419" filename="004.jpg" filemime="image/jpeg" style="border: none; vertical-align: top; max-width: 100%; height: auto; margin-top: 36px;" /></span></p>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">그렇게 동작되는 모습이 위 그림입니다. 이제 다시 전체 화면 구성을 보면</p>
                  <p class="polaroid" style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: center; clear: none; float: none;"><span class="imageblock" style="display: inline-block; width: 662px; height: auto; max-width: 100%;"><img src="https://t1.daumcdn.net/cfile/tistory/230EDC34526780A209" width="662" filename="000.jpg" filemime="image/jpeg" style="border: none; vertical-align: top; max-width: 100%; height: auto; margin-top: 36px;" /></span></p>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">이런데요. 이런 화면을 구성하기 위해서 몇몇 기능들을 사용하고 있습니다. 먼저</p>
                  <div style="margin: 0px; padding: 0.2em 0.6em; color: #000000; font-family: &quot;Noto Sans KR&quot;, sans-serif; font-size: 16px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow: auto; width: auto; border-style: solid; border-color: gray; border-image: initial; border-width: 0.1em 0.1em 0.1em 0.8em;">
                     <pre style="overflow: auto; margin-top: 0px; margin-bottom: 0px; padding: 0px; word-break: break-all; white-space: pre-wrap; overflow-wrap: break-word; line-height: 16.25px;"><span style="color: #000080; font-weight: bold;">void</span> drawGraphPanel(){
  stroke(ColorOfListBoxForeground);
  strokeWeight(<span style="color: #0000ff;">1.5</span>);
  fill(color(<span style="color: #0000ff;">255</span>,<span style="color: #0000ff;">255</span>,<span style="color: #0000ff;">255</span>));
  rect(graphPanelXPos, graphPanelYPos, graphPanelXSize, graphPanelYSize); 
  fill(ColorOfBackground);
  rect(selectPanelXPos, selectPanelYPos, selectPanelXSize, selectPanelYSize);
  pauseBt.<span style="color: #ff0000;">drawButton</span>();
  stopBt.<span style="color: #ff0000;">drawButton</span>();
  rollAngBt.<span style="color: #ff0000;">drawButton</span>();
  pitchAngBt.<span style="color: #ff0000;">drawButton</span>();
  rollAngVelBt.<span style="color: #ff0000;">drawButton</span>();
  pitchAngVelBt.<span style="color: #ff0000;">drawButton</span>();
  strokeWeight(<span style="color: #0000ff;">1</span>);
  <span style="color: #000080; font-weight: bold;">for</span> (<span style="color: #000080; font-weight: bold;">int</span> i=(graphPanelYPos+panelGridYDivision); 
              i&lt;(graphPanelYPos+graphPanelYSize); i=i+panelGridYDivision) {
    <span style="color: #000080; font-weight: bold;">for</span> (<span style="color: #000080; font-weight: bold;">int</span> j=(graphPanelXPos+buttonXDiv);
              j&lt;(graphPanelXPos+graphPanelXSize); j=j+buttonXDiv) {
      line(j+panelGridHalfLength, i, j-panelGridHalfLength, i);
      line(j,i+panelGridHalfLength, j, i-panelGridHalfLength);   
    }
  }
  <span style="color: #000080; font-weight: bold;">int</span> tmpX = graphPanelXPos+graphPanelXSize/<span style="color: #0000ff;">10</span>;
  <span style="color: #000080; font-weight: bold;">for</span> (<span style="color: #000080; font-weight: bold;">int</span> i=<span style="color: #0000ff;">0</span>; i&lt;<span style="color: #0000ff;">9</span>; i++) {
    line(tmpX-panelGridHalfLength+i*graphPanelXSize/<span style="color: #0000ff;">10</span>, graphPanelYCenterPos, 
          tmpX+panelGridHalfLength+i*graphPanelXSize/<span style="color: #0000ff;">10</span>, graphPanelYCenterPos);
  }  
}
<span style="color: #000080; font-weight: bold;">void</span> drawText() {
  textFont(bigfontText);
  fill(<span style="color: #0000ff;">100</span>);  
  text(<span style="color: #0000ff;">"NT-ARSv1 Monitor"</span>,<span style="color: #0000ff;">10</span>,<span style="color: #0000ff;">35</span>);
  textFont(fontText);
  text(<span style="color: #0000ff;">"This Processing code is monitoring program for NT-ARSv1"</span>,<span style="color: #0000ff;">280</span>,<span style="color: #0000ff;">51</span>);
  text(<span style="color: #0000ff;">"NT-ARSv1 Monitoring Ver. 0.80 by PinkWink in NTRexLAB."</span>,<span style="color: #0000ff;">280</span>,<span style="color: #0000ff;">63</span>);
  <span style="color: #008800; font-style: italic;">//text("by PinkWink in http://pinkwink.kr/",420,75);</span>
  text(<span style="color: #0000ff;">"x division = 1 second"</span>,
    graphPanelXPos+graphPanelXSize/<span style="color: #0000ff;">2</span>-<span style="color: #0000ff;">50</span>,graphPanelYPos+graphPanelYSize+<span style="color: #0000ff;">15</span>);
  <span style="color: #000080; font-weight: bold;">for</span> (<span style="color: #000080; font-weight: bold;">int</span> i=<span style="color: #0000ff;">0</span>; i&lt;<span style="color: #0000ff;">7</span>; i++){
    text(graphYLabel[i], graphPanelXPos-<span style="color: #0000ff;">20</span>, graphPanelYPos+panelGridYDivision*i+<span style="color: #0000ff;">5</span>);
  }
  text(<span style="color: #0000ff;">"RollAngle"</span>, buttonXStartPos+<span style="color: #0000ff;">11</span>, buttonYPos+<span style="color: #0000ff;">5</span>);
  text(<span style="color: #0000ff;">"PitchAngle"</span>, buttonXStartPos+buttonXDiv+<span style="color: #0000ff;">11</span>, buttonYPos+<span style="color: #0000ff;">5</span>);
  text(<span style="color: #0000ff;">"RollAngVel"</span>, buttonXStartPos+buttonXDiv*<span style="color: #0000ff;">2</span>+<span style="color: #0000ff;">11</span>, buttonYPos+<span style="color: #0000ff;">5</span>);
  text(<span style="color: #0000ff;">"PitchAngVel"</span>, buttonXStartPos+buttonXDiv*<span style="color: #0000ff;">3</span>+<span style="color: #0000ff;">11</span>, buttonYPos+<span style="color: #0000ff;">5</span>);  
  text(<span style="color: #0000ff;">"Pause"</span>, graphPanelXPos+<span style="color: #0000ff;">13</span>, graphPanelYPos-<span style="color: #0000ff;">20</span>);
  text(<span style="color: #0000ff;">"STOP"</span>, graphPanelXPos+<span style="color: #0000ff;">93</span>, graphPanelYPos-<span style="color: #0000ff;">20</span>);    
}
</pre>
                     </div>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">인데요. drawText()는 화면에서 보이는 글자들을 모두 여기서 표현하고 있습니다. 뭐 원체 딱 봐도 될 정도로 쉽지요.^^. 그리고 drawGraphPanel()은 그래프를 그릴 곳의 경계선과 눈금등을 표시하고 있습니다. 여기서 하나가 뭐냐면 드롭다운리스트는 controlP5라는 라이브러리를 가져다 사용했지만, 나머지 6개의 버튼은 모두 직접 만들었습니다. 그게 10번부터 15번행에서 그걸 사용하고 있는건데요. 그렇게 만들어서 사용한 6개의 GUI 스러운 버튼은 클래스(class)로 선언했습니다. 그부분도 한번 보죠.</p>
                  <div style="margin: 0px; padding: 0.2em 0.6em; color: #000000; font-family: &quot;Noto Sans KR&quot;, sans-serif; font-size: 16px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow: auto; width: auto; border-style: solid; border-color: gray; border-image: initial; border-width: 0.1em 0.1em 0.1em 0.8em;">
                     <pre style="overflow: auto; margin-top: 0px; margin-bottom: 0px; padding: 0px; word-break: break-all; white-space: pre-wrap; overflow-wrap: break-word; line-height: 16.25px;"><span style="color: #000080; font-weight: bold;">class</span> ArsButtons {
  <span style="color: #000080; font-weight: bold;">int</span> posX, posY;
  String type;
  <span style="color: #000080; font-weight: bold;">boolean</span> buttonClicked;    
  color ColorOfClickedButton = color(<span style="color: #0000ff;">150</span>,<span style="color: #0000ff;">150</span>,<span style="color: #0000ff;">150</span>);    
  ArsButtons(<span style="color: #000080; font-weight: bold;">int</span> tmpX, <span style="color: #000080; font-weight: bold;">int</span> tmpY, <span style="color: #000080; font-weight: bold;">boolean</span> initValue, String tmpType) {
    buttonClicked = initValue;
    posX = tmpX;
    posY = tmpY;
    type = tmpType;
    drawButton();
  }
  <span style="color: #000080; font-weight: bold;">boolean</span> isButtonClicked(<span style="color: #000080; font-weight: bold;">int</span> X, <span style="color: #000080; font-weight: bold;">int</span> Y, <span style="color: #000080; font-weight: bold;">int</span> buttonArea) {
    <span style="color: #000080; font-weight: bold;">boolean</span> buttonClickedResult = <span style="color: #000080; font-weight: bold;">false</span>;
    <span style="color: #000080; font-weight: bold;">if</span> (type==<span style="color: #0000ff;">"rec"</span>) {
      <span style="color: #000080; font-weight: bold;">if</span>(X&lt;(posX+buttonArea) &amp;amp;&amp;amp; X&gt;posX &amp;amp;&amp;amp; Y&lt;(posY+buttonArea) &amp;amp;&amp;amp; Y&gt;posY) {
        buttonClickedResult = <span style="color: #000080; font-weight: bold;">true</span>; }
    } <span style="color: #000080; font-weight: bold;">else</span><span style="color: #000080; font-weight: bold;">if</span> (type==<span style="color: #0000ff;">"ell"</span>) {      
      <span style="color: #000080; font-weight: bold;">if</span>(X&lt;(posX+buttonArea/<span style="color: #0000ff;">2</span>) &amp;amp;&amp;amp; X&gt;(posX-buttonArea/<span style="color: #0000ff;">2</span>) 
          &amp;amp;&amp;amp; Y&lt;(posY+buttonArea/<span style="color: #0000ff;">2</span>) &amp;amp;&amp;amp; Y&gt;(posY-buttonArea/<span style="color: #0000ff;">2</span>)) {
            buttonClickedResult = <span style="color: #000080; font-weight: bold;">true</span>; }
    }
    <span style="color: #000080; font-weight: bold;">return</span> buttonClickedResult;
  }
  <span style="color: #000080; font-weight: bold;">void</span> buttonClick() {
    <span style="color: #000080; font-weight: bold;">if</span> (buttonClicked) {
      buttonClicked = <span style="color: #000080; font-weight: bold;">false</span>;
    } <span style="color: #000080; font-weight: bold;">else</span> {
      buttonClicked = <span style="color: #000080; font-weight: bold;">true</span>;
    }
  }  
  <span style="color: #000080; font-weight: bold;">void</span> drawButton() {
    fill(ColorOfBackground);
    <span style="color: #000080; font-weight: bold;">if</span> (type==<span style="color: #0000ff;">"rec"</span>) {
      rect(posX, posY, <span style="color: #0000ff;">10</span>, <span style="color: #0000ff;">10</span>);
      <span style="color: #000080; font-weight: bold;">if</span> (buttonClicked) { fill(ColorOfClickedButton); } 
      <span style="color: #000080; font-weight: bold;">else</span> { fill(ColorOfBackground); } 
      rect(posX+<span style="color: #0000ff;">2</span>, posY+<span style="color: #0000ff;">2</span>, <span style="color: #0000ff;">6</span>, <span style="color: #0000ff;">6</span>);
    } <span style="color: #000080; font-weight: bold;">else</span><span style="color: #000080; font-weight: bold;">if</span> (type==<span style="color: #0000ff;">"ell"</span>) {      
      ellipse(posX, posY, <span style="color: #0000ff;">10</span>, <span style="color: #0000ff;">10</span>);
      <span style="color: #000080; font-weight: bold;">if</span> (buttonClicked) { fill(ColorOfClickedButton); } 
      <span style="color: #000080; font-weight: bold;">else</span> { fill(ColorOfBackground); }
      ellipse(posX, posY, <span style="color: #0000ff;">6</span>, <span style="color: #0000ff;">6</span>);
    } 
  }
}
</pre>
                     </div>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">이렇습니다.^^ 전체적으로는 버튼은 사각형모양과 원형모양을 지원하구요. 버튼을 그릴 좌표와 크기를 입력하면됩니다만, 크기는 10으로 고정해야합니다. 비율을 생각안했거든요.ㅠㅠ. 그리고 내부에서는 버튼이 클릭되었는지 확인하느 isButtonClicked()함수와 클릭된 버튼의 속성을 변경하는 buttonClick()함수, 그리고 버튼을 그려주는 drawButton()함수를 가지고 있습니다. 이제 버튼을 마우스로 클릭하는 액션에 대한 함수가 필요하겠죠?^^</p>
                  <div style="margin: 0px; padding: 0.2em 0.6em; color: #000000; font-family: &quot;Noto Sans KR&quot;, sans-serif; font-size: 16px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow: auto; width: auto; border-style: solid; border-color: gray; border-image: initial; border-width: 0.1em 0.1em 0.1em 0.8em;">
                     <pre style="overflow: auto; margin-top: 0px; margin-bottom: 0px; padding: 0px; word-break: break-all; white-space: pre-wrap; overflow-wrap: break-word; line-height: 16.25px;"><span style="color: #000080; font-weight: bold;">void</span> mousePressed(){
  <span style="color: #000080; font-weight: bold;">if</span>(mouseButton==LEFT){
    <span style="color: #000080; font-weight: bold;">if</span>(rollAngBt.<span style="color: #ff0000;">isButtonClicked</span>(mouseX, mouseY, <span style="color: #0000ff;">10</span>)) {
      rollAngBt.<span style="color: #ff0000;">buttonClick</span>(); }
    <span style="color: #000080; font-weight: bold;">if</span>(pitchAngBt.<span style="color: #ff0000;">isButtonClicked</span>(mouseX, mouseY, <span style="color: #0000ff;">10</span>)) {
      pitchAngBt.<span style="color: #ff0000;">buttonClick</span>(); }
    <span style="color: #000080; font-weight: bold;">if</span>(rollAngVelBt.<span style="color: #ff0000;">isButtonClicked</span>(mouseX, mouseY, <span style="color: #0000ff;">10</span>)) {
      rollAngVelBt.<span style="color: #ff0000;">buttonClick</span>(); }
    <span style="color: #000080; font-weight: bold;">if</span>(pitchAngVelBt.<span style="color: #ff0000;">isButtonClicked</span>(mouseX, mouseY, <span style="color: #0000ff;">10</span>)) {
      pitchAngVelBt.<span style="color: #ff0000;">buttonClick</span>(); }
    <span style="color: #000080; font-weight: bold;">if</span>(pauseBt.<span style="color: #ff0000;">isButtonClicked</span>(mouseX, mouseY, <span style="color: #0000ff;">10</span>)) {
      pauseBt.<span style="color: #ff0000;">buttonClick</span>(); }
    <span style="color: #000080; font-weight: bold;">if</span>(stopBt.<span style="color: #ff0000;">isButtonClicked</span>(mouseX, mouseY, <span style="color: #0000ff;">10</span>)) {
      <span style="color: #000080; font-weight: bold;">if</span>(stopBt.<span style="color: #ff0000;">buttonClicked</span>) {
        stopBt.<span style="color: #ff0000;">buttonClick</span>();
        arsPort.<span style="color: #ff0000;">write</span>(<span style="color: #0000ff;">"&lt;CAH&gt;"</span>);
        delay(<span style="color: #0000ff;">20</span>);
      }<span style="color: #000080; font-weight: bold;">else</span> {
        stopBt.<span style="color: #ff0000;">buttonClick</span>(); 
        arsPort.<span style="color: #ff0000;">write</span>(<span style="color: #0000ff;">"&lt;CAE&gt;"</span>);
        delay(<span style="color: #0000ff;">20</span>);
      }
    }
  }
}
</pre>
                     </div>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">이겁니다. Processing은 기본적으로 마우스나 키보드 액션에 대한 함수를 준비해주고 있는데요. 마우스 좌클릭이 되었을때의 좌표가 버튼 좌표위에 있는지 확인해서 버튼이 클릭되었다고 알려주게 됩니다. 위에서 이야기한 클래스에서 관련함수가 있습니다. 이제 그래프를 그리는 부분을 보도록 하겠습니다.</p>
                  <p class="polaroid" style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: center; clear: none; float: none;"><span class="imageblock" style="display: inline-block; width: 664px; height: auto; max-width: 100%;"><img src="https://t1.daumcdn.net/cfile/tistory/23179934526780A307" width="664" filename="005.jpg" filemime="image/jpeg" style="border: none; vertical-align: top; max-width: 100%; height: auto; margin-top: 36px;" /></span></p>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">위 그림처럼 그래프를 표현하는데요. 기본적으로 500개(5초)의 이전데이터를 계속 유지하면서 그걸 그리도록 되어 있습니다. 새로 데이터가 오면 배열의 끝에 위치시키고, 첫 데이터는 지우는 형태로 만들었어요. 그리고 버튼을 클릭함에 따라 4개의 데이터중에서 하나이상을 선택할 수 있습니다.</p>
                  <p class="polaroid" style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: center; clear: none; float: none;"><span class="imageblock" style="display: inline-block; width: 660px; height: auto; max-width: 100%;"><img src="https://t1.daumcdn.net/cfile/tistory/231A1534526780A302" width="660" filename="006.jpg" filemime="image/jpeg" style="border: none; vertical-align: top; max-width: 100%; height: auto; margin-top: 36px;" /></span></p>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">그래프를 그리는 부분을 한분 보도록 하겠습니다.</p>
                  <div style="margin: 0px; padding: 0.2em 0.6em; color: #000000; font-family: &quot;Noto Sans KR&quot;, sans-serif; font-size: 16px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow: auto; width: auto; border-style: solid; border-color: gray; border-image: initial; border-width: 0.1em 0.1em 0.1em 0.8em;">
                     <pre style="overflow: auto; margin-top: 0px; margin-bottom: 0px; padding: 0px; word-break: break-all; white-space: pre-wrap; overflow-wrap: break-word; line-height: 16.25px;"><span style="color: #000080; font-weight: bold;">void</span> drawGraph() {
  noFill();  
  <span style="color: #000080; font-weight: bold;">if</span> (rollAngBt.<span style="color: #ff0000;">buttonClicked</span>) {
    stroke(ColorOfRollAngLine);
    strokeWeight(<span style="color: #0000ff;">2</span>);
    beginShape();
      <span style="color: #000080; font-weight: bold;">for</span> (<span style="color: #000080; font-weight: bold;">int</span> xPos=<span style="color: #0000ff;">0</span>; xPos&lt;rollAng.<span style="color: #ff0000;">length</span>; xPos++) {
        <span style="color: #000080; font-weight: bold;">float</span> tmp = saturatingValue(rollAng[xPos], <span style="color: #0000ff;">90</span>);        
        vertex(xPos+graphPanelXPos, graphPanelYCenterPos - tmp*resizingYSize);
      }
    endShape();
  }
  <span style="color: #000080; font-weight: bold;">if</span> (pitchAngBt.<span style="color: #ff0000;">buttonClicked</span>) {
    stroke(ColorOfPitchAngLine);
    strokeWeight(<span style="color: #0000ff;">2</span>);
    beginShape();
      <span style="color: #000080; font-weight: bold;">for</span> (<span style="color: #000080; font-weight: bold;">int</span> xPos=<span style="color: #0000ff;">0</span>; xPos&lt;pitchAng.<span style="color: #ff0000;">length</span>; xPos++) {
        <span style="color: #000080; font-weight: bold;">float</span> tmp = saturatingValue(pitchAng[xPos], <span style="color: #0000ff;">90</span>); 
        vertex(xPos+graphPanelXPos, graphPanelYCenterPos - tmp*resizingYSize);
      }
    endShape();
  }
  <span style="color: #000080; font-weight: bold;">if</span> (rollAngVelBt.<span style="color: #ff0000;">buttonClicked</span>) {
    stroke(ColorOfRollAngVelLine);
    strokeWeight(<span style="color: #0000ff;">1</span>);
    beginShape();
      <span style="color: #000080; font-weight: bold;">for</span> (<span style="color: #000080; font-weight: bold;">int</span> xPos=<span style="color: #0000ff;">0</span>; xPos&lt;rollAngVel.<span style="color: #ff0000;">length</span>; xPos++) {
        <span style="color: #000080; font-weight: bold;">float</span> tmp = saturatingValue(rollAngVel[xPos], <span style="color: #0000ff;">500</span>);
        vertex(xPos+graphPanelXPos, graphPanelYCenterPos - tmp*resizingAngVelYSize);
      }
    endShape();
  }
  <span style="color: #000080; font-weight: bold;">if</span> (pitchAngVelBt.<span style="color: #ff0000;">buttonClicked</span>) {
    stroke(ColorOfPitchAngVelLine);
    strokeWeight(<span style="color: #0000ff;">1</span>);
    beginShape();
      <span style="color: #000080; font-weight: bold;">for</span> (<span style="color: #000080; font-weight: bold;">int</span> xPos=<span style="color: #0000ff;">0</span>; xPos&lt;pitchAngVel.<span style="color: #ff0000;">length</span>; xPos++) {
        <span style="color: #000080; font-weight: bold;">float</span> tmp = saturatingValue(pitchAngVel[xPos], <span style="color: #0000ff;">500</span>);
        vertex(xPos+graphPanelXPos, graphPanelYCenterPos - tmp*resizingAngVelYSize);
      }
    endShape();
  }
}
<span style="color: #000080; font-weight: bold;">void</span> serialEvent(Serial p) {
  String arsValues = <span style="color: #0000ff;">""</span>;
  arsValues = arsPort.<span style="color: #ff0000;">readStringUntil</span>(<span style="color: #0000ff;">10</span>);
  <span style="color: #000080; font-weight: bold;">if</span> (!pauseBt.<span style="color: #ff0000;">buttonClicked</span> &amp;amp;&amp;amp; (arsValues != <span style="color: #000080; font-weight: bold;">null</span>)) {
      calAngles(arsValues);
  }  
}
<span style="color: #000080; font-weight: bold;">float</span> saturatingValue(<span style="color: #000080; font-weight: bold;">float</span> target, <span style="color: #000080; font-weight: bold;">float</span> limitValue) {
  <span style="color: #000080; font-weight: bold;">float</span> resizingResult;
  <span style="color: #000080; font-weight: bold;">float</span> tmp = abs(target);
  <span style="color: #000080; font-weight: bold;">if</span> (tmp&gt;limitValue) {
    resizingResult = target/tmp*limitValue;
  } <span style="color: #000080; font-weight: bold;">else</span> {
    resizingResult = target;
  }        
  <span style="color: #000080; font-weight: bold;">return</span> resizingResult;
}
<span style="color: #000080; font-weight: bold;">void</span> calAngles(String s) {
  <span style="color: #000080; font-weight: bold;">int</span> lastPosInString = s.<span style="color: #ff0000;">indexOf</span>(<span style="color: #a61717; background-color: #e3d2d2;">'</span>&gt;<span style="color: #a61717; background-color: #e3d2d2;">'</span>);
  s = s.<span style="color: #ff0000;">substring</span>(<span style="color: #0000ff;">1</span>, lastPosInString);
  <span style="color: #000080; font-weight: bold;">int</span> arsResultArray[] = <span style="color: #000080; font-weight: bold;">int</span>(split(s, <span style="color: #800080;">','</span>));
  rollAng = subset(rollAng, <span style="color: #0000ff;">1</span>);
  pitchAng = subset(pitchAng, <span style="color: #0000ff;">1</span>);
  rollAngVel = subset(rollAngVel, <span style="color: #0000ff;">1</span>);
  pitchAngVel = subset(pitchAngVel, <span style="color: #0000ff;">1</span>);
  rollAng = append(rollAng, <span style="color: #000080; font-weight: bold;">float</span>(arsResultArray[<span style="color: #0000ff;">0</span>])*<span style="color: #0000ff;">0.001</span>*<span style="color: #0000ff;">180</span>/PI);
  pitchAng = append(pitchAng, <span style="color: #000080; font-weight: bold;">float</span>(arsResultArray[<span style="color: #0000ff;">1</span>])*<span style="color: #0000ff;">0.001</span>*<span style="color: #0000ff;">180</span>/PI);
  rollAngVel = append(rollAngVel, <span style="color: #000080; font-weight: bold;">float</span>(arsResultArray[<span style="color: #0000ff;">2</span>])*<span style="color: #0000ff;">0.001</span>*<span style="color: #0000ff;">180</span>/PI);
  pitchAngVel = append(pitchAngVel, <span style="color: #000080; font-weight: bold;">float</span>(arsResultArray[<span style="color: #0000ff;">3</span>])*<span style="color: #0000ff;">0.001</span>*<span style="color: #0000ff;">180</span>/PI);   
}
</pre>
                     </div>
                  <p style="margin-bottom: 24px; padding: 0px; font-size: 0.9375em; line-height: 1.6; color: #666666; font-family: &quot;Noto Sans KR&quot;, sans-serif; text-align: justify;">일단 45번행의 시리얼 이벤트 함수는 시리얼통신으로 데이터가 들어오는 이벤트를 처리합니다. 거기서 calAngles()함수에 받은 데이터를 넘겨주죠. calAngles()함수는 사실 이번부터 시리얼통신에서 들어온 데이터를 핸들링하는 글에서 이야기를 했었습니다.&nbsp;그 중에서 아두이노에서 핸들링한 글이 가장 가깝습니다.[<a href="http://pinkwink.kr/582" target="_blank" class="tx-link" style="color: #666666;">바로가기</a>] 아무튼 NT-ARSv1의 출력데이터는 항상 &lt;&gt;로 쌓여져 있으니 &gt;의 위치를 알아내서 &lt;와 &gt;사이에 있는 값만 취하는 겁니다. 그걸 다시 split을 이용해서 ,를 기준으로 나눠서 저장하는거죠. 그 다음 문자열인 데이터를 숫자로 바꾸고 라디안을 일반적인 도로 바꾸는 작업들이 calAngles()함수입니다. 그리고 drawGraph()함수에서는 각각 500개의 크기를 가진 배열을 그리는 일을 합니다. 여기서 for문을 500까지 돌게하면 자주 에러가 납니다. 순간적으로 시리얼 이벤트의 문제나 내부적인 문제로 그런가본데요. 그래서 Processing 공식 홈페이지에서는 배열의 크기(length)를 반복문의 끝으로 잡아달라는 내용의 문서가 있습니다. 그래서 저도 for문은 7번처럼 이후에도 꾸몄습니다.&nbsp;나머지 satratingValue()함수는 그래프를 그릴때 한계치를 순간적으로 넘는경우가 보기 싫어서 만들어 둔 겁니다. 기초부터 설명하는건 요즘같은 세상에는 안맞는듯합니다. 왜냐면 이제는 공식홈페이지 조차 개념을 잘 잡을 수 있도록 설명되어 있어서 말이죠.^^. 이번 작업을 하면서 꽤나 Processing에 대해 흥미를 느꼈습니다. 사실 이번 글은 Processing이긴하지만, 그래픽 핸들링의 간편함이라는 주제를 가지고 작업을 하나 더 해볼까.. 하는 생각이 들더라구요^^. 아 프로그램이 동작하는 동영상 한번 보시죠^^</p>
                  <p><br /></p>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
<div class="se-component se-image se-l-default" id="SE-30efd92c-fad2-4d24-b5b7-e127b116db6e" style="margin: 20px 0px 0px; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-size: medium; line-height: inherit; font-family: se-nanumgothic, &quot;\\B098눔고딕&quot;, nanumgothic, sans-serif, Meiryo; vertical-align: baseline; position: relative; color: #3d4444; background-color: #ffffff;">
   <div class="se-component-content se-component-content-normal" style="margin: 0px auto; padding: 0px 40px; border: 0px; font: inherit; vertical-align: baseline; max-width: 100%;">
      <div class="se-section se-section-image se-l-default se-section-align-justify" style="margin: 0px; padding: 0px; border: 0px; font: inherit; vertical-align: baseline; max-width: 567px;">
         <div class="se-module se-module-image" style="margin: 0px; padding: 0px; border: 0px; font: inherit; vertical-align: baseline; position: relative;">
            <div class="autosourcing-stub-extra" style="margin: 0px; padding: 0px; border: 0px; font: inherit; vertical-align: baseline; zoom: 1; opacity: 1;">&nbsp;</div>
            </div>
         </div>
      </div>
   </div>