Go Down

Topic: OSC to Serial to currupted data (Read 523 times) previous topic - next topic

Owen_vallis

Hello,

My friend and I have been working on some code for a monome arduino device. We got almost everything working, however, the LED intensity message keeps locking up the serial information. It seems that the OSC msg sends as a float between 0. and 1. , and that this is then multiplied by 15. cast as an unsigned char, and then anded against 15 again? Needless to say, we are not sure what all that is for.

All we know is that the result should be 2 Bytes which carry an address in the first byte (11000000) and an intensity between 0 and 15 in the second byte (00001100). What ends up happening is that every other 2 bytes seem to be messed up. If we stop on one of these messed up msgs, then the next OSC function sent will be ignored (it looks like it flips the bytes coming in to the arduino after the currupted data).

The OSC is proccessed with this code:

Code: [Select]

MonomeXXhDevice::oscLedIntensityChangeEvent(float intensity) //m256
{

uint8 i;


if (intensity > 1.f)
intensity = 1.f;
else if (intensity < 0.f)
intensity = 0.f;

i = ((unsigned char)(intensity * (float)0xF)) & 0xF;
//fprintf(stderr, "\n ---- TEMPORARY DEBUG! REINTENSITY %i:", i);

if (_type == kDeviceType_40h)
{ t_message message;
messagePackLedIntensity(&message, i);
write((char *)&message, sizeof(t_message));
}
}


We then receive the serial data and process it in case 3 like so:

Code: [Select]
void loop () {
 if(Serial.available() > 1)
 {
   //fetch first byte
   byte1 = Serial.read();
   //fetch second byte
   byte2 = Serial.read();
   //shift bits right to get address of function
   address = byte1 >> 4;

   switch (address){
   case 4://led displayTest
     {
       maxSingle(max7219_reg_displayTest, byte2 & 15);
     }
     break;
   case 3://led intensity
     {
       maxSingle(max7219_reg_intensity, byte2 & 15);
     }
     break;
   case 2://led on/off
     {
       state = byte1 & 15;
       x = byte2 >> 4;
       y = byte2 & 15;

       if (state == 0){
         ledmem[y] &= ~( 1 << x);
       }
       else {
         ledmem[y] |=  ( 1 << x);
       }
       maxSingle(y + 1, ledmem[y]);
     }
     break;
     /*case 5: // adc enable
      {
      if (byte2 & 15){
      enableAdc((byte2 & 240) >> 4);
      }
      else{
      disableAdc((byte2 & 240) >> 4);
      }
      break;*/
   case 6://shutdown mode
     {
       maxSingle(max7219_reg_shutdown, byte2 & 15);
     }              
     break;
   case 7://led_row
     {
       if (firstRun == 1) {
         for (x = 0; x < 8; x++) {
           ledmem[x] = 0;
           maxSingle(x + 1, ledmem[x]);
         }

         firstRun = 0;
       }

       x = ((byte1 & 15) & 0x7); // mask this value so we don't write to an invalid address.
       y = byte2;

       ledmem[x] = y;
       maxSingle(x + 1, ledmem[x]);            
     }
     break;
   case 8://led_col
     {
       if (firstRun == 1) {
         for (x = 0; x < 8; x++) {
           ledmem[x] = 0;
           maxSingle(x + 1, ledmem[x]);
         }

         firstRun = 0;
       }

       x = ((byte1 & 15) & 0x7);
       y = byte2;

       for (z = 0; z < 8; z++) {
         if (y & (1 << z))
           ledmem[z] |= 1 << x;
         else
           ledmem[z] &= ~(1 << x);

         maxSingle(z + 1, ledmem[z]);
       }
       break;
     }
   }
 }
 buttonpress();
 if(Serial.available() > 121){
   Serial.flush();
 }
}



Any thoughts would be a great help,

Owen & Jordan

Go Up