From a3d37072841ad5d7cdd4afda05449c24543246c7 Mon Sep 17 00:00:00 2001 From: jon Date: Wed, 29 Jun 2022 21:45:25 +0100 Subject: [PATCH] more stuff --- examples/galactic_unicorn/CMakeLists.txt | 13 ++ examples/galactic_unicorn/demo.cpp | 70 ++++++- examples/galactic_unicorn/demo2.cpp | 178 ++++++++++++++++++ .../galactic_unicorn/galactic_unicorn.cpp | 4 +- 4 files changed, 259 insertions(+), 6 deletions(-) create mode 100644 examples/galactic_unicorn/demo2.cpp diff --git a/examples/galactic_unicorn/CMakeLists.txt b/examples/galactic_unicorn/CMakeLists.txt index c5d9167b..60165650 100644 --- a/examples/galactic_unicorn/CMakeLists.txt +++ b/examples/galactic_unicorn/CMakeLists.txt @@ -8,3 +8,16 @@ target_link_libraries(galactic_unicorn_demo pico_stdlib hardware_pio hardware_dm # create map/bin/hex file etc. pico_add_extra_outputs(galactic_unicorn_demo) + + + +add_executable( + galactic_unicorn_demo2 + demo2.cpp +) + +# Pull in pico libraries that we need +target_link_libraries(galactic_unicorn_demo2 pico_stdlib hardware_pio hardware_dma pico_graphics galactic_unicorn) + +# create map/bin/hex file etc. +pico_add_extra_outputs(galactic_unicorn_demo2) diff --git a/examples/galactic_unicorn/demo.cpp b/examples/galactic_unicorn/demo.cpp index c491e490..517c2d68 100644 --- a/examples/galactic_unicorn/demo.cpp +++ b/examples/galactic_unicorn/demo.cpp @@ -5,6 +5,7 @@ #include "libraries/pico_graphics/pico_graphics.hpp" #include "galactic_unicorn.hpp" +#include "okcolor.hpp" using namespace pimoroni; @@ -106,10 +107,71 @@ gpio_set_function(28, GPIO_FUNC_SIO); - uint i = 0; - int v = 255; + //uint i = 0; + //int v = 255; + + float hue_offset = 0.0f; + float brightness = 0.5f; + float curve = 4.0f; + while(true) { - i++; + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_VOLUME_UP)) { + hue_offset += 0.05; + if(hue_offset > 1.0f) hue_offset = 1.0f; + } + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_VOLUME_DOWN)) { + hue_offset -= 0.05; + if(hue_offset < 0.0f) hue_offset = 0.0f; + } + + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_BRIGHTNESS_UP)) { + brightness += 0.05; + if(brightness > 1.0f) brightness = 1.0f; + } + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_BRIGHTNESS_DOWN)) { + brightness -= 0.05; + if(brightness < 0.0f) brightness = 0.0f; + } + + + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_A)) { + curve += 0.5; + if(curve > 100.0f) curve = 100.0f; + } + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_B)) { + curve -= 0.5; + if(curve < 0.5) curve = 0.5; + } + + for(int y = 0; y < 11; y++) { + for(int x = 0; x < 53; x++) { + float fx = x / 53.0f; + float fy = (y / 11.0f) - 0.5f; + float twopi = M_PI * 2; + float hue = fy + (sin(fx * twopi / curve)); + float fade = 1.0f; + /* if(hue < 0.0f) { + fade += (hue * 10.0f); + if(fade < 0.0f) fade = 0.0f; + } + if(hue > 1.0f) { + fade -= ((hue - 1.0f) * 10.0f); + if(fade < 0.0f) fade = 0.0f; + }*/ + + hue += hue_offset; + while(hue < 0.0f) {hue += 1.0f;} + while(hue > 1.0f) {hue -= 1.0f;} + hue = 1.0f - hue; +/* + uint8_t r = 0, g = 0, b = 0; + from_hsv(hue, 1.0f, brightness * fade, r, g, b);*/ + ok_color::HSL hsl{.h = hue, .s = 1.0f, .l = brightness * fade}; + ok_color::RGB rgb = ok_color::okhsl_to_srgb(hsl); + galactic_unicorn.set_pixel(x, y, rgb.r * 255.0f, rgb.g * 255.0f, rgb.b * 255.0f); + } + } + /*i++; graphics.set_pen(0, 0, 0); if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_A)) {graphics.set_pen(255, 0, 0);} @@ -151,7 +213,7 @@ graphics.set_pen(255, 255, 255); } galactic_unicorn.set_pixel(x, y, r, g, b); } - +*/ sleep_ms(10); } diff --git a/examples/galactic_unicorn/demo2.cpp b/examples/galactic_unicorn/demo2.cpp new file mode 100644 index 00000000..ce06cec1 --- /dev/null +++ b/examples/galactic_unicorn/demo2.cpp @@ -0,0 +1,178 @@ +#include +#include +#include +#include "pico/stdlib.h" + +#include "libraries/pico_graphics/pico_graphics.hpp" +#include "galactic_unicorn.hpp" +#include "okcolor.hpp" + +using namespace pimoroni; + +PicoGraphics_PenRGB565 graphics(53, 11, nullptr); +GalacticUnicorn galactic_unicorn; + +// HSV Conversion expects float inputs in the range of 0.00-1.00 for each channel +// Outputs are rgb in the range 0-255 for each channel +void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) { + float i = floor(h * 6.0f); + float f = h * 6.0f - i; + v *= 255.0f; + uint8_t p = v * (1.0f - s); + uint8_t q = v * (1.0f - f * s); + uint8_t t = v * (1.0f - (1.0f - f) * s); + + switch (int(i) % 6) { + case 0: r = v; g = t; b = p; break; + case 1: r = q; g = v; b = p; break; + case 2: r = p; g = v; b = t; break; + case 3: r = p; g = q; b = v; break; + case 4: r = t; g = p; b = v; break; + case 5: r = v; g = p; b = q; break; + } +} + +void text(std::string t, Point p, float s = 1.0f, float a = 1.0f) { + int w = graphics.measure_text(t, s); + p.x += (53 / 2) - (w / 2); + p.y += (11 / 2); + graphics.text(t, Point(p.x, p.y), -1, s, a); + graphics.text(t, Point(p.x + 1, p.y), -1, s, a); + graphics.text(t, Point(p.x + 1, p.y + 1), -1, s, a); + graphics.text(t, Point(p.x, p.y + 1), -1, s, a); +} + +struct star_t { + float dx, dy, x, y, a; + + uint8_t brightness() { + int b = a / 5; + return b > 15 ? 15 : b; + } +}; + +void init_star(star_t &s) { + s.x = ((rand() % 100) / 5.0f) - 10.0f; + s.y = ((rand() % 100) / 10.0f) - 5.0f; + + s.dx = s.x / 10.0f; + s.dy = s.y / 10.0f; + s.a = 0; +} + +void step_star(star_t &s) { + s.x += s.dx; + s.y += s.dy; + s.a++; + + if(s.a > 100) { + init_star(s); + } +} + +void number(int n, int x, int y) { + switch(n) { + case 6: { + graphics.rectangle(Rect(x, y, 6, 2)); + graphics.rectangle(Rect(x, y + 4, 6, 2)); + graphics.rectangle(Rect(x, y + 9, 6, 2)); + graphics.rectangle(Rect(x, y, 2, 11)); + graphics.rectangle(Rect(x + 4, y + 4, 2, 5)); + }break; + + case 9: { + graphics.rectangle(Rect(x, y, 6, 2)); + graphics.rectangle(Rect(x, y + 4, 6, 2)); + graphics.rectangle(Rect(x, y + 9, 6, 2)); + graphics.rectangle(Rect(x + 4, y, 2, 11)); + graphics.rectangle(Rect(x, y, 2, 5)); + }break; + + case 4: { + graphics.rectangle(Rect(x, y + 4, 6, 2)); + graphics.rectangle(Rect(x + 4, y, 2, 11)); + graphics.rectangle(Rect(x, y, 2, 5)); + }break; + + case 0: { + graphics.rectangle(Rect(x, y, 6, 2)); + graphics.rectangle(Rect(x + 4, y, 2, 11)); + graphics.rectangle(Rect(x, y, 2, 11)); + graphics.rectangle(Rect(x, y + 9, 6, 2)); + }break; + } +} + +int main() { + + galactic_unicorn.init(); + + // graphics.set_font("sans"); + + + while(true) { + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_VOLUME_UP)) { + } + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_VOLUME_DOWN)) { + } + + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_BRIGHTNESS_UP)) { + } + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_BRIGHTNESS_DOWN)) { + } + + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_A)) { + } + if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_B)) { + } + + graphics.set_pen(0, 0, 0); + graphics.clear(); + graphics.set_pen(200, 200, 0); + number(6, 23, 0); + number(9, 31, 0); + number(4, 39, 0); + number(0, 47, 0); + + graphics.set_pen(200, 0, 0); + graphics.circle(Point(8, 5), 6); + + graphics.set_pen(255, 255, 255); + std::vector triangle = {Point(5, 2), Point(10, 5), Point(5, 7)}; + graphics.polygon(triangle); + + + graphics.set_pen(255, 0, 0); + graphics.rectangle(Rect(0, 0, 10, 11)); + graphics.set_pen(0, 255, 0); + graphics.rectangle(Rect(10, 0, 10, 11)); + graphics.set_pen(0, 0, 255); + graphics.rectangle(Rect(20, 0, 10, 11)); + + uint16_t *p = (uint16_t *)graphics.frame_buffer; + for(size_t i = 0; i < 53 * 11; i++) { + int x = i % 53; + int y = i / 53; + /* uint r = ((*p & 0b0111110000000000) >> 10) << 3; + uint g = ((*p & 0b0000001111100000) >> 5) << 3; + uint b = ((*p & 0b0000000000011111) >> 0) << 3;*/ + p++; + +/* if(r > 200 && g > 200 && b > 200) { + r = hue_map[x][0]; + g = hue_map[x][1]; + b = hue_map[x][2]; + }*/ + galactic_unicorn.set_pixel(x, y, 0, 0, 255); + } + + + sleep_ms(10); + } + + + + printf("done\n"); + + return 0; +} diff --git a/libraries/galactic_unicorn/galactic_unicorn.cpp b/libraries/galactic_unicorn/galactic_unicorn.cpp index 37b6ef7a..ace3b5bd 100644 --- a/libraries/galactic_unicorn/galactic_unicorn.cpp +++ b/libraries/galactic_unicorn/galactic_unicorn.cpp @@ -218,7 +218,7 @@ namespace pimoroni { bitstream[frame_offset + 163] = 0b10001000; // BLANK low to enable column outputs // set the number of bcd ticks for this frame - uint16_t bcd_ticks = frame == BCD_FRAMES - 1 ? 65535 : 1 << frame; + uint16_t bcd_ticks = frame == BCD_FRAMES - 1 ? 1 : 1 << frame; bitstream[frame_offset + 164] = (bcd_ticks & 0xff); bitstream[frame_offset + 165] = (bcd_ticks & 0xff00) >> 8; @@ -330,7 +330,7 @@ namespace pimoroni { // determine offset in the buffer for this row uint16_t row_offset = y * (ROW_BYTES + ROW_FRAME_BYTES * BCD_FRAMES); - uint16_t bits[3] = {r_gamma_lut[r], g_gamma_lut[g], b_gamma_lut[b]}; + uint16_t bits[3] = {r_gamma_lut[b], g_gamma_lut[g], b_gamma_lut[r]}; //uint16_t gr = r_gamma_lut[r]; //uint16_t gg = g_gamma_lut[g]; //uint16_t gb = b_gamma_lut[b];