Giter VIP home page Giter VIP logo

ad-oprintstream's Introduction

OPrintStream

The extendable, not a stream but behaves like a stream, Arduino printing convenience library.

Abstract

Concept for this library is simple. If you want to write your Arduino print statements using less code and in one line using modern C++ practices, you install this library. :)

This library has a hard dependency on Arduino as it is written to specifically to use Arduino Serial printing methods. It is confirmed working for Arduino MKR 1010 WiFi, but probably needs some very simple tweaking to work on other boards. Help is appreciated in that department.

Dependencies

Building

Using

There is a basic example here. It wraps all of the Arduino Serial Print functions with << overloads so you can Print like you would using the OStringStream STL construct in modern C++.

OPrintStream<> print(USBDevice);
print << bin << "Testing... " << 1 << " " << 2 << endl;

You can declare an extern instance of the class that you can reference in each of your compilation units, just as you would normally refer to Arduino's Serial.

Extending

Extending the class to be able to print your own classes or structs is also pretty simple. You can either:

  1. Have your object extend the Arduino Printable class and implement the virtual size_t printTo(Print& p); method. In this case you would not have to extend OPrintStream.
  2. If you want to print an object that you do not own, say boost::string_view, you can extend OPrintStream using the Curiously Recurring Template Pattern to specify your class as the overload return type. Example:
// "Copyright 2021 Francisco Aguilera"

#ifndef INCLUDE_LOGGER_HPP_
#define INCLUDE_LOGGER_HPP_

#include <USB/USBAPI.h>
#include <oprintstream.hpp>
#include <boost_1_76_0.h>
#include <boost/utility/string_view.hpp>

namespace falven {
namespace ad {

class Logger : public OPrintStream<Logger> {
 public:
  Logger(USBDeviceClass &usb) : OPrintStream(usb) {}
  ~Logger() override = default;
  ...

  /**
   * @brief Prints the string referenced by the provided boost::string_view.
   *
   * @param arg The boost::stringview to print.
   * @return OPrintStream& Returns the OPrintStream reference for
   * continuation.
   */
  OPrintStream &operator<<(const boost::string_view &arg) {
    print(arg.data());
    return *this;
  }

  // Bring our base class operator overloads into scope.
  using OPrintStream<Logger>::operator<<;
};

}  // namespace ad
}  // namespace falven

#endif  // INCLUDE_LOGGER_HPP_

ad-oprintstream's People

Contributors

falven avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.