Created
December 11, 2013 15:49
-
-
Save mdevaev/7912794 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include "Matrix.h" | |
Matrix::Matrix(const int *row_pins, const int *column_pins) { | |
this->row_pins = (int *) row_pins; // FIXME | |
this->column_pins = (int *) column_pins; // FIXME | |
for (rows = 0; row_pins[rows] != -1; ++rows) { | |
pinMode(row_pins[rows], OUTPUT); | |
digitalWrite(row_pins[rows], HIGH); | |
} | |
for (columns = 0; column_pins[columns] != -1; ++columns) { | |
pinMode(column_pins[columns], OUTPUT); | |
digitalWrite(column_pins[columns], LOW); | |
} | |
pixels = (int *)calloc(rows * columns, sizeof(int)); // FIXME: Change "calloc" to "new" | |
row_index = 0; | |
random_delay = 0; | |
} | |
void Matrix::setPixel(int row, int column, int state) { | |
pixels[row * columns + column] = state; | |
} | |
void Matrix::setRandom(unsigned long delay) { | |
random_delay = delay; | |
} | |
void Matrix::clear() { | |
for (int count = 0; count < rows; ++count) | |
digitalWrite(row_pins[rows], HIGH); | |
for (int count = 0; count < columns; ++count) | |
digitalWrite(column_pins[columns], LOW); | |
} | |
void Matrix::draw() { | |
if ( random_delay > 0 ) { | |
drawRandom(); | |
return; | |
} | |
setRowState(row_index, LOW); | |
for (int column = 0; column < columns; ++column) | |
setColumnState(column, LOW); | |
if ( row_index < rows ) { | |
++row_index; | |
} else { | |
row_index = 0; | |
} | |
setRowState(row_index, HIGH); | |
for (int column = 0; column < columns; ++column) { | |
if ( pixels[row_index * columns + column] ) | |
setColumnState(column, HIGH); // XXX: break? | |
} | |
} | |
void Matrix::drawRandom() { | |
unsigned long current_time = millis(); | |
if ( ( current_time >= last_random_time && current_time - last_random_time >= random_delay ) || | |
( current_time < last_random_time && ((unsigned long) -1) - last_random_time + current_time >= random_delay ) ) { | |
int state = random(0, 2); | |
setRowState(random(0, rows), state); | |
setColumnState(random(0, columns), state); | |
last_random_time = millis(); | |
} | |
} | |
void Matrix::setRowState(int row, int state) { | |
digitalWrite(row_pins[row], !state); | |
} | |
void Matrix::setColumnState(int column, int state) { | |
digitalWrite(column_pins[column], state); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MATRIX_H | |
# define MATRIX_H | |
class Matrix { | |
public : | |
Matrix(const int *row_pins, const int *column_pins); | |
void setPixel(int row, int column, int state); | |
void setRandom(unsigned long delay); | |
void clear(); | |
void draw(); | |
private : | |
void setRowState(int row, int state); | |
void setColumnState(int column, int state); | |
void drawRandom(); | |
int rows; | |
int columns; | |
int *row_pins; | |
int *column_pins; | |
int *pixels; | |
int row_index; | |
unsigned long random_delay; | |
unsigned long last_random_time; | |
}; | |
#endif // MATRIX_H | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment