|
| 1 | +// Note - this example is from the AdaFruit Pico DVI library, but |
| 2 | +// modified to work with the SparkFun RedBoard IoT - RP2350. All |
| 3 | +// that is changed is the hardware configuration. |
| 4 | + |
| 5 | +// Double-buffered 8-bit Adafruit_GFX-compatible framebuffer for PicoDVI. |
| 6 | +// Animates without redraw flicker. Requires Adafruit_GFX >= 1.11.4 |
| 7 | + |
| 8 | +#include <SparkFun_PicoDVI.h> |
| 9 | + |
| 10 | +// Here's how a 320x240 8-bit (color-paletted) framebuffer is declared. |
| 11 | +// Second argument ('true' here) enables double-buffering for flicker-free |
| 12 | +// animation. Third argument is a hardware configuration -- examples are |
| 13 | +// written for Adafruit Feather RP2040 DVI, but that's easily switched out |
| 14 | +// for boards like the Pimoroni Pico DV (use 'pimoroni_demo_hdmi_cfg') or |
| 15 | +// the SparkFun RedBoard IoT - RP2350 - which this example is using. |
| 16 | + |
| 17 | +DVIGFX8 display(DVI_RES_320x240p60, true, sparkfun_iot_redboard_rp2350_dvi_cfg); |
| 18 | + |
| 19 | +// A 400x240 mode is possible but pushes overclocking even higher than |
| 20 | +// 320x240 mode. SOME BOARDS MIGHT SIMPLY NOT BE COMPATIBLE WITH THIS. |
| 21 | +// May require selecting QSPI div4 clock (Tools menu) to slow down flash |
| 22 | +// accesses, may require further over-volting the CPU to 1.25 or 1.3 V. |
| 23 | +// DVIGFX8 display(DVI_RES_400x240p60, true, sparkfun_iot_redboard_rp2350_dvi_cfg); |
| 24 | + |
| 25 | +#define N_BALLS 100 // Number of bouncy balls to draw, 1-254 (not 255) |
| 26 | +struct |
| 27 | +{ |
| 28 | + int16_t pos[2]; // Ball position (X,Y) |
| 29 | + int8_t vel[2]; // Ball velocity (X,Y) |
| 30 | +} ball[N_BALLS]; |
| 31 | + |
| 32 | +void setup() |
| 33 | +{ // Runs once on startup |
| 34 | + if (!display.begin()) |
| 35 | + { // Blink LED if insufficient RAM |
| 36 | + pinMode(LED_BUILTIN, OUTPUT); |
| 37 | + for (;;) |
| 38 | + digitalWrite(LED_BUILTIN, (millis() / 500) & 1); |
| 39 | + } |
| 40 | + |
| 41 | + // Randomize initial ball positions, velocities and colors |
| 42 | + for (int i = 0; i < N_BALLS; i++) |
| 43 | + { |
| 44 | + display.setColor(i + 1, 64 + random(192), 64 + random(192), 64 + random(192)); |
| 45 | + ball[i].pos[0] = 10 + random(display.width() - 20); |
| 46 | + ball[i].pos[1] = 10 + random(display.height() - 20); |
| 47 | + do |
| 48 | + { |
| 49 | + ball[i].vel[0] = 2 - random(5); |
| 50 | + ball[i].vel[1] = 2 - random(5); |
| 51 | + } while ((ball[i].vel[0] == 0) && (ball[i].vel[1] == 0)); |
| 52 | + } |
| 53 | + display.setColor(255, 0xFFFF); // Last palette entry = white |
| 54 | + display.swap(false, true); // Duplicate same palette into front & back buffers |
| 55 | +} |
| 56 | + |
| 57 | +void loop() |
| 58 | +{ |
| 59 | + // Clear back framebuffer and draw balls (circles) there. |
| 60 | + display.fillScreen(0); |
| 61 | + for (int i = 0; i < N_BALLS; i++) |
| 62 | + { |
| 63 | + display.fillCircle(ball[i].pos[0], ball[i].pos[1], 20, i + 1); |
| 64 | + // After drawing each one, update positions, bounce off edges. |
| 65 | + ball[i].pos[0] += ball[i].vel[0]; |
| 66 | + if ((ball[i].pos[0] <= 0) || (ball[i].pos[0] >= display.width())) |
| 67 | + ball[i].vel[0] *= -1; |
| 68 | + ball[i].pos[1] += ball[i].vel[1]; |
| 69 | + if ((ball[i].pos[1] <= 0) || (ball[i].pos[1] >= display.height())) |
| 70 | + ball[i].vel[1] *= -1; |
| 71 | + } |
| 72 | + // Swap front/back buffers, do not duplicate current screen state to next frame, |
| 73 | + // we'll draw it new from scratch each time. |
| 74 | + display.swap(); |
| 75 | +} |
0 commit comments