Warm-Up 2
โจทย์กำหนดให้สร้าง class StringStack เป็นคลาสที่เก็บตัวแปร String แบบ Stack โดยรับค่าจาก Serial และนำค่าออกจาก Stack โดยการกดปุ่มที่ต่อกับ Arduino แบบ Active Low
Button With Arduino
Fritzing
โดยกำหนด API ของ class StringStack ดังนี้
///////////////////////////////////////////////////////////////////////// #includeclass 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 }; /////////////////////////////////////////////////////////////////////////
Method ต่างๆใน Class
• StringStack( int capacity=10 ); ใช้ในการสร้างตัวแปร Class
• boolean put( String s ); ใช้ในการรับ String เข้ามาเก็บใน Class แบบ Stack
• boolean get( String &s ); ใช้ในการส่ง String ออกไปโดยส่งผ่านทาง Address ของตัวแปรที่รับเข้ามา
• inline int size(); ใช้ในการส่งค่าของจำนวนของ String ใน Stack
• inline boolean isEmpty(); ใช้ในการตรวจสอบว่า StringStack ว่างหรือไม่
• inline boolean isFull(); ใช้ในการตรวจสอบว่า StringStack เต็มหรือไม่
Variable ต่างๆใน Class
• int capacity; ตัวแปรสำหรับเก็บ ค่าความจุ ของ StringStack
Class StringStack <StringStack.h>
//StringStack Libraly
#ifndef StringStack_h
#define StringStack_h
#include "Arduino.h"
#include "string.h"
class StringStack {
public:
StringStack( int capacity ); // 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
String* getStack();
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
};
#endif
Class StringStack <StringStack.cpp>
#include "StringStack.h"
StringStack::StringStack( int capacity=10 ) {
this->capacity = capacity; // constructor
count = 0;
buf = new String[capacity];
}
boolean StringStack::put( String s ) {
if(!isFull()){
buf[count] = s;
if(count<9){
count++;
}
return true;
}
else{
return false;
}
}
boolean StringStack::get( String &s ) {
if(!isEmpty()){
if(count>0){
count--;
}
*&s = buf[count];
buf[count] = "";
return true;
}
else{
return false;
}
}
inline int StringStack::size() { // return the number of String objects on the stack
return count;
}
inline boolean StringStack::isEmpty() {
if(count == 0){
return true;
}
else{
return false;
}
}
inline boolean StringStack::isFull(){
if(count == 9){
return true;
}
else{
return false;
}
}
String* StringStack::getStack(){
return buf;
}
Arduino Code
#include "StringStack.h"
int num = 10; // capacity
StringStack st( num );
String str;
int state;
void setup(){
pinMode(2,INPUT);
Serial.begin(115200);
Serial.print("Put String into Stack(stack keep 20 character) : ");
}
void loop(){
str = "";
String temp;
char buffer[20];
if(digitalRead(2) == 0){
delay(100);
if(digitalRead(2) == 1){
if ( st.get( temp ) ) {
temp.toCharArray( buffer, 20 );
Serial.print("Get : [ ");
Serial.print( buffer );
Serial.println(" ] From Stack");
Serial.println();
Serial.println("Typing words to stack or Press Button to get words from stack : ");
}
else if(!st.get( temp )){
Serial.println("");
Serial.println("Stack is Emtry !!!");
Serial.println("Please !!! Typing words to stack : ");
}
else {
Serial.println( "\nGet string error!" );
}
delay(50);
}
}
while(Serial.available() > 0)
{
str = Serial.readStringUntil('\n');
}
if(str.length()>20){
str = str.substring(0, 20);
}
if(str.length()<=20 && str.length()>0){
if ( !st.put( str ) ) {
Serial.println("");
Serial.println( "Stack is Full" );
Serial.println("Press Button to get words from stack : ");
}
else {
Serial.print( "add : >> " );
Serial.print( str );
Serial.println( " << to Stack" );
Serial.print("Stack Have : [ ");
for(int i = 0 ; i < num ; i++){
if(st.getStack()[i] != ""){
Serial.print(st.getStack()[i]);
if(st.getStack()[i+1] != ""){
Serial.print(" , ");
}
}
}
Serial.println(" ]");
Serial.println("");
Serial.println("Put String into Stack or Press Button For get String : ");
}
}
}
เงื่อนไขการทำงาน
• ใส่อักขระได้ไม่เกิน 20 ตัว หากเกินจะตัดให้เหลือ 20 ตัว
• เมื่อกดปุ่มจะนำค่าออกจาก Stack มาแสดง
• เมื่อ Stack เต็มไม่สามารถเพิ่มข้อมูลได้แล้วจะแสดงคำเตือนว่า Stack is Full
• เมื่อ Queue ว่างไม่สามารถดึงข้อมูลได้แล้วจะแสดงคำเตือนว่า Stack is Empty
ผลการทดลอง
Extra >> StringQueue
ใช้ API แบบ StringStack แต่เก็บข้อมูลในรูปแบบ Queue (เข้าก่อนออกก่อน)
Class StringQueue <StringQueue.h>
//StringQueue Libraly
#ifndef StringQueue_h
#define StringQueue_h
#include "Arduino.h"
#include "String.h"
class StringQueue {
public:
StringQueue( int capacity ); // 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
String* getQueue();
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
};
#endif
Class StringQueue <StringQueue.cpp>
#include "StringQueue.h"
StringQueue::StringQueue( int capacity=10 ) {
this->capacity = capacity; // constructor
count = 0;
buf = new String[capacity];
}
boolean StringQueue::put( String s ) {
if(!isFull()){
buf[count] = s;
if(count<9){
count++;
}
return true;
}
else{
return false;
}
}
boolean StringQueue::get( String &s ) {
if(!isEmpty()){
if(count>0){
count--;
}
String temp = buf[0];
*&s = temp;
for(int i = 0;i<9;i++){
buf[i] = buf[i+1];
}
return true;
}
else{
return false;
}
}
inline int StringQueue::size() { // return the number of String objects on the Queue
return count;
}
inline boolean StringQueue::isEmpty() {
if(count == 0){
return true;
}
else{
return false;
}
}
inline boolean StringQueue::isFull(){
if(count == 9){
return true;
}
else{
return false;
}
}
String* StringQueue::getQueue(){
return buf;
}
Arduino Code
#include "StringQueue.h"
int num = 10; // capacity
StringQueue st( num );
String str;
void setup(){
pinMode(2,INPUT);
Serial.begin(115200);
Serial.print("Put String into Queue(Queue keep 20 character) : ");
}
void loop(){
str = "";
String temp;
char buffer[20];
if(digitalRead(2) == 0){
delay(100);
if(digitalRead(2) == 1){
if ( st.get( temp ) ) {
temp.toCharArray( buffer, 20 );
Serial.print("Get : [ ");
Serial.print( buffer );
Serial.println(" ] From Queue");
Serial.println();
Serial.println("Typing words to Queue or Press Button to get words from Queue : ");
}
else if(!st.get( temp )){
Serial.println("");
Serial.println("Queue is Emtry !!!");
Serial.println("Please !!! Typing words to Queue : ");
}
else {
Serial.println( "\nGet string error!" );
}
delay(50);
}
}
while(Serial.available() > 0)
{
str = Serial.readStringUntil('\n');
}
if(str.length()>20){
str = str.substring(0, 20);
}
if(str.length()<=20 && str.length()>0){
if ( !st.put( str ) ) {
Serial.println("");
Serial.println( "Queue is Full" );
Serial.println("Press Button to get words from Queue : ");
}
else {
Serial.print( "add : >> " );
Serial.print( str );
Serial.println( " << to Queue" );
Serial.print("Queue Have : [ ");
for(int i = 0 ; i < num ; i++){
if(st.getQueue()[i] != ""){
Serial.print(st.getQueue()[i]);
if(st.getQueue()[i+1] != ""){
Serial.print(" , ");
}
}
}
Serial.println(" ]");
Serial.println("");
Serial.println("Put String into Queue or Press Button For get String : ");
}
}
}

No comments:
Post a Comment