xrocusOS_ADXL355 version

Dependencies:   mbed SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UartReceiver.cpp Source File

UartReceiver.cpp

00001 #include "UartReceiver.h"
00002 #include "global.h"
00003 
00004 void isrUartRx() {
00005     pUR->concat();
00006 }
00007 
00008 /* Constructor(Accepted) */
00009 UartReceiver::UartReceiver(Serial *pSetUart)
00010 {
00011     nesthead = 0;
00012     nesttail = 0;
00013     tail = 0;
00014     head = 0;
00015     pUart = pSetUart;
00016     pLineHandler = NULL;
00017 }
00018 
00019 /* Start Handling */
00020 void UartReceiver::run(void)
00021 {
00022     pUart->attach(isrUartRx, Serial::RxIrq);
00023 }
00024 
00025 /* Stop Handling */
00026 void UartReceiver::stop(void)
00027 {
00028     pUart->attach(NULL, Serial::RxIrq);
00029 }
00030 
00031 /* Update Line Handler Handling */
00032 void* UartReceiver::setLineHandler(void(*setLineHandler)(char*))
00033 {
00034     void(*prevLineHandler)(char*) = pLineHandler;
00035     pLineHandler = setLineHandler;
00036     return (void*)prevLineHandler;
00037 }
00038 
00039 /* Export Fixed Buffer Pointer */
00040 char* UartReceiver::dequeue_str(void)
00041 {
00042     char *ret = NULL;
00043     if (nesthead != nesttail) {
00044         ret = &inner_buffer[nesthead][0];
00045         nesthead++;
00046         if (nesthead >= RXBUF_NEST) {
00047             nesthead = 0;
00048         }
00049     }
00050     return ret;
00051 }
00052 
00053 /* Recv char from UART and Concat to Liquid Buffer */
00054 void UartReceiver::concat()
00055 {
00056     char ch;
00057     int fixed;
00058     /* connect all buffers */
00059     while (pUart->readable() != 0) {
00060         ch = pUart->getc();
00061         fixed = this->enqueue_ch(ch);
00062         if (fixed != BufOpen) {
00063             if (pLineHandler != NULL) {
00064                 (this->pLineHandler)(this->dequeue_str());
00065             } else {
00066                 uprintf("CALL DEF HANDLER:%s", this->dequeue_str());
00067             }
00068         }
00069     }
00070 }
00071 
00072 /* Get Pointer */
00073 Serial *UartReceiver::getCurrentUart(void)
00074 {
00075     return this->pUart;
00076 }
00077 
00078 /* Concat to Liquid Buffer */
00079 /** return UartReceiver::BufFixed, UartReceiver::BufOpen */
00080 int UartReceiver::enqueue_ch(char ch)
00081 {
00082     int ret = BufOpen;
00083     inner_buffer[nesttail][tail] = ch;
00084     if (ch == '\n' || ch == '\r') {
00085         /** switch buffer */
00086         inner_buffer[nesttail][tail] = '\0';
00087         nesttail++;
00088         tail = 0;
00089         if (nesttail >= RXBUF_NEST) {
00090             nesttail = 0;
00091         }
00092         ret = BufFixed;
00093     } else {
00094         tail++;
00095         if (tail >= RXBUF_SIZE) {
00096             /** -- stack overflow --- */
00097             /** IGNORED **/
00098             tail = RXBUF_SIZE - 1;
00099         }
00100     }
00101     return ret;
00102 }