Skip to content

Commit 8047f29

Browse files
committed
Port Pico Display 2.0 Mandelbrot to PicoGraphics.
Move into Pico Display examples directory and update for PicoGraphics.
1 parent e66a289 commit 8047f29

File tree

4 files changed

+46
-33
lines changed

4 files changed

+46
-33
lines changed

examples/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ add_subdirectory(breakout_oled_128x128)
2727

2828
add_subdirectory(pico_display)
2929
add_subdirectory(pico_display_2)
30-
add_subdirectory(pico_display_2_mandelbrot)
3130
add_subdirectory(pico_unicorn)
3231
add_subdirectory(pico_unicorn_plasma)
3332
add_subdirectory(pico_scroll)

examples/pico_display_2/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
add_subdirectory(mandelbrot)
2+
13
set(OUTPUT_NAME pico_display2_demo)
24

35
add_executable(

examples/pico_display_2_mandelbrot/CMakeLists.txt renamed to examples/pico_display_2/mandelbrot/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ add_executable(
66
)
77

88
# Pull in pico libraries that we need
9-
target_link_libraries(${OUTPUT_NAME} pico_stdlib pico_multicore hardware_spi hardware_pwm hardware_dma pico_display_2)
9+
target_link_libraries(${OUTPUT_NAME} pico_stdlib pico_multicore hardware_spi hardware_pwm hardware_dma rgbled button pico_display_2 st7789 pico_graphics)
1010

1111
# create map/bin/hex file etc.
1212
pico_add_extra_outputs(${OUTPUT_NAME})

examples/pico_display_2_mandelbrot/demo.cpp renamed to examples/pico_display_2/mandelbrot/demo.cpp

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,30 @@
55
#if defined(MULTICORE)
66
#include "pico/multicore.h"
77
#endif
8+
89
#include "libraries/pico_display_2/pico_display_2.hpp"
10+
#include "drivers/st7789/st7789.hpp"
11+
#include "libraries/pico_graphics/pico_graphics.hpp"
12+
#include "rgbled.hpp"
13+
#include "button.hpp"
914

1015

1116
using namespace pimoroni;
1217

18+
// PicoDisplay2 is 320 by 240
19+
#define DISPLAY_WIDTH PicoDisplay2::WIDTH
20+
#define DISPLAY_HEIGHT PicoDisplay2::HEIGHT
21+
22+
ST7789 st7789(DISPLAY_WIDTH, DISPLAY_HEIGHT, ROTATE_0, false, get_spi_pins(BG_SPI_FRONT));
23+
PicoGraphics_PenRGB565 graphics(st7789.width, st7789.height, nullptr);
24+
25+
RGBLED led(PicoDisplay2::LED_R, PicoDisplay2::LED_G, PicoDisplay2::LED_B);
26+
27+
Button button_a(PicoDisplay2::A);
28+
Button button_b(PicoDisplay2::B);
29+
Button button_x(PicoDisplay2::X);
30+
Button button_y(PicoDisplay2::Y);
31+
1332
typedef int32_t fixed_t;
1433

1534
class complex_fixed_t {
@@ -33,12 +52,6 @@ static inline fixed_t fixed_multiply(fixed_t a, fixed_t b) {
3352
}
3453
#define FXD_MUL(x, y) fixed_multiply(x, y)
3554

36-
// PicoDisplay2 is 320 by 240
37-
#define DISPLAY_WIDTH PicoDisplay2::WIDTH
38-
#define DISPLAY_HEIGHT PicoDisplay2::HEIGHT
39-
40-
uint16_t g_buffer[DISPLAY_WIDTH * DISPLAY_HEIGHT];
41-
PicoDisplay2 pico_display(g_buffer);
4255

4356
class MandelbrotView {
4457
public:
@@ -161,10 +174,10 @@ void MandelbrotView::render(void) {
161174
pFrame += screenSizeX;
162175
if (core == 1 && (screenY & 0xF) == 0) {
163176
// update core1 view every 16 lines
164-
pico_display.update();
177+
st7789.update(&graphics);
165178
}
166179
}
167-
pico_display.update();
180+
st7789.update(&graphics);
168181
}
169182

170183
// HSV Conversion expects float inputs in the range of 0.0-360.0 for the h channel and 0.0-1.0 for the s and v channels
@@ -186,7 +199,7 @@ uint16_t penFromHSV(float h, float s, float v) {
186199
case 4: r = t; g = p; b = v; break;
187200
case 5: r = v; g = p; b = q; break;
188201
}
189-
return pico_display.create_pen(r, g, b);
202+
return graphics.create_pen(r, g, b);
190203
}
191204

192205
void MandelbrotView::createPalettes(int aPaletteSize) {
@@ -195,12 +208,12 @@ void MandelbrotView::createPalettes(int aPaletteSize) {
195208
pPalettes[ii] = static_cast<uint16_t*>(malloc(sizeof(uint16_t) * aPaletteSize));
196209
}
197210
for (int ii = 0; ii < aPaletteSize; ++ii) {
198-
pPalettes[0][ii] = pico_display.create_pen(255 - 255 * ii / aPaletteSize, 255 - 255 * ii / aPaletteSize, 255 - 255 * ii / aPaletteSize);
199-
pPalettes[1][ii] = pico_display.create_pen(255, 255, 255);
211+
pPalettes[0][ii] = graphics.create_pen(255 - 255 * ii / aPaletteSize, 255 - 255 * ii / aPaletteSize, 255 - 255 * ii / aPaletteSize);
212+
pPalettes[1][ii] = graphics.create_pen(255, 255, 255);
200213
pPalettes[2][ii] = penFromHSV(160.0 + 360.0 * static_cast<float>(ii) / aPaletteSize, 0.9, 1.0);
201214
pPalettes[3][ii] = penFromHSV(60.0 + 360.0 * static_cast<float>(ii) / aPaletteSize, 0.9, 1.0);
202215
pPalettes[4][ii] = penFromHSV(360.0 * static_cast<float>(ii) / aPaletteSize, 0.9, 1.0);
203-
pPalettes[5][ii] = pico_display.create_pen(ii % 4 * 64, ii % 8 * 32, ii % 16 * 16);
216+
pPalettes[5][ii] = graphics.create_pen(ii % 4 * 64, ii % 8 * 32, ii % 16 * 16);
204217
}
205218
pPalette = pPalettes[paletteIndex];
206219
}
@@ -240,15 +253,14 @@ void core1_main(void) {
240253
#endif // MULTICORE
241254

242255
int main() {
243-
pico_display.init();
244-
pico_display.set_backlight(100);
245-
pico_display.set_pen(0, 0, 0);
246-
pico_display.clear();
247-
pico_display.update();
256+
st7789.set_backlight(100);
257+
graphics.set_pen(0, 0, 0);
258+
graphics.clear();
259+
st7789.update(&graphics);
248260

249261
const int iterationLimitV0 = 40;
250262
const int iterationLimitV1 = 128;
251-
g_view0.init(DISPLAY_WIDTH, DISPLAY_HEIGHT, g_buffer, iterationLimitV1, 4, iterationLimitV0, 0);
263+
g_view0.init(DISPLAY_WIDTH, DISPLAY_HEIGHT, (uint16_t *)graphics.frame_buffer, iterationLimitV1, 4, iterationLimitV0, 0);
252264

253265
#if defined(MULTICORE)
254266
g_view1.init(g_view0, 1, iterationLimitV1, 1);
@@ -266,51 +278,51 @@ int main() {
266278
#if defined(MULTICORE)
267279
g_view1.setRange(fxdRangeR, fxdCenter);
268280
#endif
269-
pico_display.set_led(64, 0, 0);
281+
led.set_rgb(64, 0, 0);
270282

271283
g_view0.render();
272284

273-
pico_display.set_led(0, 0, 64);
285+
led.set_rgb(0, 0, 64);
274286
#if defined(MULTICORE)
275287
core1_start();
276288
#endif
277289

278-
pico_display.set_led(0, 0, 0);
290+
led.set_rgb(0, 0, 0);
279291

280292
// Loop, waiting for key presses
281293
bool keyPressed = false;
282294
while (keyPressed == false) {
283295
sleep_ms(1);
284296

285-
if (pico_display.is_pressed(pico_display.A)) {
297+
if (button_a.raw()) {
286298
// Hold A to move left/right
287-
if (pico_display.is_pressed(pico_display.X) && fxdCenter.r > FXD_FROM_INT(-3)) {
299+
if (button_x.read() && fxdCenter.r > FXD_FROM_INT(-3)) {
288300
fxdCenter.r -= fxdRangeR / 8;
289301
keyPressed = true;
290-
} else if (pico_display.is_pressed(pico_display.Y) && fxdCenter.r < FXD_FROM_INT(3)) {
302+
} else if (button_y.read() && fxdCenter.r < FXD_FROM_INT(3)) {
291303
fxdCenter.r += fxdRangeR / 8;
292304
keyPressed = true;
293-
} else if (pico_display.is_pressed(pico_display.B)) {
305+
} else if (button_b.read()) {
294306
// Press A and B together to switch palette
295307
g_view0.nextPalette();
296308
#if defined(MULTICORE)
297309
g_view1.nextPalette();
298310
#endif
299311
keyPressed = true;
300312
}
301-
} else if (pico_display.is_pressed(pico_display.B)) {
313+
} else if (button_b.raw()) {
302314
// Hold B to move up/down
303-
if (pico_display.is_pressed(pico_display.X) && fxdCenter.i > FXD_FROM_INT(-2)) {
315+
if (button_x.read() && fxdCenter.i > FXD_FROM_INT(-2)) {
304316
fxdCenter.i -= fxdRangeR / 8;
305317
keyPressed = true;
306-
} else if (pico_display.is_pressed(pico_display.Y) && fxdCenter.i < FXD_FROM_INT(2)) {
318+
} else if (button_y.read() && fxdCenter.i < FXD_FROM_INT(2)) {
307319
fxdCenter.i += fxdRangeR / 8;
308320
keyPressed = true;
309321
}
310322
} else {
311323
// Otherwise zoom in/out
312-
if (pico_display.is_pressed(pico_display.X)) {
313-
if (pico_display.is_pressed(pico_display.Y)) {
324+
if (button_x.read()) {
325+
if (button_y.read()) {
314326
// Press X and Y together to reset to initial position
315327
fxdRangeR = fxdInitialRangeR;
316328
fxdCenter = fxdInitialCenter;
@@ -319,7 +331,7 @@ int main() {
319331
fxdRangeR *= 3;
320332
}
321333
keyPressed = true;
322-
} else if (pico_display.is_pressed(pico_display.Y) && fxdRangeR < FXD_FROM_INT(3)) {
334+
} else if (button_y.read() && fxdRangeR < FXD_FROM_INT(3)) {
323335
fxdRangeR *= 4;
324336
fxdRangeR /= 3;
325337
keyPressed = true;

0 commit comments

Comments
 (0)