LoginSignup
1
1

More than 1 year has passed since last update.

Arduino スケッチを PC で動かすために

Posted at

LovyanGFX for PC を使うと Arduino core for the ESP32 用のスケッチなどを PC で動かしてみることができますが、ソースコードを 完全に共通化はできません。

Arduino の API を使うところなどちょこまかと変更しなければなりませんが、その差異を吸収するためのライブラリを作り、

「LovyanGFX for PC で Arduino スケッチを動かすために」
https://qiita.com/nanbuwks/items/50e8c7f8848a6499b5b0

で紹介、その後

「LovyanGFX for PC 完全体を g++ on Ubuntu で」
https://qiita.com/nanbuwks/items/e316398aa12a90576cc7

で日本語フォントも g++ で使えるよう改良しました。

ところが @pokibon さんより

windowsのVisual Studio上では、最初の10行のfont関連のinclude内で定義されているフォントデータが二重定義になっちゃいます。

と指摘がありました。

元々 LavyanGFX for PC は MS-Windows + Visual Studio Community + CMake + Clang の組み合わせで公開されていますが、僕は Linux + g++ + Visual Studio Code で動かしていたのでその違いかな?

なので条件コンパイルを使って Clang でもエラーが出ずに使えるように変更しました。


#ifdef LGFX_USE_V1
#ifdef  __GNUC__
#include <lgfx/Fonts/IPA/lgfx_font_japan.h>
#include <lgfx/Fonts/efont/lgfx_efont_cn.h>
#include <lgfx/Fonts/efont/lgfx_efont_ja.h>
#include <lgfx/Fonts/efont/lgfx_efont_kr.h>
#include <lgfx/Fonts/efont/lgfx_efont_tw.h>
#include <lgfx/Fonts/IPA/lgfx_font_japan.c>
#include <lgfx/Fonts/efont/lgfx_efont_cn.c>
#include <lgfx/Fonts/efont/lgfx_efont_ja.c>
#include <lgfx/Fonts/efont/lgfx_efont_kr.c>
#include <lgfx/Fonts/efont/lgfx_efont_tw.c>
#endif
#endif


#define millis(num) SDL_GetTicks()
#define String(num) to_string(num)
#define delay(msec) SDL_Delay(msec)
#include <stdint.h>
#include <stdlib.h>
#include <string>
using namespace std;
class SerialDummy
{
  public:
    void begin(int speed){
    }
    void print(string contents){
            printf("%s",contents.c_str());
    }
    void print(int contents){
            printf("%d",contents);
    }
    void println(void){
            printf("\n");
    }
    void println(int contents){
            printf("%d\n",contents);
    }
    void println(string contents){
            printf("%s\n",contents.c_str());
    }
};
SerialDummy Serial;
#ifndef LGFX_USE_V1
#include <SDL2/SDL.h>
#define setup() int setup()
#define loop() int loop()
int setup();
int loop();
int main();
main(){
int dummy;
   setup();
   while(1){
     loop();
   }
}
#endif

PC の場合はこれをインクルードすると、

  • PC 用のライブラリの読み込み
  • delay() 関数を SDL関数に置き換え
  • millis() 関数を SDL関数に置き換え
  • String(num) 関数を to_string(num) に置き換え
  • g++ を使った場合のフォント参照エラーの抑制
  • Serial.print などを使えるようにして標準出力に表示

するようにしています。

また、LovyanGFX を使わないスケッチでも main を作り setup() や loop() の面倒を見ることで、PC で動かすことができる(かもしれない)。
当然ながら PC には GPIO 入出力が無いので、digitalRead や digitalWrite などはできません。

GitHub

おもちゃのような内容で足りない機能がたくさんありますが、最初に完全を目指すのではなく必要に応じて足すつもりで。
随時拡張したソースは github に置くようにしましたので、使ってみたい方はこちらから。

「library for to run arduino sketch on pc」
https://github.com/nanbuwks/arduinolib_for_PC

不足機能は各位で書き足して補っていただくことになりますが、よろしければプルリクなどいただけましたら幸いです。

今後

当然ながら PC には GPIO 入出力が無いので、digitalRead や digitalWrite などはできません。

と書きましたが、ゆくゆくはエミュレータ的な環境を作りたいなあ・・・

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1