From e82789253b7c915270cfdc126e8e000924d90444 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Fri, 4 May 2018 20:47:39 +0200 Subject: Implement TX read(n) and readline() (not working yet) --- hal/uart.tpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'hal/uart.tpp') diff --git a/hal/uart.tpp b/hal/uart.tpp index e4b722e..148ff7d 100644 --- a/hal/uart.tpp +++ b/hal/uart.tpp @@ -25,18 +25,23 @@ extern "C" { /* templated functions */ namespace uart { + // access buffers template inline std::queue& rx_buffer() { + static_assert(dev <= devices_count, "invalid device number"); return uart::_rx_buffer[dev -1]; } template inline std::queue& tx_buffer() { + static_assert(dev <= devices_count, "invalid device number"); return uart::_tx_buffer[dev -1]; } + + // read from serial device template uint8_t read() { @@ -46,6 +51,47 @@ namespace uart return byte; } +#if 0 + template + std::string read(const unsigned len) + { + std::string str = ""; + unsigned i = len; + + if (rx_buffer().size() < len) { + i = rx_buffer().size(); + } + + while (i--) { + str += read(); + } + + return str; + } + + template + std::string readline(const std::string& eol = "\n\r") + { + int eol_c = 0; + + std::string str = ""; + unsigned i = rx_buffer().size(); + + while (i--) { + str += read(); + + // compare string endings + // TODO: make it more efficient + if (eol_c == eol.size()) { + break; + } + + if (str.back() == eol[eol_c]) { + eol_c++; + } + } + } + template unsigned read(uint8_t *buffer, const unsigned numbytes) { @@ -56,7 +102,10 @@ namespace uart *(bptr++) = read(); } } +#endif + + // write to serial device template void write(const uint8_t byte) { @@ -94,10 +143,11 @@ namespace uart /* specializations for UART1 */ -// debug +#ifdef DEBUG #include "pin.tpp" pin<2> led_blue(&LATBbits, &TRISBbits, &PORTBbits); pin<3> led_green(&LATBbits, &TRISBbits, &PORTBbits); +#endif void __ISR(_UART_1_VECTOR, IPL1AUTO) usart_1_isr() { -- cgit v1.2.1