Skip to content

Commit

Permalink
Badger2040: Enforce minimum update blocking time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Mar 25, 2022
1 parent f332dcd commit b1fd893
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
21 changes: 21 additions & 0 deletions drivers/uc8151/uc8151.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ namespace pimoroni {
void UC8151::setup(uint8_t speed) {
reset();

_update_speed = speed;

if(speed == 0) {
command(PSR, {
RES_128x296 | LUT_OTP | FORMAT_BW | SHIFT_RIGHT | BOOSTER_ON | RESET_NONE
Expand Down Expand Up @@ -468,6 +470,25 @@ namespace pimoroni {
setup(speed);
}

uint8_t UC8151::update_speed() {
return _update_speed;
}

uint32_t UC8151::update_time() {
switch(_update_speed) {
case 0:
return 5500;
case 1:
return 2600;
case 2:
return 1000;
case 3:
return 300;
default:
return 5500;
}
}

void UC8151::partial_update(int x, int y, int w, int h, bool blocking) {
// y is given in columns ("banks"), which are groups of 8 horiontal pixels
// x is given in pixels
Expand Down
4 changes: 4 additions & 0 deletions drivers/uc8151/uc8151.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ namespace pimoroni {

bool inverted = false;

uint8_t _update_speed = 0;

public:
UC8151(uint16_t width, uint16_t height) :
width(width), height(height), frame_buffer(new uint8_t[width * height / 8]) {
Expand Down Expand Up @@ -199,6 +201,8 @@ namespace pimoroni {

void invert(bool invert);
void update_speed(uint8_t speed);
uint8_t update_speed();
uint32_t update_time();
void update(bool blocking = true);
void partial_update(int x, int y, int w, int h, bool blocking = true);
void off();
Expand Down
4 changes: 4 additions & 0 deletions libraries/badger2040/badger2040.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ namespace pimoroni {
uc8151.update_speed(speed);
}

uint32_t Badger2040::update_time() {
return uc8151.update_time();
}

void Badger2040::partial_update(int x, int y, int w, int h, bool blocking) {
uc8151.partial_update(x, y, w, h, blocking);
}
Expand Down
1 change: 1 addition & 0 deletions libraries/badger2040/badger2040.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace pimoroni {
void update(bool blocking=false);
void partial_update(int x, int y, int w, int h, bool blocking=false);
void update_speed(uint8_t speed);
uint32_t update_time();
void halt();
void sleep();
bool is_busy();
Expand Down
10 changes: 8 additions & 2 deletions micropython/modules/badger2040/badger2040.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ MICROPY_EVENT_POLL_HOOK
#endif
}

absolute_time_t t_end = make_timeout_time_ms(self->badger2040->update_time());
self->badger2040->update(false);

while(self->badger2040->is_busy()) {
// Ensure blocking for the minimum amount of time
// in cases where "is_busy" is unreliable.
while(self->badger2040->is_busy() || absolute_time_diff_us(t_end, get_absolute_time()) > 0) {
#ifdef MICROPY_EVENT_POLL_HOOK
MICROPY_EVENT_POLL_HOOK
#endif
Expand Down Expand Up @@ -190,9 +193,12 @@ MICROPY_EVENT_POLL_HOOK
#endif
}

absolute_time_t t_end = make_timeout_time_ms(self->badger2040->update_time());
self->badger2040->partial_update(x, y, w, h);

while(self->badger2040->is_busy()) {
// Ensure blocking for the minimum amount of time
// in cases where "is_busy" is unreliable.
while(self->badger2040->is_busy() || absolute_time_diff_us(t_end, get_absolute_time()) > 0) {
#ifdef MICROPY_EVENT_POLL_HOOK
MICROPY_EVENT_POLL_HOOK
#endif
Expand Down

0 comments on commit b1fd893

Please sign in to comment.