โจทย์ฝึกปฏิบัติสำหรับสัปดาห์นี้
1) เขียนโค้ดสำหรับบอร์ด Arduino โดยสร้างเป็น C++ Class ดังต่อไปนี้
=> Class StringStack เป็นโครงสร้างข้อมูลแบบ Stack (กองซ้อน) สำหรับเก็บ String objects (http://arduino.cc/en/Reference/StringObject) และกำหนด API สำหรับคลาสดังกล่าว เป็นดังนี้
=> Class StringStack เป็นโครงสร้างข้อมูลแบบ Stack (กองซ้อน) สำหรับเก็บ String objects (http://arduino.cc/en/Reference/StringObject) และกำหนด API สำหรับคลาสดังกล่าว เป็นดังนี้
/////////////////////////////////////////////////////////////////////////
#include <String.h>
#include <String.h>
class StringStack {
public:
StringStack( int capacity=10 ); // constructor
boolean put( String s ); // put a String object on stack
boolean get( String &s ); // get a String object from stack
inline int size(); // return the number of String objects on the stack
inline boolean isEmpty(); // return true if stack is empty, otherwise false
inline boolean isFull(); // return true if stack is full, otherwise false
private:
int capacity; // the max. capacity of the stack
int count; // used to count the number of string objects stored
String *buf; // used to store String objects on stack
};
/////////////////////////////////////////////////////////////////////////
public:
StringStack( int capacity=10 ); // constructor
boolean put( String s ); // put a String object on stack
boolean get( String &s ); // get a String object from stack
inline int size(); // return the number of String objects on the stack
inline boolean isEmpty(); // return true if stack is empty, otherwise false
inline boolean isFull(); // return true if stack is full, otherwise false
private:
int capacity; // the max. capacity of the stack
int count; // used to count the number of string objects stored
String *buf; // used to store String objects on stack
};
/////////////////////////////////////////////////////////////////////////
จงสร้างคลาส StringStack และทดสอบการทำงานโดยใช้โค้ดตัวอย่างต่อไปนี้
และทดสอบโดยใช้ฮาร์ดแวร์จริง (ใช้บอร์ด Arduino และแสดงผลผ่าน Serial Monitor ของ Arduino IDE)
และทดสอบโดยใช้ฮาร์ดแวร์จริง (ใช้บอร์ด Arduino และแสดงผลผ่าน Serial Monitor ของ Arduino IDE)
/////////////////////////////////////////////////////////////////////////
int num = 10; // capacity
StringStack st( num );
int num = 10; // capacity
StringStack st( num );
void setup() {
Serial.begin(115200);
}
Serial.begin(115200);
}
char buf[20];
String str;
String str;
void loop() {
Serial.print( "\nPut strings: " );
for ( int i=0; i < num; i++ ) {
str = String( (i+1)*10 );
if ( !st.put( str ) ) {
Serial.println( "\nPut string error!" );
break;
} else {
Serial.print( str );
Serial.print( " " );
}
str = NULL;
delay(50);
}
delay(500);
Serial.print( "\nGet strings: " );
for ( int i=0; i < num; i++ ) {
if ( st.get( str ) ) {
str.toCharArray( buf, 20 );
Serial.print( buf );
Serial.print( " " );
} else {
Serial.println( "\nGet string error!" );
break;
}
delay(50);
}
delay(500);
}
Serial.print( "\nPut strings: " );
for ( int i=0; i < num; i++ ) {
str = String( (i+1)*10 );
if ( !st.put( str ) ) {
Serial.println( "\nPut string error!" );
break;
} else {
Serial.print( str );
Serial.print( " " );
}
str = NULL;
delay(50);
}
delay(500);
Serial.print( "\nGet strings: " );
for ( int i=0; i < num; i++ ) {
if ( st.get( str ) ) {
str.toCharArray( buf, 20 );
Serial.print( buf );
Serial.print( " " );
} else {
Serial.println( "\nGet string error!" );
break;
}
delay(50);
}
delay(500);
}
ผลการทดลอง
จากภาพจะเห็นได้ว่า StringStack เมื่อมีการกดปุ่ม1 จะทำการเพิ่ม String เข้าไปใน Stack ตามจำนวนที่กำหนด และเมื่อมีเงื่อนไขในการ Get ออกจาก Stack (ในCodeนี้ใช้การกดปุ่ม2) จะทำให้ ได้ค่า จากท้ายออกมาก่อน ดังที่ได้ ใส่ไปไว้ในตอนแรก และเมื่อใส่จนเต็ม จะ คืนแสดงว่า Full หรือ Get จนหมดแล้ว จะแสดงว่า Empty
Download Code1
2) ใช้คลาส StringStack ในข้อแรก นำมาเขียนโค้ด Arduino เพื่อให้มีพฤติกรรมการทำงานดังนี้
2.1) บอร์ด Arduino มีวงจรปุ่มกด Get ทำงานแบบ Active-Low (ใช้ตัวต้านทานแบบ Pull-up, 10k)
2.2) ผู้ใช้สามารถส่งข้อความ (ภาษาอังกฤษ) ทีละบรรทัด (ไม่เกิน 20 ตัวอักขระต่อบรรทัด) จากคอมพิวเตอร์ โดยส่งผ่าน Serial Monitor ของ Arduino IDE ไปยังบอร์ด Arduino
2.3) ข้อความแต่ละบรรทัดที่ถูกส่งไปยัง Arduino จะถูกจัดเก็บใน StringStack (เก็บบนกองซ้อน) ถ้าไม่เต็มความจุ แต่ถ้าเต็มความจุ ไม่สามารถเก็บข้อความใหม่ได้ Arduino จะต้องส่งข้อความ "Full" กลับมา
2.4) เมื่อมีการกดปุ่ม Get แล้วปล่อยหนึ่งครั้ง ข้อความบนสุด (ถ้ามี) ของ StringStack จะถูกดึงออกมาแล้วส่งผ่าน Serial Monitor ไปยังคอมพิวเตอร์
แต่ถ้าไม่ข้อความใดๆ Arduino จะต้องส่งข้อความ "Empty" กลับมา เมื่อกดปุ่มแล้วปล่อย
ผลการทดลอง
เริ่มต้น ด้วยการบอกจำนวนของหน่วยความจำที่สามารถ Stock ได้ ในที่นี้ คือ 10 และเมื่อเริ่มการทำงาน ไฟจะติด เพื่อบอกสถานะว่าพร้อมทำงาน
การใส่อักษรมากกว่า 20 ตัว
จะเห็นได้ว่า INPUT จะได้แค่ 20 ตัวอักษรเท่านั้น
เมื่อใส่ครบตามจำนวนจะตอบค่า Full กลับมา และ ทำให้ ไฟแสดงสถานะของการทำงานดับ
และการกดปุ่ม GET ค่ากลับ จะเป็นแบบกองซ้อนกันของ INPUT
Download Code2
ไม่มีความคิดเห็น:
แสดงความคิดเห็น