bbcmicrobit / micropython

Port of MicroPython for the BBC micro:bit
https://microbit-micropython.readthedocs.io
Other
603 stars 284 forks source link

neopixel effects music #600

Closed rhubarbdog closed 2 years ago

rhubarbdog commented 5 years ago

i've bought this joypad (kitronik game zip). It has a little speaker and an array of "zip LEDs" (neopixel compatible addressable LEDs) when i run the following program the music plays once at the start and then about every 5 loops randomly.

from microbit import *
import music
import neopixel

zip_led = neopixel.NeoPixel(pin0, 64)

while True:
    display.show('-')
    music.play(music.BA_DING, pin2, False)
    display.clear()

    sleep(1000 * 1)

    zip_led.clear()
    zip_led[1] = (20, 20, 0)
    zip_led.show()

    sleep(1000 * 2)

i've tried it on the 'old' web editor and the music plays, but there are issues with neopixel and all LEDs are lit bright white rather than 1 yellow LED.

dpgeorge commented 5 years ago

From the code above, it looks like the music tune is playing in the background but finishing (via sleep(1000*1)) before the LEDs are written to. So there shouldn't be any interference between the music and LEDs. What happens if the music plays in the foreground, does it work then?

rhubarbdog commented 5 years ago

i've tried the following 1) playing the music in the foreground 2) swapping the order of events for the above code. so neopixel, sleep, music ,sleep 3) swapping the order of events for playing music in the foreground

All 4 cases play the music once then stop playing the music for most loops playing 1:6 maybe. but not every 6 loops.

regarding your comment "music tune is playing in the background but finishing (via sleep(10001))" I added `sleep(10001)` to try and cast some light on the issue, without it like the above cases the music plays once then stops only playing in some subsequent loops

deshipu commented 5 years ago

Out of curiosity, how are you powering it? Perhaps it's restarting because of the voltage sag due to the neopixels lighting up? Do you get anything displayed on the serial?

rhubarbdog commented 5 years ago

I'm powering it of 3 * 1.5volt alkaline batteries. Which are new, but not heavy duty type like duracell. The microbit is powered by theses via the GPIO connector. I've tried running the code from the initial post with a usb power lead in to keep the microbit alive the music still stops after 1 play and then only plays on some loops, it appears to be more like 1:11. You ask "Do you get anything displayed on the serial?", how would i test this? i have just tried adding a couple of print statements and checked they were working using a REPL session with screen

rhubarbdog commented 5 years ago

I've tried the following 1) using a different speaker, some genuine neopixel (ws2812 addressable LEDs) and a different microbit i ran the code in my original post. I have used a level shifter between the microbit and neopixels which have their own 5volt supply. Music playback is still defective i.e. plays the first time, then 1:6 -> 1:11 playbacks:loops 2) to isolate this further i produced these blocks and music plays correctly every loop

dpgeorge commented 5 years ago

I can confirm this issue. The problem is that when there are lots of neopixels the interrupts are disabled for a long time, and the PWM (which drives the music) stops functioning. This is because it is software based, misses an interrupt, and the next time it gets a chance to service an interrupt and update the counter for the time of the next interrupt, that next interrupt already passed. In that case it has to wait for the counter to wrap around before it starts functioning again. Since the counter is 24 bits and running at 1MHz, it takes about 16seconds to start working again.

rhubarbdog commented 5 years ago

Just so you're aware. I was able to reproduce the bug with only 8 neopixels

rhubarbdog commented 4 years ago

I would like to get going with the joypad i bought i'm thinking about writing an article for it for micro:mag but need a .hex file that it would work with. has the relevant pull request been joined to a repository so i can test the fix and hopefully write a good game to show off what a cool daughter board this is for the microbit

dpgeorge commented 2 years ago

This was fixed in commit 3b55d0d9111875a5da35c199927876bf320ee5f8. I tested that it indeed fixes the original code posted at the top of this issue.