Skip to content

Commit

Permalink
o Making dropping privileges the default.
Browse files Browse the repository at this point in the history
o Make it possible to select scan mode programmatically.
  • Loading branch information
hzeller committed Aug 21, 2016
1 parent d528600 commit 6d26807
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 23 deletions.
9 changes: 2 additions & 7 deletions examples-api-use/demo-main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1030,10 +1030,7 @@ static int usage(const char *progname) {
"\t-R <rotation> : Sets the rotation of matrix. "
"Allowed: 0, 90, 180, 270. Default: 0.\n");

RGBMatrix::Options m_options;
rgb_matrix::RuntimeOptions rt_options;
rt_options.drop_privileges = 1;
rgb_matrix::PrintMatrixFlags(stderr, m_options, rt_options);
rgb_matrix::PrintMatrixFlags(stderr);

fprintf(stderr, "Demos, choosen with -D\n");
fprintf(stderr, "\t0 - some rotating square\n"
Expand Down Expand Up @@ -1063,9 +1060,7 @@ int main(int argc, char *argv[]) {

// First things first: create matrix and fish out the command line
// options it responds to. After that, we parse the remaining options.
rgb_matrix::RuntimeOptions rt_options;
rt_options.drop_privileges = 1;
RGBMatrix *matrix = CreateMatrixFromFlags(&argc, &argv, NULL, &rt_options);
RGBMatrix *matrix = CreateMatrixFromFlags(&argc, &argv);

int opt;
while ((opt = getopt(argc, argv, "dD:t:r:P:c:p:b:m:LR:")) != -1) {
Expand Down
3 changes: 3 additions & 0 deletions include/led-matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class RGBMatrix : public Canvas {
// Default: 100
int brightness;

// Scan mode: 0=progressive, 1=interlaced
int scan_mode;

bool show_refresh_rate;
bool swap_green_blue;
bool inverse_colors;
Expand Down
5 changes: 3 additions & 2 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ TARGET=librgbmatrix

###
# After you change any of the following DEFINES, make sure to 'make' again.
# Note, all these options now can be set programmatically and via command line
# options as well. So be prepared for these to be removed.
#
# Note, most of these options now can be set programmatically and via command
# line options as well. So be prepared for these to be removed.
###

# If you see that your display is inverse, you might have a matrix variant
Expand Down
2 changes: 2 additions & 0 deletions lib/framebuffer-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace internal {
class Framebuffer {
public:
Framebuffer(int rows, int columns, int parallel,
int scan_mode,
bool swap_green_blue, bool inverse_color);
~Framebuffer();

Expand Down Expand Up @@ -70,6 +71,7 @@ class Framebuffer {
const int height_; // rows * parallel
const int columns_; // Number of columns. Number of chained boards * 32.

const int scan_mode_;
const bool swap_green_blue_;
const bool inverse_color_;

Expand Down
21 changes: 14 additions & 7 deletions lib/framebuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ static PinPulser *sOutputEnablePulser = NULL;
#endif

Framebuffer::Framebuffer(int rows, int columns, int parallel,
int scan_mode,
bool swap_green_blue, bool inverse_color)
: rows_(rows),
parallel_(parallel),
height_(rows * parallel),
columns_(columns),
scan_mode_(scan_mode),
swap_green_blue_(swap_green_blue), inverse_color_(inverse_color),
pwm_bits_(kBitPlanes), do_luminance_correct_(true), brightness_(100),
double_rows_(rows / SUB_PANELS_), row_mask_(double_rows_ - 1) {
Expand Down Expand Up @@ -351,23 +353,28 @@ void Framebuffer::DumpToMatrix(GPIO *io) {
clock.bits.clock = 1;
strobe.bits.strobe = 1;

#if RGB_SCAN_INTERLACED
// info needed for interlace mode.
uint8_t rot_bits = 0;
switch (double_rows_) {
case 4: rot_bits = 1; break;
case 8: rot_bits = 2; break;
case 16: rot_bits = 3; break;
case 32: rot_bits = 4; break;
}
#endif

const int pwm_to_show = pwm_bits_; // Local copy, might change in process.
for (uint8_t row_loop = 0; row_loop < double_rows_; ++row_loop) {
#if RGB_SCAN_INTERLACED
uint8_t d_row = ((row_loop << 1) | (row_loop >> rot_bits)) & row_mask_;
#else
uint8_t d_row = row_loop;
#endif
uint8_t d_row;
switch (scan_mode_) {
case 0: // progressive
default:
d_row = row_loop;
break;

case 1: // interlaced
d_row = ((row_loop << 1) | (row_loop >> rot_bits)) & row_mask_;
}

row_address.bits.a = d_row;
row_address.bits.b = d_row >> 1;
row_address.bits.c = d_row >> 2;
Expand Down
7 changes: 7 additions & 0 deletions lib/led-matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ RGBMatrix::Options::Options()
: rows(32), chain_length(1), parallel(1), pwm_bits(11), brightness(100),
// Historically, we provided these options only as #defines. Make sure that
// things still behave as before if someone has set these.
#ifdef RGB_SCAN_INTERLACED
scan_mode(1),
#else
scan_mode(0),
#endif

#ifdef SHOW_REFRESH_RATE
show_refresh_rate(true),
#else
Expand Down Expand Up @@ -204,6 +210,7 @@ FrameCanvas *RGBMatrix::CreateFrameCanvas() {
new FrameCanvas(new internal::Framebuffer(params_.rows,
32 * params_.chain_length,
params_.parallel,
params_.scan_mode,
params_.swap_green_blue,
params_.inverse_colors));
if (created_frames_.empty()) {
Expand Down
23 changes: 16 additions & 7 deletions lib/options-initialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ RuntimeOptions::RuntimeOptions() :
#else
gpio_slowdown(1),
#endif
daemon(0), drop_privileges(0)
daemon(0), // Don't become a daemon by default.
drop_privileges(1) // Encourage good practice: drop privileges by default.
{
// Nothing to see here.
}
Expand Down Expand Up @@ -117,6 +118,8 @@ static bool FlagInit(int &argc, char **&argv,
continue;
if (ConsumeIntFlag("brightness", it, end, &mopts->brightness, &err))
continue;
if (ConsumeIntFlag("scan-mode", it, end, &mopts->scan_mode, &err))
continue;
if (ConsumeIntFlag("pwm-bits", it, end, &mopts->pwm_bits, &err))
continue;
if (ConsumeBoolFlag("show-refresh", it, &mopts->show_refresh_rate))
Expand Down Expand Up @@ -250,13 +253,15 @@ void PrintMatrixFlags(FILE *out, const RGBMatrix::Options &d,
"chains. range=1..3 (Default: %d).\n"
"\t--led-pwm-bits=<1..11> : PWM bits (Default: %d).\n"
"\t--led-brightness=<percent>: Brightness in percent (Default: %d).\n"
"\t--led-scan-mode=<0..1> : 0 = progressive; 1 = interlaced "
"(Default: %d).\n"
"\t--led-%sshow-refresh : %show refresh rate.\n"
"\t--led-%sinverse "
": Switch if your matrix has inverse colors %s.\n "
"\t--led-%sswap-green-blue : Switch if your matrix has green/blue "
"swapped %s.\n",
d.rows, d.chain_length, d.parallel,
d.pwm_bits, d.brightness,
d.pwm_bits, d.brightness, d.scan_mode,
d.show_refresh_rate ? "no-" : "", d.show_refresh_rate ? "Don't s" : "S",
d.inverse_colors ? "no-" : "", d.inverse_colors ? "off" : "on",
d.swap_green_blue ? "no-" : "", d.swap_green_blue ? "off" : "on"
Expand All @@ -274,7 +279,7 @@ void PrintMatrixFlags(FILE *out, const RGBMatrix::Options &d,
if (r.drop_privileges >= 0) {
const bool on = (r.drop_privileges > 0);
fprintf(out,
"\t--led-%sdrop-privs : %srop privileges from 'root' "
"\t--led-%sdrop-privs : %srop privileges from 'root' "
"after initializing the hardware.\n",
on ? "no-" : "", on ? "Don't d" : "D");
}
Expand All @@ -289,25 +294,29 @@ bool RGBMatrix::Options::Validate(std::string *err) {
}

if (chain_length < 1) {
err->append("Chain-length outside usable range\n");
err->append("Chain-length outside usable range.\n");
success = false;
}

if (parallel < 1 || parallel > 3) {
err->append("Parallel outside usable range.\n");
err->append("Parallel outside usable range (1..3 allowed).\n");
success = false;
}

if (brightness < 1 || brightness > 100) {
err->append("Brightness is outside usable range.\n");
err->append("Brightness outside usable range (Percent 1..100 allowed).\n");
success = false;
}

if (pwm_bits <= 0 || pwm_bits > 11) {
err->append("Invalid range of pwm-bits\n");
err->append("Invalid range of pwm-bits (0..11 allowed).\n");
success = false;
}

if (scan_mode < 0 || scan_mode > 1) {
err->append("Invalid scan mode (0 or 1 allowed).\n");
success = false;
}
return success;
}

Expand Down

0 comments on commit 6d26807

Please sign in to comment.