Arduino IDEでは、プログラムはSketch(スケッチ)と呼ばれ、C言語風のArduino言語で記述を行います。
Arduino言語の特徴
Arduino言語は高度に抽象化されていて、初学者にも判りやすい言語になっていますが、実際は、AVR GCC C/C++上に構築されており、実際は、検証や書き込み時には自動的に変換されます。
ユーザーは、スケッチ中にvoid setup()とvoid loop()を記述する必要があります。これらは、Windows版の場合
C:\Program Files\Arduino\hardware\arduino\cores\arduino\main.cpp
で中で呼び出されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/* main.cpp - Main loop for Arduino sketches Copyright (c) 2005-2013 Arduino Team. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include <Arduino.h> //Declared weak in Arduino.h to allow user redefinitions. int atexit(void (*func)()) { return 0; } // Weak empty variant initialization function. // May be redefined by variant files. void initVariant() __attribute__((weak)); void initVariant() { } int main(void) { init(); initVariant(); #if defined(USBCON) USBDevice.attach(); #endif setup(); for (;;) { loop(); if (serialEventRun) serialEventRun(); } return 0; } |
その他の設定は、ターゲットのArduinoマイコンに応じて処理される様に、init()などで定義されています。これが特にAtmel AVRマイコンの設定を強く意識せずに使用できる理由です。
まとめ
以上の様に、実際はGCC C/C++プロジェクトで、面倒な所は処理してくれているというのが、Arduino言語の実態です。それ故に、C言語の全構造と、C++の一部機能をサポートし、AVR Libcにリンクされているので、関数を利用する事が可能です。
その一方、C言語、C++言語を知らない初学者にもArduino言語は学習しやすいのが最大の特徴ですので、easy laboでは、Sketch(スケッチ)記述についても一通りフォローしていきたいと思います。Arduinoは、マイコンプログラミングの始めの一歩としては最適なアイテムと言え、特に教育現場で活用できれば、より多くの若い人に「ものづくり」への興味を持ってもらえるのではないかと思います。
Sponsored Link