// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, %r14
movl %edi, %r15d
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x2644
movq %rbx, %rdi
callq 0x26d6
movl %eax, %ecx
movl $0x1, %eax
testb %cl, %cl
je 0x261b
movl $0x8, %ebp
movl $0xa, %ebx
leaq 0x2b0f(%rip), %r12      # 0x5004
movq 0x5acc(%rip), %r13      # 0x7fc8
movl %r15d, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2070
cmpl $0x70, %eax
jne 0x251c
movq (%r13), %rdi
callq 0x2260
movl %eax, %ebp
jmp 0x24fc
cmpl $-0x1, %eax
je 0x2537
cmpl $0x64, %eax
jne 0x2619
movq (%r13), %rdi
callq 0x2260
movl %eax, %ebx
jmp 0x24fc
movl $0x78, %edi
callq 0x2190
movq %rax, %r14
leaq 0x8(%rsp), %rsi
movq %rax, %rdi
movl $0x20, %edx
movl $0x1, %ecx
movl $0x1, %r8d
callq 0x30bc
movzbl %bpl, %esi
movq %r14, %rdi
callq 0x3422
movq %r14, %rdi
callq 0x3448
movzbl %al, %esi
leaq 0x2a8a(%rip), %rdi      # 0x5009
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
callq 0x345e
movzbl %al, %esi
leaq 0x2a7b(%rip), %rdi      # 0x5016
xorl %eax, %eax
callq 0x2050
movq 0x5a2f(%rip), %rax      # 0x7fd8
movq (%rax), %rdx
leaq 0x2a76(%rip), %rsi      # 0x5029
xorl %edi, %edi
callq 0x21b0
movq %rax, %r12
leaq 0x5(%rsp), %r13
xorl %ebp, %ebp
movl $0x3, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x2240
cmpl $0x1, %eax
jne 0x25c4
movzbl 0x5(%rsp), %ecx
movzbl 0x6(%rsp), %r8d
movzbl 0x7(%rsp), %r9d
movq (%r14), %rax
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq *0x20(%rax)
incl %ebp
cmpl $0x20, %ebp
jne 0x25c4
incl %r15d
cmpl $0x20, %r15d
jne 0x25c2
movl %ebx, %edi
callq 0x21f0
xorl %r15d, %r15d
jmp 0x25c2
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x78, %esi
movq %r14, %rdi
callq 0x21a0
movq %rbx, %rdi
callq 0x22a0
nop