Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Badger 2040w C++ Library #903

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions drivers/uc8151/uc8151.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ namespace pimoroni {

void UC8151::reset() {
if(RESET == PIN_UNUSED) return;
gpio_put(RESET, 0); sleep_ms(10);
gpio_put(RESET, 1); sleep_ms(10);
gpio_put(RESET, 0); busy_wait_ms(10);
gpio_put(RESET, 1); busy_wait_ms(10);
busy_wait();
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/uc8151/uc8151.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace pimoroni {

uint8_t update_speed = 0;
bool inverted = true; // Makes 0 black and 1 white, as is foretold.
bool blocking = true;
bool blocking = false;

public:
UC8151(uint16_t width, uint16_t height, Rotation rotate) : UC8151(width, height, rotate, {PIMORONI_SPI_DEFAULT_INSTANCE, SPI_BG_FRONT_CS, SPI_DEFAULT_SCK, SPI_DEFAULT_MOSI, PIN_UNUSED, 20, PIN_UNUSED}) {};
Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ add_subdirectory(automation2040w)
add_subdirectory(plasma_stick)
add_subdirectory(plasma2040)
add_subdirectory(badger2040)
add_subdirectory(badger2040w)
add_subdirectory(tufty2040)
add_subdirectory(interstate75)
add_subdirectory(servo2040)
Expand Down
6 changes: 6 additions & 0 deletions examples/badger2040w/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include(badger2040w_sleep.cmake)
include(badger2040w_drawing.cmake)
include(badger2040w_fonts.cmake)
include(badger2040w_image.cmake)
include(badger2040w_icon.cmake)
include(badger2040w_rtc.cmake)
12 changes: 12 additions & 0 deletions examples/badger2040w/badger2040w_drawing.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(OUTPUT_NAME badger2040w_drawing)
add_executable(${OUTPUT_NAME} badger2040w_drawing.cpp)

target_link_libraries(${OUTPUT_NAME}
badger2040w
hardware_spi
)

# enable usb output
pico_enable_stdio_usb(${OUTPUT_NAME} 1)

pico_add_extra_outputs(${OUTPUT_NAME})
45 changes: 45 additions & 0 deletions examples/badger2040w/badger2040w_drawing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "pico/stdlib.h"
#include <stdio.h>

#include "common/pimoroni_common.hpp"
#include "badger2040w.hpp"

using namespace pimoroni;

Badger2040W badger;

uint32_t time() {
absolute_time_t t = get_absolute_time();
return to_ms_since_boot(t);
}

int main() {

badger.init();
stdio_init_all();
printf("\n\n=======\nbadger2040 starting up\n\n");

badger.uc8151->set_update_speed(2);
badger.graphics->set_font("bitmap8");
badger.graphics->set_thickness(2);
char time_str[11];
while(true) {

badger.graphics->set_pen(15);
badger.graphics->clear();

badger.graphics->set_pen(0);
badger.graphics->rectangle(Rect(0,badger.DISPLAY_HEIGHT / 4,badger.DISPLAY_WIDTH,badger.DISPLAY_HEIGHT / 2));
badger.graphics->set_pen(15);
badger.graphics->rectangle(Rect(5,badger.DISPLAY_HEIGHT / 4 + 5,badger.DISPLAY_WIDTH - 10,badger.DISPLAY_HEIGHT / 2 - 10));

badger.graphics->set_pen(0);
sprintf(time_str, "%ld", time());
int32_t time_x_centered = (badger.DISPLAY_WIDTH - badger.graphics->measure_text(time_str, 2.0f)) / 2;
badger.graphics->text("Time since boot (ms)", Point(5, 10), badger.DISPLAY_WIDTH, 2.0f);
badger.graphics->text(time_str, Point(time_x_centered, badger.DISPLAY_HEIGHT / 2 - 8), badger.DISPLAY_WIDTH, 2.0f);

badger.update();
sleep_ms(5000);
}
}
12 changes: 12 additions & 0 deletions examples/badger2040w/badger2040w_fonts.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(OUTPUT_NAME badger2040w_fonts)
add_executable(${OUTPUT_NAME} badger2040w_fonts.cpp)

target_link_libraries(${OUTPUT_NAME}
badger2040w
hardware_spi
)

# enable usb output
pico_enable_stdio_usb(${OUTPUT_NAME} 1)

pico_add_extra_outputs(${OUTPUT_NAME})
103 changes: 103 additions & 0 deletions examples/badger2040w/badger2040w_fonts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "pico/stdlib.h"
#include <stdio.h>
#include <cstring>
#include <string>
#include <algorithm>
#include "pico/time.h"
#include "pico/platform.h"

#include "common/pimoroni_common.hpp"
#include "badger2040w.hpp"

using namespace pimoroni;

Badger2040W badger;

uint32_t time() {
absolute_time_t t = get_absolute_time();
return to_ms_since_boot(t);
}

std::array<std::string, 8> font_names = {
"sans", "sans_bold", "gothic", "cursive",
"cursive_bold", "serif", "serif_bold", "serif_italic"
};
int8_t selected_font = 0;

void draw() {
badger.graphics->set_pen(15);
badger.graphics->clear();

badger.graphics->set_font("sans");
for(int i = 0; i < int(font_names.size()); i++) {
std::string name = font_names[i];

if(selected_font == i) {
badger.graphics->set_pen(0);
badger.graphics->rectangle(Rect(0, i * 16, 80, 16));
badger.graphics->set_pen(15);
}else{
badger.graphics->set_pen(0);
}

badger.graphics->text(name, Point(2, i * 16 + 7), badger.DISPLAY_WIDTH, 0.4f);
}

badger.graphics->set_font(font_names[selected_font]);
badger.graphics->set_thickness(2);
badger.graphics->text("The quick", Point(90, 10), badger.DISPLAY_WIDTH, 0.80f);
badger.graphics->text("brown fox", Point(90, 32), badger.DISPLAY_WIDTH, 0.80f);
badger.graphics->text("jumped over", Point(90, 54), badger.DISPLAY_WIDTH, 0.80f);
badger.graphics->text("the lazy dog.", Point(90, 76), badger.DISPLAY_WIDTH, 0.80f);
badger.graphics->text("0123456789", Point(90, 98), badger.DISPLAY_WIDTH, 0.80f);
badger.graphics->text("!\"£$%^&*()", Point(90, 120), badger.DISPLAY_WIDTH, 0.80f);
badger.graphics->set_thickness(1);

badger.update();
}

int main() {

badger.init();
stdio_init_all();

printf("\n\n=======\nbadger2040 starting up\n\n");

badger.uc8151->set_update_speed(2);

uint32_t i = 0;

while(true) {
printf("> drawing..");

draw();

printf("done!\n");

printf("> waiting for a button press..");
badger.wait_for_press();
printf("done!\n");

if(badger.pressed(badger.DOWN)) {
printf("> down pressed\n");
selected_font++;
}

if(badger.pressed(badger.UP)) {
printf("> up pressed\n");
selected_font--;
}

if(badger.pressed(badger.C)) {
printf("> C pressed\n");
badger.halt();
}

selected_font = selected_font < 0 ? int(font_names.size()) - 1 : selected_font;
selected_font = selected_font >= int(font_names.size()) ? 0 : selected_font;

printf("> newly selected font is %s (%d)\n", font_names[selected_font].c_str(), selected_font);

i++;
}
}
12 changes: 12 additions & 0 deletions examples/badger2040w/badger2040w_icon.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(OUTPUT_NAME badger2040w_icon)
add_executable(${OUTPUT_NAME} badger2040w_icon.cpp)

target_link_libraries(${OUTPUT_NAME}
badger2040w
hardware_spi
)

# enable usb output
pico_enable_stdio_usb(${OUTPUT_NAME} 1)

pico_add_extra_outputs(${OUTPUT_NAME})
36 changes: 36 additions & 0 deletions examples/badger2040w/badger2040w_icon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "pico/stdlib.h"
#include <stdio.h>

#include "common/pimoroni_common.hpp"
#include "badger2040w.hpp"

#include "badger2040w_icon_demo_icons.hpp"

using namespace pimoroni;

Badger2040W badger;

int main() {

badger.init();
stdio_init_all();

printf("\n\n=======\nbadger2040 starting up\n\n");

badger.uc8151->set_update_speed(1);

badger.graphics->set_pen(15);
badger.graphics->clear();
auto iconsize = 40;
auto x_offset = 10;
for (auto x=0; x < (int)badger.DISPLAY_WIDTH / iconsize; x++) {
for (auto i=0; i < 3; i++) {
auto idx = (i + x) % 3;
badger.icon(iconsheet, idx, iconsize * 3, Rect(x_offset + x * iconsize, iconsize * i, iconsize, iconsize));
}
}

badger.update();
badger.halt();

}
3 changes: 3 additions & 0 deletions examples/badger2040w/badger2040w_icon_demo_icons.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
static const uint8_t iconsheet[600] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x58, 0x00, 0x15, 0xff, 0x55, 0x55, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc1, 0x81, 0x87, 0x80, 0x1f, 0xff, 0xe9, 0x7f, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x64, 0x00, 0x40, 0x0f, 0xff, 0xe5, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 0x00, 0x40, 0x17, 0xff, 0xd4, 0xff, 0xfc, 0x00, 0x00, 0xff, 0x00, 0x00, 0x02, 0x00, 0x18, 0x00, 0x40, 0x1f, 0xff, 0xea, 0xff, 0xfc, 0x00, 0x01, 0xff, 0x80, 0x00, 0x02, 0x00, 0x18, 0x00, 0x40, 0x0f, 0xff, 0xd2, 0x7f, 0xfc, 0x00, 0x07, 0xff, 0xe0, 0x00, 0x02, 0x01, 0x18, 0x00, 0x40, 0x17, 0xff, 0xa9, 0xff, 0xfc, 0x00, 0xff, 0xc7, 0xe0, 0x00, 0x01, 0x00, 0x99, 0x00, 0x80, 0x1f, 0xff, 0xca, 0x7f, 0xfc, 0x01, 0xff, 0x81, 0xf0, 0x00, 0x01, 0x00, 0x7e, 0x01, 0x00, 0x0f, 0xff, 0xa5, 0x7f, 0xfc, 0x03, 0xff, 0x80, 0xf8, 0x00, 0x00, 0xc0, 0x7e, 0x01, 0x00, 0x13, 0xff, 0xd2, 0xff, 0xfc, 0x07, 0xff, 0xb4, 0xf8, 0x00, 0x00, 0x20, 0xff, 0x06, 0x00, 0x0f, 0xff, 0xd4, 0xff, 0xfc, 0x07, 0xff, 0x84, 0xff, 0x80, 0x00, 0x3f, 0xc3, 0xfc, 0x00, 0x15, 0xff, 0x85, 0x7f, 0xfc, 0x07, 0xff, 0x81, 0xff, 0xc0, 0x00, 0x61, 0x00, 0x86, 0x00, 0x0a, 0xff, 0xe9, 0xff, 0xfc, 0x07, 0xff, 0xa3, 0xff, 0xe0, 0x00, 0xc1, 0x00, 0x82, 0x00, 0x15, 0x7f, 0x94, 0x7f, 0xfc, 0x07, 0xff, 0xf7, 0xff, 0xf0, 0x00, 0x83, 0x00, 0xc1, 0x00, 0x0a, 0xbf, 0x49, 0xff, 0xcc, 0x07, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x87, 0x81, 0xe1, 0x00, 0x1a, 0x7f, 0xa4, 0x7f, 0xa4, 0x03, 0xe0, 0x7f, 0xff, 0xf0, 0x01, 0x9c, 0x7e, 0x18, 0x80, 0x05, 0x7f, 0x22, 0xff, 0x54, 0x03, 0xe0, 0x1b, 0xff, 0xf0, 0x01, 0x30, 0x3c, 0x0c, 0x80, 0x15, 0x3f, 0x94, 0xdf, 0x14, 0x01, 0xe0, 0x00, 0x7f, 0xf0, 0x01, 0xe0, 0x18, 0x07, 0x80, 0x0a, 0xaf, 0xa2, 0x77, 0x4c, 0x00, 0x62, 0x00, 0x3f, 0xf0, 0x02, 0x60, 0x18, 0x02, 0x60, 0x0d, 0x3e, 0x95, 0xfe, 0x54, 0x00, 0x63, 0x00, 0x1f, 0xf0, 0x04, 0x40, 0x18, 0x02, 0x20, 0x16, 0xbf, 0xc8, 0xef, 0x2c, 0x00, 0xc0, 0x0c, 0x1f, 0xe0, 0x0c, 0x40, 0x18, 0x02, 0x20, 0x0b, 0x5f, 0xa2, 0x7e, 0x9c, 0x00, 0xc0, 0x0c, 0x1f, 0xc0, 0x08, 0x40, 0x18, 0x02, 0x10, 0x16, 0x9f, 0x95, 0x7c, 0x7c, 0x01, 0xde, 0x00, 0x3f, 0x00, 0x08, 0x60, 0x3c, 0x02, 0x10, 0x0f, 0x4f, 0x48, 0xfd, 0x5c, 0x01, 0xff, 0x80, 0x3e, 0x00, 0x08, 0x60, 0x76, 0x06, 0x30, 0x17, 0xd7, 0x92, 0xfa, 0xbc, 0x01, 0xf3, 0xf8, 0x3e, 0x00, 0x04, 0xf8, 0x81, 0xbf, 0x20, 0x07, 0xd7, 0x49, 0x79, 0xfc, 0x01, 0xf2, 0x3c, 0x7c, 0x00, 0x06, 0xff, 0x00, 0xf9, 0x60, 0x15, 0xf7, 0x52, 0xf7, 0xfc, 0x03, 0xf8, 0x0f, 0xfc, 0x00, 0x03, 0x0f, 0x00, 0x70, 0xc0, 0x0b, 0xfe, 0xad, 0x37, 0xfc, 0x01, 0xfc, 0x0f, 0xfc, 0x00, 0x03, 0x06, 0x00, 0x60, 0xc0, 0x07, 0xfa, 0xff, 0x57, 0xfc, 0x01, 0xfe, 0x7f, 0xf8, 0x00, 0x01, 0x02, 0x00, 0x40, 0x80, 0x13, 0xfa, 0xff, 0xaf, 0xfc, 0x01, 0xff, 0xff, 0xf0, 0x00, 0x01, 0x03, 0x00, 0x00, 0x80, 0x0a, 0xfd, 0xfb, 0xdf, 0xfc, 0x00, 0xff, 0xff, 0xf0, 0x00, 0x01, 0x01, 0x00, 0x80, 0x80, 0x0b, 0xfd, 0xff, 0xff, 0xfc, 0x00, 0xcf, 0xff, 0xe0, 0x00, 0x00, 0x81, 0x81, 0x81, 0x00, 0x07, 0xff, 0xff, 0xdf, 0xfc, 0x00, 0x4f, 0xff, 0xc0, 0x00, 0x00, 0xc1, 0xff, 0x83, 0x00, 0x10, 0xfe, 0xff, 0xff, 0xfc, 0x00, 0x07, 0xff, 0x00, 0x00, 0x00, 0x63, 0xc3, 0x86, 0x00, 0x0b, 0xff, 0x7f, 0x7f, 0xfc, 0x00, 0x01, 0x8f, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x78, 0x00, 0x05, 0x7f, 0xde, 0xff, 0xfc, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x06, 0x00, 0x60, 0x00, 0x14, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x80, 0x00, 0x03, 0xbf, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0x00, 0x00, 0x15, 0x7f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x01, 0xff, 0xff, 0xfb, 0xfc
};
12 changes: 12 additions & 0 deletions examples/badger2040w/badger2040w_image.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(OUTPUT_NAME badger2040w_image)
add_executable(${OUTPUT_NAME} badger2040w_image.cpp)

target_link_libraries(${OUTPUT_NAME}
badger2040w
hardware_spi
)

# enable usb output
pico_enable_stdio_usb(${OUTPUT_NAME} 1)

pico_add_extra_outputs(${OUTPUT_NAME})
51 changes: 51 additions & 0 deletions examples/badger2040w/badger2040w_image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "pico/stdlib.h"
#include <stdio.h>

#include "common/pimoroni_common.hpp"
#include "badger2040w.hpp"

#include "badger2040w_image_demo_images.hpp"

using namespace pimoroni;

Badger2040W badger;

int main() {

badger.init();
stdio_init_all();

printf("\n\n=======\nbadger2040 starting up\n\n");

badger.uc8151->set_update_speed(2);


if(badger.pressed_to_wake(badger.A)) {
printf("> A pressed\n");
badger.image(shaun, Rect(0, 0, badger.DISPLAY_WIDTH, badger.DISPLAY_HEIGHT));
}

else if(badger.pressed_to_wake(badger.B)) {
printf("> B pressed\n");
badger.image(paul, Rect(0, 0, badger.DISPLAY_WIDTH, badger.DISPLAY_HEIGHT));
}

else if(badger.pressed_to_wake(badger.C)) {
printf("> C pressed\n");
badger.image(adam, Rect(0, 0, badger.DISPLAY_WIDTH, badger.DISPLAY_HEIGHT));
}

else {
printf("> No A/B/C key pressed\n");
badger.graphics->set_pen(15);
badger.graphics->clear();

badger.graphics->set_pen(0);
badger.graphics->set_font("sans");
badger.graphics->text("Press A, B, or C", Point(15, 65), badger.DISPLAY_WIDTH, 1.0f);
}

badger.update();
badger.halt();

}
12 changes: 12 additions & 0 deletions examples/badger2040w/badger2040w_image_demo_images.hpp

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions examples/badger2040w/badger2040w_rtc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
set(WIFI_SSID "${WIFI_SSID}" CACHE INTERNAL "WiFi SSID")
set(WIFI_PASSWORD "${WIFI_PASSWORD}" CACHE INTERNAL "WiFi password")

if ("${WIFI_SSID}" STREQUAL "" OR "${WIFI_PASSWORD}" STREQUAL "")
message(WARNING "WIFI_SSID or WIFI_PASSWORD is not defined. Skipping rtc example.")
return()
endif()

set(OUTPUT_NAME badger2040w_rtc)
add_executable(${OUTPUT_NAME} badger2040w_rtc.cpp)

target_link_libraries(${OUTPUT_NAME}
badger2040w
hardware_spi
pico_cyw43_arch_lwip_threadsafe_background
)

target_include_directories(badger2040w_rtc PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)


target_compile_definitions(badger2040w_rtc PRIVATE
WIFI_SSID=\"${WIFI_SSID}\"
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
)

# enable usb output
pico_enable_stdio_usb(${OUTPUT_NAME} 1)

pico_add_extra_outputs(${OUTPUT_NAME})
Loading