PD実践2021:LCDの使い方
lecture
2021.09.23
EV3の画面に文字や絵を表示することができるLCDのAPIを学びます。
- LCDに関するAPI
- デォルトのフォントを設定する。シミュレータは標準フォントのみ対応。
- ER ev3_lcd_set_font (lcdfont_t font)
- 例
- 小さいフォントを設定する:ev3_lcd_set_font(EV3_FONT_SMALL);
- 標準のフォントを設定する:ev3_lcd_set_font(EV3_FONT_MEDIUM);
- 指定位置に文字列を表示する(x=0,y=10)に”HELLO”を表示する。なお、減点は画面の左隅、x軸は右方向、y軸は下方向。
- ER ev3_lcd_draw_string (const char *str, int32_t x, int32_t y)
- 引数
- x 左上隅の水平方向の位置[ピクセル]
- y 左上隅の垂直方向の位置[ピクセル]
- str 文字列
- 戻り値
- 例
- 位置(x=0,y=10)に”HELLO”を表示する
- ev3_lcd_draw_string(“HELLO”, 0, 10);
- 四角を描いて色を塗る
- ER ev3_lcd_fill_rect (int32_t x, int32_t y, int32_t w, int32_t h, lcdcolor_t color)
- 引数
- x 左上隅の水平方向の位置[ピクセル]
- y 左上隅の垂直方向の位置[ピクセル]
- w 矩形の幅
- h 矩形の高さ
- color カラー
- 戻り値
- 例
- 指定した位置(x=20,y=30)を始点として、幅40、高さ50の矩形を描き、黒色を塗る
- ev3_lcd_fill_rect(20, 30, 40, 50, EV3_LCD_BLACK);
- LCD画面全体を白く塗り潰す(クリアする)
- ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
-
- サンプルプログラム
-
- /lcd_1/app.c:”hello, world”とLCDに表示するプログラム。
- 最初の2行はEV3RTのC言語のAPIとmain_task関数を使うときに必要なインクルードファイル。これがないとビルドに失敗する。普通のC言語はmain関数から始まるが、EV3RTのC言語はmain関数の代わりにmain_task関数で始まる。
- また、main関数の最後はreturn 0で終了するが、EV3RTのC言語はext_tsk()関数で終了する。リアタイムOSでは複数の処理を並列して実行させる機能があり、その処理単位をタスクとよぶ。この関数はタスクの終了処理を行う。
- LCDに文字列を表示するにはev3_lcd_draw_string関数を使う。始めの引数は文字列、2番目の引数はLCDの横方向の位置、3番目の引数はLCDの縦方向の位置で減点は画面左上隅。このサンプルではC言語バイブル「プログラング言語C」 (カーニハン&リッチー著)の最初の例と同じ”hello, world”とLCDに表示する。
#include "ev3api.h"
#include "app.h"
void main_task(intptr_t exinf) {
ev3_lcd_draw_string("hello, world", 0, 10); /* 指定位置に文字列を表示 */
ext_tsk(); /* タスク終了処理 */
}
-
- /lcd_2/app.c:カウンタと矩形を表示するプログラム。
- 変数をLCDに表示する方法を学ぶ。ev3_lcd_draw_stringは文字列しか表示できないので、sprintf関数を使い、変数countを2番目の引数の書式で文字列strに書いている。
#include "ev3api.h"
#include "app.h"
void main_task(intptr_t exinf) {
int count;
char str[32]; /* カウントを表示するための文字列 */
ev3_lcd_set_font(EV3_FONT_MEDIUM); /* LCDに表示するフォントの設定 */
while(1){
sprintf(str, "COUNT:%d", count); /* カウントを表示用の文字列作成 */
ev3_lcd_draw_string(str, 30, 60); /* 指定した位置にカウントを表示 */
ev3_lcd_fill_rect(10, 10, 100, 20, EV3_LCD_BLACK); /* 指定した位置に矩形表示 */
tslp_tsk(100000); /* 100000us(100ms) 刻みで実行 */
count++;
}
/* LCD画面全体を白く塗り潰す(クリアする)*/
ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
tslp_tsk(1000000);
ext_tsk(); /* タスク終了処理 */
}
- 演習
- サンプルプログラム1のビルドと実行
- 端末で以下のコマンドを実行する。
$ cd /ev3rt/hrp3/sdk/workspace
$ make app=lcd_1
- タッチセンサをEV3(ポート1)に接続した後、appをEV3にアップロードし、動作を確認してください。
- サンプルプログラム1と同様にサンプルプログラム2をビルドしてEV3にアップロードし,動作を確認する.
- 端末で以下のコマンドを実行する。
$ cd /ev3rt/hrp3/sdk/workspace
$ make app=lcd_2
- レポート1
- サンプルプログラムlcd_2を改造し、以下の機能を実現する。なお、サンプルプログラムは変数countが0に初期化されていないので、まず、0に初期化すること。
- countが100になったら、LCDに“Game Over!”と表示してタスクを終了する。
- 参考
- LCD API (EV3RT C API Reference)
- LCD画面の幅は178[px]、高さは128[px]である。
- LCD画面の左上隅が、座標(x=0,y=0)である。
- フォントサイズは以下の2通り。なお、シミュレータは普通サイズのフォントのみ対応している。
- EV3_FONT_SMALL 小さいサイズのフォント:幅6×高さ8[px]
- EV3_FONT_MEDIUM 普通サイズのフォント:幅10×高さ16[px]
終わり
").attr(n.scriptAttrs||{}).prop({charset:n.scriptCharset,src:n.url}).on("load error",i=function(e){r.remove(),i=null,e&&t("error"===e.type?404:200,e.type)}),C.head.appendChild(r[0])},abort:function(){i&&i()}}});var Jt,Kt=[],Zt=/(=)\?(?=&|$)|\?\?/;ce.ajaxSetup({jsonp:"callback",jsonpCallback:function(){var e=Kt.pop()||ce.expando+"_"+jt.guid++;return this[e]=!0,e}}),ce.ajaxPrefilter("json jsonp",function(e,t,n){var r,i,o,a=!1!==e.jsonp&&(Zt.test(e.url)?"url":"string"==typeof e.data&&0===(e.contentType||"").indexOf("application/x-www-form-urlencoded")&&Zt.test(e.data)&&"data");if(a||"jsonp"===e.dataTypes[0])return r=e.jsonpCallback=v(e.jsonpCallback)?e.jsonpCallback():e.jsonpCallback,a?e[a]=e[a].replace(Zt,"$1"+r):!1!==e.jsonp&&(e.url+=(At.test(e.url)?"&":"?")+e.jsonp+"="+r),e.converters["script json"]=function(){return o||ce.error(r+" was not called"),o[0]},e.dataTypes[0]="json",i=ie[r],ie[r]=function(){o=arguments},n.always(function(){void 0===i?ce(ie).removeProp(r):ie[r]=i,e[r]&&(e.jsonpCallback=t.jsonpCallback,Kt.push(r)),o&&v(i)&&i(o[0]),o=i=void 0}),"script"}),le.createHTMLDocument=((Jt=C.implementation.createHTMLDocument("").body).innerHTML="
",2===Jt.childNodes.length),ce.parseHTML=function(e,t,n){return"string"!=typeof e?[]:("boolean"==typeof t&&(n=t,t=!1),t||(le.createHTMLDocument?((r=(t=C.implementation.createHTMLDocument("")).createElement("base")).href=C.location.href,t.head.appendChild(r)):t=C),o=!n&&[],(i=w.exec(e))?[t.createElement(i[1])]:(i=Ae([e],t,o),o&&o.length&&ce(o).remove(),ce.merge([],i.childNodes)));var r,i,o},ce.fn.load=function(e,t,n){var r,i,o,a=this,s=e.indexOf(" ");return-1
").append(ce.parseHTML(e)).find(r):e)}).always(n&&function(e,t){a.each(function(){n.apply(this,o||[e.responseText,t,e])})}),this},ce.expr.pseudos.animated=function(t){return ce.grep(ce.timers,function(e){return t===e.elem}).length},ce.offset={setOffset:function(e,t,n){var r,i,o,a,s,u,l=ce.css(e,"position"),c=ce(e),f={};"static"===l&&(e.style.position="relative"),s=c.offset(),o=ce.css(e,"top"),u=ce.css(e,"left"),("absolute"===l||"fixed"===l)&&-1<(o+u).indexOf("auto")?(a=(r=c.position()).top,i=r.left):(a=parseFloat(o)||0,i=parseFloat(u)||0),v(t)&&(t=t.call(e,n,ce.extend({},s))),null!=t.top&&(f.top=t.top-s.top+a),null!=t.left&&(f.left=t.left-s.left+i),"using"in t?t.using.call(e,f):c.css(f)}},ce.fn.extend({offset:function(t){if(arguments.length)return void 0===t?this:this.each(function(e){ce.offset.setOffset(this,t,e)});var e,n,r=this[0];return r?r.getClientRects().length?(e=r.getBoundingClientRect(),n=r.ownerDocument.defaultView,{top:e.top+n.pageYOffset,left:e.left+n.pageXOffset}):{top:0,left:0}:void 0},position:function(){if(this[0]){var e,t,n,r=this[0],i={top:0,left:0};if("fixed"===ce.css(r,"position"))t=r.getBoundingClientRect();else{t=this.offset(),n=r.ownerDocument,e=r.offsetParent||n.documentElement;while(e&&(e===n.body||e===n.documentElement)&&"static"===ce.css(e,"position"))e=e.parentNode;e&&e!==r&&1===e.nodeType&&((i=ce(e).offset()).top+=ce.css(e,"borderTopWidth",!0),i.left+=ce.css(e,"borderLeftWidth",!0))}return{top:t.top-i.top-ce.css(r,"marginTop",!0),left:t.left-i.left-ce.css(r,"marginLeft",!0)}}},offsetParent:function(){return this.map(function(){var e=this.offsetParent;while(e&&"static"===ce.css(e,"position"))e=e.offsetParent;return e||J})}}),ce.each({scrollLeft:"pageXOffset",scrollTop:"pageYOffset"},function(t,i){var o="pageYOffset"===i;ce.fn[t]=function(e){return M(this,function(e,t,n){var r;if(y(e)?r=e:9===e.nodeType&&(r=e.defaultView),void 0===n)return r?r[i]:e[t];r?r.scrollTo(o?r.pageXOffset:n,o?n:r.pageYOffset):e[t]=n},t,e,arguments.length)}}),ce.each(["top","left"],function(e,n){ce.cssHooks[n]=Ye(le.pixelPosition,function(e,t){if(t)return t=Ge(e,n),_e.test(t)?ce(e).position()[n]+"px":t})}),ce.each({Height:"height",Width:"width"},function(a,s){ce.each({padding:"inner"+a,content:s,"":"outer"+a},function(r,o){ce.fn[o]=function(e,t){var n=arguments.length&&(r||"boolean"!=typeof e),i=r||(!0===e||!0===t?"margin":"border");return M(this,function(e,t,n){var r;return y(e)?0===o.indexOf("outer")?e["inner"+a]:e.document.documentElement["client"+a]:9===e.nodeType?(r=e.documentElement,Math.max(e.body["scroll"+a],r["scroll"+a],e.body["offset"+a],r["offset"+a],r["client"+a])):void 0===n?ce.css(e,t,i):ce.style(e,t,n,i)},s,n?e:void 0,n)}})}),ce.each(["ajaxStart","ajaxStop","ajaxComplete","ajaxError","ajaxSuccess","ajaxSend"],function(e,t){ce.fn[t]=function(e){return this.on(t,e)}}),ce.fn.extend({bind:function(e,t,n){return this.on(e,null,t,n)},unbind:function(e,t){return this.off(e,null,t)},delegate:function(e,t,n,r){return this.on(t,e,n,r)},undelegate:function(e,t,n){return 1===arguments.length?this.off(e,"**"):this.off(t,e||"**",n)},hover:function(e,t){return this.on("mouseenter",e).on("mouseleave",t||e)}}),ce.each("blur focus focusin focusout resize scroll click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup contextmenu".split(" "),function(e,n){ce.fn[n]=function(e,t){return 0
タイトルとURLをコピーしました
コメント