Skip to content

Commit

Permalink
added concepts: Pattern, Color, more Patterns added
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Aug 2, 2021
1 parent 925d7b6 commit febf47d
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 109 deletions.
5 changes: 3 additions & 2 deletions src/modm/driver/display/ili9341.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace modm

/// @ingroup modm_driver_ili9341
template<class Interface, class Reset, size_t BC = 1024>
// requires std::derived_from<Painter, RemotePainter<R>>
class Ili9341 : public Interface,
// : public RemotePainter<Resolution<320, 240>, Interface>,
public Display<color::Rgb565<true>, Resolution<320, 240>, true>,
Expand Down Expand Up @@ -99,11 +100,11 @@ class Ili9341 : public Interface,
scrollTo(uint16_t row);

// Write from Pattern (compiletime poly)
template<class Pattern>
template<ColorPattern P>
// FIXME Concept not accepted - but why?
// requires std::derived_from<Pattern, modm::color::Pattern<colorType>>
modm::ResumableResult<void>
writePattern(Rectangle rectangle, Pattern generator);
writePattern(Rectangle rectangle, P pattern);

// Write equal colored Buffer (compiletime poly)
template<typename R_, class Painter_>
Expand Down
4 changes: 2 additions & 2 deletions src/modm/driver/display/ili9341_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ modm::Ili9341<Interface, Reset, BC>::updateClipping()

// -- Write from Pattern ---------------------------------
template<class Interface, class Reset, size_t BC>
template<class Pattern>
template<ColorPattern P>
modm::ResumableResult<void>
modm::Ili9341<Interface, Reset, BC>::writePattern(Rectangle rectangle, Pattern pattern) {
modm::Ili9341<Interface, Reset, BC>::writePattern(Rectangle rectangle, P pattern) {
RF_BEGIN();

this->clipping = this->getIntersection(rectangle);
Expand Down
7 changes: 6 additions & 1 deletion src/modm/ui/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------
#pragma once

#include "color/rgb.hpp"
#include "color/hsv.hpp"
Expand All @@ -20,4 +21,8 @@
#include "color/rgb565.hpp"
#include "color/rgbhtml.hpp"

#include "color/pattern.hpp"
namespace modm::color {
template <class C>
concept Color = requires
{ std::convertible_to<C, modm::color::Rgb> or std::is_same_v<C, bool>; };
}
2 changes: 0 additions & 2 deletions src/modm/ui/color/grayscale.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ template<std::unsigned_integral T>
class GrayscaleT
{
public:
static constexpr bool isColor = true;

T value{0};

constexpr GrayscaleT() = default;
Expand Down
2 changes: 0 additions & 2 deletions src/modm/ui/color/hsv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ template<std::unsigned_integral T>
class HsvT
{
public:
static constexpr bool isColor = true;

T hue{0};
T saturation{0};
T value{0};
Expand Down
11 changes: 10 additions & 1 deletion src/modm/ui/color/pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@
#pragma once

#include "pattern/gradient.hpp"
#include "pattern/interference.hpp"
#include "pattern/interference.hpp"

#include "../shape.hpp"

// TODO find a good place
template <class P>
concept ColorPattern = requires(P p)
{
{ p.operator()(modm::shape::Point()) } -> std::convertible_to<modm::color::Rgb>;
};
21 changes: 10 additions & 11 deletions src/modm/ui/color/pattern/gradient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,30 @@
#pragma once

#include "pattern.hpp"
#include <modm/ui/shape.hpp>

using namespace modm::shape;

namespace modm::color
{

template<class C>
requires C::isColor class Gradient
template<Color C>
class Gradient : public BicolorPattern<C>
{
protected:
Line line;
C start, end;
const Point direction;

public:
constexpr Gradient() = default;

constexpr Gradient(Line line, C start, C end) : line(line), start(start), end(end) {}
constexpr Gradient(C start, C end, Point direction = {1, 0})
: BicolorPattern<C>(start, end), direction(direction) {}

constexpr C
operator()(Point position)
operator()(Point point)
{
// IMPLEMENT linear interpolate between start and end
// facilitating line and offset;
(void)position;
return start;

// Creates a rainbow for now
return Hsv(point.x, 255, point.y);
}
};
}
98 changes: 57 additions & 41 deletions src/modm/ui/color/pattern/interference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ namespace modm::color
namespace detail
{
template<std::unsigned_integral T>
class RectangularFunc
class RectFunc
{
protected:
const T width;
const modm::WideType<T> period;

public:
constexpr RectangularFunc(T low, T high) : width(low), period(low + high) {}
constexpr RectFunc(T low, T high) : width(low), period(low + high) {}

constexpr bool
operator()(modm::WideType<T> input)
Expand All @@ -41,100 +41,116 @@ class RectangularFunc
}
};

// Preprocesses point.x and point.y with Preprocess
// Passes result to RectangularFunc
template<class C, class Preprocess>
requires C::isColor
class PrePattern : public Pattern<C>, protected RectangularFunc<uint8_t>
/**
* @brief Generic Functor to generate a homogene 2D BicolorPattern
* Preprocess point.x and point.y with PreFn and pass result to RectFunc
* @tparam C Color
* @tparam PreFn Scalar Functor
*/
template<Color C, class PreFn>
class PrePattern : public BicolorPattern<C>, protected RectFunc<uint8_t>
{
public:
constexpr PrePattern(C odd, C even, uint8_t width)
: Pattern<C>(odd, even), RectangularFunc<uint8_t>(width, width)
: BicolorPattern<C>(odd, even), RectFunc<uint8_t>(width, width)
{}

constexpr PrePattern(C odd, C even, uint8_t low, uint8_t high)
: Pattern<C>(odd, even), RectangularFunc<uint8_t>(low, high)
: BicolorPattern<C>(odd, even), RectFunc<uint8_t>(low, high)
{}

constexpr C
operator()(Point point) final
{
return RectangularFunc<uint8_t>::operator()(Preprocess()(point.x, point.y)) ? this->odd : this->even;
return RectFunc<uint8_t>::operator()(PreFn()(point.x, point.y)) ? this->odd : this->even;
}
};

// Takes results from passing point.x and point.y to RectangularFunc
// and Postprocesses with binary function Fn
template<class C, class Postprocess>
requires C::isColor
class PostPattern : public Pattern<C>, protected RectangularFunc<uint8_t>
/**
* @brief Generic Functor to generate a homogene 2D BicolorPattern
* Pass point.x and point.y to RectFunc and Postprocess results with PostFn
* @tparam C Color
* @tparam PostFn Binary Functor
*/
template<Color C, class PostFn>
class PostPattern : public BicolorPattern<C>, protected RectFunc<uint8_t>
{
public:
constexpr PostPattern(C odd, C even, uint8_t width)
: Pattern<C>(odd, even), RectangularFunc<uint8_t>(width, width)
: BicolorPattern<C>(odd, even), RectFunc<uint8_t>(width, width)
{}

constexpr PostPattern(C odd, C even, uint8_t low, uint8_t high)
: Pattern<C>(odd, even), RectangularFunc<uint8_t>(low, high)
: BicolorPattern<C>(odd, even), RectFunc<uint8_t>(low, high)
{}

constexpr C
operator()(Point point) final
{
// return odd;
bool h = RectangularFunc<uint8_t>::operator()(point.x);
bool v = RectangularFunc<uint8_t>::operator()(point.y);
return Postprocess()(h, v) ? this->odd : this->even;
bool h = RectFunc<uint8_t>::operator()(point.x);
bool v = RectFunc<uint8_t>::operator()(point.y);
return PostFn()(h, v) ? this->odd : this->even;
}
};
} // namespace detail

using namespace detail;

// TODO I bet there's something out of the std-box
// TODO Bet there's a solution out of STL-box
template <class T> struct takeX {
T operator() (const T&, const T& y) const {return y;}
constexpr T operator() (const T&, const T& y) const {return y;}
};
template <class T> struct takeY {
constexpr T operator() (const T& x, const T&) const {return x;}
};

template<class C>
template<Color C>
class HorizontalStriped : public PrePattern<C, takeX<int16_t>>
{ public: using PrePattern<C, takeX<int16_t>>::PrePattern; };

// TODO I bet there's something out of the std-box
template <class T> struct takeY {
T operator() (const T& x, const T&) const {return x;}
};

template<class C>
template<Color C>
class VerticalStriped : public PrePattern<C, takeY<int16_t>>
{ public: using PrePattern<C, takeY<int16_t>>::PrePattern; };

template<class C>
template<Color C>
class FallingDiagonalStriped : public PrePattern<C, std::plus<int16_t>>
{ public: using PrePattern<C, std::plus<int16_t>>::PrePattern; };

template<class C>
template<Color C>
class RisingDiagonalStriped : public PrePattern<C, std::minus<int16_t>>
{ public: using PrePattern<C, std::minus<int16_t>>::PrePattern; };

template<class C>
template<Color C>
class Checkerboard : public PostPattern<C, std::bit_xor<bool>>
{ public: using PostPattern<C, std::bit_xor<bool>>::PostPattern; };
template<Color C>
class Grid : public PostPattern<C, std::bit_or<bool>>
{ public: using PostPattern<C, std::bit_or<bool>>::PostPattern; };

// These are Try & Error. Some combinations produce beautiful results
template<Color C>
class MultiplyPattern : public PrePattern<C, std::multiplies<int16_t>>
{ public: using PrePattern<C, std::multiplies<int16_t>>::PrePattern; };

template<class C>
template<Color C>
class DividePattern : public PrePattern<C, std::divides<int16_t>>
{ public: using PrePattern<C, std::divides<int16_t>>::PrePattern; };

// Well, that's useless
template<class C>
template<Color C>
class ModulusPattern : public PrePattern<C, std::modulus<int16_t>>
{ public: using PrePattern<C, std::modulus<int16_t>>::PrePattern; };

template<class C>
class Checkerboard : public PostPattern<C, std::bit_xor<bool>>
{ public: using PostPattern<C, std::bit_xor<bool>>::PostPattern; };
template<Color C>
class BitwiseAndPattern : public PrePattern<C, std::bit_and<int16_t>>
{ public: using PrePattern<C, std::bit_and<int16_t>>::PrePattern; };

template<Color C>
class BitwiseOrPattern : public PrePattern<C, std::bit_or<int16_t>>
{ public: using PrePattern<C, std::bit_or<int16_t>>::PrePattern; };

template<Color C>
class BitwiseXorPattern : public PrePattern<C, std::bit_xor<int16_t>>
{ public: using PrePattern<C, std::bit_xor<int16_t>>::PrePattern; };

template<class C>
class Grid : public PostPattern<C, std::bit_or<bool>>
{ public: using PostPattern<C, std::bit_or<bool>>::PostPattern; };
} // namespace modm::color
19 changes: 19 additions & 0 deletions src/modm/ui/color/pattern/mandelbrot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2021, Thomas Sommer
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#pragma once

namespace modm::color
{
class MandelBrot {

}
}
8 changes: 3 additions & 5 deletions src/modm/ui/color/pattern/pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@

namespace modm::color
{

/**
* @brief Unary Functor giving one of two colors for a Point
*
* @tparam C Type of Color to return
*/
template<class C>
requires C::isColor
class Pattern
template<Color C>
class BicolorPattern
{
protected:
const C odd;
const C even;
Pattern(C odd, C even) : odd(odd), even(even) {}
BicolorPattern(C odd, C even) : odd(odd), even(even) {}
public:

/**
Expand Down
2 changes: 0 additions & 2 deletions src/modm/ui/color/rgb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ template<std::unsigned_integral T>
class RgbT
{
public:
static constexpr bool isColor = true;

T red{0};
T green{0};
T blue{0};
Expand Down
2 changes: 0 additions & 2 deletions src/modm/ui/color/rgb565.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ template<bool BigEndian = false>
class Rgb565
{
public:
static constexpr bool isColor = true;

uint16_t color{0x0000};

using RgbCalcType = RgbT<uint8_t>;
Expand Down
Loading

0 comments on commit febf47d

Please sign in to comment.