// File: /workspace/github_cpp_makefile_star2_O2/eggnot[P]pixel_camera/dump32x32.cc
// Function: main
int main(int argc, char *argv[]) {
  /*
   * Set up GPIO pins. This fails when not running as root.
   */
  GPIO io;
  if (!io.Init())
    return 1;
    
  /*
   * Set up the RGBMatrix. It implements a 'Canvas' interface.
   */
  int rows = 32;    // A 32x32 display. Use 16 when this is a 16x32 display.
  int chain = 1;    // Number of boards chained together.
  int parallel = 1; // Number of chains in parallel (1..3). > 1 for plus or Pi2
  int pwm = 8;
  int delay = 10; 

  int c;
  while ((c = getopt (argc, argv, "p:d:")) != -1)
    switch (c) {
      case 'p':
        pwm = atoi(optarg);
	break;
      case 'd':
	delay = atoi(optarg);
	break;
      default:
	return 0;
    }

 
  RGBMatrix *matrix = new RGBMatrix(&io, rows, chain, parallel);

  matrix->SetPWMBits(pwm);

  printf("pwm bits: %d", matrix->pwmbits());
  printf("pwm correction: %d", matrix->luminance_correct());

  Canvas *canvas = matrix;

  //canvas->Fill(0, 0, 255);
  
  FILE *f = freopen(NULL, "rb", stdin);

  unsigned char buf[3];

  int bytes_read;
  
  while(1) { 
    for(int y=0; y<32; y++) {
      for(int x=0; x<32; x++) {
        do {
          bytes_read = fread(buf, 3, 1, f);
        } while(bytes_read != 1);
          
        canvas->SetPixel(x, y, buf[0], buf[1], buf[2]);
      }
    }
    //vsync? suspend execution for microsecond intervals
    usleep(delay);
  }

  canvas->Clear();
  delete canvas;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movl %edi, %r15d
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x259c
movq %rbx, %rdi
callq 0x2632
testb %al, %al
je 0x247e
pushq $0x8
popq %rbp
pushq $0xa
popq %rax
movl %eax, 0xc(%rsp)
leaq 0x2bca(%rip), %rbx      # 0x5004
movq 0x5b87(%rip), %r12      # 0x7fc8
movl %r15d, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2070
cmpl $0x70, %eax
jne 0x2461
movq (%r12), %rdi
callq 0x2260
movl %eax, %ebp
jmp 0x2441
cmpl $-0x1, %eax
je 0x2486
cmpl $0x64, %eax
jne 0x2572
movq (%r12), %rdi
callq 0x2260
movl %eax, 0xc(%rsp)
jmp 0x2441
pushq $0x1
popq %rax
jmp 0x2574
pushq $0x78
popq %rdi
callq 0x2190
movq %rax, %r15
leaq 0x18(%rsp), %rsi
pushq $0x20
popq %rdx
pushq $0x1
popq %rcx
movq %rax, %rdi
movl %ecx, %r8d
callq 0x3018
movzbl %bpl, %esi
movq %r15, %rdi
callq 0x3326
movq %r15, %rdi
callq 0x334c
movzbl %al, %esi
leaq 0x2b44(%rip), %rdi      # 0x5009
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2050
movq %r15, 0x10(%rsp)
movq %r15, %rdi
callq 0x3362
movzbl %al, %esi
leaq 0x2b30(%rip), %rdi      # 0x5016
xorl %eax, %eax
callq 0x2050
movq 0x5ae4(%rip), %rax      # 0x7fd8
movq (%rax), %rdx
leaq 0x2b2b(%rip), %rsi      # 0x5029
xorl %edi, %edi
callq 0x21b0
movq %rax, %r12
leaq 0x9(%rsp), %r13
pushq $0x3
popq %rbp
pushq $0x1
popq %rbx
cmpl $0x20, %r14d
je 0x2564
xorl %r15d, %r15d
cmpl $0x20, %r15d
je 0x255f
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x2240
cmpl $0x1, %eax
jne 0x2522
movzbl 0x9(%rsp), %ecx
movzbl 0xa(%rsp), %r8d
movzbl 0xb(%rsp), %r9d
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movl %r15d, %esi
movl %r14d, %edx
callq *0x20(%rax)
incl %r15d
jmp 0x251c
incl %r14d
jmp 0x2513
movl 0xc(%rsp), %edi
callq 0x21f0
xorl %r14d, %r14d
jmp 0x2513
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x78
popq %rsi
movq %r15, %rdi
callq 0x21a0
movq %rbx, %rdi
callq 0x22a0
nopl (%rax)