jnk0le / AVR-UART-lib

extremly light uart library for AVR 8 bit microcontrollers
MIT License
103 stars 36 forks source link

small code improvements #7

Open dambo1993 opened 5 years ago

dambo1993 commented 5 years ago

Hi - a little advice - you have a lot od #define in code. for example:

void uart0_reinit(uint16_t ubrr_value)
{
    #ifdef USART0_USE_SOFT_RTS
        RTS0_PORT |= (1<<RTS0_IONUM);
    #endif

    #ifdef USART0_RS485_MODE
        RS485_CONTROL0_PORT &= ~(1<<RS485_CONTROL0_IONUM); //set low
        RS485_CONTROL0_DDR |= (1<<RS485_CONTROL0_IONUM);
    #endif

          // rest of the function
}

you can simply hide #defines by implement static functions:

static void USART0_USE_SOFT_RTS_func(void)
{
    #ifdef USART0_USE_SOFT_RTS
        RTS0_PORT |= (1<<RTS0_IONUM);
    #endif
}
static void USART0_RS485_MODE_on(void)
{
    #ifdef USART0_RS485_MODE
        RS485_CONTROL0_PORT &= ~(1<<RS485_CONTROL0_IONUM); //set low
        RS485_CONTROL0_DDR |= (1<<RS485_CONTROL0_IONUM);
    #endif
}

void uart0_reinit(uint16_t ubrr_value)
{
                USART0_USE_SOFT_RTS_func();
        USART0_RS485_MODE_on();

                // rest of the function
}
}

compiler will remove all unused jumps etc - and functions are easier to read. Of course- just in my opinion.