SMFSW / Queue

Queue handling library (designed on Arduino)
BSD 3-Clause "New" or "Revised" License
97 stars 20 forks source link

can't transmit string to queue #17

Closed MarcoZheng1991 closed 2 years ago

MarcoZheng1991 commented 2 years ago

Hello First of all thank you very much for your development I am new about Arduino I want to "991" string transmit to Queue list , but always only get ‘1’.
This string ''991' has been pushed to Queue through q.push(). in addition, the string data is also read through the q.pop() ,but the read value is '1'. maybe I'm using pointer incorrectly! can you give me some help, thanks a lot.

include

include

include

define QueueLength 10

cppQueue q(sizeof(String* ),QueueLength,FIFO,true);

//Crate 5*5 array int Location[5][5] { {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0},
{0,0,0,0,0} };

int Step = 0; int occupied =1; int Null =0; String coordinate(2) ; int x =9; int y =9; int StartButton=2;

void setup() { // put your setup code here, to run once: Serial.begin(9600); Location[x][y]=1; //write 1 to strat position Step=Step+1;
// char Str1 =char(x); //char Str2 =char(y); //char Str3 =char(Step); String Str1; String * Str2; Str1.concat (x) ; Str1.concat (y) ; Str1.concat (Step) ;

Str2 = &Str1; q.push(&Str2);

}

void loop() { //get location from queue list

if (!q.isEmpty()) { //String snd; //String Str; String * Str1; for(int i=0; i<3; i++) {

 Serial.println( q.pop(&Str1)); 

 }     

delay(500); }

}

SMFSW commented 2 years ago

Hello,

you're trying here to print return value from q.pop() method, which returns 0 or 1 (1 if correctly pulled from queue).

You should decompose this: Serial.println( q.pop(&Str1)); into:

char Str1;
q.pop(&Str1);
Serial.println(*Str1);

Also, in your code, you're pushing Str2 address to queue (not what it points to); moreover, even if pushing Str2/Str1 pointer correctly, they will be freed and not exist anymore when leaving setup.

This should work for basic types, but not if using String class, even more with a pointer to String. String is a class, and you would only be able to store each String pointer in the queue to use them later, as long as they are still allocated. Queue can store any basic types, such as integers, chars (and arrays), structures, pointers... Storing pointers contents is trickier (for classes such as String which are size variable and using dynamic allocation) and shall be handled by your own code.

Regards, SMFSW

MarcoZheng1991 commented 2 years ago

Hello, thank you for your reply !
my ultimate goal is find the shortest path in a 5*5 grid. I used the breadth search method and the queue you built. I created an array of int[3], [1] X coordinate [2] Y coordinate [3] Number of steps. Before this, I created a string array, but because I didn't understand the usage of string pointers, I changed it to an int array. In Setup, input the coordinates of the starting point and the first step into the array and push it into the queue, and then continue to set the flag in the loop in the way of first right and then down until the end point is reached.

This program has now been completed, thank you very much for your development. Below is my code:

//#include

include

include

define QueueLength 10

cppQueue q(sizeof(int[3]),QueueLength,FIFO,true);

//Crate 5*5 array int Location[5][5] { {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0},
{0,0,0,0,0} };

int x; int y; int myArray[3]={0,0,0};// 1. X 2.Y 3 Step

void setup() { // put your setup code here, to run once: Serial.begin(115200);

x=myArray[0]; y=myArray[1]; Location[x][y]=1; //write 1 to strat position myArray[2]=myArray[2]+1; //Step+1 q.push(&myArray); }

void loop() {

int myArray1[3]={0,0,0};// 1. X 2.Y 3 Step

// Strat loop if currentStep greater 1 and enter location be Occupied if ( Location[4][4]==0 ) { // get data from queue q.pop(&myArray1); Serial.println(myArray1[0]); Serial.println(myArray1[1]); Serial.println(myArray1[2]); delay(500);

// move right
   x=myArray1[0]+1;           // X Coordinate+1 to int x 
   y=myArray1[1];             // y Coordinate int y 
   if(Location[x][y]==0)     // execute if value qeaul 0 in location
   {
   Location[x][y]=1;         // Ooccupy current location
   }
   myArray1[0]=myArray1[0]+1;           // X Coordinate+1 to int x 
   myArray1[2]=myArray1[2]+1;  // Step number +1  
   q.push(&myArray1);
   x=myArray1[0]-1;             // restore X coordinate
   delay(50);

 //move down
   x=myArray[0];           // X Coordinate+1 to int x 
   y=myArray[1]+1;             // y Coordinate int y 
   if(Location[x][y]==0)     // execute if value qeaul 0 in location
   {
   Location[x][y]=1;         // Ooccupy current location 
   }
    myArray[1]=myArray[1]+1;             // y Coordinate int y 
    myArray[2]=myArray[2]+1;  // Step number +1 
    q.push(&myArray);
    y=myArray[1]-1;             // restore X coordinate

   delay(50);

if (Location[4][4]==1)
{
 Serial.println("Congratulations !!! The shortest path is: "); 
 Serial.println(myArray1[2]);
 Serial.println(" Step");
  }

}

}