Play RSF from EEPROM

An example how to play RSF files from EEPROM (Arduino example)



The structure of image in EEPROM

offset    size         description
0         1 byte       number of melodies

1         2 bytes      offset from file start to first melody
3         2 bytes      melody 1 frames count

5         2 bytes      offset from file start to melody 2
7         2 bytes      melody 2 frames count
..........

X         2 bytes      offset from file start to melody N
X         2 bytes      melody N frames count

X                      RSF file 1 content

X                      RSF file 2 content

X                      RSF file 3 content
...........

X                      RSF file N content

Initialization and play

Wire.begin();
songs = readEEPROM(disk1, 0);
Serial.begin(19200); // 57600 для версий 14.0+
for(int i=0; i<songs; i++) playsong(i);

Functions

byte readEEPROM(int deviceaddress, unsigned int eeaddress )
{
  byte rdata = 0;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
  Wire.requestFrom(deviceaddress,1);
  if (Wire.available()) rdata = Wire.read();
  return rdata;
}

PlaySong Function for versions up to 13.2

void playsong(byte num)
{
  int offset = readEEPROM(disk1, 1 + num*4) + readEEPROM(disk1, 1 + num*4 + 1)*256;
  int frames = readEEPROM(disk1, 1 + num*4 + 2) + readEEPROM(disk1, 1 + num*4 + 3)*256;

  offset += 16; //RSF header

  int frame = 0;
  byte tmp;

  byte writebuf[16];
  memset(writebuf,0,14);
  writebuf[14] = 0x80;
  writebuf[15] = 0x7F;


  unsigned int tmp2, skip;

  while( frame < frames )
  {
    skip = 1;
    tmp = readEEPROM(disk1, offset++);
    if(tmp == 0xFE)
    {
         tmp = readEEPROM(disk1, offset++);
         skip = tmp * 16;
    }
    else if(tmp == 0xFD)
    {
         tmp = readEEPROM(disk1, offset++);
         skip = tmp * 2;
    }
    else if(tmp != 0xFF)
    {
         tmp2 = tmp * 256;
         tmp2 += readEEPROM(disk1, offset++);
         byte reg = 0;
         writebuf[13] = 0xFF;
         while(tmp2 > 0 && reg < 14)
         {
             if(tmp2 & 1 == 1)
             {
                 writebuf[reg] = readEEPROM(disk1, offset++);
             }
             tmp2 >>= 1;
             reg++;
         }
         Serial.write(writebuf,16);
     }
     for(int i = 1; i <= skip; i++) delay(20);

     frame += skip;
  }
  //stop
  memset(writebuf,0,14);
  writebuf[7] = 63;
  Serial.write(writebuf,16);
}

PlaySong Function for versions 14.0+ (not checked)

void playsong(byte num)
{
   int offset = readEEPROM(disk1, 1 + num*4) + readEEPROM(disk1, 1 + num*4 + 1)*256;
   int frames = readEEPROM(disk1, 1 + num*4 + 2) + readEEPROM(disk1, 1 + num*4 + 3)*256;

   offset += 16; //RSF header

   int frame = 0;
   byte tmp, tmp3;

   byte writebuf[16];

   //mute
   memset(writebuf,0,14);
   writebuf[7] = 63;

   for(int i = 0; i <14; i++) {
      Serial.write(i,1);
      Serial.write(writebuf[i],1);
   }

   memset(writebuf,0,14);
   writebuf[13] = 0xFF;

   Serial.write(writebuf[13],1); // to synchronize

   unsigned int tmp2, skip;

   while( frame < frames )
   {
       skip = 1;
       tmp = readEEPROM(disk1, offset++);
       if(tmp == 0xFE)
       {
           tmp = readEEPROM(disk1, offset++);
           skip = tmp * 16;
       }
       else if(tmp == 0xFD)
       {
           tmp = readEEPROM(disk1, offset++);
           skip = tmp * 2;
       }
       else if(tmp != 0xFF)
       {
           tmp2 = tmp * 256;
           tmp2 += readEEPROM(disk1, offset++);
           byte reg = 0;
           while(tmp2 > 0 && reg < 14)
           {
               if(tmp2 & 1 == 1)
               {
                   tmp3 = readEEPROM(disk1, offset++);
                   if(writebuf[reg] != tmp3)
                   {
                       writebuf[reg] = tmp3;
                       if(!(reg==13 && tmp3==0xFF))
                       {
                           Serial.write(reg,1);
                           Serial.write(tmp3,1);
                       }
                   }
               }
               tmp2 >>= 1;
               reg++;
           }
       }
       for(int i = 1; i <= skip; i++) delay(20);
       frame += skip;
   }
   //mute
   memset(writebuf,0,14);
   writebuf[7] = 63;

   for(int i = 0; i <14; i++) {
       Serial.write(i,1);
       Serial.write(writebuf[i],1);
   }
}

 

[ad name="HTML"]

Leave a Reply

Your email address will not be published.