Skip to content

Commit

Permalink
#43 Working example with PlasticLogic 21 using vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
martinberlin committed Jul 9, 2021
1 parent 9ddfb2a commit cd7875b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
6 changes: 3 additions & 3 deletions components/CalEPD/include/plasticlogic021.h
Expand Up @@ -31,9 +31,9 @@ class PlasticLogic021 : public PlasticLogic
void setEpdRotation(uint8_t o); // Override
private:
EpdSpi2Cs& IO;
uint8_t _buffer[PLOGIC021_BUFFER_SIZE];
// Buffer sent to EPD prefixed with 0x10:
uint8_t bufferEpd[PLOGIC021_BUFFER_SIZE+1];
vector<uint8_t> _buffer;
vector<uint8_t>::iterator buffer_it;
bool _vec_bonds_check = true;

bool _initial = true;
bool _debug_buffer = false;
Expand Down
2 changes: 1 addition & 1 deletion components/CalEPD/models/plasticlogic/plasticlogic014.cpp
Expand Up @@ -126,7 +126,7 @@ void PlasticLogic014::drawPixel(int16_t x, int16_t y, uint16_t color) {

// check that is not going to write out of Vector bonds
// #43 TODO: Check why is trying to update out of bonds anyways
if (pos >= _buffer.size()) {
if (pos > _buffer.size()) {
if (_vec_bonds_check) {
printf("x:%d y:%d Vpos:%d >out bonds\n",x,y, pos);
_vec_bonds_check = false;
Expand Down
50 changes: 33 additions & 17 deletions components/CalEPD/models/plasticlogic/plasticlogic021.cpp
Expand Up @@ -35,6 +35,7 @@ void PlasticLogic021::init(bool debug)
ESP_LOGE(TAG, "ATTENTION the size responded by the display: %d does not mach this class (21)", size);
}
_setSize(size);
clearScreen();
_wakeUp();

//printf("Epaper temperature after wakeUp: %d °C\n", IO.readTemp());
Expand All @@ -45,20 +46,25 @@ void PlasticLogic021::init(bool debug)
void PlasticLogic021::clearScreen(){
for (uint16_t x = 0; x < sizeof(_buffer); x++)
{
_buffer[x] = 0xff;
_buffer.push_back(0xff); //WHITE
}
}

int PlasticLogic021::_getPixel(int x, int y) {
// Not sure if width / height is correct since we handle rotation differently
if ((x < 0) || (x >= PLOGIC021_WIDTH) || (y < 0) || (y >= PLOGIC021_HEIGHT)) return 5;


uint16_t byteIndex = x/4 + (y) * _nextline;
if (byteIndex > _buffer.size()) {
return 0;
}

switch (x%4) {
case 0: return ((unsigned int)(_buffer[byteIndex] & 0xC0) >> 6);
case 1: return ((unsigned int)(_buffer[byteIndex] & 0x30) >> 4);
case 2: return ((unsigned int)(_buffer[byteIndex] & 0x0C) >> 2);
case 3: return ((unsigned int)(_buffer[byteIndex] & 0x03));
case 0: return ((unsigned int)(_buffer.at(byteIndex) & 0xC0) >> 6);
case 1: return ((unsigned int)(_buffer.at(byteIndex) & 0x30) >> 4);
case 2: return ((unsigned int)(_buffer.at(byteIndex) & 0x0C) >> 2);
case 3: return ((unsigned int)(_buffer.at(byteIndex) & 0x03));
}
// Should not return 0, otherwise gives error: control reaches end of non-void function
return 0;
Expand Down Expand Up @@ -93,13 +99,8 @@ void PlasticLogic021::update(uint8_t updateMode)

IO.data(pixelAccessPos, sizeof(pixelAccessPos));

bufferEpd[0] = 0x10;
// Copy GFX buffer contents:
for (int i=1; i < sizeof(bufferEpd); i++) {
bufferEpd[i] = _buffer[i-1];
}

IO.data(bufferEpd,sizeof(bufferEpd));
_buffer.insert(_buffer.begin(), 0x10);
IO.dataVector(_buffer);

_waitBusy("Buffer sent", EPD_TMG_SRT);

Expand Down Expand Up @@ -137,13 +138,28 @@ void PlasticLogic021::drawPixel(int16_t x, int16_t y, uint16_t color) {
// check rotation, move pixel around if necessary
// This is not working the same as other epapers: Research why

uint8_t pixels = _buffer[x/4 + (y) * _nextline];
uint16_t pos = x/4 + (y) * _nextline;

// check that is not going to write out of Vector bonds
// #43 TODO: Check why is trying to update out of bonds anyways
if (pos >= _buffer.size()) {
if (_vec_bonds_check) {
printf("x:%d y:%d Vpos:%d >out bonds\n",x,y, pos);
_vec_bonds_check = false;
}
return;
}
uint8_t pixels = _buffer.at(pos);
uint8_t pixel = 0xff;

switch (x%4) { //2-bit grayscale dot
case 0: _buffer[x/4 + (y) * _nextline] = (pixels & 0x3F) | ((uint8_t)color << 6); break;
case 1: _buffer[x/4 + (y) * _nextline] = (pixels & 0xCF) | ((uint8_t)color << 4); break;
case 2: _buffer[x/4 + (y) * _nextline] = (pixels & 0xF3) | ((uint8_t)color << 2); break;
case 3: _buffer[x/4 + (y) * _nextline] = (pixels & 0xFC) | (uint8_t)color; break;
case 0: pixel = (pixels & 0x3F) | ((uint8_t)color << 6); break;
case 1: pixel = (pixels & 0xCF) | ((uint8_t)color << 4); break;
case 2: pixel = (pixels & 0xF3) | ((uint8_t)color << 2); break;
case 3: pixel = (pixels & 0xFC) | (uint8_t)color; break;
}
buffer_it = _buffer.begin()+pos;
*(buffer_it) = pixel;
}

void PlasticLogic021::setEpdRotation(uint8_t o) {
Expand Down

0 comments on commit cd7875b

Please sign in to comment.