Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 10 commits into from
Jul 5, 2023
88 changes: 48 additions & 40 deletions source/Core/Drivers/OLED.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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];
Expand All @@ -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));
Expand All @@ -140,14 +144,15 @@ 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];

#ifdef OLED_128x32
stripPointers[2] = &buffer[FRAMEBUFFER_START + (2 * OLED_WIDTH)];
stripPointers[3] = &buffer[FRAMEBUFFER_START + (3 * OLED_WIDTH)];
#endif
#endif /* OLED_128x32 */
}

/*
Expand All @@ -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;
Expand Down Expand Up @@ -243,7 +247,7 @@ void OLED::maskScrollIndicatorOnOLED() {
#ifdef OLED_128x32
0x00,
0x00,
#endif
#endif /* OLED_128x32 */
// Clears two 8px strips
0x00,
0x00,
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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?
Expand Down Expand Up @@ -486,13 +492,15 @@ 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++) {
uint16_t value = (x >> (4 * (7 - i))) & 0b1111;
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};
Expand Down
26 changes: 17 additions & 9 deletions source/Core/Drivers/OLED.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,38 @@ 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
#define OLED_GRAM_END 0x7F // Should be 0x7F when we have full width
#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
#define OLED_DIVIDER 0xD3

#else
#define OLED_WIDTH 96
#define OLED_HEIGHT 16
#define OLED_VCOM_LAYOUT 0x02

#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_VCOM_LAYOUT 0x02
#define OLED_SEGMENT_MAP 0xA0
#define OLED_DIVIDER 0xD5

#endif /* OLED_128x32 */

#define OLED_ON 0xAF
#define OLED_OFF 0xAE

#endif
#define FRAMEBUFFER_START 17

enum class FontStyle {
Expand Down Expand Up @@ -88,7 +96,7 @@ class OLED {
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);
Expand All @@ -102,7 +110,7 @@ class OLED {
return !inLeftHandedMode;
#else
return inLeftHandedMode;
#endif
#endif /* OLED_FLIP */
}
static void setBrightness(uint8_t contrast);
static void setInverseDisplay(bool inverted);
Expand Down
6 changes: 6 additions & 0 deletions source/Core/Inc/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ typedef enum {
ZERO = 3, // Power on only (No heat Mode)
} autoStartMode_t;

typedef enum {
RIGHT = 0, // Right-hand screen orientation
LEFT = 1, // Left-hand screen orientation
AUTO = 2, // Automatic screen orientation based on accel.data if presented
} orientationMode_t;

// Settings wide operations
void saveSettings();
bool loadSettings();
Expand Down
15 changes: 10 additions & 5 deletions source/Core/Src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bool sanitiseSettings();
#define QC_VOLTAGE_MAX 220
#else
#define QC_VOLTAGE_MAX 140
#endif
#endif /* POW_QC_20V */

/*
* This struct must be a multiple of 2 bytes as it is saved / restored from
Expand All @@ -36,6 +36,7 @@ typedef struct {

//~1024 is common programming size, setting threshold to be lower so we have warning
static_assert(sizeof(systemSettingsType) < 512);

// char (*__kaboom)[sizeof(systemSettingsType)] = 1; // Uncomment to print size at compile time
volatile systemSettingsType systemSettings;

Expand Down Expand Up @@ -108,9 +109,9 @@ static_assert((sizeof(settingsConstants) / sizeof(SettingConstants)) == ((int)Se
void saveSettings() {
#ifdef CANT_DIRECT_READ_SETTINGS
// For these devices flash is not 1:1 mapped, so need to read into staging buffer
systemSettingsType temp;
flash_read_buffer((uint8_t *)&temp, sizeof(systemSettingsType));
if (memcmp((void *)&temp, (void *)&systemSettings, sizeof(systemSettingsType))) {
systemSettingsType settings;
Ralim marked this conversation as resolved.
Show resolved Hide resolved
flash_read_buffer((uint8_t *)&settings, sizeof(systemSettingsType));
if (memcmp((void *)&settings, (void *)&systemSettings, sizeof(systemSettingsType))) {
flash_save_buffer((uint8_t *)&systemSettings, sizeof(systemSettingsType));
}

Expand All @@ -119,7 +120,7 @@ void saveSettings() {
flash_save_buffer((uint8_t *)&systemSettings, sizeof(systemSettingsType));
}

#endif
#endif /* CANT_DIRECT_READ_SETTINGS */
}

bool loadSettings() {
Expand Down Expand Up @@ -161,6 +162,7 @@ bool sanitiseSettings() {
}
return dirty;
}

void resetSettings() {
memset((void *)&systemSettings, 0xFF, sizeof(systemSettingsType));
sanitiseSettings();
Expand All @@ -179,6 +181,7 @@ void setSettingValue(const enum SettingsOptions option, const uint16_t newValue)
}
systemSettings.settingsValues[(int)option] = constrainedValue;
}

// Lookup wrapper for ease of use (with typing)
uint16_t getSettingValue(const enum SettingsOptions option) { return systemSettings.settingsValues[(int)option]; }

Expand Down Expand Up @@ -217,6 +220,7 @@ bool prevSettingValue(const enum SettingsOptions option) {
// Return if we are at the min
return constants.min == systemSettings.settingsValues[(int)option];
}

uint16_t lookupHallEffectThreshold() {
// Return the threshold above which the hall effect sensor is "activated"
// We want this to be roughly exponentially mapped from 0-1000
Expand Down Expand Up @@ -245,6 +249,7 @@ uint16_t lookupHallEffectThreshold() {
return 0; // Off
}
}

// Lookup function for cutoff setting -> X10 voltage
/*
* 0=DC
Expand Down
Loading