/*
 * ll_consolehandler.h
 *
 *  Created on: Nov 7, 2019
 *      Author: abody
 */

#pragma once

#include <f4ll/initialized_singleton.h>
#include <f4ll/packet_usart.h>
#include <f4ll/ringbuffer.h>

namespace f4ll {

class console_handler : public usart_core, public initialized_singleton<console_handler>
{
    friend class initialized_singleton<console_handler>;

public:
    void print(char const *s);
    void flush();
    size_t append(char const *s);

private:
    console_handler(USART_TypeDef *usart, DMA_TypeDef *dma, uint32_t stream_rx, uint32_t stream_tx);

    // LL_UsartCore pure virtual function implementations
    virtual void receiver_idle(void) override;
    virtual void transmission_complete(void) override;
    virtual void framing_error(void) override;
    virtual void overrun(void) override;
    virtual void rx_dma_transfer_complete(void) override;
    virtual void rx_dma_half_transfer(void) override;
    virtual void rx_dma_error(dma_helper::dma_error_type reason) override;
    virtual void tx_dma_transfer_complete(void) override;
    virtual void tx_dma_half_transfer(void) override;
    virtual void tx_dma_error(dma_helper::dma_error_type reason) override;

    ringbuffer<128> m_tx_buffer;
    iringbuffer::size_type m_in_flight_size = 0;
};

} /* namespace f4ll */
