UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]
Dependencies: mbed SDFileSystem
common/UartReceiver.cpp@0:c347f602596d, 2019-04-20 (annotated)
- Committer:
- Inscape_ao
- Date:
- Sat Apr 20 09:23:39 2019 +0000
- Revision:
- 0:c347f602596d
- Child:
- 13:7cda5bef6390
create UartReceiver, CommandParser
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Inscape_ao | 0:c347f602596d | 1 | #include "UartReceiver.h" |
Inscape_ao | 0:c347f602596d | 2 | #include "global.h" |
Inscape_ao | 0:c347f602596d | 3 | |
Inscape_ao | 0:c347f602596d | 4 | void isrUartRx() { |
Inscape_ao | 0:c347f602596d | 5 | pUR->concat(); |
Inscape_ao | 0:c347f602596d | 6 | } |
Inscape_ao | 0:c347f602596d | 7 | |
Inscape_ao | 0:c347f602596d | 8 | /* Constructor(Accepted) */ |
Inscape_ao | 0:c347f602596d | 9 | UartReceiver::UartReceiver(Serial *pSetUart) |
Inscape_ao | 0:c347f602596d | 10 | { |
Inscape_ao | 0:c347f602596d | 11 | nesthead = 0; |
Inscape_ao | 0:c347f602596d | 12 | nesttail = 0; |
Inscape_ao | 0:c347f602596d | 13 | tail = 0; |
Inscape_ao | 0:c347f602596d | 14 | head = 0; |
Inscape_ao | 0:c347f602596d | 15 | pUart = pSetUart; |
Inscape_ao | 0:c347f602596d | 16 | pLineHandler = NULL; |
Inscape_ao | 0:c347f602596d | 17 | } |
Inscape_ao | 0:c347f602596d | 18 | |
Inscape_ao | 0:c347f602596d | 19 | /* Start Handling */ |
Inscape_ao | 0:c347f602596d | 20 | void UartReceiver::run(void) |
Inscape_ao | 0:c347f602596d | 21 | { |
Inscape_ao | 0:c347f602596d | 22 | pUart->attach(isrUartRx, Serial::RxIrq); |
Inscape_ao | 0:c347f602596d | 23 | } |
Inscape_ao | 0:c347f602596d | 24 | |
Inscape_ao | 0:c347f602596d | 25 | /* Stop Handling */ |
Inscape_ao | 0:c347f602596d | 26 | void UartReceiver::stop(void) |
Inscape_ao | 0:c347f602596d | 27 | { |
Inscape_ao | 0:c347f602596d | 28 | pUart->attach(NULL, Serial::RxIrq); |
Inscape_ao | 0:c347f602596d | 29 | } |
Inscape_ao | 0:c347f602596d | 30 | |
Inscape_ao | 0:c347f602596d | 31 | /* Update Line Handler Handling */ |
Inscape_ao | 0:c347f602596d | 32 | void* UartReceiver::setLineHandler(void(*setLineHandler)(char*)) |
Inscape_ao | 0:c347f602596d | 33 | { |
Inscape_ao | 0:c347f602596d | 34 | void(*prevLineHandler)(char*) = pLineHandler; |
Inscape_ao | 0:c347f602596d | 35 | pLineHandler = setLineHandler; |
Inscape_ao | 0:c347f602596d | 36 | return (void*)prevLineHandler; |
Inscape_ao | 0:c347f602596d | 37 | } |
Inscape_ao | 0:c347f602596d | 38 | |
Inscape_ao | 0:c347f602596d | 39 | /* Export Fixed Buffer Pointer */ |
Inscape_ao | 0:c347f602596d | 40 | char* UartReceiver::dequeue_str(void) |
Inscape_ao | 0:c347f602596d | 41 | { |
Inscape_ao | 0:c347f602596d | 42 | char *ret = NULL; |
Inscape_ao | 0:c347f602596d | 43 | if (nesthead != nesttail) { |
Inscape_ao | 0:c347f602596d | 44 | ret = &inner_buffer[nesthead][0]; |
Inscape_ao | 0:c347f602596d | 45 | nesthead++; |
Inscape_ao | 0:c347f602596d | 46 | if (nesthead >= RXBUF_NEST) { |
Inscape_ao | 0:c347f602596d | 47 | nesthead = 0; |
Inscape_ao | 0:c347f602596d | 48 | } |
Inscape_ao | 0:c347f602596d | 49 | } |
Inscape_ao | 0:c347f602596d | 50 | return ret; |
Inscape_ao | 0:c347f602596d | 51 | } |
Inscape_ao | 0:c347f602596d | 52 | |
Inscape_ao | 0:c347f602596d | 53 | /* Recv char from UART and Concat to Liquid Buffer */ |
Inscape_ao | 0:c347f602596d | 54 | void UartReceiver::concat() |
Inscape_ao | 0:c347f602596d | 55 | { |
Inscape_ao | 0:c347f602596d | 56 | char ch; |
Inscape_ao | 0:c347f602596d | 57 | int fixed; |
Inscape_ao | 0:c347f602596d | 58 | /* connect all buffers */ |
Inscape_ao | 0:c347f602596d | 59 | while (pUart->readable() != 0) { |
Inscape_ao | 0:c347f602596d | 60 | ch = pUart->getc(); |
Inscape_ao | 0:c347f602596d | 61 | fixed = this->enqueue_ch(ch); |
Inscape_ao | 0:c347f602596d | 62 | if (fixed != BufOpen) { |
Inscape_ao | 0:c347f602596d | 63 | if (pLineHandler != NULL) { |
Inscape_ao | 0:c347f602596d | 64 | (this->pLineHandler)(this->dequeue_str()); |
Inscape_ao | 0:c347f602596d | 65 | } else { |
Inscape_ao | 0:c347f602596d | 66 | pUart->printf("CALL DEF HANDLER:%s", this->dequeue_str()); |
Inscape_ao | 0:c347f602596d | 67 | } |
Inscape_ao | 0:c347f602596d | 68 | } |
Inscape_ao | 0:c347f602596d | 69 | } |
Inscape_ao | 0:c347f602596d | 70 | } |
Inscape_ao | 0:c347f602596d | 71 | |
Inscape_ao | 0:c347f602596d | 72 | /* Get Pointer */ |
Inscape_ao | 0:c347f602596d | 73 | Serial *UartReceiver::getCurrentUart(void) |
Inscape_ao | 0:c347f602596d | 74 | { |
Inscape_ao | 0:c347f602596d | 75 | return this->pUart; |
Inscape_ao | 0:c347f602596d | 76 | } |
Inscape_ao | 0:c347f602596d | 77 | |
Inscape_ao | 0:c347f602596d | 78 | /* Concat to Liquid Buffer */ |
Inscape_ao | 0:c347f602596d | 79 | /** return UartReceiver::BufFixed, UartReceiver::BufOpen */ |
Inscape_ao | 0:c347f602596d | 80 | int UartReceiver::enqueue_ch(char ch) |
Inscape_ao | 0:c347f602596d | 81 | { |
Inscape_ao | 0:c347f602596d | 82 | int ret = BufOpen; |
Inscape_ao | 0:c347f602596d | 83 | inner_buffer[nesttail][tail] = ch; |
Inscape_ao | 0:c347f602596d | 84 | if (ch == '\n' || ch == '\r') { |
Inscape_ao | 0:c347f602596d | 85 | /** switch buffer */ |
Inscape_ao | 0:c347f602596d | 86 | inner_buffer[nesttail][tail] = '\0'; |
Inscape_ao | 0:c347f602596d | 87 | nesttail++; |
Inscape_ao | 0:c347f602596d | 88 | tail = 0; |
Inscape_ao | 0:c347f602596d | 89 | if (nesttail >= RXBUF_NEST) { |
Inscape_ao | 0:c347f602596d | 90 | nesttail = 0; |
Inscape_ao | 0:c347f602596d | 91 | } |
Inscape_ao | 0:c347f602596d | 92 | ret = BufFixed; |
Inscape_ao | 0:c347f602596d | 93 | } else { |
Inscape_ao | 0:c347f602596d | 94 | tail++; |
Inscape_ao | 0:c347f602596d | 95 | if (tail >= RXBUF_SIZE) { |
Inscape_ao | 0:c347f602596d | 96 | /** -- stack overflow --- */ |
Inscape_ao | 0:c347f602596d | 97 | /** IGNORED **/ |
Inscape_ao | 0:c347f602596d | 98 | tail = RXBUF_SIZE - 1; |
Inscape_ao | 0:c347f602596d | 99 | } |
Inscape_ao | 0:c347f602596d | 100 | } |
Inscape_ao | 0:c347f602596d | 101 | return ret; |
Inscape_ao | 0:c347f602596d | 102 | } |