jump to case label [-fpermissive]

I All,

Trying to build an OSC controller for software called Qlab, and I’m just in the concept proving phase.

So far I’ve managed to send and receive data to and from Qlab, but in trying to set up serial char commands I get stuck as soon as I declare a second char read case.

Code attached:

//OSC for Qlab Test.
//Sending Workspace methods and checking for replies

#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>
#include <OSCBundle.h>

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
#define SER_BUFFER_SIZE 20
char serialBuffer[SER_BUFFER_SIZE];
uint8_t serialBufferPointer = 255;

bool loadSerialCommand()  {  // Call in loop() to check for commands
  if (serialBufferPointer==255)  {  // Initialization
    serialBuffer[0]=0;  // Null-terminate empty buffer
    serialBufferPointer = 0;
  }
  if (Serial.available())  {  // A line of characters has been sent from the serial monitor - no <lf> termination! Just waiting...
    serialBufferPointer=0;  // so, we can start over again filling the buffer
    delay(10);  // Wait for all characters to arrive.
    while(Serial.available())  {
      char c = Serial.read();
      if (serialBufferPointer < SER_BUFFER_SIZE-1)  { // one byte for null termination reserved
        serialBuffer[serialBufferPointer] = c;
        serialBuffer[serialBufferPointer+1] = 0;
        serialBufferPointer++;
      } 
      else {
        Serial.println(F("ERROR: Buffer overflow."));
      }    
    }
    //Serial << F("SERIAL COMMAND: ") << serialBuffer << F("\n");
    return true;
  }
  return false;
}


// you can find this written on the board of some Arduino Ethernets or shields
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 

// NOTE: Alternatively, you can assign a fixed IP to configure your
//       Ethernet shield.
byte ip[] = { 192, 168, 1, 177 };
IPAddress outIp(192, 168, 1, 1);


int serverPort  = 53001; //TouchOSC (incoming port)
int destPort = 53000;    //TouchOSC (outgoing port)
int ledPin =  13;       //pin 13 on Arduino Uno. Pin 6 on a Teensy++2
int ledState = LOW;

//Create UDP message object
EthernetUDP Udp;

void setup(){
  Serial.begin(115200); 
  Serial.println("OSC test");

  // start the Ethernet connection:
  Ethernet.begin(mac, ip);   

  // print your local IP address:
  Serial.print("Arduino IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  
  }

  Udp.begin(serverPort);
  Serial.print("Remote IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(outIp[thisByte], DEC);
    Serial.print("."); 

  Udp.begin(destPort);
  
  OSCMessage msgOUT("/workspace/test/connect");

  Udp.beginPacket(outIp, destPort);
  msgOUT.send(Udp); // send the bytes
  Udp.endPacket(); // mark the end of the OSC Packet
  delay(20);
  msgOUT.send(Serial);
  Serial.println();
  msgOUT.empty(); // free space occupied by message 

  } 
}

void loop(){

  if (loadSerialCommand())  {
    switch((char)serialBuffer[0])  {
    case 't':
      Udp.begin(destPort);
      OSCMessage msgOUT("/workspace/test/thump");
      Udp.beginPacket(outIp, destPort);
      msgOUT.send(Udp); // send the bytes
      Udp.endPacket(); // mark the end of the OSC Packet
      delay(20);
      msgOUT.send(Serial);
      Serial.println();
      msgOUT.empty(); // free space occupied by message 
      break;
    case 'z':
      break;
        
    }
  }
  

  
  
  //process received messages
  OSCMessage msgIN;
  int size;
  if((size = Udp.parsePacket())>0){
    while(size--)
      //Serial.println(Udp.read());
      msgIN.fill(Udp.read());
    if(!msgIN.hasError()){
      Serial.println("Reply:");
      msgIN.send(Serial);
      Serial.println();
      
    }
     else{
        Serial.println("I got something but it wasn't OSC");
    }
  }
  
}

Here are the errors I get:

Arduino: 1.6.9 (Mac OS X), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

/Users/hlk/Documents/Arduino/osc_test_1/osc_test_1.ino: In function ‘void loop()’:
osc_test_1:111: error: jump to case label [-fpermissive]
case ‘z’:
^
osc_test_1:102: error: crosses initialization of ‘OSCMessage msgOUT’
OSCMessage msgOUT("/workspace/test/thump");
^
exit status 1
jump to case label [-fpermissive]

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

I’m very new to programming any help is greatly appreciated. :o

Thanks
Henri

Put some braces between the end of the first case and its break.

Hi AWOL,

Thank you for quick reply.

Sorry for sounding stupid here, but this all very new to me... do you mean:

msgOUT.send(Serial);
      Serial.println();
      msgOUT.empty();  { // free space occupied by message 
      }
      break;
    case 'z':
      break;
switch((char)serialBuffer[0])  {
    case 't':
    {
      Udp.begin(destPort);
      OSCMessage msgOUT("/workspace/test/thump");
      Udp.beginPacket(outIp, destPort);
      msgOUT.send(Udp); // send the bytes
      Udp.endPacket(); // mark the end of the OSC Packet
      delay(20);
      msgOUT.send(Serial);
      Serial.println();
      msgOUT.empty(); // free space occupied by message
    }  
    break;
    case 'z':
    {
    }
    break;

AWOL:

switch((char)serialBuffer[0])  {

case 't':
    {
      Udp.begin(destPort);
      OSCMessage msgOUT("/workspace/test/thump");
      Udp.beginPacket(outIp, destPort);
      msgOUT.send(Udp); // send the bytes
      Udp.endPacket(); // mark the end of the OSC Packet
      delay(20);
      msgOUT.send(Serial);
      Serial.println();
      msgOUT.empty(); // free space occupied by message
    } 
    break;
    case 'z':
    {
    }
    break;

Nice. Learned something there.

I thought OP's initial code looked ok, but according to C++ reference, it seems a case should contain (at most) one statement. But it also says that one can use break, which must then not be considered a statement when inside a switch block.

Does this mean, for instance, that one can not do

switch (something) {
   case 1 : {
      if (someCondition) break; // else fallthrough
   }
   case 2: blah blah
}

... or does the break following the if() revert back to being a statement?

:slight_smile:

HenriKoenig:
Sorry for sounding stupid here, but this all very new to me… do you mean:

It may be helpful to understand why you have to use the brackets…

You see, each case in a switch-case statement shares the same scope. In your code, compiler cannot resolve the scope of the new objects you are trying to create within the switch case statement.

So AWOL creates “more” scope by encapsulating the several lines of code within braces, making it clear to the compiler the scope of the new objects you create.

1 Like

Rupert909:

switch (something) {

case 1 : {
      if (someCondition) break; // else fallthrough
  }
  case 2: blah blah
}

hmmmm.... Not sure what you are running at there...

Since switch expressions must resolve to an integral type, how would you ever expect two cases to be satisfied?

BulldogLowell:
hmmmm.... Not sure what you are running at there...

Since switch expressions must resolve to an integral type, how would you ever expect two cases to be satisfied?

Switch statements have fall-through functionality for the case where multiple values should be handled the same. Nevermind, I can test this myself, later.

according to C++ reference, it seems a case should contain (at most) one statement.

That seems very unlikely. What reference are you quoting ?

UKHeliBob:
That seems very unlikely. What reference are you quoting ?

http://en.cppreference.com/w/cpp/language/switch

I see, compund statement means multiple statements then ... :slight_smile:

There's also Robin2's excellent tutorial on serial handling basics.