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

With fixed overlay and division by zero fixed #358

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion src/OLEDDisplayUi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ OLEDDisplayUi::OLEDDisplayUi(OLEDDisplay *display) {
frameCount = 0;
nextFrameNumber = -1;
overlayCount = 0;
fixedOverlayFramesCount =0 ;
fixedOverlayFrames = nullptr;
fixedOverlayCallback = nullptr;
indicatorDrawState = 1;
loadingDrawFunction = LoadingDrawDefault;
updateInterval = 33;
Expand All @@ -68,6 +71,10 @@ OLEDDisplayUi::OLEDDisplayUi(OLEDDisplay *display) {
setTimePerTransition(500);
}

OLEDDisplayUi::~OLEDDisplayUi() {
if(fixedOverlayFramesCount>0) free(fixedOverlayFrames);
}

void OLEDDisplayUi::init() {
this->display->init();
}
Expand Down Expand Up @@ -146,11 +153,49 @@ void OLEDDisplayUi::setFrames(FrameCallback* frameFunctions, uint8_t frameCount)
}

// -/----- Overlays ------\-
void OLEDDisplayUi::setOverlays(OverlayCallback* overlayFunctions, uint8_t overlayCount){
void OLEDDisplayUi::setOverlays(OverlayCallback* overlayFunctions, uint8_t overlayCount) {
this->overlayFunctions = overlayFunctions;
this->overlayCount = overlayCount;
}

void OLEDDisplayUi::setFixedOverlay(FixedOverlayCallback fixedOverlayCallback) {
this->fixedOverlayCallback = fixedOverlayCallback;
}

void OLEDDisplayUi::setFixedOverlayFrames(const uint8_t* fixedOverlayFramesList, int fixedOverlayFramesCount) {
this->fixedOverlayFrames = (uint8_t*)calloc(fixedOverlayFramesCount, sizeof(int8_t));
if(this->fixedOverlayFrames != nullptr) {
this->fixedOverlayFramesCount = fixedOverlayFramesCount;
}
memcpy((void*) this->fixedOverlayFrames, (void*) fixedOverlayFramesList, fixedOverlayFramesCount * sizeof(int8_t));
}

void OLEDDisplayUi::setFixedOverlayFrames(const FrameCallback* fixedOverlayFramesList, int fixedOverlayFramesCount) {
this->fixedOverlayFrames = (uint8_t*)calloc(fixedOverlayFramesCount, sizeof(int8_t));
if(this->fixedOverlayFrames != nullptr) {
this->fixedOverlayFramesCount = fixedOverlayFramesCount;
uint8_t foundFrameNo=0;
for(uint8_t noOverlayFrameNo = 0; noOverlayFrameNo < fixedOverlayFramesCount; noOverlayFrameNo++) {
for(uint8_t frameNo = 0; frameNo < frameCount; frameNo++) {
if(fixedOverlayFramesList[noOverlayFrameNo] == frameFunctions[frameNo])
{
this->fixedOverlayFrames[foundFrameNo++] = frameNo;
break;
}
}
}
if(foundFrameNo != fixedOverlayFramesCount) {
this->fixedOverlayFramesCount = 0;
this->fixedOverlayFrames = (uint8_t*)realloc(this->fixedOverlayFrames, foundFrameNo * sizeof(int8_t));
{
if(this->fixedOverlayFrames != nullptr) {
this->fixedOverlayFramesCount = foundFrameNo;
}
}
}
}
}

// -/----- Loading Process -----\-

void OLEDDisplayUi::setLoadingDrawFunction(LoadingDrawFunction loadingDrawFunction) {
Expand Down Expand Up @@ -297,6 +342,36 @@ void OLEDDisplayUi::resetState() {
this->state.isIndicatorDrawn = true;
}

void OLEDDisplayUi::drawFixedOverlay(int16_t x, int16_t y, int16_t x1, int16_t y1){
if(fixedOverlayCallback == nullptr) return;

bool currentFrameHasFixedOverlay = false;
bool nextFrameHasFixedOverlay = false;

for(int i = 0; i < fixedOverlayFramesCount; i++) {
if(this->state.currentFrame == fixedOverlayFrames[i]) {
currentFrameHasFixedOverlay = true;
}
if(this->getNextFrameNumber() == fixedOverlayFrames[i]) {
nextFrameHasFixedOverlay = true;
}
}
if(!currentFrameHasFixedOverlay && !nextFrameHasFixedOverlay) return;

if((currentFrameHasFixedOverlay && nextFrameHasFixedOverlay) || (this->state.frameState==FIXED && currentFrameHasFixedOverlay)) {
(this->fixedOverlayCallback)(this->display, &this->state, 0, 0);
return;
}
if(currentFrameHasFixedOverlay && this->state.frameState!=FIXED) {
(this->fixedOverlayCallback)(this->display, &this->state, x, y);
return;
}
if(nextFrameHasFixedOverlay && this->state.frameState!=FIXED) {
(this->fixedOverlayCallback)(this->display, &this->state, x1, y1);
return;
}
}

void OLEDDisplayUi::drawFrame(){
switch (this->state.frameState){
case IN_TRANSITION: {
Expand Down Expand Up @@ -348,6 +423,9 @@ void OLEDDisplayUi::drawFrame(){
this->enableIndicator();
(this->frameFunctions[this->getNextFrameNumber()])(this->display, &this->state, x1, y1);

// Draw fixed overlay
this->drawFixedOverlay(x, y, x1, y1);

// Build up the indicatorDrawState
if (drawnCurrentFrame && !this->state.isIndicatorDrawn) {
// Drawn now but not next
Expand All @@ -372,6 +450,7 @@ void OLEDDisplayUi::drawFrame(){
this->indicatorDrawState = 0;
this->enableIndicator();
(this->frameFunctions[this->state.currentFrame])(this->display, &this->state, 0, 0);
this->drawFixedOverlay(0, 0, 0, 0);
break;
}
}
Expand Down Expand Up @@ -467,5 +546,6 @@ void OLEDDisplayUi::drawOverlays() {

uint8_t OLEDDisplayUi::getNextFrameNumber(){
if (this->nextFrameNumber != -1) return this->nextFrameNumber;
if(this->frameCount == 0) return(-1);
return (this->state.currentFrame + this->frameCount + this->state.frameTransitionDirection) % this->frameCount;
}
25 changes: 24 additions & 1 deletion src/OLEDDisplayUi.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

#include "OLEDDisplay.h"

#include <stdlib.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you need this one for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, there’s a war in Ukraine and my Ukrainian home has been destroyed by russian nazi so I cannot check. I suppose, vscode added it automatically for some call. Could you check it please. It’s easy - just comment it out and try to compile

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi!
Maybe we could return to this, could we? What should I do? Add an example and that's all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for coming back to this. If you could address my feedback I'd certainly appreciate it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gimme some time pls, I cannot get arduion working yet. looks like they changed something


//#define DEBUG_OLEDDISPLAYUI(...) Serial.printf( __VA_ARGS__ )

#ifndef DEBUG_OLEDDISPLAYUI
Expand Down Expand Up @@ -108,6 +110,7 @@ struct LoadingStage {

typedef void (*FrameCallback)(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
typedef void (*OverlayCallback)(OLEDDisplay *display, OLEDDisplayUiState* state);
typedef void (*FixedOverlayCallback)(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
typedef void (*LoadingDrawFunction)(OLEDDisplay *display, LoadingStage* stage, uint8_t progress);

class OLEDDisplayUi {
Expand Down Expand Up @@ -143,6 +146,10 @@ class OLEDDisplayUi {
OverlayCallback* overlayFunctions;
uint8_t overlayCount;

uint8_t* fixedOverlayFrames;
uint8_t fixedOverlayFramesCount;
FixedOverlayCallback fixedOverlayCallback;

// Will the Indicator be drawn
// 3 Not drawn in both frames
// 2 Drawn this frame but not next
Expand All @@ -166,12 +173,14 @@ class OLEDDisplayUi {
void drawIndicator();
void drawFrame();
void drawOverlays();
void drawFixedOverlay(int16_t x, int16_t y, int16_t x1, int16_t y1);
void tick();
void resetState();

public:

OLEDDisplayUi(OLEDDisplay *display);
~OLEDDisplayUi();

/**
* Initialise the display
Expand Down Expand Up @@ -273,10 +282,24 @@ class OLEDDisplayUi {
// Overlay

/**
* Add overlays drawing functions that are draw independent of the Frames
* Add fixed overlay drawing functions that are draw independent of the Frames
*/
void setOverlays(OverlayCallback* overlayFunctions, uint8_t overlayCount);

/**
* Set the list of frames with no fixed overlay
*/
void setFixedOverlayFrames(const uint8_t* fixedOverlayFramesList, int fixedOverlayFramesCount);

/**
* Set the list of frames with no fixed overlay
*/
void setFixedOverlayFrames(const FrameCallback* fixedOverlayFramesList, int fixedOverlayFramesCount);

/**
* Set fixed overlay drawing function that will be drawn on fixed overlay frames only
*/
void setFixedOverlay(FixedOverlayCallback fixedOverlayCallback);

// Loading animation
/**
Expand Down
Loading