Eric Wu / Mbed 2 deprecated WifiRobot

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TimeoutMultipleSerial.cpp Source File

TimeoutMultipleSerial.cpp

00001 #include "TimeoutMultipleSerial.h"
00002 
00003 int TimeoutMultipleSerial::readMultChars (uint8_t* const dest, 
00004     const size_t size) {
00005 
00006     timer.reset();
00007     timer.start();
00008 
00009     size_t counter = 0;
00010     while (counter < size 
00011         && timer.read_ms() < timeout) {
00012 
00013         while (!port.readable() 
00014             && timer.read_ms() < timeout) wait_ms(1);
00015 
00016         if (port.readable()) {
00017             *(dest + counter) = (uint8_t) (port.getc());
00018             ++counter;
00019         }
00020     }
00021 
00022     timer.stop();
00023 
00024     if (counter != size) {
00025         return -1;
00026     }
00027 
00028     return 1;
00029 }
00030 
00031 int TimeoutMultipleSerial::writeMultChars (const uint8_t* source,
00032     const size_t size) {
00033 
00034     timer.reset();
00035     timer.start();
00036 
00037     size_t counter = 0;
00038     while (counter < size
00039         && timer.read_ms() < timeout) {
00040         
00041         while (!port.writeable()
00042             && timer.read_ms() < timeout) wait_ms(1);
00043 
00044         if (port.writeable()) {
00045             port.putc((int) (*(source + counter)));
00046             ++counter;
00047         }
00048     }
00049 
00050     timer.stop();
00051     if (counter != size) {
00052         return -1;
00053     }
00054 
00055     return 1;
00056 }
00057 
00058 int TimeoutMultipleSerial::clearAll() {
00059     while (port.readable()) port.getc();
00060     return 1;
00061 }
00062 
00063 void TimeoutMultipleSerial::setTimeout (uint32_t tout) {
00064     timeout = tout;
00065 }
00066 
00067 void TimeoutMultipleSerial::setBaud (uint32_t baud) {
00068     port.baud(baud);
00069 }
00070 
00071 void TimeoutMultipleSerial::setFormat (int bits, 
00072     SerialBase::Parity parity, 
00073     int stop_bits) {
00074 
00075     port.format(bits, parity, stop_bits);
00076 }