2023-02-08 19:17:58 +00:00
# include "image.h"
Image : : Image ( )
{
}
2023-02-11 17:26:52 +00:00
void Image : : serialPrintInt ( uint16_t source )
{
for ( uint8_t i = 0 ; i < 16 ; i + + )
Serial . print ( ( source & ( 1 < < i ) ) > 0 ) ;
}
2023-02-08 19:17:58 +00:00
void Image : : init ( )
{
flipdot . init ( ) ; //sets pin modes etc.
2023-02-08 20:00:15 +00:00
flag_updating = false ;
update_counter = 0 ;
2023-02-11 17:26:52 +00:00
updateDelay = 5 ;
2023-02-08 19:17:58 +00:00
}
uint8_t Image : : getW ( ) {
return COLUMNS ;
}
uint8_t Image : : getH ( ) {
return ROWS ;
}
2023-02-08 20:00:15 +00:00
void Image : : setBuffer_solid ( bool set )
{
for ( uint8_t x = 0 ; x < getW ( ) ; x + + ) {
if ( set ) {
backBuffer [ x ] = 0xffff ; //all white
} else {
backBuffer [ x ] = 0x0 ; //all black
}
}
flag_updating = true ; //make update run
}
2023-02-08 20:29:10 +00:00
void Image : : setBuffer_random ( uint8_t randomness )
2023-02-08 20:00:15 +00:00
{
2023-02-08 20:29:10 +00:00
for ( uint8_t x = 0 ; x < getW ( ) ; x + + ) {
uint16_t randomnumber = 0 ;
for ( uint8_t y = 0 ; y < getH ( ) ; y + + ) {
if ( random ( 256 ) < randomness ) { //the higher the parameter the more white
randomnumber + = pow ( 2 , y ) ;
}
}
backBuffer [ x ] = randomnumber ;
//backBuffer[x]=(uint16_t)random(0x10000);
2023-02-08 20:00:15 +00:00
}
2023-02-08 20:29:10 +00:00
flag_updating = true ; //make update run
}
2023-02-11 16:31:49 +00:00
UpdateReturn Image : : updateByColumn ( bool direction , bool clearFirst , bool optimizeClear , bool optimizeSet )
2023-02-08 20:29:10 +00:00
{
2023-02-08 20:00:15 +00:00
if ( ! flag_updating ) {
2023-02-11 16:31:49 +00:00
return nochange ; //finished
2023-02-08 20:00:15 +00:00
}
2023-02-11 17:26:52 +00:00
if ( millis ( ) - lastUpdateMillis < updateDelay ) { //too early
2023-02-11 16:31:49 +00:00
return wait ; //not finished
2023-02-08 20:29:10 +00:00
}
2023-02-11 17:26:52 +00:00
unsigned long functionStartMillis = millis ( ) ;
2023-02-08 20:29:10 +00:00
2023-02-11 17:26:52 +00:00
2023-02-08 20:00:15 +00:00
uint8_t x = update_counter / 2 ;
2023-02-11 17:26:52 +00:00
bool setDot = ( update_counter % 2 = = 1 ) ;
2023-02-08 20:00:15 +00:00
2023-02-11 17:26:52 +00:00
if ( ! setDot ) { //even counter numbers are for clearing
uint16_t _colClear = frontBuffer [ x ] & ~ backBuffer [ x ] ; //Front & ~Back = Which bits have to be cleared
if ( ! optimizeClear | | _colClear > 0 ) { //at least on dot has to be cleared in this column
flipdot . setRow ( 0 ) ;
flipdot . selectColumnClear ( x ) ;
if ( ! flipdot . clearSelectedColumn ( ) ) {
Serial . println ( " Error clearing column! " ) ;
}
lastUpdateMillis = millis ( ) ;
frontBuffer [ x ] = 0 ;
2023-02-08 20:00:15 +00:00
}
2023-02-11 17:26:52 +00:00
} 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
}
2023-02-08 20:00:15 +00:00
}
update_counter + + ; //next column
2023-02-11 17:26:52 +00:00
unsigned long _duration = millis ( ) - functionStartMillis ; //how long it took to shift data and flip dots in this update
2023-02-08 20:29:10 +00:00
updateDuration = max ( updateDuration , _duration ) ; //store maximum value
2023-02-08 20:00:15 +00:00
if ( update_counter / 2 > = getW ( ) ) { //reached last column
flag_updating = false ;
update_counter = 0 ;
2023-02-11 16:31:49 +00:00
return finished ; //finished
2023-02-08 20:00:15 +00:00
}
2023-02-11 16:31:49 +00:00
return updating ; //not finished
2023-02-08 20:00:15 +00:00
}
2023-02-08 19:17:58 +00:00
void Image : : loop_testDots ( ) {
static bool init = false ;
if ( ! init ) {
flipdot . setRow ( 0 ) ;
Serial . println ( " Clearing Display " ) ;
for ( int l = 0 ; l < COLUMNS ; l + + ) {
flipdot . selectColumnClear ( l % COLUMNS ) ;
if ( ! flipdot . clearSelectedColumn ( ) ) {
Serial . println ( " Error clearing column! " ) ;
} else {
Serial . println ( " Cleared " ) ;
}
delay ( 50 ) ;
}
init = true ;
Serial . println ( " Finished Clearing " ) ;
delay ( 1000 ) ;
}
Serial . println ( " Clearing " ) ;
flipdot . setRow ( 0 ) ;
flipdot . selectColumnClear ( 23 ) ;
if ( ! flipdot . clearSelectedColumn ( ) ) {
Serial . println ( " Error clearing column! " ) ;
}
delay ( 50 ) ;
flipdot . setRow ( 0 ) ;
flipdot . selectColumnClear ( 24 ) ;
if ( ! flipdot . clearSelectedColumn ( ) ) {
Serial . println ( " Error clearing column! " ) ;
}
delay ( 100 ) ;
Serial . println ( " Setting " ) ;
for ( int i = 23 ; i < 25 ; i + + ) {
flipdot . selectColumnSet ( i ) ; //lower column number is on the left
flipdot . setRow ( 0 ) ;
flipdot . setRow ( flipdot . getRow ( ) + pow ( 2 , 4 ) ) ; //low significant bits are lower rows (when connector at top)
flipdot . setRow ( flipdot . getRow ( ) + pow ( 2 , 5 ) ) ; //low significant bits are lower rows (when connector at top)
flipdot . setSelectedDot ( ) ;
delay ( 50 ) ;
}
delay ( 100 ) ;
countz + + ;
countz % = 14 ;
//init=false;
}
void Image : : loop_drawClearTest ( ) {
static bool init = false ;
if ( ! init ) {
flipdot . setRow ( 0 ) ;
Serial . println ( " Clearing Display " ) ;
for ( int l = 0 ; l < COLUMNS ; l + + ) {
flipdot . selectColumnClear ( l % COLUMNS ) ;
if ( ! flipdot . clearSelectedColumn ( ) ) {
Serial . println ( " Error clearing column! " ) ;
} else {
Serial . println ( " Cleared " ) ;
}
delay ( 20 ) ;
}
init = true ;
delay ( 1000 ) ;
}
Serial . print ( " count= " ) ;
Serial . print ( countz ) ; Serial . print ( " : " ) ;
//setting colX to 128, 32, 8,2 (or a combination of), then appling 12V to driver and GND to Clear, clears these colums
// this applies +12v to selected columns
//setting colX to 64,16,4,1 (or a combination of), then setting row shift registers to some setting sets the selected dots
// this applies GND to selected columns
//reset pin on annax board input should be used (not pulled to gnd for a short time) after dots have been flipped (to disable potentially activated transistors)
//cycle testing set dots
flipdot . selectColumnSet ( countz / ROWS ) ; //lower column number is on the left
flipdot . setRow ( pow ( 2 , ( countz ) % ROWS ) ) ; //low significant bits are lower rows (when connector at top)
/*Serial.print("Row="); Serial.print(row); Serial.print(" Col=");
for ( uint8_t i = 0 ; i < 7 ; i + + ) {
Serial . print ( " , " ) ; Serial . print ( col [ i ] ) ;
}
Serial . println ( ) ; */
//reset pin on ribbon cable high (12Vpullup/open), then low (via Transistor)
unsigned long starttime = micros ( ) ;
flipdot . setSelectedDot ( ) ;
unsigned long shiftduration = micros ( ) - starttime ;
Serial . println ( " " ) ;
Serial . print ( " Duration= " ) ; Serial . println ( shiftduration ) ;
countz + + ;
if ( countz > = ROWS * COLUMNS ) {
countz = 0 ;
init = false ;
}
}