单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.
查看:1458|回复:0
打印上一主题下一主题

基于ESP32手势切换天气相册的制作 附程序

[复制链接]
跳转到指定楼层
楼主
ID:228147发表于 2023-3-6 16:27|只看该作者|只看大图回帖奖励
硬件部分
一块ESP32开发板、一块TFT显示屏(驱动ILI9341、不用触摸、尺寸自选,用的是240*320,2.8寸)、Micro SD卡读取模块、micro SD卡(我测试的时候2G的挂载成功但是无法读取,最后用的128M的)、PAJ7620手势传感器。

SP32模块作为主控,实现天气信息获取以及将SD卡内图片推送到TFT屏幕上。在连线时,需要注意一点:因为TFT屏幕与SD卡共用一套SPI,所以他们的一些引脚是接在一起的,只需要控制片选引脚即可,PAJ7620使用的是IIC总线(PAJ7620未在图中绘出),在源码里我注释了接线的引脚。
软件流程
流程根据功能化为4个部分,心知天气API获取,网页图片上传、手势传感器切花,FTF显示,每个部分的相关操作如下图:

硬件部分与软件流程图:





部分代码
  1. #include "JpegDecoder.h"
  2. #include< math.h>
  3. #include< stdlib.h>

  4. using namespace std;

  5. //---------------------------------------------------------------------------


  6. void JpegDecoder::DecoderTable()
  7. {
  8.        int val = 0; // type of table ( DC(0,1) or AC(0,1) )
  9.        uint8_t buf[16];

  10.        fread(&val, 1, 1, fp); // get type of table
  11.        fread(buf, 1, 16, fp); // get key value
  12.        map<string, uint8_t>& tb = huffman[val];

  13.        string keyStr = "";
  14.        for (int i = 0; i< 16; i++) // length of key (i.e. i = 2 means key = 000 , 001 , 010 , 011 or ...)
  15.        {
  16.            int cnt = buf[i]; // number of key, which length is (i+1)

  17.            /* alignment */
  18.            for (int k = keyStr.length(); k< = i; k++)
  19.            {
  20.                  keyStr += "0";
  21.            }

  22.            while (cnt > 0)
  23.            {
  24.                  /* value of key */
  25.                  fread(&val, 1, 1, fp); // read value
  26.                  //printf("%s = %X\n", keyStr.c_str(), val);
  27.                  tb.insert(pair<string, uint8_t>(keyStr, val));

  28.                  /* increment */
  29.                  int carry = 1; //??λ
  30.                  for (int k = keyStr.length() - 1; k >= 0; k--)
  31.                  {
  32.                        int tmpVal = (keyStr[k] + carry - '0'); //?????λ
  33.                        carry = tmpVal / 2;
  34.                        keyStr[k] = tmpVal % 2 + '0'; //??????λ???
  35.                  }
  36.                  cnt = cnt - 1;
  37.            }
  38.        }
  39. }

  40. //-------------------------------------------------------------------------------

  41. Mtx JpegDecoder::InveseSample(Mtx& block, int number)
  42. {
  43.        Mtx ret;

  44.        int x = (number / 2) * 4;
  45.        int y = (number % 2) * 4;
  46.        for (int i = 0; i< 8; i += 2)
  47.        {
  48.            for (int j = 0; j< 8; j += 2)
  49.            {
  50.                  ret[i][j] = ret[i][j + 1] = ret[i + 1][j] = ret[i + 1][j + 1] = block[x + i / 2][y + j / 2];
  51.            }
  52.        }

  53.        return ret;
  54. }


  55. /* @brief ????????? YCbCr -> RGB
  56. */
  57. void JpegDecoder::ConvertClrSpace(Mtx& Y, Mtx& Cb, Mtx& Cr, Pixel out[8][8])
  58. {
  59.        for (int i = 0; i< 8; i++)
  60.        {
  61.            for (int j = 0; j< 8; j++)
  62.            {
  63.                  out[i][j].R = Y[i][j] + 1.402 * Cr[i][j] + 128;
  64.                  out[i][j].G = Y[i][j] - 0.34414 * Cb[i][j] - 0.71414 * Cr[i][j] + 128;
  65.                  out[i][j].B = Y[i][j] + 1.772 * Cb[i][j] + 128;

  66.                  /* ??? */
  67.                  if (out[i][j].R > 255) out[i][j].R = 255;
  68.                  if (out[i][j].G > 255) out[i][j].G = 255;
  69.                  if (out[i][j].B > 255) out[i][j].B = 255;

  70.                  /* ??? */
  71.                  if (out[i][j].R< 0) out[i][j].R = 0;
  72.                  if (out[i][j].G< 0) out[i][j].G = 0;
  73.                  if (out[i][j].B< 0) out[i][j].B = 0;
  74.            }
  75.        }
  76. }


  77. /* @brief ???? ConvertClrSpace(), ?? MCU ?е? YCbCr ??????? RGB ???
  78. */
  79. void JpegDecoder::Convert()
  80. {
  81.        Mtx cb;
  82.        Mtx cr;
  83.        Pixel out[8][8];


  84.        cb = InveseSample(mcu.cbMtx, 0);
  85.        cr = InveseSample(mcu.crMtx, 0);
  86.        ConvertClrSpace(mcu.yMtx[0], cb, cr, out);
  87.        WriteToRGBBuffer(out, 0);

  88.        cb = InveseSample(mcu.cbMtx, 1);
  89.        cr = InveseSample(mcu.crMtx, 1);
  90.        ConvertClrSpace(mcu.yMtx[1], cb, cr, out);
  91.        WriteToRGBBuffer(out, 1);

  92.        cb = InveseSample(mcu.cbMtx, 2);
  93.        cr = InveseSample(mcu.crMtx, 2);
  94.        ConvertClrSpace(mcu.yMtx[2], cb, cr, out);
  95.        WriteToRGBBuffer(out, 2);

  96.        //for (int i = 0; i< 8; i++)
  97.        //{
  98.        //       for (int j = 0; j< 8; j++)
  99.        //       {
  100.        //           cr[i][j] = mcu.crMtx[4 + i % 4][4 + j % 4];
  101.        //           cb[i][j] = mcu.cbMtx[4 + i % 4][4 + i % 4];
  102.        //       }
  103.        //}

  104.        cb = InveseSample(mcu.cbMtx, 3);
  105.        cr = InveseSample(mcu.crMtx, 3);
  106.        ConvertClrSpace(mcu.yMtx[3], cb, cr, out);
  107.        WriteToRGBBuffer(out, 3);
  108. }


  109. /* @brief ??????????RGB????д??RGB????????
  110.        @buf: ????????
  111.        @blockIndex: ?????? ????Χ: 00, 01, 10, 11 (??????) -> 0??1??2??3 (?????)

  112. */
  113. void JpegDecoder::WriteToRGBBuffer(Pixel buf[8][8], int blockIndex)
  114. {
  115.        int xOffset = 8 * (blockIndex& 0x02) >> 1; // binary: blockIndex& 10 (i.e. if blockIndex =  01 => blockIndex& 0x02 == 01& 10 => xOffset = 0 * 8 = 0 )
  116.        int yOffset = 8 * (blockIndex& 0x01);     // binary: blockIndex& 01 (i.e. if blockIndex =  01 => blockIndex& 0x01 == 01& 01 => yOffset = 1 * 8 = 8 )
  117.        for (int i = 0; i< 8; i++)
  118.        {
  119.            for (int j = 0; j< 8; j++)
  120.            {
  121.                  rgbBuf[xOffset + i][yOffset + j].R = buf[i][j].R;
  122.                  rgbBuf[xOffset + i][yOffset + j].G = buf[i][j].G;
  123.                  rgbBuf[xOffset + i][yOffset + j].B = buf[i][j].B;
  124.            }
  125.        }
  126. }

  127. //-------------------------------------------------------------------------------

  128. /* @brief ??????? MCU
  129. */
  130. void JpegDecoder::DecoderNextMCU()
  131. {
  132.        /* Y ?????? ??? 0??? */
  133.        DecoderMtx(mcu.yMtx[0], 0, quantY, dcY);
  134.        DecoderMtx(mcu.yMtx[1], 0, quantY, dcY);
  135.        DecoderMtx(mcu.yMtx[2], 0, quantY, dcY);
  136.        DecoderMtx(mcu.yMtx[3], 0, quantY, dcY);

  137.        /* Cb, Cr ?????? ??? 1??? */
  138.        DecoderMtx(mcu.cbMtx, 1, quantC, dcCr);
  139.        DecoderMtx(mcu.crMtx, 1, quantC, dcCb);
  140. }

  141. //--------------------------------------------------------------------

  142. /* @brief ??????? 8 x 8 ??????
  143. */
  144. void JpegDecoder::DecoderMtx(Mtx& block, int table, uint8_t *quant, int& dc)
  145. {
  146.        if (endOfDecoder) return; // ???????

  147.        // reset matrix
  148.        for (int i = 0; i< 64; i++) block[i / 8][i % 8] = 0x0;

  149.        // decoder DC of matrix
  150.        int length = FindKeyValue(table);
  151.        int value = GetRealValue(length);
  152.        dc += value; // DC
  153.        block[0][0] = dc;

  154.        // decoder AC of matrix, table = table + 16 => table = 0x00 (DC-0)-> table = 0x10 (AC-0)
  155.        for (int i = 1; i< 64; i++)
  156.        {
  157.            length = FindKeyValue(table + 16);
  158.            if (length == 0x0) break; // ????????

  159.            value = GetRealValue(length& 0xf); // ??? 4λ??????????
  160.            i += (length >> 4);       // ??? 4λ???г????
  161.            block[i / 8][i % 8] = value; // AC
  162.        }


  163.        // ??????
  164.        for (int i = 0; i< 64; i++) block[i / 8][i % 8] *= quant[i];

  165.        // ?? Zig-Zag ????
  166.        UnZigZag(block);

  167.        // ?????????任
  168.        IDCT(block);
  169. }

  170. /* @brief ?????????????????????Ч?
  171. */
  172. int JpegDecoder::FindKeyValue(int table)
  173. {
  174.        map<string, uint8_t>& huf = huffman[table];

  175.        string keyStr = "";
  176.        while (huf.find(keyStr) == huf.end())
  177.        {
  178.            //printf("%s\n", keyStr.c_str());
  179.            keyStr += (NextBit() + '0'); // char of 0 or 1
  180.        }

  181.        //printf("%s = %d\n", keyStr.c_str(), huf[keyStr]);
  182.        return huf[keyStr];
  183. }


  184. /* ????????????????????? */
  185. int JpegDecoder::GetRealValue(int length)
  186. {
  187.        int retVal = 0;
  188.        for (int i = 0; i< length; i++)
  189.        {
  190.            retVal = (retVal<< 1) + NextBit();
  191.        }

  192.        return (retVal >= pow(2, length - 1) ? retVal : retVal - pow(2, length) + 1);
  193. }
  194. //---------------------------------------------------------------------

  195. /* @brief ????
  196. */
  197. BitmapImage& JpegDecoder::Decoder()
  198. {
  199.        /* decoder quant table */
  200.        DecoderQuant();

  201.        /* decoder width and height of image */
  202.        DecoderSize();

  203.        /* decoder huffman table of DC and AC */
  204.        DecoderHuffman();

  205.        /* decoder data */
  206.        ToStartOfData();

  207.        /* decoder MCU */
  208.        int totalBlock = xNumberOfBlock * yNumberOfBlock;
  209.        while (!endOfDecoder&& (idxOfBlock< totalBlock))
  210.        {
  211.            DecoderNextMCU();

  212.            Convert();

  213.            WirteBlock(rgbBuf);
  214.        }

  215.        /* end of decoder */
  216.        return img;
  217. }
复制代码

51hei截图20230306162537.png(9.34 KB, 下载次数: 163)

压缩文件

压缩文件

程序.7z

269.86 KB, 下载次数: 24, 下载积分: 黑币 -5

评分

黑币 +50
收起理由
+ 50
共享资料的黑币奖励!

查看全部评分

手机版|小黑屋|51黑电子论坛|51黑电子论坛6群QQ管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网