1 year ago

#305166

test-img

Hassan Elsaied

optimization code - 2D-array indexing in c

I read data from 2D-array buffer and write data to lcd using the 2-index row index and column index how to optimization this two byte (gu8LcdBufferColumn, gu8LcdBufferRow) to (single byte) in which the last 2-bit as a row index and first the 6 bit as column index.

two case the LCD_UPDATE_WRITE is write data into lcd and LCD_UPDATE_POS update the line of lcd

case LCD_UPDATE_WRITE:
    if (gu8LcdBufferColumn != LCD_NUMBER_OF_BYTE) {
       lcdWriteByte(gu8LCDBuffer[gu8LcdBufferRow][gu8LcdBufferColumn]);
         gu8LcdBufferColumn++;
    } else {
        gu8LcdBufferColumn = 0;
        gu8LcdBufferRow++;
        if (gu8LcdBufferRow == LCD_NUMBER_OF_LINES) {
            gu8LcdBufferRow = 0;
            genLCDUpdateState = LCD_UPDATE_CHECK;
            return (LCD_OK);
        } else {
            genLCDUpdateState = LCD_UPDATE_POS;
        }
    }
    break;

case LCD_UPDATE_POS:
    digitalPinWrite(LCD_RS, GPIO_LOW);
    if (gu8LcdBufferRow == 0) {
        lcdWriteByte(LCD_LINE_ONE_START);
    } else if (gu8LcdBufferRow == 1) {
        lcdWriteByte(LCD_LINE_TWO_START);
    } else if (gu8LcdBufferRow == 2) {
        lcdWriteByte(LCD_LINE_THREE_START);
    } else if (gu8LcdBufferRow == 3) {
        lcdWriteByte(LCD_LINE_FOUR_START);
    }
    digitalPinWrite(LCD_RS, GPIO_HIGH);
    genLCDUpdateState = LCD_UPDATE_WRITE;
    break;

last edit of this code with sync task two write data into lcd from buffer the macros lcd_line0_start and others is a hardware address in lcd convert this code to async task using switch case and write byte in each loop

 case LCD_UPDATE_WRITE:
        u8odd = 0;
        u8even = 0;

    if (bitIsClear(gu8LcdOPtion, LCD_DISPLAY)) {
        /*the display on write command*/
        digitalPinWrite(LCD_RS, GPIO_LOW);
        /*set the display on*/
        lcdWriteByte(LCD_DISPLAY_ON_COMMAND);
        digitalPinWrite(LCD_RS, GPIO_HIGH);
        genLCDUpdateState = LCD_UPDATE_CHECK; /*default state*/
        return (LCD_OK);
    }
    for (uint8_t i = 0; i < LCD_NUMBER_OF_LINES; i++) {
        digitalPinWrite(LCD_RS, GPIO_LOW);
        if (i % 2 == 0) {
            /*the number is even the start is 0x00 and D7 equal 1 = 0x80*/
            /*line 0 and line 2*/
            lcdWriteByte(LCD_LINE0_START + u8even);
            u8even = LCD_NUMBER_OF_BYTE;

        } else {
            /*the number is odd*/
            /*line 1 and line 3*/
            /*the start is 0x40 and D7 = 1  a value is 0xc0*/
            lcdWriteByte(LCD_LINE1_START + u8odd);
            u8odd = (LCD_NUMBER_OF_BYTE);
        }
        digitalPinWrite(LCD_RS, GPIO_HIGH);
        for (int j = 0; j < LCD_NUMBER_OF_BYTE; j++) {
            lcdWriteByte(gu8LCDBuffer[i][j]);
        }
    }

    /*the display on write command*/
    digitalPinWrite(LCD_RS, GPIO_LOW);
    /*set the display on*/
    lcdWriteByte(LCD_DISPLAY_ON_COMMAND);
    digitalPinWrite(LCD_RS, GPIO_HIGH);
    genLCDUpdateState = LCD_UPDATE_POS; /*default state*/

    break;

c

embedded

lcd

0 Answers

Your Answer

Accepted video resources