|
| 1 | +#include <string.h> |
| 2 | +#include <math.h> |
| 3 | +#include <vector> |
| 4 | +#include <cstdlib> |
| 5 | + |
| 6 | +#include "libraries/pico_display_28/pico_display_28.hpp" |
| 7 | +#include "drivers/st7789/st7789.hpp" |
| 8 | +#include "libraries/pico_graphics/pico_graphics.hpp" |
| 9 | +#include "rgbled.hpp" |
| 10 | +#include "button.hpp" |
| 11 | + |
| 12 | +using namespace pimoroni; |
| 13 | + |
| 14 | +ST7789 st7789(320, 240, ROTATE_0, false, get_spi_pins(BG_SPI_FRONT)); |
| 15 | +PicoGraphics_PenRGB332 graphics(st7789.width, st7789.height, nullptr); |
| 16 | + |
| 17 | +RGBLED led(PicoDisplay28::LED_R, PicoDisplay28::LED_G, PicoDisplay28::LED_B); |
| 18 | + |
| 19 | +Button button_a(PicoDisplay28::A); |
| 20 | +Button button_b(PicoDisplay28::B); |
| 21 | +Button button_x(PicoDisplay28::X); |
| 22 | +Button button_y(PicoDisplay28::Y); |
| 23 | + |
| 24 | +int main() { |
| 25 | + char fpsbuf[512]; // Make sure buffer don't overflow |
| 26 | + int frame_counter; // Counter for number of frames displayed |
| 27 | + uint64_t start, elapsed; // Microsecond timers |
| 28 | + float fps; // Frames per second to display |
| 29 | + |
| 30 | + frame_counter = 0; |
| 31 | + |
| 32 | + st7789.set_backlight(255); |
| 33 | + led.set_rgb(0, 0, 0); |
| 34 | + |
| 35 | + struct pt { |
| 36 | + float x; |
| 37 | + float y; |
| 38 | + uint8_t r; |
| 39 | + float dx; |
| 40 | + float dy; |
| 41 | + uint16_t pen; |
| 42 | + }; |
| 43 | + |
| 44 | + std::vector<pt> shapes; |
| 45 | + for(int i = 0; i < 100; i++) { |
| 46 | + pt shape; |
| 47 | + shape.x = rand() % graphics.bounds.w; |
| 48 | + shape.y = rand() % graphics.bounds.h; |
| 49 | + shape.r = (rand() % 10) + 3; |
| 50 | + shape.dx = float(rand() % 255) / 64.0f; |
| 51 | + shape.dy = float(rand() % 255) / 64.0f; |
| 52 | + shape.pen = graphics.create_pen(rand() % 255, rand() % 255, rand() % 255); |
| 53 | + shapes.push_back(shape); |
| 54 | + } |
| 55 | + |
| 56 | + Point text_location(0, 0); |
| 57 | + |
| 58 | + Pen BG = graphics.create_pen(120, 40, 60); |
| 59 | + Pen WHITE = graphics.create_pen(255, 255, 255); |
| 60 | + |
| 61 | + start = time_us_64(); |
| 62 | + // Get the start time |
| 63 | + |
| 64 | + while(true) { |
| 65 | + if(button_a.raw()) text_location.x -= 1; |
| 66 | + if(button_b.raw()) text_location.x += 1; |
| 67 | + |
| 68 | + if(button_x.raw()) text_location.y -= 1; |
| 69 | + if(button_y.raw()) text_location.y += 1; |
| 70 | + |
| 71 | + graphics.set_pen(BG); |
| 72 | + graphics.clear(); |
| 73 | + |
| 74 | + for(auto &shape : shapes) { |
| 75 | + shape.x += shape.dx; |
| 76 | + shape.y += shape.dy; |
| 77 | + if((shape.x - shape.r) < 0) { |
| 78 | + shape.dx *= -1; |
| 79 | + shape.x = shape.r; |
| 80 | + } |
| 81 | + if((shape.x + shape.r) >= graphics.bounds.w) { |
| 82 | + shape.dx *= -1; |
| 83 | + shape.x = graphics.bounds.w - shape.r; |
| 84 | + } |
| 85 | + if((shape.y - shape.r) < 0) { |
| 86 | + shape.dy *= -1; |
| 87 | + shape.y = shape.r; |
| 88 | + } |
| 89 | + if((shape.y + shape.r) >= graphics.bounds.h) { |
| 90 | + shape.dy *= -1; |
| 91 | + shape.y = graphics.bounds.h - shape.r; |
| 92 | + } |
| 93 | + |
| 94 | + graphics.set_pen(shape.pen); |
| 95 | + graphics.circle(Point(shape.x, shape.y), shape.r); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + // Since HSV takes a float from 0.0 to 1.0 indicating hue, |
| 100 | + // then we can divide millis by the number of milliseconds |
| 101 | + // we want a full colour cycle to take. 5000 = 5 sec |
| 102 | + RGB p = RGB::from_hsv((float)millis() / 5000.0f, 1.0f, 0.5f + sinf(millis() / 100.0f / 3.14159f) * 0.5f); |
| 103 | + |
| 104 | + led.set_rgb(p.r, p.g, p.b); |
| 105 | + |
| 106 | + frame_counter++; |
| 107 | + |
| 108 | + elapsed = time_us_64(); |
| 109 | + // Get the current time to calculate the elapsed time next |
| 110 | + fps = (frame_counter / float(elapsed - start)) * 1000000; |
| 111 | + sprintf(fpsbuf, "Frame = %d, FPS = %5.2f", frame_counter, fps); |
| 112 | + // Create output text |
| 113 | + |
| 114 | + graphics.set_pen(WHITE); |
| 115 | + graphics.text(fpsbuf, text_location, 320, 2); |
| 116 | + // Render Frame counter and FPS to screen |
| 117 | + |
| 118 | + // update screen |
| 119 | + st7789.update(&graphics); |
| 120 | + } |
| 121 | + |
| 122 | + return 0; |
| 123 | +} |
0 commit comments