Skip to content

Commit e022503

Browse files
committed
Had a go at updating the img stuff. (Not tested)
1 parent 25eacf5 commit e022503

File tree

2 files changed

+47
-60
lines changed

2 files changed

+47
-60
lines changed

src/imgNeoPixel.cpp

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,63 @@ imgNeoPixel::imgNeoPixel(Adafruit_NeoPixel* theLEDs,baseImage* theBMPImage) {
88
mImage = theBMPImage;
99
mRGBArray = NULL;
1010
mNumPixels = 0;
11-
mImageFile = NULL;
11+
mReadyToDraw = false;
1212
}
1313

1414

1515
imgNeoPixel::~imgNeoPixel(void) {
1616

17-
resizeBuff(0,&mRGBArray);
18-
if (mFile) {
19-
mFile.close();
20-
}
17+
resizeBuff(0,(byte**)&mRGBArray); // Deallocate the offscreen buffer.
18+
if (mImage) mImage->closeDocFile(); // Close but don't delete the image file.
2119
}
2220

23-
24-
bool imgNeoPixel::setupOffscreen(int numPixels) {
2521

26-
int maxWidth;
22+
// Open up the image file, make sure its readable. Then, if so.. Setup to allocate the
23+
// offscreen buffer. Basically an array of RGBpack(s). There are three numbers to take
24+
// into account.
25+
// First : The number of pixels we have on the string.
26+
// Second : The width of the image we have to read from.
27+
// Third : The actual value the uses puts in as the value, and that default to zero.
28+
//
29+
// How to choose?
30+
void imgNeoPixel::setupOffscreen(int numPixels) {
31+
32+
int maxWidth;
2733

28-
mImageFile = NULL; // Just in case.
29-
maxWidth = min(mPixels->numPixels(),mImage->getWidth());
30-
if (numPixels==0) {
31-
mNumPixels = maxWidth;
32-
} else {
33-
mNumPixels = min(maxWidth,mNumPixels);
34-
}
35-
if (resizeBuff(sizeof(RGBpack)*mNumPixels,&mRGBArray)) {
36-
mFile = SD.open(mImage->getDocFilePath());
37-
if (mFile) {
38-
mImageFile = &mFile;
39-
return true;
40-
} else {
41-
resizeBuff(0,&mRGBArray);
42-
mNumPixels = 0;
34+
mReadyToDraw = false; // We ain't ready yet..
35+
if (mImage) { // If we have an image file..
36+
if (mImage->openDocFile(FILE_READ)) { // If we can open/read said image file..
37+
maxWidth = min(mPixels->numPixels(),mImage->getWidth()); // Grab the smaller of the image width and the number of pixels we have.
38+
if (numPixels==0) { // If the numPixels passed in is zero..
39+
mNumPixels = maxWidth; // We just use the maxWidth we just calculated.
40+
} else { // Else, we have non zero value passed in..
41+
mNumPixels = min(maxWidth,numPixels); // We use the smallar of the previous max width and this new passed in value.
42+
}
43+
if (resizeBuff(sizeof(RGBpack)*mNumPixels,(byte**)&mRGBArray)) { // If we can grab the RAM..
44+
mReadyToDraw = true; // Ok, we are ready to draw!
45+
return; // We call all of this a success! And exit.
46+
} else { // Else, we could NOT allocate the buffer..
47+
mImage->closeDocFile(); // Close the image file.
48+
}
4349
}
44-
}
45-
return false;
50+
} // And if we got here? We call this a failure. And exit.
4651
}
47-
48-
49-
void imgNeoPixel::clearOffscreen(void) {
50-
51-
resizeBuff(0,&mRGBArray);
52-
mNumPixels = 0;
53-
if (mFile) {
54-
mFile.close();
55-
}
56-
mImageFile = NULL;
57-
}
58-
52+
5953

6054
void imgNeoPixel::setLine(int row,int numPixels) {
6155

6256
int maxWidth;
63-
colorObj aPixel;
6457

65-
maxWidth = min(mPixels->numPixels(),mImage->getWidth());
66-
if (numPixels==0) {
67-
numPixels = maxWidth;
68-
} else {
69-
mNumPixels = min(maxWidth,mNumPixels);
70-
}
71-
if (mImageFile && mRGBArray) {
72-
mImage->getRow(row,mRGBArray,mNumPixels,mImageFile);
73-
for (int i=0;i<mNumPixels;i++) {
74-
mPixels->setPixelColor(i,mRGBArray[i].r,mRGBArray[i].g,mRGBArray[i].b);
58+
if (mReadyToDraw) {
59+
if (numPixels) {
60+
maxWidth = min(mNumPixels,numPixels);
61+
} else {
62+
maxWidth = mNumPixels;
7563
}
76-
} else {
77-
for (int i=0;i<maxWidth;i++) {
78-
aPixel = mImage->getPixel(i,row);
79-
mPixels->setPixelColor(i,aPixel.getRed(),aPixel.getGreen(),aPixel.getBlue());
64+
if (mImage->getRow(0,row,maxWidth,mRGBArray)) {
65+
for (int i=0;i<maxWidth;i++) {
66+
mPixels->setPixelColor(i,mRGBArray[i].r,mRGBArray[i].g,mRGBArray[i].b);
67+
}
8068
}
8169
}
82-
}
83-
84-
85-
70+
}

src/imgNeoPixel.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef imgNeoPixel_h
2+
#define imgNeoPixel_h
3+
14

25
#include "baseImage.h"
36
#include "Adafruit_NeoPixel.h"
@@ -9,16 +12,15 @@ class imgNeoPixel {
912
imgNeoPixel(Adafruit_NeoPixel* theLEDs,baseImage* theBMPImage);
1013
virtual ~imgNeoPixel(void);
1114

15+
void setupOffscreen(int numPixels=0);
1216
virtual void setLine(int row,int numPixels=0);
13-
bool setupOffscreen(int numPixels=0);
14-
void clearOffscreen(void);
1517

1618
protected:
1719
Adafruit_NeoPixel* mPixels;
1820
baseImage* mImage;
1921
RGBpack* mRGBArray;
2022
int mNumPixels;
21-
File mFile;
22-
File* mImageFile;
23+
bool mReadyToDraw;
2324
};
2425

26+
#endif

0 commit comments

Comments
 (0)