On Sat, 28 Feb 2009 19:24:38 -0700 "Weddington, Eric" <ewedding...@cso.atmel.com> wrote: > You wouldn't need *nested* ifs, but an if-else-if structure, or better yet, a > table of function pointers, also known as a dispatch table. Each method > depends on the type of data that you're switching on.
I switch on an unsigned byte, with contiguous values (0-24). A Function table sounds elegant to my ear, but it would mean 25 functions ! In my case the work is done in-situ, within the switch statement, as it's takes only 2 or 3 statements to process a givne "case". Using functions would be both overkill and overwhelming to manage I think !! ;-) I pasted my switch statement below for the curious. -- Vince void var_format(char *buff, uint8_t var_num) { uint16_t tmp16; uint32_t tmp32; int16_t tmpS16; char Yes[] = " Yes"; char No[] = " No"; char Unknown[] = "-\?\?\?-"; switch (var_num) { case ECU_COOLANT: {// (N * 0.75) - 40 DB41 -40 to +150 °C tmpS16 = (int16_t)(KLineFrameM1[41]) * 3 / 4 - 40; var_format_S16(buff, tmpS16, 0); break; } case ECU_ENGINE_SPEED: {// MSB:LSB DB12:13 0 to 9999 RPM ATOMIC_BLOCK(ATOMIC_FORCEON) { tmp16 = ((uint16_t)KLineFrameM1[12] << 8) + (uint16_t)KLineFrameM1[13]; } var_format_S16(buff, (int16_t)tmp16, 0); break; } case ECU_ROAD_SPEED: {// DB14 uint8_t MPH var_format_byte(buff, KLineFrameM1[8]); break; } case ECU_BARO_AIR_PRESSURE: {// ((N-128)/100)+1 DB24 -0.28 to +2.27 Bar tmpS16 = (int16_t)(KLineFrameM1[24]) - 28; var_format_S16(buff, tmpS16, 2); break; } case ECU_MAP_PRESSURE: {// ((N-130)/100)+1 DB25 -0.30 to +2.25 Bar tmpS16 = (int16_t)(KLineFrameM1[25]) - 30; var_format_S16(buff, tmpS16, 2); break; } case ECU_MAT_TEMP: {// (N * 0.75) - 40 DB42 -40 to +150 °C tmpS16 = (int16_t)(KLineFrameM1[42]) * 3 / 4 - 40; var_format_S16(buff, tmpS16, 0); break; } case ECU_THROTTLE_POSITION: {// N / 2.55 DB27 0 to 100 % tmp16 = (uint16_t)KLineFrameM1[27] * 100 / 255; var_format_byte(buff, (uint8_t)tmp16); break; } case ECU_ENGINE_LOAD: {// DB36 0 - 100 % var_format_byte(buff, KLineFrameM1[36]); break; } case ECU_KNOCK_COUNT: {// DB43 uint8_t var_format_byte(buff, KLineFrameM1[43]); break; } case ECU_KNOCK_RETARD: {// (N * 45) / 255 DB44 0 to 45 Deg tmp16 = (uint16_t)KLineFrameM1[44] * 90 / 51; var_format_S16(buff, (int16_t)tmp16, 1); break; } case ECU_SPARK_ADVANCE: {// (N * 9000)/256 MSB:LSB DB39:40 0.00 Degrees ATOMIC_BLOCK(ATOMIC_FORCEON) { tmp16 = ((uint16_t)KLineFrameM1[39] << 8) + (uint16_t)KLineFrameM1[40]; } tmp32 = (uint32_t)tmp16 * 9000 / 256; var_format_S16(buff, (int16_t)tmp32, 2); break; } case ECU_BOOST_DC: {// DB31 uint8_t or %, don't know var_format_byte(buff, KLineFrameM1[31]); break; } case ECU_MAIN_INJ_DC: {// DB45 uint8_t var_format_byte(buff, KLineFrameM1[45]); break; } case ECU_SECONDARY_INJ_DC: {// DB37 uint8_t var_format_byte(buff, KLineFrameM1[37]); break; } case ECU_AF_RATIO: {// N * 10 DB47 0.0 to 25.5 var_format_S16(buff, (int16_t)KLineFrameM1[47], 1); break; } case ECU_IAC_POSITION: {// DB23 0 to 170 var_format_byte(buff, KLineFrameM1[23]); break; } case ECU_COOLANT_START: {// (N * 0.75) - 40 DB9 -40 to +150 °C tmpS16 = (int16_t)(KLineFrameM1[9]) * 3 / 4 - 40; var_format_S16(buff, tmpS16, 0); break; } case ECU_DESIRED_IDLE: {// N * 12.5 DB11 0 to 3187 RPM tmp16 = (uint16_t)(KLineFrameM1[11]) * 25 / 2; var_format_S16(buff, (int16_t)tmp16, 0); break; } case ECU_RAW_ADC_COOLANT: {// DB8 uint8_t var_format_byte(buff, KLineFrameM1[8]); break; } case ECU_RAW_ADC_THROTTLE: {// DB10 uint8_t var_format_byte(buff, KLineFrameM1[10]); break; } case ECU_RAW_ADC_MAT: {// DB29 uint8_t var_format_byte(buff, KLineFrameM1[29]); break; } case ECU_CLOSED_LOOP_MODE: {// DB63 & 0x80 YES/NO if ( KLineFrameM1[63] & 0x80 ) strcpy(buff, Yes); else strcpy(buff, No); break; } case ECU_AIR_CON_DEMAND: {// DB6 & 0x08 YES/NO if ( KLineFrameM1[6] & 0x08 ) strcpy(buff, Yes); else strcpy(buff, No); break; } case ECU_AIR_CON_ENGAGED: {// DB7 & 0x01 YES/NO if ( KLineFrameM1[7] & 0x01 ) strcpy(buff, Yes); else strcpy(buff, No); break; } case ECU_FAN_RUNNING: {// DB7 & 0x04 YES/NO if ( KLineFrameM1[7] & 0x04 ) strcpy(buff, Yes); else strcpy(buff, No); break; } default: strcpy(buff, Unknown); } } _______________________________________________ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list