SMFSW / Queue

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

Here's a more simple example script, in case it's useful to anyone #16

Closed wrybread closed 1 year ago

wrybread commented 2 years ago

I had some trouble using the SimpleQueue.ino example script, here's SimplerQueue.ino on the off chance it's useful to anyone:

#include <cppQueue.h>

typedef struct strRec {
  char  url[300];
  char  misc1[255];
} Rec;

cppQueue  queue1(sizeof(Rec), 10, LIFO, true); 

void setup() {
  Serial.begin(115200); 

  // Put a sample record into the queue
  Rec test = {"http://google.com", "some stuff"};
  queue1.push(&test);

}

void loop() {  

  // iterate through the queue
  while (!queue1.isEmpty())
  {
    Rec rec;
    queue1.pop(&rec);
    Serial.println("There's something in the queue!");
    Serial.println();
    Serial.print(rec.url);
    Serial.print(" ");
    Serial.print(rec.misc1);

    Serial.println();
    String url = rec.url;
    Serial.println("URL = " + url);

    Serial.println();
  }   
  delay(1000);
}
SMFSW commented 2 years ago

thanks for sharing to user it may help, and for your interest in, using Queue library!

You may create your own example as a new sketch in examples folder and make a pull request.

Regards, SMFSW

Jarboer commented 2 years ago

@wrybread I really liked your example as it was more straight forward and closer to the idea I would want to implement in a project of mine. However, I ended up unable to run it properly on my Arduino Uno. I was so confused because if I modified it to take integers instead then it worked. It eventually occurred to me I might be running out of RAM and well sure enough by cutting down a few things I was able to get it working with the char arrays! So if anyone else was having issues getting it to work then here's what I ended up with to get it to work:

#include <cppQueue.h>

typedef struct strRec {
  /* Reduced both char array sizes significantly as they were extremely 
     large and causing the program to not work correctly */
  char url[50];
  char misc1[50];
} Record;

cppQueue queue1(sizeof(Record), 10, LIFO, false);

void setup() {
  Serial.begin(9600);

  Record test = { "http://google.com", "some stuff" };
  queue1.push(&test);
}

void loop() {
  // iterate through the queue
  while (!queue1.isEmpty()) {
    Record queueRecord;
    queue1.pop(&queueRecord);

    /* Surrounded the String in F() to move it from RAM 
       to the flash memory (which there is more of) */
    Serial.println(F("There's something in the queue!"));
    Serial.println();
    Serial.print(queueRecord.url);
    Serial.print(" ");
    Serial.print(queueRecord.misc1);

    /* NOTE: Using the String class increases the flash memory 
             and RAM usage but I didn't need to remove this */
    Serial.println();
    String url = queueRecord.url;
    Serial.print("URL = ");
    Serial.println(url);

    Serial.println();
  }

  delay(1000);
}