我正在为OLED屏幕绘制位图。每个图像的格式类似
`const static unsigned char wadle_dee_0[]PROGMEM={位};
目前,我一直在尝试添加动画并清理代码。为此,我创建了类"位图"。
这个类将存储诸如大小、宽度和一个数组之类的内容,该数组包含指向图像每帧的指针,例如
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数组相同,这样我就可以循环索引,按顺序一次绘制一个位图。当我有一个硬编码的数组时,我的代码就可以工作了,但在尝试对其进行泛化后,我遇到了大量的类型错误和不一致。
如果有人能带领我朝着正确的方向前进,我似乎已经迷失在指针系统中了。谢谢
如果您想将bitmap_frames
声明为常量灵活数组,则不能复制到它,应该在构造函数中初始化它
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)) {}