/*
- Copyright (c) 2021 by M5Stack
-
Equipped with M5Core2 sample source code
-
配套 M5Core2 示例源代码
- Visit the website for more information:https://docs.m5stack.com/en/core/core2
- 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2
- describe:Hello World
- date:2021/7/21
*/
#include <Wire.h>
#include <M5Core2.h>
#include <string>
#include <Adafruit_LIS3MDL.h>
#include <Adafruit_Sensor.h>
#include <Fonts/EVA_20px.h>
#include "Adafruit_VL53L1X.h"
#define IRQ_PIN 2
#define XSHUT_PIN 3
// LCDに表示する座標用の構造体
typedef struct _LPos {
int x;
int y;
} LPos_t;
// LCDに文字座標
LPos_t TextLine[4] = {
{10,10}, // 1行目
{10,30}, // 2行目
{10,50}, // 3行目
{10,70} // 4行目
};
TFT_eSprite sprite(&M5.Lcd);
int count;
// setup()
/* After M5Core2 is started or reset
the program in the setUp () function will be run, and this part will only be run once.
在 M5Core2 启动或者复位后,即会开始执行setup()函数中的程序,该部分只会执行一次。 /
void setup(){
M5.begin(); //Init M5Core2. 初始化 M5Core2
/ Power chip connected to gpio21, gpio22, I2C device
Set battery charging voltage and current
If used battery, please call this function in your project */
M5.Lcd.setBrightness(200); //バックライトの明るさを0(消灯)~255(点灯)で制御
M5.Lcd.fillScreen(BLACK);
//M5.Lcd.print("Hello World"); // Print text on the screen (string) 在屏幕上打印文本(字符串)
M5.Lcd.setTextSize(2);//文字の大きさを設定(1~7)
// スプライトの作成
sprite.createSprite(M5.Lcd.width(),M5.Lcd.height());
count = 0;
}
// loop()
/* After the program in setup() runs, it runs the program in loop()
The loop() function is an infinite loop in which the program runs repeatedly
在setup()函数中的程序执行完后,会接着执行loop()函数中的程序
loop()函数是一个死循环,其中的程序会不断的重复运行 */
void loop() {
std::string str;
// スプライトを黒く塗りつぶす
sprite.fillScreen(BLACK);
//int count =0;
// 文字描画
str="1:Hello, World"+std::to_string(count);
sprite.setCursor(TextLine[0].x, TextLine[0].y); //文字表示の左上位置を設定
sprite.setTextColor(WHITE); //文字色設定(背景は透明)(WHITE, BLACK, RED, GREEN, BLUE, YELLOW...)
sprite.printf("%s", str.c_str());
str.clear();
//
Adafruit_VL53L1X vl53 = Adafruit_VL53L1X(XSHUT_PIN, IRQ_PIN);
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println(F("Adafruit VL53L1X sensor demo"));
Wire.begin();
if (! vl53.begin(0x29, &Wire)) {
Serial.print(F("Error on init of VL sensor: "));
Serial.println(vl53.vl_status);
while (1) delay(10);
}
Serial.println(F("VL53L1X sensor OK!"));
Serial.print(F("Sensor ID: 0x"));
Serial.println(vl53.sensorID(), HEX);
if (! vl53.startRanging()) {
Serial.print(F("Couldn't start ranging: "));
Serial.println(vl53.vl_status);
while (1) delay(10);
}
Serial.println(F("Ranging started"));
// Valid timing budgets: 15, 20, 33, 50, 100, 200 and 500ms!
vl53.setTimingBudget(50);
Serial.print(F("Timing budget (ms): "));
Serial.println(vl53.getTimingBudget());
/*
vl.VL53L1X_SetDistanceThreshold(100, 300, 3, 1);
vl.VL53L1X_SetInterruptPolarity(0);
*/
}
void loop() {
int16_t distance;
if (vl53.dataReady()) {
// new measurement for the taking!
distance = vl53.distance();
if (distance == -1) {
// something went wrong!
Serial.print(F("Couldn't get distance: "));
Serial.println(vl53.vl_status);
return;
}
Serial.print(F("Distance: "));
Serial.print(distance);
Serial.println(" mm");
// data is read out, time for another reading! vl53.clearInterrupt();
}
}
sprite.setTextColor(RED); //文字色設定と背景色設定(WHITE, BLACK, RED, GREEN, BLUE, YELLOW...)
sprite.setCursor(TextLine[1].x, TextLine[1].y); //文字表示の左上位置を設定
sprite.printf("%s", str.c_str());
str.clear();
//
sprite.setTextColor(GREEN); //文字色設定と背景色設定(WHITE, BLACK, RED, GREEN, BLUE, YELLOW...)
sprite.setCursor(TextLine[2].x, TextLine[2].y); //文字表示の左上位置を設定
sprite.printf("%s", str.c_str());
str.clear();
//
sprite.setTextColor(YELLOW); //文字色設定と背景色設定(WHITE, BLACK, RED, GREEN, BLUE, YELLOW...)
sprite.setCursor(TextLine[3].x, TextLine[3].y); //文字表示の左上位置を設定
sprite.printf("%s", str.c_str());
str.clear();
// スプライトを表示画面に転送する
sprite.pushSprite(0, 0);
count++;
delay(50); // 1000ms待つ
これをコンパイルすると「a function-definition is not allowed here before '{' token」とエラーの表示がされます。どうすれば良いのでしょうか?
よろしくお願いします。