Skip to content

Commit

Permalink
rewrite modm::ui::color
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Sep 15, 2023
1 parent 7f30961 commit ab380dc
Show file tree
Hide file tree
Showing 50 changed files with 2,502 additions and 1,731 deletions.
2 changes: 1 addition & 1 deletion examples/arduino_nano/color/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Sensorthread : public modm::pt::Protothread
if (PT_CALL(sensor.readColor()))
{
const auto rgb = data.getColor();
MODM_LOG_INFO << "RGB: " << rgb << "\tHSV: " << modm::color::Hsv(rgb) << modm::endl;
MODM_LOG_INFO << "RGB: " << rgb << "\tHSV: " << modm::color::Hsv888(rgb) << modm::endl;
}
}

Expand Down
10 changes: 3 additions & 7 deletions examples/avr/display/dogm128/image/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,9 @@ drawNumber(modm::glcd::Point cursor, uint8_t number)
int
main()
{
led::R::set();
led::G::set();
led::B::reset();

led::R::setOutput();
led::G::setOutput();
led::B::setOutput();
led::R::setOutput(true);
led::G::setOutput(true);
led::B::setOutput(false);

lcd::SPI::connect<lcd::Sck::BitBang, lcd::Mosi::BitBang>();
lcd::SPI::initialize<SystemClock, 2_MHz>();
Expand Down
2 changes: 1 addition & 1 deletion examples/nucleo_f446re/color/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ThreadOne : public modm::pt::Protothread
if (PT_CALL(sensor.readColor()))
{
const auto rgb = data.getColor();
MODM_LOG_INFO << "RGB: " << rgb << "\tHSV: " << modm::color::Hsv(rgb) << modm::endl;
MODM_LOG_INFO << "RGB: " << rgb << "\tHSV: " << modm::color::Hsv888(rgb) << modm::endl;
}
timeout.restart(500ms);
PT_WAIT_UNTIL(timeout.isExpired());
Expand Down
6 changes: 3 additions & 3 deletions examples/stm32f469_discovery/game_of_life/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ uint16_t * displayBuffer;
#define TRAIL_LENGTH ((1 << TRAIL_POWER) + 1)
constexpr uint8_t alive = (1 << TRAIL_POWER);

#define COLOR_SHADE(red, green, blue, fraction) modm::color::Rgb(\
#define COLOR_SHADE(red, green, blue, fraction) modm::color::Rgb888(\
uint8_t(uint32_t(red) * (fraction) / TRAIL_LENGTH), \
uint8_t(uint32_t(green) * (fraction) / TRAIL_LENGTH), \
uint8_t(uint32_t(blue) * (fraction) / TRAIL_LENGTH) )
Expand Down Expand Up @@ -137,7 +137,7 @@ static inline void touch(framebuffer_t buffer)

static inline void setPixel(int x, int y, uint8_t color)
{
#define DRAW(x, y) displayBuffer[(y) * 800 + (x)] = GET_TRAIL_COLOR(color).color;
#define DRAW(x, y) displayBuffer[(y) * 800 + (x)] = GET_TRAIL_COLOR(color).value();
#if SCALE >= 8
// >:v x:y
// 0 | |
Expand All @@ -148,7 +148,7 @@ static inline void setPixel(int x, int y, uint8_t color)
// 5 | x x |
// 6 | xxxx |
// 7 | |
GET_TRAIL_COLOR(color).color;
GET_TRAIL_COLOR(color).value();
// 1
DRAW(x+2, y+1);
DRAW(x+3, y+1);
Expand Down
2 changes: 1 addition & 1 deletion examples/stm32f4_discovery/colour_tcs3414/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ThreadOne : public modm::pt::Protothread
{
if (PT_CALL(sensor.readColor())) {
const auto rgb = data.getColor();
stream << "RGB: " << rgb << "\tHSV: " << modm::color::Hsv(rgb) << modm::endl;
stream << "RGB: " << rgb << "\tHSV: " << modm::color::Hsv888(rgb) << modm::endl;
}
timeout.restart(500ms);
PT_WAIT_UNTIL(timeout.isExpired());
Expand Down
10 changes: 5 additions & 5 deletions src/modm/board/disco_f469ni/board_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DsiDisplay : public modm::ColorGraphicDisplay
void
clear() final
{
std::fill(buffer, buffer + this->getBufferWidth()*this->getBufferHeight(), this->backgroundColor.color);
std::fill(buffer, buffer + this->getBufferWidth()*this->getBufferHeight(), this->backgroundColor.value());
}

void
Expand All @@ -62,21 +62,21 @@ class DsiDisplay : public modm::ColorGraphicDisplay
setPixel(int16_t x, int16_t y) final
{
if (x < 0 or 800 <= x or y < 0 or 480 <= y) return;
buffer[y * 800 + x] = this->foregroundColor.color;
buffer[y * 800 + x] = this->foregroundColor.value();
}

void
clearPixel(int16_t x, int16_t y) final
{
if (x < 0 or 800 <= x or y < 0 or 480 <= y) return;
buffer[y * 800 + x] = this->backgroundColor.color;
buffer[y * 800 + x] = this->backgroundColor.value();
}

modm::color::Rgb565
getPixel(int16_t x, int16_t y) const final
{
if (x < 0 or 800 <= x or y < 0 or 480 <= y) return false;
return buffer[y * 800 + x];
if (x < 0 or 800 <= x or y < 0 or 480 <= y) return modm::color::html::Black;
return modm::color::Rgb565(buffer[y * 800 + x]);
}

protected:
Expand Down
2 changes: 1 addition & 1 deletion src/modm/driver/color/tcs3414.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ struct tcs3414
};


using Rgb = color::RgbT<uint16_t>;
using Rgb = color::Rgb161616;

struct modm_packed
Data
Expand Down
2 changes: 1 addition & 1 deletion src/modm/driver/color/tcs3472.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ struct tcs3472
addr(uint8_t version=5)
{ return version < 5 ? 0x39 : 0x29; }

using Rgb = color::RgbT<uint16_t>;
using Rgb = color::Rgb161616;

struct modm_packed
Data
Expand Down
20 changes: 10 additions & 10 deletions src/modm/driver/display/ili9341_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void
Ili9341<Interface, Reset, Backlight, BufferSize>::drawHorizontalLine(
glcd::Point start, uint16_t length)
{
uint16_t const pixelValue { modm::toBigEndian(foregroundColor.color) };
uint16_t const pixelValue { modm::toBigEndian(foregroundColor.value()) };
auto minLength { std::min(std::size_t(length), BufferSize) };
uint16_t *buffer16 { reinterpret_cast<uint16_t *>(buffer) };
std::fill(buffer16, buffer16+minLength, pixelValue);
Expand All @@ -223,7 +223,7 @@ void
Ili9341<Interface, Reset, Backlight, BufferSize>::drawVerticalLine(
glcd::Point start, uint16_t length)
{
uint16_t const pixelValue { modm::toBigEndian(foregroundColor.color) };
uint16_t const pixelValue { modm::toBigEndian(foregroundColor.value()) };
auto minLength { std::min(std::size_t(length), BufferSize) };
uint16_t *buffer16 { reinterpret_cast<uint16_t *>(buffer) };
std::fill(buffer16, buffer16+minLength, pixelValue);
Expand All @@ -248,7 +248,7 @@ Ili9341<Interface, Reset, Backlight, BufferSize>::fillRectangle(
auto const y { upperLeft.getY() };
std::size_t pixelCount { std::size_t(width) * std::size_t(height) };

uint16_t const pixelValue { modm::toBigEndian(foregroundColor.color) };
uint16_t const pixelValue { modm::toBigEndian(foregroundColor.value()) };
auto minLength { std::min(std::size_t(pixelCount), BufferSize) };
uint16_t *buffer16 { reinterpret_cast<uint16_t *>(buffer) };
std::fill(buffer16, buffer16+minLength, pixelValue);
Expand All @@ -270,8 +270,8 @@ void
Ili9341<Interface, Reset, Backlight, BufferSize>::fillCircle(
glcd::Point center, uint16_t radius)
{
uint8_t const setColor[] { uint8_t((foregroundColor.color >> 8) & 0xff),
uint8_t(foregroundColor.color & 0xff) };
uint8_t const setColor[] { uint8_t((foregroundColor.value() >> 8) & 0xff),
uint8_t(foregroundColor.value() & 0xff) };

int16_t f = 1 - radius;
int16_t ddF_x = 0;
Expand Down Expand Up @@ -317,10 +317,10 @@ void
Ili9341<Interface, Reset, Backlight, BufferSize>::drawImageRaw(glcd::Point upperLeft,
uint16_t width, uint16_t height, modm::accessor::Flash<uint8_t> data)
{
uint8_t const setColor[] { uint8_t((foregroundColor.color >> 8) & 0xff),
uint8_t(foregroundColor.color & 0xff) };
uint8_t const clearColor[] { uint8_t((backgroundColor.color >> 8) & 0xff),
uint8_t(backgroundColor.color & 0xff) };
uint8_t const setColor[] { uint8_t((foregroundColor.value() >> 8) & 0xff),
uint8_t(foregroundColor.value() & 0xff) };
uint8_t const clearColor[] { uint8_t((backgroundColor.value() >> 8) & 0xff),
uint8_t(backgroundColor.value() & 0xff) };

BatchHandle h(*this);

Expand Down Expand Up @@ -392,7 +392,7 @@ Ili9341<Interface, Reset, Backlight, BufferSize>::setColoredPixel(
int16_t x, int16_t y, color::Rgb565 const &color)
{
auto const pixelColor { color };
uint8_t const setColor[] { uint8_t((pixelColor.color >> 8) & 0xff), uint8_t(pixelColor.color & 0xff) };
uint8_t const setColor[] { uint8_t((pixelColor.value() >> 8) & 0xff), uint8_t(pixelColor.value() & 0xff) };

BatchHandle h(*this);

Expand Down
8 changes: 4 additions & 4 deletions src/modm/driver/display/parallel_tft_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ modm::ParallelTft<INTERFACE>::clear()
interface.writeIndex(0x0022);
for (uint32_t i = 0; i < MAX_X * MAX_Y; i++)
{
interface.writeData(backgroundColor.color);
interface.writeData(backgroundColor.value());
}
}

Expand All @@ -123,7 +123,7 @@ modm::ParallelTft<INTERFACE>::setPixel(int16_t x, int16_t y)
}

writeCursor(x, y);
interface.writeRegister(0x0022, foregroundColor.color);
interface.writeRegister(0x0022, foregroundColor.value());
}

template <typename INTERFACE>
Expand All @@ -138,7 +138,7 @@ modm::ParallelTft<INTERFACE>::clearPixel(int16_t x, int16_t y)
// }
//
// writeCursor(x, y);
// interface.writeRegister(0x0022, color.getValue());
// interface.writeRegister(0x0022, color.value());
}

template <typename INTERFACE>
Expand All @@ -148,7 +148,7 @@ modm::ParallelTft<INTERFACE>::getPixel(int16_t x, int16_t y) const
(void) x;
(void) y;

return false;
return modm::color::Rgb565();
}

// ----------------------------------------------------------------------------
Expand Down
11 changes: 5 additions & 6 deletions src/modm/driver/pwm/apa102.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,28 @@ class Apa102
}

bool
setColorBrightness(size_t index, const color::Rgb &color, uint8_t brightness)
setColorBrightness(size_t index, const color::RgbPallete<8,8,8> &color, uint8_t brightness)
{
if (index >= LEDs) return false;
uint32_t value = (color.red << 24) | (color.green << 16) |
(color.blue << 8) | (brightness | 0xe0);
uint32_t value = color.value() << 8 | (brightness | 0xe0);
reinterpret_cast<uint32_t*>(data)[1+index] = value;
return true;
}

bool
setColor(size_t index, const color::Rgb &color)
setColor(size_t index, const color::RgbPallete<8,8,8> &color)
{
if (index >= LEDs) return false;
// read the brightness value and clear all colors
uint32_t value = reinterpret_cast<const uint32_t*>(data)[1+index] & 0xfful;
// set all colors
value |= (color.red << 24) | (color.green << 16) | (color.blue << 8);
value |= color.value() << 8;
// write back entire value
reinterpret_cast<uint32_t*>(data)[1+index] = value;
return true;
}

color::Rgb
color::Rgb888
getColor(size_t index) const
{
if (index >= LEDs) return {};
Expand Down
10 changes: 5 additions & 5 deletions src/modm/driver/pwm/sk6812w.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class Sk6812w
}

void
setColorBrightness(size_t index, const color::Rgb &color, uint8_t brightness)
setColorBrightness(size_t index, const color::Rgb888 &color, uint8_t brightness)
{
if (index >= LEDs) return;

const uint8_t colors[] = {color.green, color.red, color.blue, brightness};
const uint8_t colors[] = {color.green(), color.red(), color.blue(), brightness};
for (size_t ii = 0; ii < 4; ii++)
{
const uint32_t c = (spread(colors[ii]) << 12) | spread(colors[ii] >> 4);
Expand All @@ -86,11 +86,11 @@ class Sk6812w
}

void
setColor(size_t index, const color::Rgb &color)
setColor(size_t index, const color::Rgb888 &color)
{
if (index >= LEDs) return;

const uint8_t colors[] = {color.green, color.red, color.blue};
const uint8_t colors[] = {color.green(), color.red(), color.blue()};
for (size_t ii = 0; ii < 3; ii++)
{
const uint32_t c = (spread(colors[ii]) << 12) | spread(colors[ii] >> 4);
Expand All @@ -99,7 +99,7 @@ class Sk6812w
}
}

color::Rgb
color::Rgb888
getColor(size_t index) const
{
if (index >= LEDs) return {};
Expand Down
6 changes: 3 additions & 3 deletions src/modm/driver/pwm/ws2812b.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class Ws2812b
}

void
setColor(size_t index, const color::Rgb &color)
setColor(size_t index, const color::Rgb888 &color)
{
if (index >= LEDs) return;

const uint8_t colors[3] = {color.green, color.red, color.blue};
const uint8_t colors[3] = {color.green().value(), color.red().value(), color.blue().value()};
for (size_t ii = 0; ii < 3; ii++)
{
const uint32_t c = (spread(colors[ii]) << 12) | spread(colors[ii] >> 4);
Expand All @@ -85,7 +85,7 @@ class Ws2812b
}
}

color::Rgb
color::Rgb888
getColor(size_t index) const
{
if (index >= LEDs) return {};
Expand Down
6 changes: 3 additions & 3 deletions src/modm/math/filter/s_curve_controller_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#error "Don't include this file directly, use 's_curve_controller.hpp' instead!"
#endif


#include <algorithm>
#include <modm/math/utils.hpp>

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -113,9 +113,9 @@ modm::SCurveController<T>::update(T error, const T& speed)
outputDecrement = std::sqrt(error * parameter.decreaseFactor * 2);
}

output = modm::min(outputIncrement, outputDecrement);
output = std::min(outputIncrement, outputDecrement);
// TODO smooth breaking if the speedMaximum has changed to a lower value
output = modm::min(output, parameter.speedMaximum);
output = std::min(output, parameter.speedMaximum);

if (output < parameter.speedMinimum) {
output = parameter.speedMinimum;
Expand Down
Loading

0 comments on commit ab380dc

Please sign in to comment.