Skip to content

Commit

Permalink
Save a few data bytes by using initializer lists
Browse files Browse the repository at this point in the history
also fix a few warnings
  • Loading branch information
gsauthof committed Apr 30, 2022
1 parent 5a51294 commit e29b017
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
50 changes: 22 additions & 28 deletions Lcd/src/lcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ extern "C"
#include "spi.h"
#include "lcd.h"

struct LcdCmdData
{
uint8_t _cmd ;
uint8_t _size ;
uint8_t _data[16] ; // max size
} ;

Lcd::Lcd(Spi &spi, const uint8_t *font, uint8_t fontHeight, uint8_t fontWidth)
// LCD: B0 RS, B1 RST, B2 CS
Expand Down Expand Up @@ -47,43 +41,43 @@ void Lcd::setup()
// Display Inversion On
cmd(0x21) ;
// Frame Rate Control (In normal mode/ Full colors)
cmd({0xb1, 3, { 0x05, 0x3a, 0x3a } }) ;
cmd(0xb1, { 0x05, 0x3a, 0x3a } ) ;
// Frame Rate Control (In Idle mode/ 8-colors)
cmd({0xb2, 3, { 0x05, 0x3a, 0x3a } }) ;
cmd(0xb2, { 0x05, 0x3a, 0x3a } ) ;
// Frame Rate Control (In Partial mode/ full colors)
cmd({0xB3, 6, { 0x05, 0x3A, 0x3A, 0x05, 0x3A, 0x3A } }) ;
cmd(0xB3, { 0x05, 0x3A, 0x3A, 0x05, 0x3A, 0x3A } ) ;
// Display Inversion Control
cmd({0xB4, 1, { 0x03 } }) ;
cmd(0xB4, { 0x03 } ) ;
// Power Control 1
cmd({0xC0, 3, { 0x62, 0x02, 0x04} }) ;
cmd(0xC0, { 0x62, 0x02, 0x04} ) ;
// Power Control 2
cmd({0xC1, 1, { 0xC0 } }) ;
cmd(0xC1, { 0xC0 } ) ;
// Power Control 3 (in Normal mode/ Full colors)
cmd({0xC2, 2, { 0x0D, 0x00 } }) ;
cmd(0xC2, { 0x0D, 0x00 } ) ;
// Power Control 4 (in Idle mode/ 8-colors)
cmd({0xC3, 2, { 0x8D, 0x6A } }) ;
cmd(0xC3, { 0x8D, 0x6A } ) ;
// Power Control 5 (in Partial mode/ full-colors)
cmd({0xC4, 2, { 0x8D, 0xEE } }) ;
cmd(0xC4, { 0x8D, 0xEE } ) ;
// VCOM Control 1
cmd({0xC5, 1, { 0x0E } }) ;
cmd(0xC5, { 0x0E } ) ;
// Gamma (‘+’polarity) Correction Characteristics Setting
cmd({0xE0, 16, { 0x10, 0x0E, 0x02, 0x03, 0x0E, 0x07, 0x02, 0x07, 0x0A, 0x12, 0x27, 0x37, 0x00, 0x0D, 0x0E, 0x10 } }) ;
cmd(0xE0, { 0x10, 0x0E, 0x02, 0x03, 0x0E, 0x07, 0x02, 0x07, 0x0A, 0x12, 0x27, 0x37, 0x00, 0x0D, 0x0E, 0x10 } ) ;
// Gamma ‘-’polarity Correction Characteristics Setting
cmd({0xE1, 16, { 0x10, 0x0E, 0x03, 0x03, 0x0F, 0x06, 0x02, 0x08, 0x0A, 0x13, 0x26, 0x36, 0x00, 0x0D, 0x0E, 0x10 } }) ;
cmd(0xE1, { 0x10, 0x0E, 0x03, 0x03, 0x0F, 0x06, 0x02, 0x08, 0x0A, 0x13, 0x26, 0x36, 0x00, 0x0D, 0x0E, 0x10 } ) ;
// Interface Pixel Format
cmd({0x3A, 1, { 0x06 } }) ; // 18 bit/pixel
cmd(0x3A, { 0x06 } ) ; // 18 bit/pixel
// Memory Data Access Control
cmd({0x36, 1, { 0xa8 } }) ; // orientation 08, c8, 78, a8
cmd(0x36, { 0xa8 } ) ; // orientation 08, c8, 78, a8
// Display On
cmd(0x29) ;
}

void Lcd::fill(uint8_t x1, uint8_t x2, uint8_t y1, uint8_t y2, uint32_t rgb)
{
// Column Address Set
cmd({0x2a, 4, { 0x00, 1+x1, 0x00, 1+x2 } }) ; // x-offset 1
cmd(0x2a, { 0x00, uint8_t(1+x1), 0x00, uint8_t(1+x2) } ) ; // x-offset 1
// Row Address Set
cmd({0x2b, 4, { 0x00, 26+y1, 0x00, 26+y2 } }) ; // y-offset 26
cmd(0x2b, { 0x00, uint8_t(26+y1), 0x00, uint8_t(26+y2) } ) ; // y-offset 26
// Memory Write
cmd(0x2c) ;

Expand Down Expand Up @@ -119,8 +113,8 @@ void Lcd::putChar(char ch)
uint8_t x = (uint8_t)_txtPosX + 1 ; // x-offset 1
uint8_t y = (uint8_t)_txtPosY + 26 ; // y-offset 26
_txtPosX += _fontWidth ;
cmd({0x2a, 4, { 0x00, x+0, 0x00, x+_fontWidth -1 } }) ;
cmd({0x2b, 4, { 0x00, y+0, 0x00, y+_fontHeight-1 } }) ;
cmd(0x2a, { 0x00, uint8_t(x+0), 0x00, uint8_t(x+_fontWidth -1) } ) ;
cmd(0x2b, { 0x00, uint8_t(y+0), 0x00, uint8_t(y+_fontHeight-1) } ) ;

// Memory Write
cmd(0x2c) ;
Expand Down Expand Up @@ -225,17 +219,17 @@ void Lcd::data(uint8_t data)
csHi() ;
}

void Lcd::cmd(const LcdCmdData &cmdData)
void Lcd::cmd(uint8_t cmd, std::initializer_list<uint8_t> data)
{
csLo() ;

rsLo() ;
_spi.putByte(cmdData._cmd) ;
_spi.putByte(cmd) ;
while (_spi.isTransmit()) ;

rsHi() ;
for (uint8_t i = 0 ; i < cmdData._size ; ++i)
_spi.putByte(cmdData._data[i]) ;
for (uint8_t x : data)
_spi.putByte(x) ;
while (_spi.isTransmit()) ;

csHi() ;
Expand Down
5 changes: 2 additions & 3 deletions Lcd/src/lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#pragma once

#include <stdint.h>

class LcdCmdData ;
#include <initializer_list>


// LCD library for the Sitronix ST7735S controller
Expand Down Expand Up @@ -35,7 +34,7 @@ class Lcd

void cmd(uint8_t cmd) ;
void data(uint8_t data) ;
void cmd(const LcdCmdData &cmdData) ;
void cmd(uint8_t cmd, std::initializer_list<uint8_t> data) ;

Spi &_spi ;
rcu_periph_enum _rcuGpio ;
Expand Down

0 comments on commit e29b017

Please sign in to comment.