目次
前
次
ファイル処理
カラーセンサーの取得情報をファイルに保存し
後で解析できるように機能拡張してみます。
日付、時刻を取得するために使う関数は以下。
- year()
- month()
- day()
- hour()
- minute()
- second()
ファイル名に、日付を使うのなら、次のように記述します。
(文字列のデータ型はString)
String curday = nf(month(),2)+nf(day(),2);
ファイル作成には、PrintWriteオブジェクトを使います。
ファイルの型は、テキストファイルでよいので、ファイル
名を生成するには、次のように定義。
String sFileName = nf(month(),2)+nf(day(),2)+nf(hour(),2)+nf(minute(),2)+".txt" ;
データ保存は、次の処理でよいでしょう。
saveStrings(sFileName,cstmp);
どのタイミングで、ファイルにデータを保存するのかを
考えていきます。
スケッチを起動したときに、ファイルを自動作成すると
考えることが少なくなります。setup中で、日付を使った
ファイル名でファイルを作成します。
sFileName = nf(month(),2)+nf(day(),2)+nf(hour(),2)+nf(minute(),2)+".txt" ;
sensorFN = createWriter(sFileName);
lflag = false ;
cstmp = new String[0];
sFileNameはグローバル変数にしておき、どの
関数からでもアクセスできるようにします。
データ解析したいのは、連続でデータ取得して
並べたとき。そこで、executeをクリックして
フラグをセット。
フラグを利用してデータ保存かスキップするかを
判断できるようにしておきます。
フラグはひとつだけ用意。
boolean lflag
フラグlflagは、ボタンexecuteのクリックでセットし
ボタンidleのクリックでリセットします。
/* execute */
if ( isRangeOk(mouseX, 80,130) &&
isRangeOk(mouseY,300,320) ) {
/* debug */
println("Execute");
/* send command */
cport.write('E');
cport.write('\r');
/* set flag */
lflag = true ;
}
/* idle */
if ( isRangeOk(mouseX,140,190) &&
isRangeOk(mouseY,300,320) ) {
/* debug */
println("Idle");
/* send command */
cport.write('I');
cport.write('\r');
/* reset flag */
lflag = false ;
}
ボタンexecuteのクリックでは、データ保存開始の
時刻を入れて、後で解析するときにわかりやすく
しておくべきでしょう。定義は、以下。
if ( isRangeOk(mouseX, 80,130) &&
isRangeOk(mouseY,300,320) ) {
/* debug */
println("Execute");
/* send command */
cport.write('E');
cport.write('\r');
/* set flag */
lflag = true ;
/* generate time */
String tt = nf(hour(),2)+":"+nf(minute(),2)+":"+nf(second(),2) ;
/* store current time */
cstmp = append(cstmp,tt);
}
データは、String型の配列を用意して、ローカルエリア
に保存しておきます。ボタンidelをクリックしたときに
ファイルへと格納すればよいでしょう。
/* idle */
if ( isRangeOk(mouseX,140,190) &&
isRangeOk(mouseY,300,320) ) {
/* debug */
println("Idle");
/* send command */
cport.write('I');
cport.write('\r');
/* reset flag */
lflag = false ;
/* store */
saveStrings(sFileName,cstmp);
}
ファイル生成関数createWriterを利用した時点で、ファイルは
オープンされているので、クローズはボタンEXITのクリック時
とします。
/* Exit */
if ( mouseButton == RIGHT ) {
/* send command */
cport.write('I');
cport.write('\r');
store_status();
/* store */
sensorFN.close();
exit();
}
スケッチにまとめると以下。
import processing.serial.*;
Serial cport;
PImage imgGet ;
PImage imgExecute ;
PImage imgIdle ;
PImage imgExit ;
PImage imgSense ;
PImage imgXtime ;
PImage imgXmask ;
PImage imgGmask ;
int FRATE = 25 ;
String stmp ;
boolean scflag ;
int loop ;
int rxa ;
int gxa ;
int bxa ;
int ixa ;
int rxb ;
int gxb ;
int bxb ;
int ixb ;
String xsense ;
String xtime ;
String xmask ;
String gmask ;
boolean eflag ;
boolean oflag ;
boolean gflag ;
boolean lflag ;
PrintWriter outFN ;
PrintWriter sensorFN ;
String sFileName ;
String[] cstmp ;
boolean isRangeOk(int x,int bx,int ex)
{
boolean result ;
/* default */
result = false ;
/* judge */
if ( bx <= x && x <= ex ) { result = true ; }
return result ;
}
boolean isdigitx(char x)
{
boolean result ;
/* default */
result = false ;
/* judge */
if ( '0' <= x && x <= '9' ) { result = true ; }
return result ;
}
boolean isacode(char x)
{
boolean result ;
/* default */
result = false ;
/* judge */
if ( x == 'A' ) { result = true ; }
if ( x == 'B' ) { result = true ; }
if ( x == 'R' ) { result = true ; }
if ( x == 'G' ) { result = true ; }
if ( x == 'I' ) { result = true ; }
if ( x == ':' ) { result = true ; }
if ( x == ' ' ) { result = true ; }
return result ;
}
boolean isAcode(char x)
{
boolean result ;
/* default */
result = false ;
/* judge */
if ( isdigitx(x) && !isacode(x) ) {
result = true ;
}
return result ;
}
void show_values(int x,int y,int z,int u,int wx)
{
int sx = 20 ;
int sy = 80 ;
int lx ;
int ly ;
int offset = 30 ;
String msg_r ;
String msg_g ;
String msg_b ;
String msg_ix ;
/* generate text */
msg_r = "AR :";
msg_g = "AG :";
msg_b = "AB :";
msg_ix = "AIR:";
if ( wx == 1 ) {
msg_r = "BR :";
msg_g = "BG :";
msg_b = "BB :";
msg_ix = "BIR:";
}
msg_r += nf(x,5) ;
msg_g += nf(y,5) ;
msg_b += nf(z,5) ;
msg_ix += nf(u,5) ;
/* set location */
lx = sx ;
ly = sy ;
if ( wx == 1 ) { lx += 190 ; }
/* show */
textSize(15) ;
if ( gflag == true ) {
text(msg_g ,lx,ly+offset*1);
} else {
text(msg_r ,lx,ly+offset*0);
text(msg_g ,lx,ly+offset*1);
text(msg_b ,lx,ly+offset*2);
text(msg_ix,lx,ly+offset*3);
}
}
void show_bars(int x,int y,int z,int u,int wx)
{
int rgbi ;
int rate_r ;
int rate_g ;
int rate_b ;
int rate_i ;
int sx = 100 ;
int sy = 70 ;
int offset = 27 ;
int lx ;
int ly ;
/* calculate ratio */
rgbi = max(max(x,y),max(z,u)) ;
rate_r = (int)( (x * 100.0) / rgbi ) ;
rate_g = (int)( (y * 100.0) / rgbi ) ;
rate_b = (int)( (z * 100.0) / rgbi ) ;
rate_i = (int)( (u * 100.0) / rgbi ) ;
/* calcualte location */
lx = sx ;
ly = sy ;
if ( wx == 1 ) { lx = 290 ; }
/* show bar */
if ( gflag == true ) {
rate_g = 25 ;
fill( 0,255, 0); rect(lx,ly+offset*1,rate_g,20);
} else {
fill(255, 0, 0); rect(lx,ly+offset*0,rate_r,20);
fill( 0,255, 0); rect(lx,ly+offset*1,rate_g,20);
fill( 0, 0,255); rect(lx,ly+offset*2,rate_b,20);
fill(255,255,255); rect(lx,ly+offset*3,rate_i,20);
}
}
void show_green_bars(int x,int y)
{
int rra,rrb ;
/* default */
rra = 100 ; rrb = 100 ;
/* compare */
if ( x == y ) {
rra = 50 ; rrb = 50 ;
} else {
if ( x < y ) {
rra = (int)(x * 100.0 / y);
} else {
rrb = (int)(y * 100.0 / x);
}
}
/* draw */
fill( 0,255, 0); rect(100,70+27,rra,20);
fill( 0,255, 0); rect(290,70+27,rrb,20);
}
void show_btn()
{
image(imgGet , 20,300);
image(imgExecute, 80,300);
image(imgIdle ,140,300);
image(imgExit ,220,300);
image(imgSense ,300,200);
image(imgXtime ,300,240);
image(imgXmask ,300,280);
image(imgGmask ,300,320);
}
void show_caption_p()
{
/* white */
fill(255,255,255);
/* size */
textSize(12);
/* caption */
text("Sensivility",300,196);
text("Exposure",300,236);
text("Mask",300,276);
text("Green",300,316);
}
void clear_area()
{
/* black */
fill(0,0,0);
/* clear status area */
rect(390,200,100,140);
}
void show_caption()
{
clear_area();
/* white */
fill(255,255,255);
/* size */
textSize(12);
/* status */
text(xsense,400,214);
text(xtime ,400,254);
text(xmask ,400,294);
text(gmask ,400,334);
}
void send_sense_cmd(boolean x)
{
cport.write('S');
if ( x ) { cport.write('1'); }
else { cport.write('0'); }
cport.write('\r');
}
void send_exposure_cmd(int x)
{
if ( 0 < x && x < 5 ) {
cport.write('L');
if ( x == 1 ) { cport.write('1'); }
if ( x == 2 ) { cport.write('2'); }
if ( x == 3 ) { cport.write('3'); }
if ( x == 4 ) { cport.write('4'); }
cport.write('\r');
}
}
void send_mask_cmd(boolean x)
{
cport.write('M');
if ( x ) { cport.write('1'); }
else { cport.write('0'); }
cport.write('\r');
}
void store_status()
{
String stmp ;
/* clear */
stmp = "" ;
/* sensivility */
if ( xsense == "HIGH" ) { stmp += "1" ; }
else { stmp += "0" ; }
/* exposure time */
if ( xtime == "1" ) { stmp += "1" ; }
if ( xtime == "2" ) { stmp += "2" ; }
if ( xtime == "3" ) { stmp += "3" ; }
if ( xtime == "4" ) { stmp += "4" ; }
/* mask */
if ( xmask == "YES" ) { stmp += "1" ; }
else { stmp += "0" ; }
/* gmask */
if ( gmask == "YES" ) { stmp += "1" ; }
else { stmp += "0" ; }
println(stmp);
/* file handling */
outFN = createWriter("istatus.txt");
outFN.println(stmp);
outFN.close();
}
void setup()
{
size(440,340);
/* title caption */
surface.setTitle("Test 07");
/* select framerate */
frameRate(FRATE);
/* select back ground color with BLACK */
background(0,0,0);
//show serial port list (0:COM1 , 1:COM2 ... )
//println(Serial.list());
String arduinoPort = Serial.list()[4];
/* initialize serial port */
cport = new Serial(this,arduinoPort,9600);
cport.clear();
cport.bufferUntil('\n');
scflag = false ;
/* get image */
imgGet = loadImage("get.png");
imgExecute = loadImage("execute.png");
imgIdle = loadImage("idle.png");
imgExit = loadImage("exit.png");
imgSense = loadImage("sense.png");
imgXtime = loadImage("xtime.png");
imgXmask = loadImage("xmask.png");
imgGmask = loadImage("gmask.png");
/* set caption */
String[] xline = loadStrings("istatus.txt");
char[] ss = xline[0].toCharArray();
/* set status */
xsense = "LOW" ;
if ( ss[0] == '1' ) { xsense = "HIGH" ; }
xtime = "1" ;
if ( ss[1] == '2' ) { xtime = "2" ; }
if ( ss[1] == '3' ) { xtime = "3" ; }
if ( ss[1] == '4' ) { xtime = "4" ; }
xmask = "NO" ;
if ( ss[2] == '1' ) { xmask = "YES" ; }
gmask = "NO" ;
gflag = false ;
if ( ss[3] == '1' ) {
gmask = "YES" ;
gflag = true ;
}
/* first one shot */
oflag = true ;
/* file handling */
sFileName = nf(month(),2)+nf(day(),2)+nf(hour(),2)+nf(minute(),2)+".txt" ;
sensorFN = createWriter(sFileName);
lflag = false ;
cstmp = new String[0];
}
void draw()
{
/* first one shot */
if ( oflag ) {
oflag = false ;
/* sensivility */
if ( xsense == "HIGH" ) { send_sense_cmd(true); }
else { send_sense_cmd(false); }
/* exposure time */
send_exposure_cmd(int(xtime));
/* mask */
if ( xmask == "YES" ) { send_mask_cmd(true); }
else { send_mask_cmd(false); }
}
/* button */
show_btn();
/* caption */
show_caption_p();
show_caption();
/* serial receive */
if ( scflag == true ) {
scflag = false ;
/* check */
stmp = trim(cport.readStringUntil('\n'));
/* separate */
char[] ss = stmp.toCharArray();
loop = stmp.indexOf("BR :");
/* judge */
eflag = true ;
if ( loop < 40 ) { eflag = false ; }
/* store data in local storage */
if ( lflag == true && eflag == true ) {
cstmp = append(cstmp,stmp);
}
/* show */
if ( eflag ) {
/* clear area */
background(0,0,0);
/* show */
textSize(20);
if ( gflag == true ) {
String xstmp ;
int idx ;
xstmp = stmp.substring(0,loop-1) ;
idx = xstmp.indexOf("AG :");
text(xstmp.substring(idx,idx+10),20,40);
xstmp = stmp.substring(loop,stmp.length()) ;
idx = xstmp.indexOf("BG :");
text(xstmp.substring(idx,idx+10),20,60);
} else {
text(stmp.substring(0,loop-1),20,40);
text(stmp.substring(loop,stmp.length()),20,60);
}
/* clear */
loop = 0 ;
rxa = gxa = bxa = ixa = 0 ;
rxb = gxb = bxb = ixb = 0 ;
for (char ch : ss) {
/* check entry */
if ( ch == ':' ) { loop++ ; }
/* calculate */
if ( isAcode(ch) == true ) {
if ( loop == 1 ) { rxa = rxa * 10 + (ch-'0') ; }
if ( loop == 2 ) { gxa = gxa * 10 + (ch-'0') ; }
if ( loop == 3 ) { bxa = bxa * 10 + (ch-'0') ; }
if ( loop == 4 ) { ixa = ixa * 10 + (ch-'0') ; }
if ( loop == 5 ) { rxb = rxb * 10 + (ch-'0') ; }
if ( loop == 6 ) { gxb = gxb * 10 + (ch-'0') ; }
if ( loop == 7 ) { bxb = bxb * 10 + (ch-'0') ; }
if ( loop == 8 ) { ixb = ixb * 10 + (ch-'0') ; }
}
}
print('\n');
/* clear information */
fill(0,0,0);
rect(10,70,300,200);
fill(255,255,255);
/* show values */
show_values(rxa,gxa,bxa,ixa,0);
show_values(rxb,gxb,bxb,ixb,1);
/* draw bars */
int rate_rgbi ;
if ( gflag == true ) {
show_green_bars(gxa,gxb);
} else {
rate_rgbi = max(max(rxa,gxa),max(bxa,ixa));
if ( rate_rgbi > 0 ) { show_bars(rxa,gxa,bxa,ixa,0); }
rate_rgbi = max(max(rxb,gxb),max(bxb,ixb));
if ( rate_rgbi > 0 ) { show_bars(rxb,gxb,bxb,ixb,1); }
}
}
}
}
void mouseClicked()
{
if ( mouseButton == LEFT ) {
/* get */
if ( isRangeOk(mouseX, 20, 70) &&
isRangeOk(mouseY,300,320) ) {
/* debug */
println("Get");
/* send command */
cport.write('C');
cport.write('\r');
}
/* execute */
if ( isRangeOk(mouseX, 80,130) &&
isRangeOk(mouseY,300,320) ) {
/* debug */
println("Execute");
/* send command */
cport.write('E');
cport.write('\r');
/* set flag */
lflag = true ;
/* generate time */
String tt = nf(hour(),2)+":"+nf(minute(),2)+":"+nf(second(),2) ;
/* store current time */
cstmp = append(cstmp,tt);
}
/* idle */
if ( isRangeOk(mouseX,140,190) &&
isRangeOk(mouseY,300,320) ) {
/* debug */
println("Idle");
/* send command */
cport.write('I');
cport.write('\r');
/* reset flag */
lflag = false ;
/* store */
saveStrings(sFileName,cstmp);
}
/* exit */
if ( isRangeOk(mouseX,220,390) &&
isRangeOk(mouseY,300,320) ) {
/* send command */
cport.write('I');
cport.write('\r');
/* status */
store_status();
exit();
}
/* sensivility (HIGH) */
if ( isRangeOk(mouseX,301,339) &&
isRangeOk(mouseY,200,220) ) {
/* send command */
send_sense_cmd(true);
/* update */
xsense = "HIGH" ;
}
/* sensivility (LOW) */
if ( isRangeOk(mouseX,341,379) &&
isRangeOk(mouseY,200,220) ) {
/* send command */
send_sense_cmd(false);
/* update */
xsense = "LOW" ;
}
/* xtime (1) */
if ( isRangeOk(mouseX,301,319) &&
isRangeOk(mouseY,240,260) ) {
/* send command */
send_exposure_cmd(1);
/* update */
xtime = "1" ;
}
/* xtime (2) */
if ( isRangeOk(mouseX,321,339) &&
isRangeOk(mouseY,240,260) ) {
/* send command */
send_exposure_cmd(2);
/* update */
xtime = "2" ;
}
/* xtime (3) */
if ( isRangeOk(mouseX,341,359) &&
isRangeOk(mouseY,240,260) ) {
/* send command */
send_exposure_cmd(3);
/* update */
xtime = "3" ;
}
/* xtime (4) */
if ( isRangeOk(mouseX,361,379) &&
isRangeOk(mouseY,240,260) ) {
/* send command */
send_exposure_cmd(4);
/* update */
xtime = "4" ;
}
/* xmask (YES) */
if ( isRangeOk(mouseX,301,339) &&
isRangeOk(mouseY,280,300) ) {
/* send command */
send_mask_cmd(true);
/* update */
xmask = "YES" ;
}
/* xmask (NO) */
if ( isRangeOk(mouseX,341,379) &&
isRangeOk(mouseY,280,300) ) {
/* send command */
send_mask_cmd(false);
/* update */
xmask = "NO" ;
}
/* gmask (YES) */
if ( isRangeOk(mouseX,301,339) &&
isRangeOk(mouseY,320,340) ) {
/* update */
gmask = "YES" ;
gflag = true ;
}
/* gmask (NO) */
if ( isRangeOk(mouseX,341,379) &&
isRangeOk(mouseY,320,340) ) {
/* update */
gmask = "NO" ;
gflag = false ;
}
}
/* Exit */
if ( mouseButton == RIGHT ) {
/* send command */
cport.write('I');
cport.write('\r');
store_status();
/* store */
sensorFN.close();
exit();
}
}
void serialEvent(Serial p)
{
scflag = true ;
}
ファイル名は、月日時分を2けたずつ利用しているので
スケッチを起動、終了していれば同一となる確率は、小
さいです。
複数の条件で、それらを変えながらテストするときには
1分以上の測定を推奨します。
目次
前
次