Moved MP examples over to PicoGraphics

This commit is contained in:
ZodiusInfuser 2022-08-01 17:07:38 +01:00 committed by Phil Howard
parent c8b5ffff8c
commit b35ed5d5ba
9 changed files with 110 additions and 38 deletions

View File

@ -387,6 +387,39 @@ namespace pimoroni {
}
void GalacticUnicorn::update(PicoGraphics *graphics) {
if(graphics->pen_type == PicoGraphics::PEN_RGB888) {
uint32_t *p = (uint32_t *)graphics->frame_buffer;
for(size_t j = 0; j < 53 * 11; j++) {
int x = j % 53;
int y = j / 53;
uint32_t col = *p;
uint8_t r = (col & 0xff0000) >> 16;
uint8_t g = (col & 0x00ff00) >> 8;
uint8_t b = (col & 0x0000ff) >> 0;
p++;
set_pixel(x, y, r, g, b);
}
}
else if(graphics->pen_type == PicoGraphics::PEN_RGB565) {
uint16_t *p = (uint16_t *)graphics->frame_buffer;
for(size_t j = 0; j < 53 * 11; j++) {
int x = j % 53;
int y = j / 53;
uint16_t col = __builtin_bswap16(*p);
uint8_t r = (col & 0b1111100000000000) >> 8;
uint8_t g = (col & 0b0000011111100000) >> 3;
uint8_t b = (col & 0b0000000000011111) << 3;
p++;
set_pixel(x, y, r, g, b);
}
}
}
void GalacticUnicorn::update(PicoGraphics_PenRGB565 &graphics) {
uint16_t *p = (uint16_t *)graphics.frame_buffer;
for(size_t j = 0; j < 53 * 11; j++) {

View File

@ -76,6 +76,7 @@ namespace pimoroni {
void clear();
void update(PicoGraphics *graphics);
void update(PicoGraphics_PenRGB565 &graphics);
void update(PicoGraphics_PenRGB888 &graphics);

View File

@ -9,10 +9,11 @@ namespace pimoroni {
}
}
void PicoGraphics_PenRGB888::set_pen(uint c) {
color = RGB(c, c, c).to_rgb888();
color = c;
}
void PicoGraphics_PenRGB888::set_pen(uint8_t r, uint8_t g, uint8_t b) {
color = RGB(r, g, b).to_rgb888();
src_color = {r, g, b};
color = src_color.to_rgb888();
}
int PicoGraphics_PenRGB888::create_pen(uint8_t r, uint8_t g, uint8_t b) {
return RGB(r, g, b).to_rgb888();

View File

@ -1,8 +1,10 @@
import time
import random
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
gu = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY)
colour = (230, 150, 0)
@ -25,10 +27,15 @@ def draw():
for y in range(height):
for x in range(width):
if age[x][y] < lifetime[x][y] * 0.3:
gu.set_pixel(x, y, colour[0], colour[1], colour[2])
graphics.set_pen(graphics.create_pen(colour[0], colour[1], colour[2]))
elif age[x][y] < lifetime[x][y] * 0.5:
decay = (lifetime[x][y] * 0.5 - age[x][y]) * 5.0
gu.set_pixel(x, y, int(decay * colour[0]), int(decay * colour[1]), int(decay * colour[2]))
graphics.set_pen(graphics.create_pen(int(decay * colour[0]), int(decay * colour[1]), int(decay * colour[2])))
else:
graphics.set_pen(0)
graphics.pixel(x, y)
gu.update(graphics)
@micropython.native # noqa: F821

View File

@ -1,8 +1,16 @@
import time
import random
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
gu = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY)
fire_colours = [graphics.create_pen(0, 0, 0),
graphics.create_pen(20, 20, 20),
graphics.create_pen(180, 30, 0),
graphics.create_pen(220, 160, 0),
graphics.create_pen(255, 255, 180)]
@micropython.native # noqa: F821
@ -60,17 +68,19 @@ def draw_landscape():
for y in range(GalacticUnicorn.HEIGHT):
for x in range(GalacticUnicorn.WIDTH):
value = heat[x + 1][y]
if value < 0.15:
gu.set_pixel(x, y, 0, 0, 0)
graphics.set_pen(fire_colours[0])
elif value < 0.25:
gu.set_pixel(x, y, 20, 20, 20)
graphics.set_pen(fire_colours[1])
elif value < 0.35:
gu.set_pixel(x, y, 180, 30, 0)
graphics.set_pen(fire_colours[2])
elif value < 0.45:
gu.set_pixel(x, y, 220, 160, 0)
graphics.set_pen(fire_colours[3])
else:
gu.set_pixel(x, y, 255, 255, 180)
graphics.set_pen(fire_colours[4])
graphics.pixel(x, y)
gu.update(graphics)
@micropython.native # noqa: F821
@ -78,17 +88,19 @@ def draw_portrait():
for y in range(GalacticUnicorn.WIDTH):
for x in range(GalacticUnicorn.HEIGHT):
value = heat[x + 1][y]
if value < 0.15:
gu.set_pixel(y, x, 0, 0, 0)
graphics.set_pen(fire_colours[0])
elif value < 0.25:
gu.set_pixel(y, x, 20, 20, 20)
graphics.set_pen(fire_colours[1])
elif value < 0.35:
gu.set_pixel(y, x, 180, 30, 0)
graphics.set_pen(fire_colours[2])
elif value < 0.45:
gu.set_pixel(y, x, 220, 160, 0)
graphics.set_pen(fire_colours[3])
else:
gu.set_pixel(y, x, 255, 255, 180)
graphics.set_pen(fire_colours[4])
graphics.pixel(y, x)
gu.update(graphics)
landscape = True

View File

@ -2,8 +2,10 @@ import time
import random
import math
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
gu = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY)
blob_count = 10
@ -40,17 +42,17 @@ def from_hsv(h, s, v):
i = int(i) % 6
if i == 0:
return int(v), int(t), int(p)
return graphics.create_pen(int(v), int(t), int(p))
if i == 1:
return int(q), int(v), int(p)
return graphics.create_pen(int(q), int(v), int(p))
if i == 2:
return int(p), int(v), int(t)
return graphics.create_pen(int(p), int(v), int(t))
if i == 3:
return int(p), int(q), int(v)
return graphics.create_pen(int(p), int(q), int(v))
if i == 4:
return int(t), int(p), int(v)
return graphics.create_pen(int(t), int(p), int(v))
if i == 5:
return int(v), int(p), int(q)
return graphics.create_pen(int(v), int(p), int(q))
@micropython.native # noqa: F821
@ -93,9 +95,9 @@ def draw_portrait():
global hue
hue += 0.001
dark_r, dark_g, dark_b = from_hsv(hue, 1.0, 0.3)
mid_r, mid_g, mid_b = from_hsv(hue, 1.0, 0.6)
bright_r, bright_g, bright_b = from_hsv(hue, 1.0, 1.0)
dark = from_hsv(hue, 1.0, 0.3)
mid = from_hsv(hue, 1.0, 0.6)
bright = from_hsv(hue, 1.0, 1.0)
for y in range(height):
for x in range(width):
@ -104,13 +106,16 @@ def draw_portrait():
# select a colour for this pixel based on how much
# "blobfluence" there is at this position in the liquid
if v >= 1.5:
gu.set_pixel(y, x, bright_r, bright_g, bright_b)
graphics.set_pen(bright)
elif v >= 1.25:
gu.set_pixel(y, x, mid_r, mid_g, mid_b)
graphics.set_pen(mid)
elif v >= 1.0:
gu.set_pixel(y, x, dark_r, dark_g, dark_b)
graphics.set_pen(dark)
else:
gu.set_pixel(y, x, 0, 0, 0)
graphics.set_pen(0)
graphics.pixel(y, x)
gu.update(graphics)
setup_portrait()

View File

@ -1,7 +1,9 @@
import time
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
gu = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY)
c64 = [
@ -60,20 +62,26 @@ prompt = 0
@micropython.native # noqa: F821
def draw(image, fg, bg, time_ms):
fg_pen = graphics.create_pen(fg[0], fg[1], fg[2])
bg_pen = graphics.create_pen(bg[0], bg[1], bg[2])
for y in range(len(image)):
row = image[y]
for x in range(len(row)):
pixel = row[x]
# draw the prompt text
if pixel == 'O':
gu.set_pixel(x, y, fg[0], fg[1], fg[2])
graphics.set_pen(fg_pen)
# draw the caret blinking
elif pixel == 'X' and (time_ms // 300) % 2:
gu.set_pixel(x, y, fg[0], fg[1], fg[2])
graphics.set_pen(fg_pen)
else:
gu.set_pixel(x, y, bg[0], bg[1], bg[2])
graphics.set_pen(bg_pen)
graphics.pixel(x, y)
gu.update(graphics)
gu.set_brightness(0.5)

View File

@ -1,8 +1,10 @@
import time
import math
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
gu = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY)
width = GalacticUnicorn.WIDTH
height = GalacticUnicorn.HEIGHT
@ -41,11 +43,10 @@ def draw():
for y in range(height):
v = ((math.sin((x + y) / stripe_width + phase_percent) + 1.5) / 2.5)
r = int(colour[0] * v)
g = int(colour[1] * v)
b = int(colour[2] * v)
graphics.set_pen(graphics.create_pen(int(colour[0] * v), int(colour[1] * v), int(colour[2] * v)))
graphics.pixel(x, y)
gu.set_pixel(x, y, r, g, b)
gu.update(graphics)
hue_map = [from_hsv(x / width, 1.0, 1.0) for x in range(width)]

View File

@ -9,6 +9,7 @@ using namespace pimoroni;
extern "C" {
#include "galactic_unicorn.h"
#include "micropython/modules/pimoroni_i2c/pimoroni_i2c.h"
#include "py/builtin.h"
@ -25,7 +26,10 @@ typedef struct _ModPicoGraphics_obj_t {
mp_obj_base_t base;
PicoGraphics *graphics;
DisplayDriver *display;
void *spritedata;
void *buffer;
_PimoroniI2C_obj_t *i2c;
//mp_obj_t scanline_callback; // Not really feasible in MicroPython
} ModPicoGraphics_obj_t;
/***** Print *****/
@ -107,9 +111,9 @@ extern mp_obj_t GalacticUnicorn_clear(mp_obj_t self_in) {
extern mp_obj_t GalacticUnicorn_update(mp_obj_t self_in, mp_obj_t graphics_in) {
_GalacticUnicorn_obj_t *self = MP_OBJ_TO_PTR2(self_in, _GalacticUnicorn_obj_t);
ModPicoGraphics_obj_t *picographics = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
ModPicoGraphics_obj_t *picographics = MP_OBJ_TO_PTR2(graphics_in, ModPicoGraphics_obj_t);
self->galactic->update(*((PicoGraphics_PenRGB888 *)picographics->graphics));
self->galactic->update(picographics->graphics);
return mp_const_none;
}