1
0
forked from me/IronOS

Cosmetic code style refactoring: add enum for screen orientation & define for OLED state (#1732)

* saveSettings: add comment for #endif, update var name to reflect its purpose regardless its one-time & temporal

* Settings.h: add enum for orientation mode

* settingsGUI.cpp: add markings for #endifs, add/remove extra new lines to propose better code read-ability in my humble vision from the side, didnt touch any functionality only cosmetic syntax

* settingsGUI.cpp: remove added-by-accident new line in the end of the file

* OLED.hpp: unify ifdef section, add markings for #endifs, add readable macros for ON/OFF OLED state instead of magic numbers

* OLED.cpp: add markings for #endifs, add readable macros for ON/OFF OLED state instead of magic numbers, trying unify common style for the whole file for better read-ability

* Settings.cpp: unify code style

* settingsGUI.cpp: revert true/false for setDisplayRotation

* OLED.cpp: unify comments style
This commit is contained in:
Ivan Zorin
2023-07-05 05:41:16 +03:00
committed by GitHub
parent 8c17a085f1
commit cbde61edb4
5 changed files with 152 additions and 109 deletions

View File

@@ -25,41 +25,43 @@ uint8_t OLED::displayOffset;
uint8_t OLED::screenBuffer[16 + (OLED_WIDTH * (OLED_HEIGHT / 8)) + 10]; // The data buffer
uint8_t OLED::secondFrameBuffer[16 + (OLED_WIDTH * (OLED_HEIGHT / 8)) + 10];
uint32_t OLED::displayChecksum;
/*Setup params for the OLED screen*/
/*http://www.displayfuture.com/Display/datasheet/controller/SSD1307.pdf*/
/*All commands are prefixed with 0x80*/
/*Data packets are prefixed with 0x40*/
/*
* Setup params for the OLED screen
* http://www.displayfuture.com/Display/datasheet/controller/SSD1307.pdf
* All commands are prefixed with 0x80
* Data packets are prefixed with 0x40
*/
I2C_CLASS::I2C_REG OLED_Setup_Array[] = {
/**/
{0x80, 0xAE, 0}, /*Display off*/
{0x80, OLED_DIVIDER, 0}, /*Set display clock divide ratio / osc freq*/
{0x80, 0x52, 0}, /*Divide ratios*/
{0x80, 0xA8, 0}, /*Set Multiplex Ratio*/
{0x80, OLED_HEIGHT - 1, 0}, /*Multiplex ratio adjusts how far down the matrix it scans*/
{0x80, 0xC0, 0}, /*Set COM Scan direction*/
{0x80, 0xD3, 0}, /*Set vertical Display offset*/
{0x80, 0x00, 0}, /*0 Offset*/
{0x80, 0x40, 0}, /*Set Display start line to 0*/
{0x80, OLED_OFF, 0}, /* Display off */
{0x80, OLED_DIVIDER, 0}, /* Set display clock divide ratio / osc freq */
{0x80, 0x52, 0}, /* Divide ratios */
{0x80, 0xA8, 0}, /* Set Multiplex Ratio */
{0x80, OLED_HEIGHT - 1, 0}, /* Multiplex ratio adjusts how far down the matrix it scans */
{0x80, 0xC0, 0}, /* Set COM Scan direction */
{0x80, 0xD3, 0}, /* Set vertical Display offset */
{0x80, 0x00, 0}, /* 0 Offset */
{0x80, 0x40, 0}, /* Set Display start line to 0 */
#ifdef OLED_SEGMENT_MAP_REVERSED
{0x80, 0xA1, 0}, /*Set Segment remap to normal*/
{0x80, 0xA1, 0}, /* Set Segment remap to normal */
#else
{0x80, 0xA0, 0}, /*Set Segment remap to normal*/
{0x80, 0xA0, 0}, /* Set Segment remap to normal */
#endif
{0x80, 0x8D, 0}, /*Charge Pump*/
{0x80, 0x14, 0}, /*Charge Pump settings*/
{0x80, 0xDA, 0}, /*Set VCOM Pins hardware config*/
{0x80, OLED_VCOM_LAYOUT, 0}, /*Combination 0x2 or 0x12 depending on OLED model*/
{0x80, 0x81, 0}, /*Brightness*/
{0x80, 0x00, 0}, /*^0*/
{0x80, 0xD9, 0}, /*Set pre-charge period*/
{0x80, 0xF1, 0}, /*Pre charge period*/
{0x80, 0xDB, 0}, /*Adjust VCOMH regulator ouput*/
{0x80, 0x30, 0}, /*VCOM level*/
{0x80, 0xA4, 0}, /*Enable the display GDDR*/
{0x80, 0XA6, 0}, /*Normal display*/
{0x80, 0x20, 0}, /*Memory Mode*/
{0x80, 0x00, 0}, /*Wrap memory*/
{0x80, 0xAF, 0}, /*Display on*/
{0x80, 0x8D, 0}, /* Charge Pump */
{0x80, 0x14, 0}, /* Charge Pump settings */
{0x80, 0xDA, 0}, /* Set VCOM Pins hardware config */
{0x80, OLED_VCOM_LAYOUT, 0}, /* Combination 0x2 or 0x12 depending on OLED model */
{0x80, 0x81, 0}, /* Brightness */
{0x80, 0x00, 0}, /* ^0 */
{0x80, 0xD9, 0}, /* Set pre-charge period */
{0x80, 0xF1, 0}, /* Pre charge period */
{0x80, 0xDB, 0}, /* Adjust VCOMH regulator ouput */
{0x80, 0x30, 0}, /* VCOM level */
{0x80, 0xA4, 0}, /* Enable the display GDDR */
{0x80, 0xA6, 0}, /* Normal display */
{0x80, 0x20, 0}, /* Memory Mode */
{0x80, 0x00, 0}, /* Wrap memory */
{0x80, OLED_ON, 0}, /* Display on */
};
// Setup based on the SSD1307 and modified for the SSD1306
@@ -115,6 +117,7 @@ static uint16_t lerp(uint16_t a, uint16_t b, uint16_t t) { return a + t * (b - a
void OLED::initialize() {
cursor_x = cursor_y = 0;
inLeftHandedMode = false;
#ifdef OLED_128x32
stripPointers[0] = &screenBuffer[FRAMEBUFFER_START];
stripPointers[1] = &screenBuffer[FRAMEBUFFER_START + OLED_WIDTH];
@@ -124,7 +127,8 @@ void OLED::initialize() {
#else
stripPointers[0] = &screenBuffer[FRAMEBUFFER_START];
stripPointers[1] = &screenBuffer[FRAMEBUFFER_START + OLED_WIDTH];
#endif
#endif /* OLED_128x32 */
displayOffset = 0;
memcpy(&screenBuffer[0], &REFRESH_COMMANDS[0], sizeof(REFRESH_COMMANDS));
memcpy(&secondFrameBuffer[0], &REFRESH_COMMANDS[0], sizeof(REFRESH_COMMANDS));
@@ -140,6 +144,7 @@ void OLED::initialize() {
setDisplayState(DisplayState::ON);
initDone = true;
}
void OLED::setFramebuffer(uint8_t *buffer) {
stripPointers[0] = &buffer[FRAMEBUFFER_START];
stripPointers[1] = &buffer[FRAMEBUFFER_START + OLED_WIDTH];
@@ -147,7 +152,7 @@ void OLED::setFramebuffer(uint8_t *buffer) {
#ifdef OLED_128x32
stripPointers[2] = &buffer[FRAMEBUFFER_START + (2 * OLED_WIDTH)];
stripPointers[3] = &buffer[FRAMEBUFFER_START + (3 * OLED_WIDTH)];
#endif
#endif /* OLED_128x32 */
}
/*
@@ -156,7 +161,6 @@ void OLED::setFramebuffer(uint8_t *buffer) {
* Precursor is the command char that is used to select the table.
*/
void OLED::drawChar(const uint16_t charCode, const FontStyle fontStyle) {
const uint8_t *currentFont;
static uint8_t fontWidth, fontHeight;
uint16_t index;
@@ -243,7 +247,7 @@ void OLED::maskScrollIndicatorOnOLED() {
#ifdef OLED_128x32
0x00,
0x00,
#endif
#endif /* OLED_128x32 */
// Clears two 8px strips
0x00,
0x00,
@@ -266,7 +270,7 @@ void OLED::transitionSecondaryFramebuffer(bool forwardNavigation) {
#ifdef OLED_128x32
stripBackPointers[2] = &secondFrameBuffer[OLED_WIDTH * 2];
stripBackPointers[3] = &secondFrameBuffer[OLED_WIDTH * 3];
#endif
#endif /* OLED_128x32 */
TickType_t totalDuration = TICKS_100MS * 5; // 500ms
TickType_t duration = 0;
@@ -297,17 +301,19 @@ void OLED::transitionSecondaryFramebuffer(bool forwardNavigation) {
memmove(&stripPointers[0][oldStart], &stripPointers[0][oldPrevious], OLED_WIDTH - progress);
memmove(&stripPointers[1][oldStart], &stripPointers[1][oldPrevious], OLED_WIDTH - progress);
#ifdef OLED_128x32
memmove(&stripPointers[2][oldStart], &stripPointers[2][oldPrevious], OLED_WIDTH - progress);
memmove(&stripPointers[3][oldStart], &stripPointers[3][oldPrevious], OLED_WIDTH - progress);
#endif
#endif /* OLED_128x32 */
memmove(&stripPointers[0][newStart], &stripBackPointers[0][newEnd], progress);
memmove(&stripPointers[1][newStart], &stripBackPointers[1][newEnd], progress);
#ifdef OLED_128x32
memmove(&stripPointers[2][newStart], &stripBackPointers[2][newEnd], progress);
memmove(&stripPointers[3][newStart], &stripBackPointers[3][newEnd], progress);
#endif
#endif /* OLED_128x32 */
refresh(); // Now refresh to write out the contents to the new page
vTaskDelayUntil(&startDraw, TICKS_100MS / 7);
@@ -367,7 +373,7 @@ void OLED::transitionScrollDown() {
secondFrameBuffer[firstStripPos] = (secondFrameBuffer[firstStripPos] >> 1) | ((secondFrameBuffer[secondStripPos] & 0x01) << 7);
// Finally on the bottom row; we shuffle it up ready
secondFrameBuffer[secondStripPos] >>= 1;
#endif
#endif /* OLED_128x32 */
}
if (getButtonState() != BUTTON_NONE) {
// Exit early, but have to transition whole buffer
@@ -383,7 +389,7 @@ void OLED::transitionScrollDown() {
void OLED::setRotation(bool leftHanded) {
#ifdef OLED_FLIP
leftHanded = !leftHanded;
#endif
#endif /* OLED_FLIP */
if (inLeftHandedMode == leftHanded) {
return;
}
@@ -399,7 +405,7 @@ void OLED::setRotation(bool leftHanded) {
} else {
OLED_Setup_Array[9].val = 0xA0;
}
#endif
#endif /* OLED_SEGMENT_MAP_REVERSED */
// send command struct again with changes
if (leftHanded) {
OLED_Setup_Array[5].val = 0xC8; // c1?
@@ -486,6 +492,7 @@ inline void stripLeaderZeros(char *buffer, uint8_t places) {
}
}
}
void OLED::drawHex(uint32_t x, FontStyle fontStyle, uint8_t digits) {
// print number to hex
for (uint_fast8_t i = 0; i < digits; i++) {
@@ -493,6 +500,7 @@ void OLED::drawHex(uint32_t x, FontStyle fontStyle, uint8_t digits) {
drawChar(value + 2, fontStyle);
}
}
// maximum places is 5
void OLED::printNumber(uint16_t number, uint8_t places, FontStyle fontStyle, bool noLeaderZeros) {
char buffer[7] = {0};

View File

@@ -36,7 +36,9 @@ extern "C" {
#endif
#define DEVICEADDR_OLED (0x3c << 1)
#ifdef OLED_128x32
#define OLED_WIDTH 128
#define OLED_HEIGHT 32
#define OLED_GRAM_START 0x00 // Should be 0x00 when we have full width
@@ -44,22 +46,28 @@ extern "C" {
#define OLED_GRAM_START_FLIP 0
#define OLED_GRAM_END_FLIP 0x7F
#define OLED_VCOM_LAYOUT 0x12
#define OLED_VCOM_LAYOUT 0x12
#define OLED_SEGMENT_MAP_REVERSED
#define OLED_DIVIDER 0xD3
#else
#define OLED_WIDTH 96
#define OLED_HEIGHT 16
#define OLED_VCOM_LAYOUT 0x02
#define OLED_DIVIDER 0xD3
#else
#define OLED_WIDTH 96
#define OLED_HEIGHT 16
#define OLED_GRAM_START 0x20
#define OLED_GRAM_END 0x7F
#define OLED_GRAM_START_FLIP 0
#define OLED_GRAM_END_FLIP 95
#define OLED_DIVIDER 0xD5
#define OLED_SEGMENT_MAP 0xA0
#endif
#define OLED_VCOM_LAYOUT 0x02
#define OLED_SEGMENT_MAP 0xA0
#define OLED_DIVIDER 0xD5
#endif /* OLED_128x32 */
#define OLED_ON 0xAF
#define OLED_OFF 0xAE
#define FRAMEBUFFER_START 17
enum class FontStyle {
@@ -88,7 +96,7 @@ public:
static void setDisplayState(DisplayState state) {
if (state != displayState) {
displayState = state;
screenBuffer[1] = (state == ON) ? 0xAF : 0xAE;
screenBuffer[1] = (state == ON) ? OLED_ON : OLED_OFF;
// Dump the screen state change out _now_
I2C_CLASS::Transmit(DEVICEADDR_OLED, screenBuffer, FRAMEBUFFER_START - 1);
osDelay(TICKS_10MS);
@@ -102,7 +110,7 @@ public:
return !inLeftHandedMode;
#else
return inLeftHandedMode;
#endif
#endif /* OLED_FLIP */
}
static void setBrightness(uint8_t contrast);
static void setInverseDisplay(bool inverted);