IF/ELSE und Sprünge
-
Hallo!
Ich habe einen Code mit GOTO drin. Ich komme einfach nicht drauf wie man die GOTOS weg bekommt. Könnt ihr mir helfen?case 0xa8: // Need first byte to transmit case 0xb8: // Need following bytes to transmit { static UINT8 *packet = 0; static UINT16 idx; static UINT16 size = 0; if (packet) { have_packet: I20DAT = packet[idx++]; if (idx > size) { PP_FreePacket (packet); goto new_packet; } } else { new_packet: packet = PP_IOP_GetNextTxEntry(); if (packet) { idx = 0; size = GET_MEMBER (packet, PP_PACKET, data)->datasize; goto have_packet; } else { I20DAT = DUMMY_BYTE; } } } break;
Kurze Erklärung
Der Code ist in einem Interrupt und sendet Daten über eine I^2C Schnittstelle.Viele Grüße
Micha
-
lass drin, wen juckts ?
-
wieso willst es raus machen?
-
Ich wollte das Ganze ein Wenig strukturierter haben. Mit GOTOS wirkt der Code sehr unübersichtlich. Hat niemand eine Idee?
Grüße,
Micha
-
Ich finde wenn die Sprungmarke auf der selben "Seite" wie das goto ist, dann geht es doch. Ich würde persöhnlich die Sprungmarken, ganz nach vorne der Zeile setzen und nicht einrücken, dann sieht man das besser. Ist aber Ansichtssache.
schirrmie
-
lassmastecken schrieb:
lass drin, wen juckts ?
richtig. der code ist ein schönes beispiel für effizienz mit 'gotos'.
ohne goto geht's zwar auch, aber dann wirds umständlich.
-
guck mal oben
case 0xa8: // Need first byte to transmit case 0xb8: // Need following bytes to transmit
du gehst beim ersten fall gleich mit in den zweiten, weiß nicht obs gewollt ist aber überprüfs mal.
-
ich hab deinen code mal umgeschrieben.
case ... { static UINT8 *packet; static UINT16 idx; static UINT16 size; if (packet == 0) { packet = PP_IOP_GetNextTxEntry(); if (packet == 0) { I20DAT = DUMMY_BYTE; break; } idx = 0; size = GET_MEMBER (packet, PP_PACKET, data)->datasize; } I20DAT = packet[idx++]; if (idx == size) { PP_FreePacket (packet); packet = 0; } } break;
ganz ohne gotos und es sind sogar weniger verzweigungen drin. probier mal aus ob's läuft.