renderbug/lib/Figments/Display.h

111 lines
3.2 KiB
C
Raw Permalink Normal View History

2019-05-10 05:17:29 +00:00
#pragma once
2021-03-29 08:10:55 +00:00
#include <FastLED.h>
2019-05-10 05:17:29 +00:00
#include "Geometry.h"
2023-03-03 18:43:51 +00:00
/**
* Generic interface for converting between virtual and physical coordinates
*/
2019-05-10 05:17:29 +00:00
struct CoordinateMapping {
2023-03-03 18:43:51 +00:00
/**
* Maps physical coordinates to virtual coordinates.
* Virtual coordinates range from 0 to 255. Physical coordinates are
* hardware-dependent.
*/
2019-05-10 05:17:29 +00:00
virtual VirtualCoordinates physicalToVirtualCoords(const PhysicalCoordinates localCoords) const = 0;
2023-03-03 18:43:51 +00:00
/**
* Maps virtual coordinates to physical (aka, hardware) coordinates.
* Virtual coordinates range from 0 to 255. Physical coordinates are
* hardware-dependent.
*/
2019-05-10 05:17:29 +00:00
virtual PhysicalCoordinates virtualToPhysicalCoords(const VirtualCoordinates virtualCoords) const = 0;
2023-03-03 18:43:51 +00:00
/**
* Returns the index of the underlying FastLED array, i.e., the physical
* hardware pixel.
*/
2019-05-10 05:17:29 +00:00
virtual int physicalCoordsToIndex(const PhysicalCoordinates localCoords) const = 0;
virtual unsigned int physicalPixelCount() const = 0;
};
2023-03-03 18:43:51 +00:00
/**
* Basic mapping for a contiguous 1-d linear display, eg, an LED strip.
*
* X value ranges from 0 to 255; all Y values are flattened to Y=0
*/
2019-05-10 05:17:29 +00:00
struct LinearCoordinateMapping: CoordinateMapping {
unsigned int pixelCount = 1;
unsigned int startPixel = 0;
LinearCoordinateMapping() {}
LinearCoordinateMapping(unsigned int count, unsigned int start) : pixelCount(count), startPixel(start) {}
VirtualCoordinates physicalToVirtualCoords(const PhysicalCoordinates localCoords) const override {
return VirtualCoordinates{map8(localCoords.x, 0, pixelCount), 0};
2019-05-10 05:17:29 +00:00
}
PhysicalCoordinates virtualToPhysicalCoords(const VirtualCoordinates virtualCoords) const override {
2021-03-29 08:10:55 +00:00
return PhysicalCoordinates{scale8(pixelCount, virtualCoords.x), 0};
2019-05-10 05:17:29 +00:00
}
int physicalCoordsToIndex(const PhysicalCoordinates localCoords) const override {
return localCoords.x + startPixel;
}
unsigned int physicalPixelCount() const override {
2019-05-10 05:17:29 +00:00
return pixelCount;
}
};
2023-03-03 18:43:51 +00:00
/**
* The connection between the rendering engine and the physical hardware.
* Provides direct access to the underlying FastLED array.
*/
2019-05-10 05:17:29 +00:00
class Display {
public:
2021-03-29 08:10:55 +00:00
Display(CRGB* pixels, int pixelCount, const CoordinateMapping* map)
2019-05-10 05:17:29 +00:00
: m_pixels(pixels), m_pixelCount(pixelCount), m_coordMap(map) {}
2023-03-03 18:43:51 +00:00
/**
* Returns the physical pixel at the given physical coordinates
*/
2021-03-29 08:10:55 +00:00
CRGB& pixelAt(const PhysicalCoordinates coords);
2023-03-03 18:43:51 +00:00
/**
* Returns the physical pixel at the given virtual coordinates
*/
2021-03-29 08:10:55 +00:00
CRGB& pixelAt(const VirtualCoordinates coords);
2023-03-03 18:43:51 +00:00
/**
* Returns the physical pixel in the underlying FastLED array
*/
2021-03-29 08:10:55 +00:00
CRGB& pixelAt(int idx);
2019-05-10 05:17:29 +00:00
2023-03-03 18:43:51 +00:00
/**
* Returns how many pixels are in this display
*/
2019-05-10 05:17:29 +00:00
int pixelCount() const;
2023-03-03 18:43:51 +00:00
/**
* Returns the raw underlying FastLED array
*/
2021-03-29 08:10:55 +00:00
CRGB* pixelBacking() const;
2019-05-10 05:17:29 +00:00
const CoordinateMapping* coordinateMapping() const;
2023-03-03 18:43:51 +00:00
/**
* Sets every pixel to (0, 0, 0)
*/
2019-05-10 05:17:29 +00:00
void clear();
2023-03-03 18:43:51 +00:00
/**
* Sets every pixel to the given display
*/
2021-03-29 08:10:55 +00:00
void clear(const CRGB& color);
2019-05-10 05:17:29 +00:00
private:
2021-03-29 08:10:55 +00:00
CRGB* m_pixels;
2019-05-10 05:17:29 +00:00
int m_pixelCount;
const CoordinateMapping* m_coordMap;
};