====== DSair BASIC (DSbasic) ====== Keywords: BASIC, Programming, プログラミング, 教育, Education, Maker, DIY, interpreter, 【お知らせ】DSbasicはバグ報告は受け付けますが、使用方法については一切サポートしません。 [[DSair2_ukeng]] ([[DSair2|JPN]]) DSbasic is a very easy programming solution on your smartphone! This function works in DSair2's webapp on your browser.\\ DSbasicは、スマートフォン(Android,iOS)上で動作する、鉄道模型制御向けに最適化されたBASIC開発環境です。命令を打ち込んで実行するだけで、お好きな自動運転や、運転支援機能を実現できます。BASICファイルは、SDカードに置けば、ファイル一覧からロードできます。修正したプログラムは、スマホのメモリに保存できます。 {{http://buin2gou.sakura.ne.jp/sblo_files/powerele/image/DSbasic_21-thumbnail2.png}}\\ [[http://buin2gou.sakura.ne.jp/sblo_files/powerele/image/DSbasic_21.png|Large Image]] ===== Features ===== BASIC is very easy programming environment. DSbasic is implemented in Webapp control software. You can control automatically DCC and Marklin Locomotives on your smartphone. * BASIC language powered by [[https://github.com/google/wwwbasic|wwwbasic]] BASIC interpreter engine. * Works in smartphone's browser * Seperated Editor and Display. Easy to use. * [[https://ace.c9.io/|ACE]] based powerful text editor on browser. * Support Locomotive and Turnouts control via BASIC language * MP3 Play suported in MP3 files of FlashAir SD card. ===== BASIC Function ===== ==== DSair2 Unique Functions ==== LocAddr is Locomotive address. Default protocol is DCC. If you want to control Marklin's, configure protocol on web app. ^Function ^Parameter ^Notes ^ |PWR| TrackPower | ON=1, OFF=0 | |SPD| LocAddr,Speed | LocAddr=0-9999, Speed=0-1023 | |ACC| AccAddr, Direction| diverse=0, straight=1 | |DIR| LocAddr, Direction| FWD=1, REV=2 | |FNC| LocAddr, FuncNo, FuncON/OFF| FuncNo=0-28, FuncON/OFF=0(OFF) or 1(ON) | |FNX| LocAddr, FuncNo| FuncNo=0-28,ファンクションのOFF->ON->ON操作をします。 | |MP3PLAY| FileName with Path | MP3PLAY "Horn.mp3" | |MP3STOP| - | MP3STOP | |GETACC| AccAddr(1-2044) |指定アドレスのポイント状態(0=div, or 1=str)を返します。 | |GETACCW| AccAddr(1-2044) |指定アドレス以降16個分のポイント状態(0=div, or 1=str)を返します。 | |getslotaddr| SlotNo(0-3) | 4つのユーザー指定アドレススロットからアドレスを取得します。 | |getslotspd| SlotNo(0-3) | 4つのユーザー指定アドレススロットから速度を取得します。 | |getslotfnc| SlotNo(0-3), FuncNo(0-28) | 4つのユーザー指定アドレススロットからファンクション状態(0=OFF,1=ON)を返します。 | |getslotfncw| SlotNo(0-3) | 4つのユーザー指定アドレススロットからファンクション状態を29bit分返します。 | |S88START| - | S88機能を有効にします。S88の関数を使う場合は必ずBASICプログラムの最初に記述して下さい。括弧はつけないでください。 | |S88GET,GETS88| S88Sensor address(1-16) | S88センサーの値(0=OFF,1=ON)を返します。 | |GETS88W| S88Sensor address(1-16) | S88センサーの値を16bit分返します。 | |DCPWM| PWMDuty(0-1023) | | |DCDIR| Direction| FWD=1, REV=2 | The parameters of function mean as the followings.\\ 引数の意味は以下の通りです。 |AccAddr| 1-2044 |ポイントアドレス,MM2の場合は1-320| |LocAddr| 1-9999 |機関車アドレス,MM2の場合は1-255。| |FuncNo | 0-28 | ファンクション番号| |FuncON/OFF | 0-1 | 0=OFF, 1=ON | |Direction | 1-2| 0 or 1=FWD, 2=REV | BASIC functions are described in [[https://github.com/google/wwwbasic|wwwbasic]] user manual. ==== Main functions ==== * FOR * NEXT * IF * GOTO * END IF * ELSE * WHILE * WEND * CALL * ==== Drawing functions ==== * PRINT * COLOR * LOCATE * LINE * PAINT * CIRCLE * PUT * SCREEN * CLS * WIDTH * PALETTE * ==== Math functions ==== asc, sqr, int, cint abs, cos, sin, tan atn, exp, log, rnd, atan2 ==== Text functions ==== * val 文字を数値に変換 * str$ 数値を文字列に変換 * ltrim$ 左側文字列を抜き出す * rtrim$ 右側文字列を抜き出す * left$ * right$ * mid$ * string$ * space$ スペースを抜き出す * tab * ucase$ 英字を大文字にする * lcase$ 英字を小文字にする * mid$ * ==== Key Input functions ==== * inkey$ * input ==== Mouse Input functions ==== * getmouse * ==== Etc functions ==== * timer * SLEEP * swap * ===== Examples ===== ==== Shuttle train ==== pwr 1 spd 69,0 fnc 69,0,1 fnc 69,1,1 dir 69,1 mp3play "Horn.mp3" FOR i = 1 to 10 COLOR i, 0 PRINT "Speed=" + i*10 spd 69,i*10 sleep 500 NEXT i FOR i = 1 to 10 COLOR i, 0 PRINT "Speed=" + (100-i*10) spd 69,100-i*10 sleep 500 NEXT i spd 69,0 dir 69,0 ==== Turnout automatic control ==== if getacc(1)=1 then if getacc(2)=0 then print "route=A" acc 2,1 acc 3,1 end if end if ==== Locomotive automatic control ==== 連動記事は[[|こちら]]。 screen(7) print "Start BASIC Shuttle Control" pwr 1 print "Power on" fnc 69,0,1 fnc 69,1,1 print "F0,F1 on" dir 69,1 fnx 69,2 print "Go FWD!" spd 69,0 sleep 6000 spd 69,100 sleep 8000 print "Run 200/1023" spd 69,200 sleep 5000 spd 69,100 sleep 5000 spd 69,0 sleep 3000 print "Stop" dir 69,2 fnx 69,2 print "Go REV!" spd 69,0 sleep 6000 spd 69,100 sleep 8000 print "Run 200/1023" spd 69,200 sleep 5000 spd 69,100 sleep 5000 spd 69,0 sleep 3000 print "STOP! Finish!" ==== S88 decoder example ==== S88デコーダのセンサデータを取得し、Roco BR621(Addr.3)の往復運転を行うサンプルです。 Detail: [[http://powerele.sblo.jp/article/185548716.html|DSair2で自動運転をしてみる]] screen(7) s88start pwr 1 print "START S88 Control" dir 3,1 fnc 3,0,1 fnc 3,1,1 fnx 3,2 spd 3,200 while s88get(1)=0 print "RUN!" sleep 1000 wend print "S88 Adr.1 Detected" spd 3,0 sleep 2000 print "REV" dir 3,2 spd 3,200 sleep 12000 spd 3,0 fnc 3,0,0 fnc 3,1,0 print "end." end ==== Speed based Announce example ==== This automatic announce program is triggered by slot 1 speed. [[http://powerele.sblo.jp/article/185561119.html|DSbasicで運転支援機能]] {{http://buin2gou.sakura.ne.jp/sblo_files/powerele/image/DSbasic_21-thumbnail2.png}} screen(7) print "Announce supporter" print "Addr:";getslotaddr(0) print "Speed:";getslotspd(0) state=0 currentspd = getslotspd(0) while 1 sleep(1000) if currentspd<>getslotspd(0) then print "Current Speed is ";getslotspd(0) currentspd = getslotspd(0) end if if state=0 then if getslotspd(0)>512 then state = 1 print "Next Station" mp3play "NextStationAnnounce.mp3" end if end if if state=1 then if getslotspd(0)<100 then state = 0 print "Stop at Station" mp3play "StopStationAnnounce.mp3" end if end if wend end ===== Hints ===== ==== Font size / 文字を大きくする ==== screen(7)コマンドを最初に宣言すると、文字が大きくなります(おおよそ二倍) screen(7) ==== Comment / コメント ==== If you want to use comment, type like the following. 'Comment example 'コメントです ==== 関数・サブルーチンを宣言する ==== 以下の通り、宣言と、実現部を記述すると関数としてコールできます。 DECLARE FUNCTION GetNantoka(n) FUNCTION GetNantoka(n) digits = 1 GetNantoka= digits END FUNCTION ==== WHILEを使う ==== 以下のように記述できます。 n=100 i=0 WHILE i> 2 i = i+ 1 n = n / 3 WEND ==== FOR文 ==== FOR文は以下のように使えます。 FOR i = 0 TO 3000 PRINT i; NEXT i STEPコマンドを使うと、WHILE文を使わなくても上がり方を調整できます。 FOR i = 0 TO 3000 STEP 100 PRINT i; NEXT i ===== 活用例 ===== 皆様のDSbasicの活用例、サンプル、解説などお待ちしております。 * [[http://powerele.sblo.jp/article/185725380.html|DSbasicでポイントを使った自動運転をしてみた]] (DesktopStation公式ブログ) * [[https://tsujiru.hatenablog.com/entry/2019/04/30/195231|DSbasic 入門してみる(1)]],[[https://tsujiru.hatenablog.com/entry/2019/04/30/211900|(2)]],[[https://tsujiru.hatenablog.com/entry/2019/05/01/195229|(3)]],[[https://tsujiru.hatenablog.com/entry/2019/05/01/195229|(4)]],[[https://tsujiru.hatenablog.com/entry/2019/05/02/123319|(5)]],[[https://tsujiru.hatenablog.com/entry/2019/05/02/192817|(6)]],[[https://tsujiru.hatenablog.com/entry/2019/05/03/193437|(7)]] (つじるの忘れんぼ鉄道) * [[http://powerele.sblo.jp/article/185561119.html|DSbasicで運転支援機能]] (DesktopStation公式ブログ) * [[http://powerele.sblo.jp/article/185548716.html|DSair2で自動運転をしてみる]] (DesktopStation公式ブログ) * [[https://desktopstation.net/bb/showthread.php?tid=126|DSbasic (DSair用BASIC) 相談室]] デジタル鉄道模型フォーラム ===== Special Thanks ===== DSbasic is powered by [[https://github.com/google/wwwbasic|wwwbasic]] and Ace text editor.