Skip to content

Commit

Permalink
Move scroll_text, show_text, bitmap_1d and set_pixels into C++ library
Browse files Browse the repository at this point in the history
Move scroll_text into the C++ library and make it support std::string.

Move show_bitmap_1d to set_bitmap_1d in the C++ library. Use it as the basis for show_text and scroll_text.

Change show_text to set_text since it does not implicitly show the result.

Add a new pico-scroll demo to show off the scrolling text functionality.
  • Loading branch information
Gadgetoid committed Apr 21, 2021
1 parent 4f358c6 commit cc3e997
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 142 deletions.
12 changes: 2 additions & 10 deletions examples/pico_scroll/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
add_executable(
scroll
demo.cpp
)

# Pull in pico libraries that we need
target_link_libraries(scroll pico_stdlib pico_scroll)

# create map/bin/hex file etc.
pico_add_extra_outputs(scroll)
include(demo.cmake)
include(scroll-text.cmake)
10 changes: 10 additions & 0 deletions examples/pico_scroll/demo.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_executable(
scroll
demo.cpp
)

# Pull in pico libraries that we need
target_link_libraries(scroll pico_stdlib pico_scroll)

# create map/bin/hex file etc.
pico_add_extra_outputs(scroll)
1 change: 1 addition & 0 deletions examples/pico_scroll/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ PicoScroll pico_scroll;
int main() {

pico_scroll.init();
pico_scroll.scroll_text("Hello World, how are you today?", 255, 100);


bool a_pressed = false;
Expand Down
10 changes: 10 additions & 0 deletions examples/pico_scroll/scroll-text.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_executable(
scroll-text
scroll-text.cpp
)

# Pull in pico libraries that we need
target_link_libraries(scroll-text pico_stdlib pico_scroll)

# create map/bin/hex file etc.
pico_add_extra_outputs(scroll-text)
41 changes: 41 additions & 0 deletions examples/pico_scroll/scroll-text.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "pico/stdlib.h"

#include "pico_scroll.hpp"

using namespace pimoroni;

PicoScroll pico_scroll;

int main() {
stdio_init_all();
pico_scroll.init();

while(true) {
pico_scroll.scroll_text("Hello World, how are you today?", 64, 100);
sleep_ms(500);

pico_scroll.set_text("Test", 64);
pico_scroll.update();
sleep_ms(1000);

// Set pixels to a chessboard pattern
char pixels[PicoScroll::WIDTH * PicoScroll::HEIGHT] = {};

pico_scroll.set_pixels(pixels);
for (int y = 0; y < PicoScroll::HEIGHT; y++) {
for (int x = 0; x < PicoScroll::WIDTH; x++) {
pixels[y * PicoScroll::WIDTH + x] = ((x + y) & 0b1) * 64;
}
}

pico_scroll.set_pixels(pixels);
pico_scroll.update();

sleep_ms(1000);
}

return 0;
}
11 changes: 1 addition & 10 deletions libraries/pico_scroll/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
add_library(pico_scroll INTERFACE)

target_sources(pico_scroll INTERFACE
${CMAKE_CURRENT_LIST_DIR}/pico_scroll.cpp
)

target_include_directories(pico_scroll INTERFACE ${CMAKE_CURRENT_LIST_DIR})

# Pull in pico libraries that we need
target_link_libraries(pico_scroll INTERFACE pico_stdlib hardware_i2c)
include(pico_scroll.cmake)
6 changes: 5 additions & 1 deletion libraries/pico_scroll/pico_scroll.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
add_library(pico_scroll INTERFACE)

target_sources(pico_scroll INTERFACE
set(PICO_SCROLL_SOURCES
${CMAKE_CURRENT_LIST_DIR}/pico_scroll.cpp
${CMAKE_CURRENT_LIST_DIR}/pico_scroll_font.cpp)

target_sources(pico_scroll INTERFACE
${PICO_SCROLL_SOURCES}
)

target_include_directories(pico_scroll INTERFACE ${CMAKE_CURRENT_LIST_DIR})
Expand Down
72 changes: 72 additions & 0 deletions libraries/pico_scroll/pico_scroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "hardware/i2c.h"

#include "pico_scroll.hpp"
#include "pico_scroll_font.hpp"

enum pin {
SDA = 4,
Expand Down Expand Up @@ -84,6 +85,77 @@ namespace pimoroni {
memset(__fb, 0, BUFFER_SIZE);
}

void PicoScroll::set_pixels(const char *pixels) {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
set_pixel(x, y, pixels[y * WIDTH + x]);
}
}
}

void PicoScroll::set_bitmap_1d(const char *bitmap, size_t bitmap_len, int brightness, int offset) {
for (int x = 0; x < WIDTH; x++) {
int k = offset + x;
if ((k >= 0) && (k < (int)bitmap_len)) {
unsigned char col = bitmap[k];
for (int y = 0; y < HEIGHT; y++) {
int val = brightness * ((col >> y) & 1);
set_pixel(x, y, val);
}
}
}
}

void PicoScroll::set_text(const char *text, size_t text_len, int brightness, int offset) {
int draw_buffer_len = PicoScroll::WIDTH + 7;
unsigned char draw_buffer[draw_buffer_len];

// clear the scroll, so only need to write visible bytes
clear();

if ((offset < -WIDTH) || (offset > (int)(6 * text_len))) {
return;
}

// compute what can actually be seen, render only that...
// modify offset and bfr_len accordingly
if (offset < 0) {
int space = 1 + (WIDTH + offset) / 6;
if (space < (int)text_len) {
text_len = space;
}
} else {
int start = offset / 6;
offset -= start * 6;
text_len = text_len - start;
if (text_len > 4) {
text_len = 4;
}
}

if (draw_buffer_len > (int)(6 * text_len)) {
draw_buffer_len = 6 * text_len;
}

render_text(text, text_len, draw_buffer, draw_buffer_len);
set_bitmap_1d((const char *)draw_buffer, draw_buffer_len, brightness, offset);
}

void PicoScroll::scroll_text(const char *text, size_t text_len, int brightness, int delay_ms) {
int draw_buffer_len = 6 * text_len;

unsigned char *draw_buffer = (unsigned char *)malloc(sizeof(unsigned char) * draw_buffer_len);
render_text(text, text_len, draw_buffer, draw_buffer_len);

for (int offset = -WIDTH; offset < draw_buffer_len; offset++) {
clear();
set_bitmap_1d((const char *)draw_buffer, draw_buffer_len, brightness, offset);
update();
sleep_ms(delay_ms);
}
free(draw_buffer);
}

void PicoScroll::update() {
i2c_write(COLOR_OFFSET, (const char *)__fb, BUFFER_SIZE);
}
Expand Down
11 changes: 11 additions & 0 deletions libraries/pico_scroll/pico_scroll.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <string>
#pragma once

namespace pimoroni {
Expand All @@ -20,6 +21,16 @@ namespace pimoroni {
public:
void init();
void update();
void set_pixels(const char *pixels);
void set_bitmap_1d(const char *bitmap, size_t bitmap_len, int brightness, int offset);
void scroll_text(const char *text, size_t text_len, int brightness, int delay_ms);
void scroll_text(std::string text, int brightness, int delay_ms=100) {
scroll_text(text.c_str(), text.length(), brightness, delay_ms);
};
void set_text(const char *text, size_t text_len, int brightness, int offset);
void set_text(std::string text, int brightness, int offset=0) {
set_text(text.c_str(), text.length(), brightness, offset);
}
void set_pixel(uint8_t x, uint8_t y, uint8_t v);
void clear();
bool is_pressed(uint8_t button);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "pico_scroll_font.h"
#include "pico_scroll_font.hpp"

/* static font data */
static unsigned char __bitmap[256][5] = {
Expand Down Expand Up @@ -133,12 +133,12 @@ static unsigned char __bitmap[256][5] = {
};

/* render a text string to a pre-allocated buffer - strlen(text) * 6 bytes */
int render(unsigned char *text, int nchr, unsigned char *buffer, int nbfr) {
int render_text(const char *text, unsigned int nchr, unsigned char *buffer, unsigned int nbfr) {
// TODO check nbfr >= 6 * nchr

for (int i = 0; i < nchr; i++) {
unsigned char *symbol = __bitmap[text[i]];
for (int j = 0; j < 5; j++) {
for (unsigned int i = 0; i < nchr; i++) {
unsigned char *symbol = __bitmap[(unsigned int)text[i]];
for (unsigned int j = 0; j < 5; j++) {
buffer[i * 6 + j] = symbol[j];
}
buffer[i * 6 + 5] = 0x0;
Expand Down
4 changes: 4 additions & 0 deletions libraries/pico_scroll/pico_scroll_font.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

// external font API
int render_text(const char *text, unsigned int nchr, unsigned char *buffer, unsigned int nbfr);
5 changes: 3 additions & 2 deletions micropython/modules/pico_scroll/micropython.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
include(${CMAKE_CURRENT_LIST_DIR}/../../../libraries/pico_scroll/pico_scroll.cmake)

add_library(usermod_pico_scroll INTERFACE)

target_sources(usermod_pico_scroll INTERFACE
${CMAKE_CURRENT_LIST_DIR}/pico_scroll.c
${CMAKE_CURRENT_LIST_DIR}/pico_scroll_font.c
${CMAKE_CURRENT_LIST_DIR}/pico_scroll.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/pico_scroll/pico_scroll.cpp
${PICO_SCROLL_SOURCES}
)

target_include_directories(usermod_pico_scroll INTERFACE
Expand Down
Loading

0 comments on commit cc3e997

Please sign in to comment.