Go Down

Topic: MP3 SPI bus transfer question (Read 2014 times) previous topic - next topic

drwhofan

Hi, 
I'm successfully using Bill Porter's mp3 shield library to launch mp3s with an IR controller.  I also want a string of ws2801 lights to run at the same time.  Since both are using the SPI bus I need to transfer per the code here (the notes indicate that this example also has a shift register on the SPI bus):

Code: [Select]

  //disable interrupts to avoid collisions on the SPI bus between this code //and the MP3player library
  MP3player.pauseDataStream();

  //shift data
  tempIO = SPI.transfer(HIBYTE(output));
  tempIO<<8;
  tempIO = SPI.transfer(LOBYTE(output));

  //latch output on shift registers
  digitalWrite(OUTLATCH,LOW);
  digitalWrite(OUTLATCH,HIGH);
  digitalWrite(OUTLATCH,LOW);

  //enable interrupts
  MP3player.resumeDataStream();


Here's my attempt, but it's not working and I was hoping someone could point out my errors:

Code: [Select]

void loop() {
int key = getIRKey();
if (key !=0) {
switch(key) {
case 144: MP3player.playTrack(1); break;
//disable interrupts to avoid collisions on the SPI bus between this code //and the MP3player library
MP3player.pauseDataStream();
colorWipe(Color(0, 0, 0), 500);
colorWipe(Color(10, 10, 10), 20);
colorWipe(Color(50, 50, 50), 10);
colorWipe(Color(100, 100, 100), 20);
colorWipe(Color(150, 150, 150), 10);
colorWipe(Color(200, 200, 200), 10);
colorWipe(Color(255, 255, 255), 20);
colorWipe(Color(100, 100, 100), 20);
colorWipe(Color(10, 10, 10), 10);
colorWipe(Color(0, 0, 0), 10);
//enable interrupts
MP3player.resumeDataStream();
case 145: MP3player.playTrack(2); break;
case 146: MP3player.playTrack(3); break;
case 147: MP3player.playTrack(4); break;
}
}
}


What happens now is that the music will play but he lights won't. 
Thanks.   

CrossRoads

Code: [Select]

switch(key) {
case 144: MP3player.playTrack(1); break;  <<< when the code hits this break, it goes to the }; at the end of the switch(){...} and continues
//disable interrupts to avoid collisions on the SPI bus between this code //and the MP3player library
MP3player.pauseDataStream();
colorWipe(Color(0, 0, 0), 500);
colorWipe(Color(10, 10, 10), 20);
colorWipe(Color(50, 50, 50), 10);
colorWipe(Color(100, 100, 100), 20);
colorWipe(Color(150, 150, 150), 10);
colorWipe(Color(200, 200, 200), 10);
colorWipe(Color(255, 255, 255), 20);
colorWipe(Color(100, 100, 100), 20);
colorWipe(Color(10, 10, 10), 10);
colorWipe(Color(0, 0, 0), 10);
//enable interrupts
MP3player.resumeDataStream();
case 145: MP3player.playTrack(2); break;
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

drwhofan

Hi -
Thanks so much for taking a look at what's up.  I tried your code but didn't have any luck.  Music still played but no lights.  Then I tried moving break; around to a few places and what happens in one instance is kind of promising:

If I put it here:
Code: [Select]

         //enable interrupts
        MP3player.resumeDataStream(); break;


The lights turn on (they don't do the color wipe).  Then, after the length of time that would be the light sequence- the mp3 plays.  The lights just stay on.  What do you think?


CrossRoads

I don't know what the colorwipe command does, maybe you need some time in between each one to see any effect?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

drwhofan

Hi,
I really appreciate you helping me out like this.  Here's a bit about the colorwipe.  It's from Adafruit strandtest:  https://github.com/adafruit/Adafruit-WS2801-Library and I just kind of tweaked it to get the result I want.  It basically just steps up the lights one after another and then back down again. 

In conjunction with the code I posted above for the function I'm working with, it also has this component towards the end of the sketch:
Code: [Select]

// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(uint32_t c, uint8_t wait) {
  int i;
 
  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}
/* Helper functions */
// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}


So that's all the colorwipe does.  Now when I run it by itself it fades the strand up and down no problem.  When I try it as I described, I get either nothing or the weird response depending on where I put break;  So, I'm wondering if any of this new info helps you to help me? 

Thanks again!

CrossRoads

When the code hits
break;
it jumps to the end of the switch segment.

So if you want the music, and the colorwipe, I imagine it would need to be:
Code: [Select]

case 144: MP3player.playTrack(1);
//disable interrupts to avoid collisions on the SPI bus between this code //and the MP3player library
MP3player.pauseDataStream();
colorWipe(Color(0, 0, 0), 500);
colorWipe(Color(10, 10, 10), 20);
colorWipe(Color(50, 50, 50), 10);
colorWipe(Color(100, 100, 100), 20);
colorWipe(Color(150, 150, 150), 10);
colorWipe(Color(200, 200, 200), 10);
colorWipe(Color(255, 255, 255), 20);
colorWipe(Color(100, 100, 100), 20);
colorWipe(Color(10, 10, 10), 10);
colorWipe(Color(0, 0, 0), 10);
//enable interrupts
MP3player.resumeDataStream();
break;


the delay here in colorwipe
      delay(wait);
will keep anything else from happening
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

drwhofan

Still not the solution.  The lights go on full white and stay on--they don't execute properly.  At the same time (and this is promising because it shows there's a bit of SPI sharing happening) I get a split-second burst of the mp3.  Then the lights just hang around on.  I pulled this from the developer's board and was wondering if it might have an impact?  Not sure how to implement if it is.

"Noting spi.transfer() is also called by refill from an interrupt and hence it needs to be interlocked with the interrupts by surrounding it: e.i."

Code: [Select]

MP3player.pauseDataStream();
bar = SPI.transfer(foo);
MP3player.resumeDataStream();

CrossRoads

Need some real programming help here, this is  beyond my SPI experience. Sorry.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

drwhofan

OK.  Thanks so much for helping at all.  I'll let you know if I can crack it.

Go Up