implement buffer with pixel operations

This commit is contained in:
Adrien Marquès 2020-05-31 22:13:05 +02:00
parent 33c4af6dc3
commit de31ddb395
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
2 changed files with 87 additions and 9 deletions

View File

@ -52,6 +52,7 @@ void Display::send_commands(const uint8_t *cc, uint8_t n){
Display::Display(const uint8_t resetPin){
_rstPin = resetPin;
_wire = &Wire;
_buffer = (uint8_t*) malloc(_width * ((_height + 7) / 8));
}
void Display::begin(){
@ -118,7 +119,29 @@ void Display::begin(){
return;
}
#define swap(a, b) \
(((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) ///< No-temp-var swap operation
void Display::clear(){
memset(_buffer, 0, _width * ((_height + 7) / 8));
}
void Display::setPixel(int16_t x, int16_t y, const bool white){
if( x > _width || y > _height || x < 0 || y < 0 ){
return;
}
// swap(x, y);
// x = _width - x - 1;
// y = _height - y - 1;
if( white ){
_buffer[x + (y/8)*_width] |= (1 << (y&7));
return;
}
_buffer[x + (y/8)*_width] &= ~(1 << (y&7));
}
void Display::apply(){
TRANSACTION_START
static const uint8_t PROGMEM dlist1[] = {
@ -131,9 +154,10 @@ void Display::clear(){
send_commands(dlist1, sizeof(dlist1));
send_command(_width - 1); // Column end address
uint16_t count = _width * ((_height + 7) / 8);
uint8_t* ptr = _buffer;
_wire->beginTransmission(_i2caddr);
_wire->write((uint8_t) 0x40);
uint16_t count = _width * ((_height + 7) / 8);
uint8_t written = 1;
while(count--){
if( written >= WIRE_MAX ){
@ -142,15 +166,65 @@ void Display::clear(){
_wire->write((uint8_t) 0x40);
written = 1;
}
_wire->write(0);
_wire->write(*ptr++);
written++;
}
_wire->endTransmission();
TRANSACTION_END
}
void Display::setTitle(const String title){
// todo
}
// draws a progress bar to a fixed location
// the progress value is between 0 and 255
void Display::progress(const uint8_t progress){
// the progress value is between 0 and 1023
void Display::progress(const short progress, const uint8_t startLine, const uint8_t endLine, const bool absolute){
// top line
for( uint8_t i = 0 ; i < 128 ; i++ ){
setPixel(i, startLine, true);
}
// bottom line
for( uint8_t i = 0 ; i < 128 ; i++ ){
setPixel(i, endLine, true);
}
// center tip
for( uint8_t i = startLine+2 ; i < endLine-1 ; i++ ){
setPixel(64, endLine, true);
}
uint16_t scaledProgress = (progress+7) / 8;
// draw absolute progression
if( absolute ){
for( uint8_t x = 0 ; x < scaledProgress ; x++ ){
for( uint8_t y = startLine+1 ; y < endLine ; y++ ){
setPixel(x, y, true);
}
}
return;
}
// draw left relative progression
if( progress < 512 ){
for( uint8_t x = scaledProgress ; x < 64 ; x++ ){
for( uint8_t y = startLine+1 ; y < endLine ; y++ ){
setPixel(x, y, true);
}
}
// draw right relative progression
} else {
for( uint8_t x = 64 ; x < scaledProgress ; x++ ){
for( uint8_t y = startLine+1 ; y < endLine ; y++ ){
setPixel(x, y, true);
}
}
}
}

View File

@ -49,14 +49,18 @@
Display(const uint8_t resetPin=-1);
void begin();
void clear();
void progress(const uint8_t progress);
void setTitle(const String title);
void setPixel(int16_t x, int16_t y, const bool white=true);
void progress(const short progress, const uint8_t startLine, const uint8_t endLine, const bool absolute=true);
void apply();
protected:
void send_command(const uint8_t c);
void send_commands(const uint8_t *cc, const uint8_t n);
private:
const uint8_t _width { 128 };
const uint8_t _height { 32 };
const static int16_t _width { 128 };
const static int16_t _height { 32 };
uint8_t* _buffer { nullptr };
const uint8_t _comPin { 0x02 };
const uint8_t _i2caddr { 0x3C };
const uint8_t _contrast { 0x8F };