File logger_impl.h¶
File List > external-docs > libDaisy > src > hid > logger_impl.h
Go to the documentation of this file
Source Code¶
#pragma once
#ifndef __DSY_LOGGER_IMPL_H
#define __DSY_LOGGER_IMPL_H
#include <unistd.h>
#include <cstdint>
#include <cassert>
#include "hid/usb.h"
#include "per/uart.h"
#include "sys/system.h"
#include "daisy_core.h"
namespace daisy
{
enum LoggerDestination
{
LOGGER_NONE,
LOGGER_INTERNAL,
LOGGER_EXTERNAL,
LOGGER_SEMIHOST,
LOGGER_UART,
};
template <LoggerDestination dest>
class LoggerImpl
{
public:
static void Init() {}
static bool Transmit(const void* buffer, size_t bytes) { return true; }
};
template <>
class LoggerImpl<LOGGER_INTERNAL>
{
public:
static void Init()
{
static_assert(1u == sizeof(usb_handle_), "UsbHandle is not static");
usb_handle_.Init(UsbHandle::FS_INTERNAL);
}
static bool Transmit(const void* buffer, size_t bytes)
{
return UsbHandle::Result::OK
== usb_handle_.TransmitInternal((uint8_t*)buffer, bytes);
}
protected:
static UsbHandle usb_handle_;
};
template <>
class LoggerImpl<LOGGER_EXTERNAL>
{
public:
static void Init()
{
static_assert(1u == sizeof(usb_handle_), "UsbHandle is not static");
usb_handle_.Init(UsbHandle::FS_EXTERNAL);
}
static bool Transmit(const void* buffer, size_t bytes)
{
return UsbHandle::Result::OK
== usb_handle_.TransmitExternal((uint8_t*)buffer, bytes);
}
protected:
static UsbHandle usb_handle_;
};
template <>
class LoggerImpl<LOGGER_SEMIHOST>
{
public:
static void Init() {}
static bool Transmit(const void* buffer, size_t bytes)
{
write(STDOUT_FILENO, buffer, bytes);
return true;
}
};
template <>
class LoggerImpl<LOGGER_UART>
{
public:
struct Config
{
UartHandler::Config::Peripheral uart;
Pin tx_pin;
uint32_t baudrate = 115200;
};
static void Configure(const Config cfg) { config_ = cfg; }
static void Init()
{
if(!config_.tx_pin.IsValid())
return;
UartHandler::Config uart_cfg;
uart_cfg.mode = UartHandler::Config::Mode::TX;
uart_cfg.periph = config_.uart;
uart_cfg.pin_config.tx = config_.tx_pin;
uart_cfg.baudrate = config_.baudrate;
uart_.Init(uart_cfg);
}
static bool Transmit(const void* buffer, size_t bytes)
{
return uart_.BlockingTransmit((uint8_t*)(buffer), bytes, 20)
== UartHandler::Result::OK;
}
protected:
static Config config_;
static UartHandler uart_;
};
} /* namespace daisy */
#endif //__DSY_LOGGER_IMPL_H