Playing basic audio samples now works, added rainbow example for photoshoot

feature/galactic_unicorn
jon 2022-07-26 04:42:17 +01:00 zatwierdzone przez Phil Howard
rodzic 63183d28be
commit a486150470
5 zmienionych plików z 142 dodań i 82 usunięć

Wyświetl plik

@ -13,16 +13,16 @@ pico_add_extra_outputs(galactic_unicorn_demo)
add_executable(
galactic_unicorn_demo2
demo2.cpp
rainbow
rainbow.cpp
)
# Pull in pico libraries that we need
target_link_libraries(galactic_unicorn_demo2 pico_stdlib hardware_pio hardware_adc hardware_dma pico_graphics galactic_unicorn)
pico_enable_stdio_usb(galactic_unicorn_demo2 1)
target_link_libraries(rainbow pico_stdlib hardware_pio hardware_adc hardware_dma pico_graphics galactic_unicorn)
pico_enable_stdio_usb(rainbow 1)
# create map/bin/hex file etc.
pico_add_extra_outputs(galactic_unicorn_demo2)
pico_add_extra_outputs(rainbow)

Wyświetl plik

@ -71,19 +71,20 @@ int main() {
galactic_unicorn.init();
// galactic_unicorn.play_sample(left_channel_bin, left_channel_bin_len);
galactic_unicorn.set_brightness(0.5f);
while(true) {
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_BRIGHTNESS_UP)) {
galactic_unicorn.adjust_brightness(+0.01);
galactic_unicorn.adjust_brightness(+0.01f);
}
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_BRIGHTNESS_DOWN)) {
galactic_unicorn.adjust_brightness(-0.01);
galactic_unicorn.adjust_brightness(-0.01f);
}
uint ms = to_ms_since_boot(get_absolute_time());
uint8_t test = (ms / 5000) % 4;
uint8_t test = (ms / 1000) % 4;
graphics.set_pen(0, 0, 0);
graphics.clear();
@ -113,6 +114,17 @@ int main() {
printf("%d\n", galactic_unicorn.light());
std::string text = "";
static bool was_a_pressed = false;
if(galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_A)) {
if(!was_a_pressed) {
galactic_unicorn.play_sample(left_channel_bin, left_channel_bin_len);
}
was_a_pressed = true;
}else{
was_a_pressed = false;
}
if(galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_A)) {
text = "Button A";
}
@ -143,9 +155,6 @@ int main() {
outline_text(text);
graphics.set_pen(255, 255, 255);
graphics.clear();
galactic_unicorn.update(graphics);
sleep_ms(50);

Wyświetl plik

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "pico/stdlib.h"
#include "libraries/pico_graphics/pico_graphics.hpp"
@ -13,7 +14,19 @@ PicoGraphics_PenRGB565 graphics(53, 11, nullptr);
GalacticUnicorn galactic_unicorn;
// extra row of pixels for sourcing flames and averaging
float heat[53][15] = {0.0f};
int width = 53;
int height = 15;
// a buffer that's at least big enough to store 55 x 15 values (to allow for both orientations)
float heat[1000] = {0.0f};
void set(int x, int y, float v) {
heat[x + y * width] = v;
}
float get(int x, int y) {
return heat[x + y * width];
}
int main() {
@ -21,6 +34,8 @@ int main() {
galactic_unicorn.init();
bool landscape = true;
while(true) {
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_BRIGHTNESS_UP)) {
galactic_unicorn.adjust_brightness(+0.01);
@ -29,59 +44,77 @@ int main() {
galactic_unicorn.adjust_brightness(-0.01);
}
graphics.set_pen(0, 0, 0);
graphics.clear();
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_A)) {
landscape = true;
width = 53;
height = 15;
memset(heat, 0, sizeof(heat));
}
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_B)) {
landscape = false;
width = 11;
height = 55;
memset(heat, 0, sizeof(heat));
}
for(int y = 0; y < 14; y++) {
for(int x = 0; x < 53; x++) {
if(heat[x][y] > 0.5f) {
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
float value = get(x, y);
graphics.set_pen(0, 0, 0);
if(value > 0.5f) {
graphics.set_pen(255, 255, 180);
graphics.pixel(Point(x, y));
}else if(heat[x][y] > 0.4f) {
}else if(value > 0.4f) {
graphics.set_pen(220, 160, 0);
graphics.pixel(Point(x, y));
}else if(heat[x][y] > 0.3f) {
}else if(value > 0.3f) {
graphics.set_pen(180, 50, 0);
graphics.pixel(Point(x, y));
}else if(heat[x][y] > 0.2f) {
}else if(value > 0.2f) {
graphics.set_pen(40, 40, 40);
graphics.pixel(Point(x, y));
}
if(landscape) {
graphics.pixel(Point(x, y));
}else{
graphics.pixel(Point(y, x));
}
// update this pixel by averaging the below pixels
float average = 0.0f;
if(x == 0) {
heat[x][y] = (heat[x][y] + heat[x][y + 2] + heat[x][y + 1] + heat[x + 1][y + 1]) / 4.0f;
average = (get(x, y) + get(x, y + 2) + get(x, y + 1) + get(x + 1, y + 1)) / 4.0f;
} else if(x == 52) {
heat[x][y] = (heat[x][y] + heat[x][y + 2] + heat[x][y + 1] + heat[x - 1][y + 1]) / 4.0f;
average = (get(x, y) + get(x, y + 2) + get(x, y + 1) + get(x - 1, y + 1)) / 4.0f;
} else {
heat[x][y] = (heat[x][y] + heat[x][y + 2] + heat[x][y + 1] + heat[x - 1][y + 1] + heat[x + 1][y + 1]) / 5.0f;
}
average = (get(x, y) + get(x, y + 2) + get(x, y + 1) + get(x - 1, y + 1) + get(x + 1, y + 1)) / 5.0f;
}
heat[x][y] -= 0.01f;
heat[x][y] = heat[x][y] < 0.0f ? 0.0f: heat[x][y];
average -= landscape ? 0.01f : 0.004f;
average = average < 0.0f ? 0.0f: average;
set(x, y, average);
}
}
galactic_unicorn.update(graphics);
// clear the bottom row and then add a new fire seed to it
for(int x = 0; x < 53; x++) {
heat[x][14] = 0.0f;
for(int x = 0; x < width; x++) {
set(x, height - 1, 0.0f);
}
// add a new random heat source
for(int c = 0; c < 5; c++) {
int px = (rand() % 51) + 1;
heat[px][13] = 1.0f;
heat[px + 1][13] = 1.0f;
heat[px - 1][13] = 1.0f;
heat[px][14] = 1.0f;
heat[px + 1][14] = 1.0f;
heat[px - 1][14] = 1.0f;
int source_count = landscape ? 5 : 1;
for(int c = 0; c < source_count; c++) {
int px = (rand() % (width - 4)) + 2;
set(px , height - 2, 1.0f);
set(px + 1, height - 2, 1.0f);
set(px - 1, height - 2, 1.0f);
set(px , height - 1, 1.0f);
set(px + 1, height - 1, 1.0f);
set(px - 1, height - 1, 1.0f);
}
sleep_ms(50);
sleep_ms(20);
}
return 0;

Wyświetl plik

@ -109,19 +109,28 @@ graphics.set_font("bitmap8");
uint i = 0;
float i = 0;
float hue_offset = 0.0f;
bool animate = true;
float stripe_width = 3.0f;
float speed = 1.0f;
float curve = 0.0f;
while(true) {
i++;
if(animate) {
i += speed;
}
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_VOLUME_UP)) {
hue_offset += 0.05;
curve += 0.05;
if(hue_offset > 1.0f) hue_offset = 1.0f;
}
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_VOLUME_DOWN)) {
hue_offset -= 0.05;
curve -= 0.05;
if(hue_offset < 0.0f) hue_offset = 0.0f;
}
@ -132,8 +141,31 @@ graphics.set_font("bitmap8");
galactic_unicorn.adjust_brightness(-0.01);
}
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_SLEEP)) {
animate = false;
}
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_A)) {
speed += 0.05f;
speed = speed >= 10.0f ? 10.0f : speed;
animate = true;
}
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_B)) {
speed -= 0.05f;
speed = speed <= 0.0f ? 0.0f : speed;
animate = true;
}
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_C)) {
stripe_width += 0.05f;
stripe_width = stripe_width >= 10.0f ? 10.0f : stripe_width;
}
if(galactic_unicorn.is_pressed(galactic_unicorn.SWITCH_D)) {
stripe_width -= 0.05f;
stripe_width = stripe_width <= 1.0f ? 1.0f : stripe_width;
}
/*
graphics.set_pen(255, 255, 255);
float s = 0.65f;//0.65f + (sin(i / 25.0f) * 0.15f);
@ -145,9 +177,14 @@ graphics.set_font("bitmap8");
for(int x = 0; x < 53; x++) {
for(int y = 0; y < 11; y++) {
int v = ((sin((x + y) / 3.0f + i / 15.0f) + 1.5f) / 2.5f) * 255.0f;
int v = ((sin((x + y) / stripe_width + (sin((y * 3.1415927f * 2.0f) / 11.0f) * curve) + i / 15.0f) + 1.5f) / 2.5f) * 255.0f;
graphics.set_pen(v, v, v);
uint8_t r = (hue_map[x][0] * v) / 256;
uint8_t g = (hue_map[x][1] * v) / 256;
uint8_t b = (hue_map[x][2] * v) / 256;
graphics.set_pen(r, g, b);
graphics.pixel(Point(x, y));
}
}

Wyświetl plik

@ -39,7 +39,7 @@
// .. and back to the start
constexpr uint32_t ROW_COUNT = 11;
constexpr uint32_t BCD_FRAME_COUNT = 14;
constexpr uint32_t BCD_FRAME_COUNT = 12;
constexpr uint32_t BCD_FRAME_BYTES = 60;
constexpr uint32_t ROW_BYTES = BCD_FRAME_COUNT * BCD_FRAME_BYTES;
constexpr uint32_t BITSTREAM_LENGTH = (ROW_COUNT * ROW_BYTES);
@ -274,48 +274,29 @@ namespace pimoroni {
// setup audio pio program
/*
audio_pio = pio0;
audio_sm = pio_claim_unused_sm(audio_pio, true);
audio_sm_offset = pio_add_program(audio_pio, &audio_i2s_program);
audio_i2s_program_init(audio_pio, audio_sm, audio_sm_offset, I2S_DATA, I2S_BCLK);
//pio_sm_set_enabled(audio_pio, audio_sm, true);
pio_gpio_init(audio_pio, I2S_DATA);
pio_gpio_init(audio_pio, I2S_BCLK);
pio_gpio_init(audio_pio, I2S_LRCLK);
audio_i2s_program_init(audio_pio, audio_sm, audio_sm_offset, I2S_DATA, I2S_BCLK);
uint32_t system_clock_frequency = clock_get_hz(clk_sys);
uint32_t divider = system_clock_frequency * 4 / 22050; // avoid arithmetic overflow
pio_sm_set_clkdiv_int_frac(audio_pio, audio_sm, divider >> 8u, divider & 0xffu);
pio_sm_set_enabled(audio_pio, audio_sm, true);
audio_dma_channel = dma_claim_unused_channel(true);
dma_channel_config audio_config = dma_channel_get_default_config(audio_dma_channel);
channel_config_set_transfer_data_size(&audio_config, DMA_SIZE_32);
channel_config_set_bswap(&audio_config, false); // byte swap to reverse little endian
channel_config_set_transfer_data_size(&audio_config, DMA_SIZE_16);
//channel_config_set_bswap(&audio_config, false); // byte swap to reverse little endian
channel_config_set_dreq(&audio_config, pio_get_dreq(audio_pio, audio_sm, true));
dma_channel_configure(audio_dma_channel, &audio_config, &audio_pio->txf[audio_sm], NULL, 0, false);
dma_channel_set_irq0_enabled(audio_dma_channel, true);
irq_set_enabled(pio_get_dreq(audio_pio, audio_sm, true), true);*/
//irq_set_exclusive_handler(DMA_IRQ_0, dma_complete);
//irq_set_enabled(DMA_IRQ_0, true);
/* dma_channel_set_trans_count(audio_dma_channel, BITSTREAM_LENGTH / 4, false);
dma_channel_set_read_addr(audio_dma_channel, bitstream, true);*/
//pio_sm_config audio_i2s_config = audio_i2s_program_get_default_config(audio_sm_offset);
// osr shifts right, autopull on, autopull threshold 8
//sm_config_set_out_shift(&audio_i2s_config, true, true, 32);
// // configure out, set, and sideset pins
// sm_config_set_out_pins(&audio_i2s_config, ROW_BIT_0, 4);
// sm_config_set_set_pins(&audio_i2s_config, COLUMN_DATA, 3);
// sm_config_set_sideset_pins(&audio_i2s_config, COLUMN_CLOCK);
// // join fifos as only tx needed (gives 8 deep fifo instead of 4)
// sm_config_set_fifo_join(&audio_i2s_config, PIO_FIFO_JOIN_TX);
//pio_sm_init(audio_pio, audio_sm, audio_sm_offset, &audio_i2s_config);
//pio_sm_set_enabled(audio_pio, audio_sm, true);
//dma_channel_set_irq0_enabled(audio_dma_channel, true);
irq_set_enabled(pio_get_dreq(audio_pio, audio_sm, true), true);
}
@ -328,7 +309,7 @@ namespace pimoroni {
}
void GalacticUnicorn::play_sample(uint8_t *data, uint32_t length) {
dma_channel_transfer_from_buffer_now(audio_dma_channel, data, length / 4);
dma_channel_transfer_from_buffer_now(audio_dma_channel, data, length / 2);
}
void GalacticUnicorn::set_pixel(int x, int y, uint8_t r, uint8_t g, uint8_t b) {