【2022得捷电子创新设计大赛】内存不够,不能蛮干
[复制链接]
本帖最后由 nemon 于 2022-10-30 14:54 编辑
本准备用零零碎碎的器件做电子标签,没搞定。突然想到 ESP32-S2-Kaluga-1 既有无线、又有显示屏 ,完全可以胜任啊。
于是打了个wamp,放了个id2info.php在根目录:
<?php
$id=$_GET["id"];
switch ($id)
{
case "0x01":
echo "http://192.168.137.1/book.jpg?id=1" ;
break;
case "0x02":
echo "It is a square." ;
break;
case "0x03":
echo "It is a rectangle." ;
break;
case "0x0111":
echo "It is RED." ;
break;
case "0x0222":
echo "It is GREEN." ;
break;
case "0x0333":
echo "It is BLUE." ;
break;
default:
echo "ERROR." ;
}
然后看了下官方例程,发现 http_request适合干这个,于是改写了官方代码,从get到的内容里,取最后一行。主要方法如下:
…………
char recv_buf[64];
char result_buf[64];
int result_buf_ptr=0;
int result_buf_cnt=0;
…………
result_buf_ptr=0;
result_buf_cnt=0;
bzero(result_buf, sizeof(result_buf));
do {
bzero(recv_buf, sizeof(recv_buf));
r = read(s, recv_buf, sizeof(recv_buf)-1);
for(int i = 0; i < r; i++) {
putchar(recv_buf[i]);
result_buf_cnt++;
if(result_buf_cnt>=150 && recv_buf[i]=='\n'){//让过http头,150是随便写的,0也无所谓
result_buf_ptr=0;
bzero(result_buf, sizeof(result_buf));
}
if(result_buf_ptr<64 && recv_buf[i]!='\n' ){
result_buf[result_buf_ptr]=recv_buf[i];
result_buf_ptr++;
}
}
} while(r > 0);
在git找到了一个文件很少,编译后也小的库(https://github.com/ricmoo/QRCode),没费多大劲就用上了:
// Create the QR code
/* */
QRCode qrcode;
uint8_t qrcodeData[qrcode_getBufferSize( MY_QR_VER )];
qrcode_initText(&qrcode, qrcodeData, MY_QR_VER , 0, msg);
uint16_t *data_buf = (uint16_t *)heap_caps_calloc(IMAGE_WIDTH * IMAGE_HIGHT, sizeof(uint16_t), MALLOC_CAP_SPIRAM);
for (int j = 0; j < IMAGE_HIGHT; j++) {
for (int i = 0; i < IMAGE_WIDTH; i++) {
color = color_white;
data_buf[i + IMAGE_WIDTH * j] =color;
}
}
/*
*************************************************************/
int p_x = 4 ,p_y = 4;
for(int n_y =0 ; n_y< MY_QR_SIZE ;n_y++){
p_x = 4 ;
for(int n_x =0 ; n_x< MY_QR_SIZE ;n_x++){
color = qrcode_getModule(&qrcode, n_y, n_x )==1?color_black:color_white;
for(int n_d_j =0 ; n_d_j<8;n_d_j++){
for(int n_d_i =0 ; n_d_i<8;n_d_i++){
data_buf[ ( p_x + n_d_i ) + IMAGE_WIDTH * ( p_y + n_d_j ) ] = color;
}
}
p_x +=8;
}
p_y +=8;
}
lcd_set_index(0, 0, IMAGE_WIDTH - 1, IMAGE_HIGHT - 1);
lcd_write_data((uint8_t *)data_buf, IMAGE_WIDTH * IMAGE_HIGHT * sizeof(uint16_t));
vTaskDelay(2000 / portTICK_RATE_MS);
free(data_buf);
return;
}
刚开始内存分配不够,总崩溃,后来改用heap_caps_calloc来分配就可以了,但是要注意,需要开启外部SPI RAM。方法是通过idf.py menuconfig->component config->ESP32-specific来操作。首先运行idf.py menuconfig,出现界面后按下面更改:
这样空间就够用了,效果如下:
VID_20221029_202815 电子标签
附赠代码:
ESP32-S2 显示二维码
|