added image demo for badger2040
This commit is contained in:
parent
f7d61b587a
commit
ad0ebee16b
|
@ -1,3 +1,4 @@
|
|||
include(badger2040_drawing.cmake)
|
||||
include(badger2040_fonts.cmake)
|
||||
include(badger2040_sleep.cmake)
|
||||
include(badger2040_image.cmake)
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
set(OUTPUT_NAME badger2040_image)
|
||||
add_executable(${OUTPUT_NAME} badger2040_image.cpp)
|
||||
|
||||
target_link_libraries(${OUTPUT_NAME}
|
||||
badger2040
|
||||
hardware_spi
|
||||
)
|
||||
|
||||
# enable usb output
|
||||
pico_enable_stdio_usb(${OUTPUT_NAME} 1)
|
||||
|
||||
pico_add_extra_outputs(${OUTPUT_NAME})
|
|
@ -0,0 +1,64 @@
|
|||
#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 "badger2040.hpp"
|
||||
|
||||
#include "badger2040_image_demo_images.hpp"
|
||||
|
||||
using namespace pimoroni;
|
||||
|
||||
Badger2040 badger;
|
||||
|
||||
uint32_t time() {
|
||||
absolute_time_t t = get_absolute_time();
|
||||
return to_ms_since_boot(t);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
stdio_init_all();
|
||||
|
||||
sleep_ms(500);
|
||||
|
||||
printf("\n\n=======\nbadger2040 starting up\n\n");
|
||||
|
||||
badger.init();
|
||||
|
||||
badger.pen(15);
|
||||
badger.clear();
|
||||
|
||||
badger.pen(0);
|
||||
badger.font("sans");
|
||||
badger.text("Press A, B, or C", 15, 65, 1.0f);
|
||||
badger.update();
|
||||
|
||||
while(true) {
|
||||
|
||||
printf("> waiting for a button press..");
|
||||
badger.wait_for_press();
|
||||
printf("done!\n");
|
||||
|
||||
if(badger.pressed(badger.A)) {
|
||||
printf("> A pressed\n");
|
||||
badger.image(shaun);
|
||||
}
|
||||
|
||||
if(badger.pressed(badger.B)) {
|
||||
printf("> B pressed\n");
|
||||
badger.image(paul);
|
||||
}
|
||||
|
||||
if(badger.pressed(badger.C)) {
|
||||
printf("> C pressed\n");
|
||||
badger.image(adam);
|
||||
}
|
||||
|
||||
badger.update();
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,64 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# converts images into a format suitable for display on badger2040. this
|
||||
# includes scaling the image fit the longest edge, cropping down to 296x128
|
||||
# and reducing to black and white with dither. the data is then output as an
|
||||
# array that can be embedded directly into your c++ code
|
||||
|
||||
import argparse, sys, os, glob
|
||||
from PIL import Image, ImageEnhance
|
||||
from pathlib import Path
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Converts images into the format used by Badger2040.')
|
||||
parser.add_argument('file', nargs="+", help='input files to convert')
|
||||
|
||||
options = None
|
||||
try:
|
||||
options = parser.parse_args()
|
||||
except:
|
||||
parser.print_help()
|
||||
sys.exit(0)
|
||||
|
||||
def convert_image(img):
|
||||
img = img.resize((296, 128)) # resize and crop
|
||||
enhancer = ImageEnhance.Contrast(img)
|
||||
img = enhancer.enhance(2.0)
|
||||
img = img.convert("1") # convert to black and white
|
||||
return img
|
||||
|
||||
|
||||
# create map of images based on input filenames
|
||||
for input_filename in options.file:
|
||||
with Image.open(input_filename) as img:
|
||||
img = convert_image(img)
|
||||
|
||||
image_name = Path(input_filename).stem
|
||||
|
||||
h, w = img.size
|
||||
|
||||
data = Image.Image.getdata(img)
|
||||
|
||||
bytes = []
|
||||
byte = 0
|
||||
byte_idx = 0
|
||||
x = 0
|
||||
for v in data:
|
||||
byte <<= 1
|
||||
byte |= 1 if v == 0 else 0
|
||||
byte_idx += 1
|
||||
|
||||
if byte_idx == 8: # next byte...
|
||||
bytes.append(str(byte))
|
||||
byte_idx = 0
|
||||
byte = 0
|
||||
|
||||
image_code = '''\
|
||||
static const uint8_t {image_name}[{count}] = {{
|
||||
{byte_data}
|
||||
}};
|
||||
'''.format(image_name=image_name, count=len(bytes), byte_data=", ".join(bytes))
|
||||
|
||||
print(image_code)
|
||||
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 44 KiB |
Binary file not shown.
After Width: | Height: | Size: 49 KiB |
Binary file not shown.
After Width: | Height: | Size: 46 KiB |
|
@ -115,6 +115,21 @@ namespace pimoroni {
|
|||
}
|
||||
}
|
||||
|
||||
void Badger2040::image(const uint8_t *data) {
|
||||
for(uint32_t y = 0; y < 128; y++) {
|
||||
for(uint32_t x = 0; x < 296; x++) {
|
||||
// work out byte offset in source data
|
||||
uint32_t o = (y * (296 >> 3)) + (x >> 3);
|
||||
|
||||
// extract bitmask for this pixel
|
||||
uint32_t bm = 0b10000000 >> (x & 0b111);
|
||||
|
||||
// draw the pixel
|
||||
uc8151.pixel(x, y, data[o] & bm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Badger2040::rectangle(int32_t x, int32_t y, int32_t w, int32_t h) {
|
||||
for(int cy = y; cy < y + h; cy++) {
|
||||
for(int cx = x; cx < x + w; cx++) {
|
||||
|
@ -254,9 +269,15 @@ namespace pimoroni {
|
|||
}
|
||||
|
||||
void Badger2040::wait_for_press() {
|
||||
update_button_states();
|
||||
while(_button_states == 0) {
|
||||
update_button_states();
|
||||
tight_loop_contents();
|
||||
}
|
||||
|
||||
uint32_t mask = (1UL << A) | (1UL << B) | (1UL << C) | (1UL << D) | (1UL << E);
|
||||
while(gpio_get_all() & mask) {
|
||||
tight_loop_contents();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -43,6 +43,7 @@ namespace pimoroni {
|
|||
void pixel(int32_t x, int32_t y);
|
||||
void line(int32_t x1, int32_t y1, int32_t x2, int32_t y2);
|
||||
void rectangle(int32_t x, int32_t y, int32_t w, int32_t h);
|
||||
void image(const uint8_t *data);
|
||||
|
||||
// text (fonts: sans, sans_bold, gothic, cursive_bold, cursive, serif_italic, serif, serif_bold)
|
||||
void text(std::string message, int32_t x, int32_t y, float s = 1.0f);
|
||||
|
|
Loading…
Reference in New Issue