首页 > 解决方案 > 指针数组,指向程序中的位图

问题描述

我正在将位图绘制到 OLED 屏幕上。每个图像的格式如下

`const static unsigned char waddle_dee_0[] PROGMEM ={ bits };

目前,我一直在尝试添加动画并清理代码。为此,我创建了“位图”类。

此类将存储诸如大小、宽度和包含指向图像每一帧的指针的数组,例如

const static unsigned char* const waddle_table[] PROGMEM = {
  waddle_dee_0,
  waddle_dee_1,
  waddle_dee_2,
  waddle_dee_3,
  waddle_dee_4,
  waddle_dee_5
};

在 Bitmap.cpp 中,我有一个构造函数和一个函数

#include "bitmaps.h"

Bitmap::Bitmap(double w, double h, uint8_t f, size_t s, const unsigned char* const b){
    setWidth(w);
    setHeight(h);
    setFrames(f);
    setSize(s);
    setAllFrames(b);
}

void Bitmap::drawFrames(){

size_t currSize = this->getSize();
uint8_t numbOfFrames = this->getFrames();
double width = this->getWidth();
double height = this->getHeight();

Serial.println(currSize);
Serial.println(numbOfFrames);

    for (int i = 0; i < numbOfFrames; i++)
    {
      const unsigned char* frameAt = this->getSingleFrame(i);
      drawBitmap(0,0,width,height, frameAt, currSize);
      delay(100);
    }

}

在头文件中我有定义

#include <stdint.h>
#include "Arduino.h"

// ensure this library description is only included once
#ifndef Bitmap_h
#define Bitmap_h

// library interface description
class Bitmap
{
  // user-accessible "public" interface
  public:
    Bitmap(double w, double h, uint8_t f, size_t s, const unsigned char* const b);
    double getWidth(){return width;}
    double getHeight(){return height;}
    uint8_t getFrames(){return frames;}
    size_t getSize(){return size;}

    double setWidth(double w){width = w;}
    double setHeight(double h){height = h;}
    uint8_t setFrames(uint8_t f){frames = f;}
    size_t setSize(size_t s){size = s;}

    //const unsigned char* const* getFrameArr(){return bitmap_frames;}
    void drawFrames();
    const unsigned char* const* getAllFrames(){return bitmap_frames;}
    void setAllFrames(const unsigned char* const b){*bitmap_frames = b;}
    const unsigned char* getSingleFrame(uint8_t f){return bitmap_frames[f];}

private:
    double width;
    double height;
    uint8_t frames;
    size_t size;
    const unsigned char* const bitmap_frames[];
};
#endif

我的问题来自指针数组,以及如何正确复制这些帧或将指针正确复制到新数组(bitmap_frames)。目标是让 bitmap_frames 与 waddle_table 数组相同,因此我可以循环遍历索引以按顺序一次绘制每个位图。当我有一个硬编码数组时,我的代码正在工作,但是在尝试对其进行概括后,我遇到了许多类型错误和不一致。

如果有人能引导我走向正确的方向,我似乎迷失在指针系统中。谢谢!

标签: androidc++arrayspointersbitmap

解决方案


如果你想声明bitmap_frames为 const 灵活数组,你不能复制到它,应该在构造函数中初始化它:

Bitmap::Bitmap() : bitmap_frames {waddle_dee_0, waddle_dee_1, waddle_dee_2, waddle_dee_3, waddle_dee_4, waddle_dee_5} {}

或者您可以bitmap_frames在类定义中声明为静态:

static const unsigned char *const bitmap_frames[];

并在源文件中定义它:

const unsigned char *const Bitmap::bitmap_frames[] = {
  waddle_dee_0,
  waddle_dee_1,
  waddle_dee_2,
  waddle_dee_3,
  waddle_dee_4,
  waddle_dee_5};

你也可以使用std::vector

const std::vector<const unsigned char*> bitmap_frames;

在构造函数中:

Bitmap::Bitmap() : bitmap_frames(std::begin(waddle_table), std::end(waddle_table)) {}

推荐阅读