【toit:esp32】toit文法は難しい?

スポンサーリンク

arduinoはC++と同じではありませんがC++の文法で記述できます。

ではtoit.ioで使われる”toit文法は?”ということでarduinoと対比しながら実行してみました。

スポンサーリンク

toit と c++ の比較

arduinoのスケッチ(プログラム)はあまり意識しませんがC++の文法でコードを記述しているので、C++とtoitを比較してみました。 

*** Toit C++
Current object this this
Single line comments // //
Logical ‘and’, ‘or’ and ‘not’ operators and or not && || ! (or and or not)

*** Toit C++
Shift left, right, unsigned right << >> >>> << >> >> (on unsigned type)
Integer division / (on integer types) / (on integer type)
Integer sizes 64 8/16/32/64…
Statement grouping (indent)
Define a class Foo that inherits from Bar class Foo extends Bar: class Foo : public Bar {
Define constructor for class Foo constructor x: Foo(int x) {
Define constructor for class Foo that calls constructor of superclass Bar constructor x: super x Foo(int x) : Bar(x) {
Constructor that assigns to fields constructor .x: Foo(int x) : x(x) {
Check object has type bar is Foo (dynamic_cast(bar) != null)
Check object does not have type bar is not Foo (dynamic_cast(bar) == null)
Call a method foo with two arguments foo x y foo(x, y);
Declare a member variable in a class x := null x := ? x/int := 0 x/int := ? int x; int x = 0;
Declare a local variable in a method x := null x := ? x/int := 0 x/int := ? int x; int x = 0;
Define a constant X ::= 0 const int X = 0;
Define a constant in a class static X ::= 0 static const int X = 0;
Define a top-level function foo x y: int foo(int x, int y) {
Define an instance method in a class foo x y: int foo(int x, int y) {
Define a static method in a class static foo x y static int foo(int x, int y) {
If statement if condition: if (condition) {
Fixed loop end.repeat: i | | for (int i = 0; i < end; i++) {
Three-part expression loop for i := 0; i < end; i++: for (int i = 0; i < end; i++) {
Iterate over collection collection.do: x | | for (auto x : collection) {
While loop while condition: while (condition) {
Import local from library import .library #include “file”
Import from library import library #include
Import into current namespace import library show * #include using namespace file;
Print/log print “Hello” cout << "Hello\n"
Print with interpolation print “Hello $name” cout << "Hello " << name << "\n";

スポンサーリンク

lf 条件式

構文

if 条件式 :
x := 5
main:
  if x == 5 :
    print "true"

実行結果

[おうち栽培] 2022-02-06T02:28:13.919593Z: [おうち栽培] true
[おうち栽培] 2022-02-06T02:28:14.389776Z:
スポンサーリンク

&&

toit の記述法では”&&” ⇒ ”and”ということでそのものズバリ!です。^^;

x := 5
y := 3
main:
  if x == 5 and y == 2 :
     print "true"
  else :
    print "false"
[おうち栽培] 2022-02-06T02:15:30.151037Z: [おうち栽培] false
[おうち栽培] 2022-02-06T02:15:31.859656Z:

正しく機能しています。^^;

スポンサーリンク

||

構文

A or B

x := 5
y := 3
main:
  if x == 5 or y == 5 :
    print "true"

実行結果

[おうち栽培] 2022-02-06T02:43:35.846957Z: [おうち栽培] true
[おうち栽培] 2022-02-06T02:43:36.379575Z:
スポンサーリンク

!

構文

not A
x := 5
main:
  if not x == 3 :
    print "true"

実行結果

[おうち栽培] 2022-02-06T03:09:35.210009Z: [おうち栽培] true
[おうち栽培] 2022-02-06T03:09:35.726220Z:
スポンサーリンク

|| !

構文

A or not B

x := 5
y := 3
main:
  if x == 3 or not y == 3:  
     print "true"
[おうち栽培] 2022-02-06T03:00:56.250123Z: [おうち栽培] 2022-02-06T03:00:56.774173Z:

x = 5 ,y = 3 であり、どちらの条件にも当てはまらないので何も出力されません。

x := 5
y := 3
main:
  if x == 3  or not y == 5 :  
    print "true"

実行結果

[おうち栽培] 2022-02-06T03:26:16.469036Z: [おうち栽培] true
[おうち栽培] 2022-02-06T03:26:16.986342Z:

x = 5 で条件には当てはまりませんが、”又は” という別の条件ではy は5 では無い( y = 3 )という条件に当てはまるので”true”になります。

スポンサーリンク

for で繰り返すには?

構文

for i := 0; i < end; i++:
end := 10
main:
  for i := 0; i < end; i++:
    print "$i"

実行結果

[おうち栽培] 2022-02-06T03:47:07.188131Z: [おうち栽培] 0
[おうち栽培] 1
[おうち栽培] 2
[おうち栽培] 3
[おうち栽培] 4
[おうち栽培] 5
[おうち栽培] 6
[おうち栽培] 7
[おうち栽培] 8
[おうち栽培] 9
[おうち栽培] 2022-02-06T03:47:07.962791Z:
スポンサーリンク

float ⇒ int型に変換する

float型をint型に変換します。

x := 3.1415926
main:
  y / int := (x).to_int
  print y

実行結果

[おうち栽培] 2022-02-06T05:14:54.646172Z: [おうち栽培] 3
[おうち栽培] 2022-02-06T05:14:55.165492Z:
スポンサーリンク

float ⇒ 桁数を指定する

小数以下の長い出力を指定した桁数で表示します。

x := 3.1415926
main:
  y / string := "$(%0.2f x)"
  print y

実行結果

[おうち栽培] 2022-02-06T05:34:09.106804Z: [おうち栽培] 3.14
[おうち栽培] 2022-02-06T05:34:09.624160Z:
0.2f ⇒ 0.3f とすると 3.142 と出力するんだね…

fixed_point

toitにはarduino には無い機能の一つに”fixed_point”があります。

使い方は”import fixed_point show FixedPoint”の一行を加えます。

import fixed_point show FixedPoint
x := 3.1415926
main:
  y := FixedPoint --decimals = 3 x
  print y

実行結果

[おうち栽培] 2022-02-06T09:09:27.269500Z: [おうち栽培] 3.142
[おうち栽培] 2022-02-06T09:09:32.411292Z:

コンマ以下3桁を出力します。

タイトルとURLをコピーしました