Skip to content

Commit 080e6ad

Browse files
committed
Add display_pack_28 and demo with FPS, fix spi for rp2350 in st7789
1 parent 871399a commit 080e6ad

File tree

10 files changed

+268
-1
lines changed

10 files changed

+268
-1
lines changed

drivers/st7789/st7789.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ namespace pimoroni {
4747

4848
// The ST7789 requires 16 ns between SPI rising edges.
4949
// 16 ns = 62,500,000 Hz
50-
static const uint32_t SPI_BAUD = 62'500'000;
50+
// 2350 doesn't support 62,500,000 so use 75,000,000 seems to work.
51+
#if !PICO_RP2350
52+
static const uint32_t SPI_BAUD = 62'500'000;
53+
#else
54+
static const uint32_t SPI_BAUD = 75'000'000;
55+
#endif
56+
5157

5258

5359
public:

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_subdirectory(breakout_mlx90640)
3030

3131
add_subdirectory(pico_display)
3232
add_subdirectory(pico_display_2)
33+
add_subdirectory(pico_display_28)
3334
add_subdirectory(pico_unicorn)
3435
add_subdirectory(pico_unicorn_plasma)
3536
add_subdirectory(pico_scroll)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set(OUTPUT_NAME pico_display28_demo)
2+
3+
add_executable(
4+
${OUTPUT_NAME}
5+
pico_display_28_demo.cpp
6+
)
7+
8+
# Pull in pico libraries that we need
9+
target_link_libraries(${OUTPUT_NAME} pico_stdlib hardware_spi hardware_pwm hardware_dma rgbled button pico_display_28 st7789 pico_graphics)
10+
11+
# create map/bin/hex file etc.
12+
pico_add_extra_outputs(${OUTPUT_NAME})
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}

libraries/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_subdirectory(breakout_bh1745)
2020
add_subdirectory(pico_graphics)
2121
add_subdirectory(pico_display)
2222
add_subdirectory(pico_display_2)
23+
add_subdirectory(pico_display_28)
2324
add_subdirectory(pico_unicorn)
2425
add_subdirectory(pico_scroll)
2526
add_subdirectory(pico_explorer)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
set(LIB_NAME pico_display_28)
2+
add_library(${LIB_NAME} INTERFACE)
3+
4+
target_sources(${LIB_NAME} INTERFACE
5+
${CMAKE_CURRENT_LIST_DIR}/${LIB_NAME}.cpp
6+
)
7+
8+
target_include_directories(${LIB_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
9+
10+
# Pull in pico libraries that we need
11+
target_link_libraries(${LIB_NAME} INTERFACE pico_stdlib hardware_spi hardware_pwm hardware_dma st7789 pico_graphics)

libraries/pico_display_28/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Pico Display 2.8" Pack <!-- omit in toc -->
2+
3+
Our Pico Display Pack offers a vibrant 2.8" (320x240) IPS LCD screen for your Raspberry Pi Pico it also includes four switches and and an RGB LED!
4+
5+
- [Example Program](#example-program)
6+
- [Function Reference](#function-reference)
7+
- [PicoGraphics](#picographics)
8+
- [ST7789](#st7789)
9+
10+
## Example Program
11+
12+
The following example sets up Pico Display, displays some basic demo text and graphics and will illuminate the RGB LED green if the A button is pressed.
13+
14+
```c++
15+
#include "pico_display_28.hpp"
16+
#include "drivers/st7789/st7789.hpp"
17+
#include "libraries/pico_graphics/pico_graphics.hpp"
18+
#include "rgbled.hpp"
19+
#include "button.hpp"
20+
21+
// Display driver
22+
ST7789 st7789(PicoDisplay28::WIDTH, PicoDisplay28::HEIGHT, ROTATE_0, false, get_spi_pins(BG_SPI_FRONT));
23+
24+
// Graphics library - in RGB332 mode you get 256 colours and optional dithering for 75K RAM.
25+
PicoGraphics_PenRGB332 graphics(st7789.width, st7789.height, nullptr);
26+
27+
// RGB LED
28+
RGBLED led(PicoDisplay28::LED_R, PicoDisplay28::LED_G, PicoDisplay28::LED_B);
29+
30+
// And each button
31+
Button button_a(PicoDisplay28::A);
32+
Button button_b(PicoDisplay28::B);
33+
Button button_x(PicoDisplay28::X);
34+
Button button_y(PicoDisplay28::Y);
35+
36+
int main() {
37+
38+
// set the backlight to a value between 0 and 255
39+
// the backlight is driven via PWM and is gamma corrected by our
40+
// library to give a gorgeous linear brightness range.
41+
st7789.set_backlight(100);
42+
43+
while(true) {
44+
// detect if the A button is pressed (could be A, B, X, or Y)
45+
if(button_a.raw(display.A)) {
46+
// make the led glow green
47+
// parameters are red, green, blue all between 0 and 255
48+
// these are also gamma corrected
49+
led.set_rgb(0, 255, 0);
50+
}
51+
52+
// set the colour of the pen
53+
// parameters are red, green, blue all between 0 and 255
54+
graphics.set_pen(30, 40, 50);
55+
56+
// fill the screen with the current pen colour
57+
graphics.clear();
58+
59+
// draw a box to put some text in
60+
graphics.set_pen(10, 20, 30);
61+
Rect text_rect(10, 10, 150, 150);
62+
graphics.rectangle(text_rect);
63+
64+
// write some text inside the box with 10 pixels of margin
65+
// automatically word wrapping
66+
text_rect.deflate(10);
67+
graphics.set_pen(110, 120, 130);
68+
graphics.text("This is a message", Point(text_rect.x, text_rect.y), text_rect.w);
69+
70+
// now we've done our drawing let's update the screen
71+
st7789.update(&graphics);
72+
}
73+
}
74+
```
75+
76+
## Function Reference
77+
78+
### PicoGraphics
79+
80+
Pico Display uses our Pico Graphics library to draw graphics and text. For more information [read the Pico Graphics function reference.](../pico_graphics/README.md#function-reference).
81+
82+
### ST7789
83+
84+
Pico Display uses the ST7789 display driver to handle the LCD. For more information [read the ST7789 README.](../../drivers/st7789/README.md).
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
add_library(pico_display_28 INTERFACE)
2+
3+
target_sources(pico_display_28 INTERFACE
4+
${CMAKE_CURRENT_LIST_DIR}/pico_display_28.cpp
5+
)
6+
7+
target_include_directories(pico_display_28 INTERFACE ${CMAKE_CURRENT_LIST_DIR})
8+
9+
# Pull in pico libraries that we need
10+
target_link_libraries(pico_display_28 INTERFACE pico_stdlib)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "pico_display_28.hpp"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "pico/stdlib.h"
4+
5+
namespace pimoroni {
6+
class PicoDisplay28 {
7+
public:
8+
static const int WIDTH = 320;
9+
static const int HEIGHT = 240;
10+
static const uint8_t A = 12;
11+
static const uint8_t B = 13;
12+
static const uint8_t X = 14;
13+
static const uint8_t Y = 15;
14+
static const uint8_t LED_R = 26;
15+
static const uint8_t LED_G = 27;
16+
static const uint8_t LED_B = 28;
17+
};
18+
}

0 commit comments

Comments
 (0)