5
5
#if defined(MULTICORE)
6
6
#include " pico/multicore.h"
7
7
#endif
8
+
8
9
#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"
9
14
10
15
11
16
using namespace pimoroni ;
12
17
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
+
13
32
typedef int32_t fixed_t ;
14
33
15
34
class complex_fixed_t {
@@ -33,12 +52,6 @@ static inline fixed_t fixed_multiply(fixed_t a, fixed_t b) {
33
52
}
34
53
#define FXD_MUL (x, y ) fixed_multiply(x, y)
35
54
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);
42
55
43
56
class MandelbrotView {
44
57
public:
@@ -161,10 +174,10 @@ void MandelbrotView::render(void) {
161
174
pFrame += screenSizeX;
162
175
if (core == 1 && (screenY & 0xF ) == 0 ) {
163
176
// update core1 view every 16 lines
164
- pico_display .update ();
177
+ st7789 .update (&graphics );
165
178
}
166
179
}
167
- pico_display .update ();
180
+ st7789 .update (&graphics );
168
181
}
169
182
170
183
// 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) {
186
199
case 4 : r = t; g = p; b = v; break ;
187
200
case 5 : r = v; g = p; b = q; break ;
188
201
}
189
- return pico_display .create_pen (r, g, b);
202
+ return graphics .create_pen (r, g, b);
190
203
}
191
204
192
205
void MandelbrotView::createPalettes (int aPaletteSize) {
@@ -195,12 +208,12 @@ void MandelbrotView::createPalettes(int aPaletteSize) {
195
208
pPalettes[ii] = static_cast <uint16_t *>(malloc (sizeof (uint16_t ) * aPaletteSize));
196
209
}
197
210
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 );
200
213
pPalettes[2 ][ii] = penFromHSV (160.0 + 360.0 * static_cast <float >(ii) / aPaletteSize, 0.9 , 1.0 );
201
214
pPalettes[3 ][ii] = penFromHSV (60.0 + 360.0 * static_cast <float >(ii) / aPaletteSize, 0.9 , 1.0 );
202
215
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 );
204
217
}
205
218
pPalette = pPalettes[paletteIndex];
206
219
}
@@ -240,15 +253,14 @@ void core1_main(void) {
240
253
#endif // MULTICORE
241
254
242
255
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);
248
260
249
261
const int iterationLimitV0 = 40 ;
250
262
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 );
252
264
253
265
#if defined(MULTICORE)
254
266
g_view1.init (g_view0, 1 , iterationLimitV1, 1 );
@@ -266,51 +278,51 @@ int main() {
266
278
#if defined(MULTICORE)
267
279
g_view1.setRange (fxdRangeR, fxdCenter);
268
280
#endif
269
- pico_display. set_led (64 , 0 , 0 );
281
+ led. set_rgb (64 , 0 , 0 );
270
282
271
283
g_view0.render ();
272
284
273
- pico_display. set_led (0 , 0 , 64 );
285
+ led. set_rgb (0 , 0 , 64 );
274
286
#if defined(MULTICORE)
275
287
core1_start ();
276
288
#endif
277
289
278
- pico_display. set_led (0 , 0 , 0 );
290
+ led. set_rgb (0 , 0 , 0 );
279
291
280
292
// Loop, waiting for key presses
281
293
bool keyPressed = false ;
282
294
while (keyPressed == false ) {
283
295
sleep_ms (1 );
284
296
285
- if (pico_display. is_pressed (pico_display. A )) {
297
+ if (button_a. raw ( )) {
286
298
// 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 )) {
288
300
fxdCenter.r -= fxdRangeR / 8 ;
289
301
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 )) {
291
303
fxdCenter.r += fxdRangeR / 8 ;
292
304
keyPressed = true ;
293
- } else if (pico_display. is_pressed (pico_display. B )) {
305
+ } else if (button_b. read ( )) {
294
306
// Press A and B together to switch palette
295
307
g_view0.nextPalette ();
296
308
#if defined(MULTICORE)
297
309
g_view1.nextPalette ();
298
310
#endif
299
311
keyPressed = true ;
300
312
}
301
- } else if (pico_display. is_pressed (pico_display. B )) {
313
+ } else if (button_b. raw ( )) {
302
314
// 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 )) {
304
316
fxdCenter.i -= fxdRangeR / 8 ;
305
317
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 )) {
307
319
fxdCenter.i += fxdRangeR / 8 ;
308
320
keyPressed = true ;
309
321
}
310
322
} else {
311
323
// 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 ( )) {
314
326
// Press X and Y together to reset to initial position
315
327
fxdRangeR = fxdInitialRangeR;
316
328
fxdCenter = fxdInitialCenter;
@@ -319,7 +331,7 @@ int main() {
319
331
fxdRangeR *= 3 ;
320
332
}
321
333
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 )) {
323
335
fxdRangeR *= 4 ;
324
336
fxdRangeR /= 3 ;
325
337
keyPressed = true ;
0 commit comments