|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <math.h> |
| 4 | +#include <string.h> |
| 5 | +#include "pico/stdlib.h" |
| 6 | + |
| 7 | +#include "libraries/pico_graphics/pico_graphics.hpp" |
| 8 | +#include "stellar_unicorn.hpp" |
| 9 | + |
| 10 | +using namespace pimoroni; |
| 11 | + |
| 12 | +PicoGraphics_PenRGB888 graphics(16, 16, nullptr); |
| 13 | +StellarUnicorn stellar_unicorn; |
| 14 | + |
| 15 | +// extra row of pixels for sourcing flames and averaging |
| 16 | +int width = 16; |
| 17 | +int height = 17; |
| 18 | + |
| 19 | +// a buffer that's at least big enough to store 55 x 15 values (to allow for both orientations) |
| 20 | +float heat[2000] = {0.0f}; |
| 21 | + |
| 22 | +void set(int x, int y, float v) { |
| 23 | + heat[x + y * width] = v; |
| 24 | +} |
| 25 | + |
| 26 | +float get(int x, int y) { |
| 27 | + /*if(x < 0 || x >= width || y < 0 || y >= height) { |
| 28 | + return 0.0f; |
| 29 | + }*/ |
| 30 | + x = x < 0 ? 0 : x; |
| 31 | + x = x >= width ? width - 1 : x; |
| 32 | + |
| 33 | + return heat[x + y * width]; |
| 34 | +} |
| 35 | + |
| 36 | +int main() { |
| 37 | + |
| 38 | + stdio_init_all(); |
| 39 | + |
| 40 | + stellar_unicorn.init(); |
| 41 | + stellar_unicorn.set_brightness(0.2); |
| 42 | + |
| 43 | + bool landscape = true; |
| 44 | +/* |
| 45 | + while(true) { |
| 46 | + stellar_unicorn.set_pixel(0, 0, 255, 0, 0); |
| 47 | + stellar_unicorn.set_pixel(1, 1, 0, 255, 0); |
| 48 | + stellar_unicorn.set_pixel(2, 2, 0, 0, 255); |
| 49 | + }*/ |
| 50 | + |
| 51 | + while(true) { |
| 52 | + if(stellar_unicorn.is_pressed(stellar_unicorn.SWITCH_BRIGHTNESS_UP)) { |
| 53 | + stellar_unicorn.adjust_brightness(+0.01); |
| 54 | + } |
| 55 | + if(stellar_unicorn.is_pressed(stellar_unicorn.SWITCH_BRIGHTNESS_DOWN)) { |
| 56 | + stellar_unicorn.adjust_brightness(-0.01); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + for(int y = 0; y < height; y++) { |
| 61 | + for(int x = 0; x < width; x++) { |
| 62 | + float value = get(x, y); |
| 63 | + |
| 64 | + graphics.set_pen(0, 0, 0); |
| 65 | + if(value > 0.5f) { |
| 66 | + graphics.set_pen(255, 255, 180); |
| 67 | + }else if(value > 0.4f) { |
| 68 | + graphics.set_pen(220, 160, 0); |
| 69 | + }else if(value > 0.3f) { |
| 70 | + graphics.set_pen(180, 30, 0); |
| 71 | + }else if(value > 0.22f) { |
| 72 | + graphics.set_pen(20, 20, 20); |
| 73 | + } |
| 74 | + |
| 75 | + if(landscape) { |
| 76 | + graphics.pixel(Point(x, y)); |
| 77 | + }else{ |
| 78 | + graphics.pixel(Point(y, x)); |
| 79 | + } |
| 80 | + |
| 81 | + // update this pixel by averaging the below pixels |
| 82 | + float average = (get(x, y) + get(x, y + 2) + get(x, y + 1) + get(x - 1, y + 1) + get(x + 1, y + 1)) / 5.0f; |
| 83 | + |
| 84 | + // damping factor to ensure flame tapers out towards the top of the displays |
| 85 | + average *= 0.95f; |
| 86 | + |
| 87 | + // update the heat map with our newly averaged value |
| 88 | + set(x, y, average); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + stellar_unicorn.update(&graphics); |
| 93 | + |
| 94 | + // clear the bottom row and then add a new fire seed to it |
| 95 | + for(int x = 0; x < width; x++) { |
| 96 | + set(x, height - 1, 0.0f); |
| 97 | + } |
| 98 | + |
| 99 | + // add a new random heat source |
| 100 | + int source_count = landscape ? 5 : 1; |
| 101 | + for(int c = 0; c < source_count; c++) { |
| 102 | + int px = (rand() % (width - 4)) + 2; |
| 103 | + set(px , height - 2, 1.0f); |
| 104 | + set(px + 1, height - 2, 1.0f); |
| 105 | + set(px - 1, height - 2, 1.0f); |
| 106 | + set(px , height - 1, 1.0f); |
| 107 | + set(px + 1, height - 1, 1.0f); |
| 108 | + set(px - 1, height - 1, 1.0f); |
| 109 | + } |
| 110 | + |
| 111 | + sleep_ms(20); |
| 112 | + } |
| 113 | + |
| 114 | + return 0; |
| 115 | +} |
0 commit comments