質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

1回答

4042閲覧

ンパイルすると「a function-definition is not allowed here before '{' token」とエラーの表示がされま

ooido

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2022/03/25 03:30

/*



*/
#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」とエラーの表示がされます。どうすれば良いのでしょうか?
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maisumakun

2022/03/25 03:33

> エラーの表示がされます。 行番号などから位置は特定できませんか? あと、このコードはJavaではなさそうなので、タグは適切なものに差し替えたほうがいいかと思います。
y_waiwai

2022/03/25 04:08

このままではコードが読めないので、質問を編集し、</>(コードの挿入)ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
ozwk

2022/03/25 04:19

見た感じArduino(c++)のコードですけど、Javaというタグは付け間違えたのか、それともこのコードをJavaだと勘違いしているのかどちらでしょう?
guest

回答1

0

そのエラーは、カッコの数があってないときにでます
{と、}の数を数えてみよう

投稿2022/03/25 05:08

y_waiwai

総合スコア87719

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問