This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct | |
{ | |
int data; | |
}st_struct; | |
void my_fn(st_struct *st) | |
{ | |
... | |
} | |
void setup() | |
{ | |
... | |
} | |
void loop() | |
{ | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino.h" | |
void my_fn(st_struct *st); | |
void setup(); | |
void loop(); | |
#line 1 | |
typedef struct | |
{ | |
int data; | |
}st_struct; | |
void my_fn(st_struct *st) | |
{ | |
... | |
} | |
void setup() | |
{ | |
... | |
} | |
void loop() | |
{ | |
... | |
} |
ไฟล์แรก (arduino_code.c) เป็นไฟล์ที่เขียนที่ Arduino IDE เราเขียน struct ขึ้นก่อนจากนั้นค่อยสร้าง function my_fn
แต่ทีนี้พอ compile Arduino IDE จัดการกับ code เราใหม่ ดูจากไฟล์ที่ 2 (arduino_code2.c) Arduino IDE ได้ copy ชื่อ function ไปทำเป็น function prototype แล้วค่อย ประกาศ struct เวลาไป คอมไฟล์มันก็เลย error สิ
แล้ววิธีแก้ล่ะ จริงมันก็ทำได้หลายวิธี ผมเสนอ 2 วิธี
- ใช้ void * แทนชื่อ struct แล้วใน function ก็ค่อย cast ค่าไปเป็น struct ของเรา (arduino_code3.c)
- สร้างไฟล์ struct.h (arduino_code_st.h) มาเขียน struct ทั้งหมดในนั้น แล้วค่อย include เข้ามา (arduino_code4.c)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct | |
{ | |
int data; | |
}st_struct; | |
void my_fn(void *st) | |
{ | |
st_struct *s = (st_struct *)st; | |
... | |
} | |
void setup() | |
{ | |
... | |
} | |
void loop() | |
{ | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "arduino_code_st.h" | |
void my_fn(st_struct *st) | |
{ | |
} | |
void setup() | |
{ | |
} | |
void loop() | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//File path ARDUINO_LIBRARY/arduino_code_st/arduino_code_st.h | |
typedef struct | |
{ | |
int data; | |
}st_struct; | |
0 ความคิดเห็น:
แสดงความคิดเห็น