diff options
Diffstat (limited to '')
-rw-r--r-- | hal/uart.tpp | 52 |
1 files changed, 51 insertions, 1 deletions
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<unsigned dev> inline std::queue<uint8_t>& rx_buffer() { + static_assert(dev <= devices_count, "invalid device number"); return uart::_rx_buffer[dev -1]; } template<unsigned dev> inline std::queue<uint8_t>& tx_buffer() { + static_assert(dev <= devices_count, "invalid device number"); return uart::_tx_buffer[dev -1]; } + + // read from serial device template<unsigned dev> uint8_t read() { @@ -46,6 +51,47 @@ namespace uart return byte; } +#if 0 + template<unsigned dev> + std::string read(const unsigned len) + { + std::string str = ""; + unsigned i = len; + + if (rx_buffer<dev>().size() < len) { + i = rx_buffer<dev>().size(); + } + + while (i--) { + str += read<dev>(); + } + + return str; + } + + template<unsigned dev> + std::string readline(const std::string& eol = "\n\r") + { + int eol_c = 0; + + std::string str = ""; + unsigned i = rx_buffer<dev>().size(); + + while (i--) { + str += read<dev>(); + + // compare string endings + // TODO: make it more efficient + if (eol_c == eol.size()) { + break; + } + + if (str.back() == eol[eol_c]) { + eol_c++; + } + } + } + template<unsigned dev> unsigned read(uint8_t *buffer, const unsigned numbytes) { @@ -56,7 +102,10 @@ namespace uart *(bptr++) = read<dev>(); } } +#endif + + // write to serial device template<unsigned dev> 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() { |