From c48f81bc90eca2db90f95c5e0d2a2eaaaffde25e Mon Sep 17 00:00:00 2001 From: Gee Bartlett Date: Thu, 29 Sep 2022 12:01:35 +0100 Subject: [PATCH] started on converting pico display demo --- drivers/CMakeLists.txt | 1 + drivers/st7567/CMakeLists.txt | 2 +- drivers/st7567/st7567.cmake | 2 +- examples/CMakeLists.txt | 1 + examples/gfx_pack/CMakeLists.txt | 12 +++ examples/gfx_pack/gfx_demo.cpp | 124 ++++++++++++++++++++++++++++++ libraries/CMakeLists.txt | 1 + libraries/gfx_pack/CMakeLists.txt | 11 +++ libraries/gfx_pack/README.md | 84 ++++++++++++++++++++ libraries/gfx_pack/gfx_pack.cmake | 10 +++ libraries/gfx_pack/gfx_pack.cpp | 1 + libraries/gfx_pack/gfx_pack.hpp | 19 +++++ 12 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 examples/gfx_pack/CMakeLists.txt create mode 100644 examples/gfx_pack/gfx_demo.cpp create mode 100644 libraries/gfx_pack/CMakeLists.txt create mode 100644 libraries/gfx_pack/README.md create mode 100644 libraries/gfx_pack/gfx_pack.cmake create mode 100644 libraries/gfx_pack/gfx_pack.cpp create mode 100644 libraries/gfx_pack/gfx_pack.hpp diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index bb474e1d..5c005228 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -38,3 +38,4 @@ add_subdirectory(vl53l5cx) add_subdirectory(pcf85063a) add_subdirectory(pms5003) add_subdirectory(sh1107) +add_subdirectory(st7567) \ No newline at end of file diff --git a/drivers/st7567/CMakeLists.txt b/drivers/st7567/CMakeLists.txt index 080cb792..6a0bb2be 100644 --- a/drivers/st7567/CMakeLists.txt +++ b/drivers/st7567/CMakeLists.txt @@ -1 +1 @@ -include(st7735.cmake) +include(st7567.cmake) diff --git a/drivers/st7567/st7567.cmake b/drivers/st7567/st7567.cmake index 85c8ae60..9207751e 100644 --- a/drivers/st7567/st7567.cmake +++ b/drivers/st7567/st7567.cmake @@ -1,4 +1,4 @@ -set(DRIVER_NAME st7735) +set(DRIVER_NAME st7567) add_library(${DRIVER_NAME} INTERFACE) target_sources(${DRIVER_NAME} INTERFACE diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index c7de23d1..ee8884fb 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -55,3 +55,4 @@ add_subdirectory(servo2040) add_subdirectory(motor2040) add_subdirectory(inventor2040w) add_subdirectory(encoder) +add_subdirectory(gfx_pack) diff --git a/examples/gfx_pack/CMakeLists.txt b/examples/gfx_pack/CMakeLists.txt new file mode 100644 index 00000000..90a7b0b7 --- /dev/null +++ b/examples/gfx_pack/CMakeLists.txt @@ -0,0 +1,12 @@ +set(OUTPUT_NAME gfx_pack_demo) + +add_executable( + ${OUTPUT_NAME} + gfx_demo.cpp +) + +# Pull in pico libraries that we need +target_link_libraries(${OUTPUT_NAME} pico_stdlib hardware_spi hardware_pwm hardware_dma rgbled button pico_display_2 st7568 pico_graphics) + +# create map/bin/hex file etc. +pico_add_extra_outputs(${OUTPUT_NAME}) \ No newline at end of file diff --git a/examples/gfx_pack/gfx_demo.cpp b/examples/gfx_pack/gfx_demo.cpp new file mode 100644 index 00000000..ad9d9bdf --- /dev/null +++ b/examples/gfx_pack/gfx_demo.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include + +#include "libraries/pico_display_2/pico_display_2.hpp" +#include "drivers/st7789/st7789.hpp" +#include "libraries/pico_graphics/pico_graphics.hpp" +#include "rgbled.hpp" +#include "button.hpp" + +using namespace pimoroni; + +ST7789 st7789(320, 240, ROTATE_0, false, get_spi_pins(BG_SPI_FRONT)); +PicoGraphics_PenRGB332 graphics(st7789.width, st7789.height, nullptr); + +RGBLED led(PicoDisplay2::LED_R, PicoDisplay2::LED_G, PicoDisplay2::LED_B); + +Button button_a(PicoDisplay2::A); +Button button_b(PicoDisplay2::B); +Button button_x(PicoDisplay2::X); +Button button_y(PicoDisplay2::Y); + +// HSV Conversion expects float inputs in the range of 0.00-1.00 for each channel +// Outputs are rgb in the range 0-255 for each channel +void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) { + float i = floor(h * 6.0f); + float f = h * 6.0f - i; + v *= 255.0f; + uint8_t p = v * (1.0f - s); + uint8_t q = v * (1.0f - f * s); + uint8_t t = v * (1.0f - (1.0f - f) * s); + + switch (int(i) % 6) { + case 0: r = v; g = t; b = p; break; + case 1: r = q; g = v; b = p; break; + case 2: r = p; g = v; b = t; break; + case 3: r = p; g = q; b = v; break; + case 4: r = t; g = p; b = v; break; + case 5: r = v; g = p; b = q; break; + } +} + +int main() { + st7789.set_backlight(255); + + struct pt { + float x; + float y; + uint8_t r; + float dx; + float dy; + uint16_t pen; + }; + + std::vector shapes; + for(int i = 0; i < 100; i++) { + pt shape; + shape.x = rand() % graphics.bounds.w; + shape.y = rand() % graphics.bounds.h; + shape.r = (rand() % 10) + 3; + shape.dx = float(rand() % 255) / 64.0f; + shape.dy = float(rand() % 255) / 64.0f; + shape.pen = graphics.create_pen(rand() % 255, rand() % 255, rand() % 255); + shapes.push_back(shape); + } + + Point text_location(0, 0); + + Pen BG = graphics.create_pen(120, 40, 60); + Pen WHITE = graphics.create_pen(255, 255, 255); + + while(true) { + if(button_a.raw()) text_location.x -= 1; + if(button_b.raw()) text_location.x += 1; + + if(button_x.raw()) text_location.y -= 1; + if(button_y.raw()) text_location.y += 1; + + graphics.set_pen(BG); + graphics.clear(); + + for(auto &shape : shapes) { + shape.x += shape.dx; + shape.y += shape.dy; + if((shape.x - shape.r) < 0) { + shape.dx *= -1; + shape.x = shape.r; + } + if((shape.x + shape.r) >= graphics.bounds.w) { + shape.dx *= -1; + shape.x = graphics.bounds.w - shape.r; + } + if((shape.y - shape.r) < 0) { + shape.dy *= -1; + shape.y = shape.r; + } + if((shape.y + shape.r) >= graphics.bounds.h) { + shape.dy *= -1; + shape.y = graphics.bounds.h - shape.r; + } + + graphics.set_pen(shape.pen); + graphics.circle(Point(shape.x, shape.y), shape.r); + + } + + // Since HSV takes a float from 0.0 to 1.0 indicating hue, + // then we can divide millis by the number of milliseconds + // we want a full colour cycle to take. 5000 = 5 sec. + uint8_t r = 0, g = 0, b = 0; + from_hsv((float)millis() / 5000.0f, 1.0f, 0.5f + sinf(millis() / 100.0f / 3.14159f) * 0.5f, r, g, b); + led.set_rgb(r, g, b); + + + graphics.set_pen(WHITE); + graphics.text("Hello World", text_location, 320); + + // update screen + st7789.update(&graphics); + } + + return 0; +} diff --git a/libraries/CMakeLists.txt b/libraries/CMakeLists.txt index b7fa264f..6d69f7d0 100644 --- a/libraries/CMakeLists.txt +++ b/libraries/CMakeLists.txt @@ -35,3 +35,4 @@ add_subdirectory(inventor2040w) add_subdirectory(adcfft) add_subdirectory(jpegdec) add_subdirectory(inky_frame) +add_subdirectory(gfx_pack) diff --git a/libraries/gfx_pack/CMakeLists.txt b/libraries/gfx_pack/CMakeLists.txt new file mode 100644 index 00000000..3b2ea54e --- /dev/null +++ b/libraries/gfx_pack/CMakeLists.txt @@ -0,0 +1,11 @@ +set(LIB_NAME gfx_pack) +add_library(${LIB_NAME} INTERFACE) + +target_sources(${LIB_NAME} INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/${LIB_NAME}.cpp +) + +target_include_directories(${LIB_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR}) + +# Pull in pico libraries that we need +target_link_libraries(${LIB_NAME} INTERFACE pico_stdlib hardware_spi hardware_pwm hardware_dma st7789 pico_graphics) \ No newline at end of file diff --git a/libraries/gfx_pack/README.md b/libraries/gfx_pack/README.md new file mode 100644 index 00000000..53ad9c6d --- /dev/null +++ b/libraries/gfx_pack/README.md @@ -0,0 +1,84 @@ +# Pico Display 2.0" Pack + +Our Pico Display Pack offers a vibrant 1.14" (240x135) IPS LCD screen for your Raspberry Pi Pico it also includes four switches and and an RGB LED! + +- [Example Program](#example-program) +- [Function Reference](#function-reference) + - [PicoGraphics](#picographics) + - [ST7789](#st7789) + +## Example Program + +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. + +```c++ +#include "pico_display_2.hpp" +#include "drivers/st7789/st7789.hpp" +#include "libraries/pico_graphics/pico_graphics.hpp" +#include "rgbled.hpp" +#include "button.hpp" + +// Display driver +ST7789 st7789(PicoDisplay2::WIDTH, PicoDisplay2::HEIGHT, ROTATE_0, false, get_spi_pins(BG_SPI_FRONT)); + +// Graphics library - in RGB332 mode you get 256 colours and optional dithering for 75K RAM. +PicoGraphics_PenRGB332 graphics(st7789.width, st7789.height, nullptr); + +// RGB LED +RGBLED led(PicoDisplay2::LED_R, PicoDisplay2::LED_G, PicoDisplay2::LED_B); + +// And each button +Button button_a(PicoDisplay2::A); +Button button_b(PicoDisplay2::B); +Button button_x(PicoDisplay2::X); +Button button_y(PicoDisplay2::Y); + +int main() { + + // set the backlight to a value between 0 and 255 + // the backlight is driven via PWM and is gamma corrected by our + // library to give a gorgeous linear brightness range. + st7789.set_backlight(100); + + while(true) { + // detect if the A button is pressed (could be A, B, X, or Y) + if(button_a.raw(display.A)) { + // make the led glow green + // parameters are red, green, blue all between 0 and 255 + // these are also gamma corrected + led.set_rgb(0, 255, 0); + } + + // set the colour of the pen + // parameters are red, green, blue all between 0 and 255 + graphics.set_pen(30, 40, 50); + + // fill the screen with the current pen colour + graphics.clear(); + + // draw a box to put some text in + graphics.set_pen(10, 20, 30); + Rect text_rect(10, 10, 150, 150); + graphics.rectangle(text_rect); + + // write some text inside the box with 10 pixels of margin + // automatically word wrapping + text_rect.deflate(10); + graphics.set_pen(110, 120, 130); + graphics.text("This is a message", Point(text_rect.x, text_rect.y), text_rect.w); + + // now we've done our drawing let's update the screen + st7789.update(&graphics); + } +} +``` + +## Function Reference + +### PicoGraphics + +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). + +### ST7789 + +Pico Display uses the ST7789 display driver to handle the LCD. For more information [read the ST7789 README.](../../drivers/st7789/README.md). \ No newline at end of file diff --git a/libraries/gfx_pack/gfx_pack.cmake b/libraries/gfx_pack/gfx_pack.cmake new file mode 100644 index 00000000..b7348688 --- /dev/null +++ b/libraries/gfx_pack/gfx_pack.cmake @@ -0,0 +1,10 @@ +add_library(pico_display_2 INTERFACE) + +target_sources(pico_display_2 INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/pico_display_2.cpp +) + +target_include_directories(pico_display_2 INTERFACE ${CMAKE_CURRENT_LIST_DIR}) + +# Pull in pico libraries that we need +target_link_libraries(pico_display_2 INTERFACE pico_stdlib) \ No newline at end of file diff --git a/libraries/gfx_pack/gfx_pack.cpp b/libraries/gfx_pack/gfx_pack.cpp new file mode 100644 index 00000000..162f2bc7 --- /dev/null +++ b/libraries/gfx_pack/gfx_pack.cpp @@ -0,0 +1 @@ +#include "gfx_pack.hpp" \ No newline at end of file diff --git a/libraries/gfx_pack/gfx_pack.hpp b/libraries/gfx_pack/gfx_pack.hpp new file mode 100644 index 00000000..8a231874 --- /dev/null +++ b/libraries/gfx_pack/gfx_pack.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "pico/stdlib.h" + +namespace pimoroni { + class gfx_pack { + public: + static const int WIDTH = 128; + static const int HEIGHT = 64; + static const uint8_t A = 12; + static const uint8_t B = 13; + static const uint8_t X = 14; + static const uint8_t Y = 15; + static const uint8_t BL_R = 6; + static const uint8_t BL_G = 7; + static const uint8_t BL_B = 8; + static const uint8_t BL_W = 9; + }; +}