Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:043b1271ed1b, committed 2017-08-02
- Comitter:
- williampeers
- Date:
- Wed Aug 02 03:21:02 2017 +0000
- Child:
- 1:f9e6627f1f59
- Commit message:
- Working, in isolation
Changed in this revision
| objectQueue.cpp | Show annotated file Show diff for this revision Revisions of this file |
| objectQueue.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/objectQueue.cpp Wed Aug 02 03:21:02 2017 +0000
@@ -0,0 +1,108 @@
+#include "objectQueue.h"
+
+StrQueue::StrQueue() {
+ chars = (char*)malloc(sizeof(char[1]));
+ size = 1;
+ front = 0;
+ back = 0;
+ empty = true;
+ full = false;
+}
+
+StrQueue::StrQueue(int size) {
+ chars = (char*)malloc(sizeof(char[size]));
+ size = size;
+ front = 0;
+ back = 0;
+ empty = true;
+ full = false;
+}
+
+StrQueue::~StrQueue(void) {
+ free(chars);
+}
+
+int StrQueue::put(const char* message, int length) {
+ lock.lock();
+ int i;
+ if (length == 0) {
+ for (i = 0; empty||(front != (back+i)%size); i++) {
+ if ((chars[back + i] = message[i]) == '\0') {
+ back = (back + i + 1)%size;
+ empty = false;
+ if (front == back) {
+ full = true;
+ }
+ lock.unlock();
+ return(i);
+ }
+ }
+ if (front == back) {
+ full = true;
+ }
+ lock.unlock();
+ return(0);
+ }
+ else {
+ if ((back - front)%size <= length) {
+ lock.unlock();
+ return(0);
+ }
+ for (i = 0; empty||(i <= length); i++) {
+ chars[back + i] = message[i];
+ }
+ back = (back + i + 1)%size;
+ empty = false;
+ if (front == back) {
+ full = true;
+ }
+ lock.unlock();
+ return(i);
+ }
+}
+
+int StrQueue::get(char* out, int length) {
+ lock.lock();
+ int i = 0;
+ if (empty) {
+ lock.unlock();
+ return(0);
+ }
+ for (i = 0;; i++) {
+ if (i > length) {
+ strncpy(out, "", length);
+ return(0);
+ }
+ if ((out[i] = chars[(front + i)%size]) == '\0') {
+ front = (front + i + 1)%size;
+ break;
+ }
+ }
+ if (front == back) {
+ empty = true;
+ }
+ lock.unlock();
+
+ return(i);
+}
+
+int StrQueue::getChars(char* buffer, int size) {
+ int ret = 0, total = 0;
+ lock.lock();
+ if (empty) {return(0);}
+ while ((ret = get(buffer + ret, size - total)) > 0) {
+ buffer += ret;
+ total += ret;
+ ret = 0;
+ }
+ lock.unlock();
+ return(total);
+}
+
+bool StrQueue::isfull() {
+ return(full);
+}
+
+bool StrQueue::isEmpty() {
+ return(empty);
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/objectQueue.h Wed Aug 02 03:21:02 2017 +0000
@@ -0,0 +1,25 @@
+#ifndef __STRQUEUE_INCLUDED__
+#define __STRQUEUE_INCLUDED__
+
+#include "mbed.h"
+
+class StrQueue {
+public:
+ StrQueue();
+ StrQueue(int size);
+ ~StrQueue();
+ int put(const char* message, int length = 0);
+ int get(char* str, int size);
+ int getChars(char* chars, int size);
+ bool isfull();
+ bool isEmpty();
+
+private:
+ Mutex lock;
+ bool empty, full;
+ int front, back;
+ int size;
+ char* chars;
+};
+
+#endif
\ No newline at end of file