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.
Dependents: CameraC328_TestProgram CameraC328_Thresholding Camera_TestProgram_2015 Camera_TestProgram_2015 ... more
Diff: SerialBuffered.cpp
- Revision:
- 15:49cfda6c547f
- Parent:
- 13:17cf1e2015f7
diff -r 640f564075af -r 49cfda6c547f SerialBuffered.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialBuffered.cpp Wed Oct 13 10:45:29 2010 +0000
@@ -0,0 +1,99 @@
+#include "mbed.h"
+#include "SerialBuffered.h"
+
+/**
+ * Create a buffered serial class.
+ *
+ * @param tx A pin for transmit.
+ * @param rx A pin for receive.
+ */
+SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) {
+ indexContentStart = 0;
+ indexContentEnd = 0;
+ timeout = 1;
+ attach(this, &SerialBuffered::handleInterrupt);
+}
+
+/**
+ * Destroy.
+ */
+SerialBuffered::~SerialBuffered() {
+}
+
+/**
+ * Set timeout for getc().
+ *
+ * @param ms milliseconds. (-1:Disable timeout)
+ */
+void SerialBuffered::setTimeout(int ms) {
+ timeout = ms;
+}
+
+/**
+ * Read requested bytes.
+ *
+ * @param bytes A pointer to a buffer.
+ * @param requested Length.
+ *
+ * @return Readed byte length.
+ */
+size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) {
+ int i = 0;
+ while (i < requested) {
+ int c = getc();
+ if (c < 0) {
+ break;
+ }
+ bytes[i] = c;
+ i++;
+ }
+ return i;
+}
+
+/**
+ * Get a character.
+ *
+ * @return A character. (-1:timeout)
+ */
+int SerialBuffered::getc() {
+ timer.reset();
+ timer.start();
+ while (indexContentStart == indexContentEnd) {
+ wait_ms(1);
+ if ((timeout > 0) && (timer.read_ms() > timeout)) {
+ /*
+ * Timeout occured.
+ */
+ // printf("Timeout occured.\n");
+ return EOF;
+ }
+ }
+ timer.stop();
+
+ uint8_t result = buffer[indexContentStart++];
+ indexContentStart = indexContentStart % BUFFERSIZE;
+
+ return result;
+}
+
+/**
+ * Returns 1 if there is a character available to read, 0 otherwise.
+ */
+int SerialBuffered::readable() {
+ return indexContentStart != indexContentEnd;
+}
+
+void SerialBuffered::handleInterrupt() {
+ while (Serial::readable()) {
+ if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) {
+ /*
+ * Buffer overrun occured.
+ */
+ // printf("Buffer overrun occured.\n");
+ Serial::getc();
+ } else {
+ buffer[indexContentEnd++] = Serial::getc();
+ indexContentEnd = indexContentEnd % BUFFERSIZE;
+ }
+ }
+}
CameraC328