今回は UDP 通信。DesignWave 誌 Cortex-M3 基板の Gainer - Processing のデモに含まれていたマウス操作で立方体を動かすプログラムをちょいと書き換えてみた。Gainer ではシリアル通信であったが、こちらは UDP 通信でデータを取得している。ColdFire 基板からデータが送られてくると、基板の傾きに応じて画面上の立方体が動く。
まず、オリジナルの ad プログラムを x, y 両方を出力するように変更する。単にふたつの数値をスペースを挟んでつなげただけ。こちらがクライアントで、使い方はオリジナルと同じ。telnet で SilentC に接続して ad::udpsend("PC側IPアドレス") とする。
type ad adinit(void){ InitAd(0x70); } disp(char ch){ PrNum(GetAd(ch)); } udpsend(char *str){ char *soc,*buf1,*buf2; long ip; ip=GetIP(str); if(ip==0)return; buf1=MemoryAlloc(12); buf2=MemoryAlloc(12); soc=CreateSocket(0); InitAd(0x70); /* #stop 0 */ for(;;){ GetDigit(GetAd(4),buf1); GetDigit(GetAd(5),buf2); StrCat(buf1," "); StrCat(buf1,buf2); SendTo(soc,ip,30049,buf1,StrLen(buf1)); if(Getc(0)=='q') break; } CloseSocket(soc); MemoryFree(buf); MemoryFree(buf2); MemoryFree(buf1); }
受信(サーバ)側の Processing プログラム。UDP 通信ライブラリは Processing に標準では含まれていないから http://ubaa.net/shared/processing/udp/ から udp.zip をダウンロード、展開して udp フォルダを丸ごと libraries フォルダ以下にコピーして使う。
/** * RGB Cube for IF-ColdFire board * masato at bird.email.ne.jp * * The three primary colors of the additive color model are red, green, and blue. * This RGB color cube displays smooth transitions between these colors. */ /** * NOTE: * Modified from the original to control Z and X axis by a mouse. * Replace the mouseX and mouseY by a physical controller. */ import hypermedia.net.*; UDP udp; void setup() { size(400, 400, P3D); noStroke(); colorMode(RGB, 1); udp = new UDP( this, 30049 ); //udp.log( true ); // printout the connection activity udp.listen( true ); } void draw() {;} void receive( byte[] data, String ip, int port ) { { int x, y; String message = new String(data); String p[] = splitTokens(message); x = int(p[0]); y = int(p[1]); background(0.5, 0.5, 0.45); pushMatrix(); translate(width/2, height/2, -30); rotateZ((x/float(width) - 0.5) * PI); rotateX((y/float(height) - 0.5) * PI); scale(100); beginShape(QUADS); fill(0, 1, 1); vertex(-1, 1, 1); fill(1, 1, 1); vertex( 1, 1, 1); fill(1, 0, 1); vertex( 1, -1, 1); fill(0, 0, 1); vertex(-1, -1, 1); fill(1, 1, 1); vertex( 1, 1, 1); fill(1, 1, 0); vertex( 1, 1, -1); fill(1, 0, 0); vertex( 1, -1, -1); fill(1, 0, 1); vertex( 1, -1, 1); fill(1, 1, 0); vertex( 1, 1, -1); fill(0, 1, 0); vertex(-1, 1, -1); fill(0, 0, 0); vertex(-1, -1, -1); fill(1, 0, 0); vertex( 1, -1, -1); fill(0, 1, 0); vertex(-1, 1, -1); fill(0, 1, 1); vertex(-1, 1, 1); fill(0, 0, 1); vertex(-1, -1, 1); fill(0, 0, 0); vertex(-1, -1, -1); fill(0, 1, 0); vertex(-1, 1, -1); fill(1, 1, 0); vertex( 1, 1, -1); fill(1, 1, 1); vertex( 1, 1, 1); fill(0, 1, 1); vertex(-1, 1, 1); fill(0, 0, 0); vertex(-1, -1, -1); fill(1, 0, 0); vertex( 1, -1, -1); fill(1, 0, 1); vertex( 1, -1, 1); fill(0, 0, 1); vertex(-1, -1, 1); endShape(); popMatrix(); }
SilentC の CGI と HTTP クライアントを使うことも考えたが、やはりこちらのほうがすっきりする。
[2008.08.07]
orangebook さんでも同じようなことを試されているが、基板の傾きをそのまま再現しているのが面白い。
おそらく携帯電話等からは投稿できません。日本語文字列を含まないコメントやトラックバック、および当サイトへの言及を含まないトラックバックは御遠慮いただいております。また、90日以上経過した記事へのコメントはできません。