zhanghuafeng24 2024-04-12 17:33 采纳率: 0%
浏览 13

c语言使用(const char*)遇到的问题

img

img

为什么这个'white'变量一定要定义使用它的运算前面(和图片相同的位置我下面原文件标注的232行),不能定义在函数的开头(和图片相同的位置我下面标注的189行)?下面是原文件

void xpt2046_calibration(void)
{    const char* lcd_name = "lcd";
    const char* touch_name = "xpt0";
    const uint32_t black = 0x0000;
**189__**
    rt_device_t lcd = rt_device_find(lcd_name);
    if (lcd == RT_NULL)
    {
        LOG_E(LOG_TAG" cannot find lcd device named %s\n", lcd_name);
        return;
    }
    if (rt_device_open(lcd, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
    {
        LOG_E(LOG_TAG" cannot open lcd device named %s\n", lcd_name);
        return;
    }

    rt_xpt2046_t touch = (rt_xpt2046_t)rt_device_find(touch_name);
    if (touch == RT_NULL)
    {
        LOG_E(LOG_TAG" cannot find touch device named %s\n", touch_name);
        return;
    }
    if (rt_device_open(touch, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
    {
        LOG_E(LOG_TAG" cannot open touch device named %s\n", touch_name);
        return;
    }
    struct rt_device_graphic_info lcd_info;
    rt_device_control(lcd, RTGRAPHIC_CTRL_GET_INFO, &lcd_info);

    for (rt_uint32_t y = 0; y < lcd_info.height; ++y)
    {

        rt_graphix_ops(lcd)->draw_hline((const char *)(&black), 0, lcd_info.width, y);
    }
    rt_uint32_t cross_size = (lcd_info.width > lcd_info.height ? lcd_info.height : lcd_info.width) / 10;
    rt_uint32_t x0 = cross_size;
    rt_uint32_t y0 = cross_size;
    rt_uint32_t x1 = lcd_info.width - cross_size;
    rt_uint32_t y1 = cross_size;
    rt_uint32_t x2 = lcd_info.width - cross_size;
    rt_uint32_t y2 = lcd_info.height - cross_size;
    rt_uint32_t x3 = cross_size;
    rt_uint32_t y3 = lcd_info.height - cross_size;

    // Upper left cross
 ** 232  const rt_uint32_t white = 0xFFFFFFFF;__**
    rt_graphix_ops(lcd)->draw_hline((const char *)(&white), 0, x0+cross_size, y0);
    rt_graphix_ops(lcd)->draw_vline((const char *)(&white), x0, 0, y0+cross_size);
    lcd_show_string(50,50,16,"Touch screen calibration");
    touch->min_raw_x = 0;
    touch->min_raw_y = 0;
    touch->max_raw_x = 4096;
    touch->max_raw_y = 4096;
    touch->parent.info.range_x = 4096;
    touch->parent.info.range_y = 4096;

    rt_uint16_t x_raw[4];
    rt_uint16_t y_raw[4];
    rt_uint8_t raw_idx = 0;
    rt_memset(&x_raw, 0, sizeof(rt_uint32_t)*4);
    rt_memset(&y_raw, 0, sizeof(rt_uint32_t)*4);
    while (1)
    { const rt_uint32_t white = 0xFFFFFFFF;
        struct rt_touch_data read_data;
        rt_memset(&read_data, 0, sizeof(struct rt_touch_data));
        if (rt_device_read(touch, 0, &read_data, 1) == 1)
        {
            x_raw[raw_idx] = read_data.x_coordinate;
            y_raw[raw_idx++] = read_data.y_coordinate;
            LOG_I(LOG_TAG" %d point capture", raw_idx-1);
            for (rt_uint32_t y = 0; y < lcd_info.height; ++y)
            {

                rt_graphix_ops(lcd)->draw_hline((const char *)(&black), 0, lcd_info.width, y);
            }
            rt_thread_mdelay(1000);
            if (raw_idx >= 4)
            {
                break;
            }

            switch(raw_idx)
            {
                case 1:
                    // Upper right cross
                    rt_graphix_ops(lcd)->draw_hline((const char *)(&white), x1-cross_size, lcd_info.width, y1);
                    rt_graphix_ops(lcd)->draw_vline((const char *)(&white), x1, 0, y1+cross_size);
                    rt_thread_mdelay(1000);
                    break;
                case 2:
                    // lower right cross
                    rt_graphix_ops(lcd)->draw_hline((const char *)(&white), x2-cross_size, lcd_info.width, y2);
                    rt_graphix_ops(lcd)->draw_vline((const char *)(&white), x2, y2-cross_size, lcd_info.height);
                    rt_thread_mdelay(1000);
                    break;
                case 3:
                    // lower left cross
                    rt_graphix_ops(lcd)->draw_hline((const char *)(&white), 0, x3+cross_size, y3);
                    rt_graphix_ops(lcd)->draw_vline((const char *)(&white), x3, y3-cross_size, lcd_info.height);
                    rt_thread_mdelay(1000);
                    break;
                default:
                    break;
            }
        }
        rt_thread_mdelay(10);
    }
    rt_uint32_t min_x = (x_raw[0]+x_raw[3])/2;
    rt_uint32_t max_x = (x_raw[1]+x_raw[2])/2;
    rt_uint32_t min_y = (y_raw[0]+y_raw[1])/2;
    rt_uint32_t max_y = (y_raw[2]+y_raw[3])/2;

    rt_uint32_t x_raw_cnt_per_pixel = (max_x-min_x) / (x1-x0);
    rt_uint32_t y_raw_cnt_per_pixel = (max_y-min_y) / (y2-y1);

    min_x -= cross_size * x_raw_cnt_per_pixel;
    max_x += cross_size * x_raw_cnt_per_pixel;
    min_y -= cross_size * y_raw_cnt_per_pixel;
    max_y += cross_size * y_raw_cnt_per_pixel;

    touch->min_raw_x = min_x;
    touch->min_raw_y = min_y;
    touch->max_raw_x = max_x;
    touch->max_raw_y = max_y;
    touch->parent.info.range_x = lcd_info.width;
    touch->parent.info.range_y = lcd_info.height;

    LOG_I(LOG_TAG" Calibration result, min_x:%d\n, min_y:%d\n, max_x:%d\n, max_y:%d\n", min_x, min_y, max_x, max_y);
    rt_thread_mdelay(1000);

    rt_device_close(lcd);
    rt_device_close(touch);
}

img

  • 写回答

2条回答 默认 最新

  • GISer Liu 2024-04-12 18:28
    关注

    该回答引用自GPT-3.5,由博主GISer Liu编写:

    这个问题是因为在C语言中,常量字符数组(const char数组)的定义方式是在函数内部定义时,需要将其初始化为一个字符串字面值,例如const char lcd_name[] = "lcd";。而在你的代码中,const char lcd_name = "lcd";这样的写法是错误的,因为lcd_name被声明为一个单个字符的常量,而不是字符数组。
    正确的写法应该是:
    cCopy code

    void xpt2046_calibration(void) {
    /* Find the TFT LCD device */
    const char lcd_name[] = "lcd";
    const char* touch_name = "xpt0";
    const uint32_t black = 0x0000;
    rt_device_t lcd = rt_device_find(lcd_name);
    if (lcd == RT_NULL) {
    LOG_E(LOG_TAG" cannot find lcd device named %s\n", lcd_name);
    return;
    }
    // 其他代码保持不变
    }
    

    这样修改后,lcd_nametouch_name都被正确地定义为字符串数组,而不是单个字符的常量。

    如果该回答解决了您的问题,请采纳!如果没有,请私信联系或评论您的疑惑

    评论

报告相同问题?

问题事件

  • 修改了问题 4月13日
  • 修改了问题 4月13日
  • 修改了问题 4月13日
  • 修改了问题 4月13日
  • 展开全部

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助