optimize updatebycolumn function to only flip necessary dots

This commit is contained in:
interfisch 2023-02-11 18:26:52 +01:00
parent 9ff297bd6c
commit a9711fd46d
4 changed files with 46 additions and 46 deletions

View File

@ -10,8 +10,6 @@
#define ROWS 16 #define ROWS 16
#define UPDATE_INTERVAL 5 //TODO: remove this
enum UpdateReturn { enum UpdateReturn {
wait, wait,
finished, finished,
@ -37,8 +35,9 @@ private:
unsigned long lastUpdateMillis; //time when last dots where started flipping unsigned long lastUpdateMillis; //time when last dots where started flipping
unsigned long updateInterval; unsigned long updateDelay;
void serialPrintInt(uint16_t source);

View File

@ -61,6 +61,7 @@ void Flipdot::selectColumnSet(uint8_t selcolumn) {
selectColumn(selcolumn, false); selectColumn(selcolumn, false);
} }
void Flipdot::selectColumn(uint8_t selcolumn, bool clear) { void Flipdot::selectColumn(uint8_t selcolumn, bool clear) {
//set shift registers for columns to select one column to positive voltage
uint8_t sc_bit=3-(selcolumn%4); //each two shift registers control four columns uint8_t sc_bit=3-(selcolumn%4); //each two shift registers control four columns
uint8_t sc_byte=selcolumn/4; uint8_t sc_byte=selcolumn/4;

View File

@ -8,13 +8,19 @@ Image::Image()
} }
void Image::serialPrintInt(uint16_t source)
{
for(uint8_t i = 0; i < 16; i++)
Serial.print((source & (1 << i)) > 0);
}
void Image::init() void Image::init()
{ {
flipdot.init(); //sets pin modes etc. flipdot.init(); //sets pin modes etc.
flag_updating=false; flag_updating=false;
update_counter=0; update_counter=0;
updateInterval=30; updateDelay=5;
} }
uint8_t Image::getW() { uint8_t Image::getW() {
@ -63,34 +69,53 @@ UpdateReturn Image::updateByColumn(bool direction, bool clearFirst, bool optimiz
return nochange; //finished return nochange; //finished
} }
if (millis()-lastUpdateMillis<updateInterval){ //too early if (millis()-lastUpdateMillis<updateDelay){ //too early
return wait; //not finished return wait; //not finished
} }
unsigned long functionStartMillis=millis();
lastUpdateMillis=millis();
uint8_t x=update_counter/2; uint8_t x=update_counter/2;
bool setDot= (update_counter%2==1);
if (update_counter%2==0) { //even counter numbers are for clearing if (!setDot) { //even counter numbers are for clearing
flipdot.setRow(0); uint16_t _colClear=frontBuffer[x]& ~backBuffer[x]; //Front & ~Back = Which bits have to be cleared
flipdot.selectColumnClear(x);
if (!flipdot.clearSelectedColumn()) { if (!optimizeClear || _colClear>0) { //at least on dot has to be cleared in this column
Serial.println("Error clearing column!"); flipdot.setRow(0);
flipdot.selectColumnClear(x);
if (!flipdot.clearSelectedColumn()) {
Serial.println("Error clearing column!");
}
lastUpdateMillis=millis();
frontBuffer[x]=0;
} }
frontBuffer[x]=0;
}else{ //odd counter numbers are for setting
flipdot.selectColumnSet(x); //lower column number is on the left
flipdot.setRow(backBuffer[x]);
flipdot.setSelectedDot();
frontBuffer[x]=backBuffer[x]; //flip current column in buffer }else{ //odd counter numbers are for setting
uint16_t _colSet=backBuffer[x]& ~frontBuffer[x];
if (!optimizeSet || _colSet>0) { //at least on dot has to be set in this column (if optimize is enabled)
flipdot.selectColumnSet(x); //lower column number is on the left
if (!optimizeSet) {
flipdot.setRow(backBuffer[x]); //flip all set dots in this column
}else{
flipdot.setRow(_colSet); //flip only currently not set dots in this column that have to be set
}
if (!flipdot.setSelectedDot()) {
Serial.println("Error setting dots!");
}
lastUpdateMillis=millis();
frontBuffer[x]=backBuffer[x]; //flip current column in buffer
}
} }
update_counter++; //next column update_counter++; //next column
unsigned long _duration=millis()-lastUpdateMillis; //how long it took to shift data and flip dots in this update unsigned long _duration=millis()-functionStartMillis; //how long it took to shift data and flip dots in this update
updateDuration=max(updateDuration,_duration); //store maximum value updateDuration=max(updateDuration,_duration); //store maximum value

View File

@ -33,7 +33,7 @@ void loop() {
//flip.setBuffer_solid(color); //flip.setBuffer_solid(color);
//color=1-color; //color=1-color;
uint8_t _randomvalue=random(256); uint8_t _randomvalue=random(64);
Serial.print("set buffer random. "); Serial.print("set buffer random. ");
Serial.println(_randomvalue); Serial.println(_randomvalue);
flip.setBuffer_random(_randomvalue); flip.setBuffer_random(_randomvalue);
@ -42,7 +42,7 @@ void loop() {
} }
UpdateReturn result=flip.updateByColumn(0,0,0,0); //0=not finished, 1=finished UpdateReturn result=flip.updateByColumn(0,0,true,true); //0=not finished, 1=finished
if (result == finished) //just finished if (result == finished) //just finished
{ {
unsigned long duration=millis()-last_change; unsigned long duration=millis()-last_change;
@ -50,30 +50,5 @@ void loop() {
Serial.print("Update max took "); Serial.print(flip.updateDuration); Serial.println(" ms"); Serial.print("Update max took "); Serial.print(flip.updateDuration); Serial.println(" ms");
flip.updateDuration=0; //reset flip.updateDuration=0; //reset
} }
/*
if (loopmillis > last_update + UPDATE_INTERVAL)
{
Serial.print("UpdateByColumn ");
bool result=flip.updateByColumn(0,0,0,0);
Serial.println(result);
last_update=loopmillis;
}
*/
/*
if (loopmillis > last_update + UPDATE_INTERVAL)
{
flip.loop_drawClearTest();
//loop_testDots();
last_update=loopmillis;
}
*/
} }