优化代码 - c 中的二维数组索引

发布于 2025-01-15 05:12:12 字数 2756 浏览 3 评论 0原文

我从 2D 数组缓冲区读取数据并使用 2 索引行索引和列索引将数据写入 lcd 如何将这两个字节(gu8LcdBufferColumn、gu8LcdBufferRow)优化为(单字节),其中最后 2 位作为行索引首先是 6 位作为列索引。

两种情况 LCD_UPDATE_WRITE 是将数据写入 lcd 和 LCD_UPDATE_POS 更新 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;

使用同步任务最后编辑此代码 两种情况将数据从缓冲区写入 lcd 宏 lcd_line0_start 和其他是 lcd 中的硬件地址 使用 switch 将此代码转换为异步任务case 并在每个循环中写入字节

 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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文