// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x2640
leaq -0x30(%rbp), %rdi
callq 0x2780
testb $0x1, %al
jne 0x244b
movl $0x1, -0x4(%rbp)
jmp 0x262b
movl $0x20, -0x34(%rbp)
movl $0x1, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl $0x8, -0x40(%rbp)
movl $0xa, -0x44(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x6b88(%rip), %rdx      # 0x9004
callq 0x2070
movl %eax, -0x48(%rbp)
cmpl $-0x1, %eax
je 0x24dc
movl -0x48(%rbp), %eax
movl %eax, -0x84(%rbp)
subl $0x64, %eax
je 0x24ba
jmp 0x2499
movl -0x84(%rbp), %eax
subl $0x70, %eax
jne 0x24ce
jmp 0x24a6
movq 0xbb1b(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
callq 0x2270
movl %eax, -0x40(%rbp)
jmp 0x24da
movq 0xbb07(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
callq 0x2270
movl %eax, -0x44(%rbp)
jmp 0x24da
movl $0x0, -0x4(%rbp)
jmp 0x262b
jmp 0x246e
movl $0x78, %edi
callq 0x21a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
movl -0x34(%rbp), %edx
movl -0x38(%rbp), %ecx
movl -0x3c(%rbp), %r8d
leaq -0x30(%rbp), %rsi
callq 0x51b0
jmp 0x2508
movq -0x90(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movl -0x40(%rbp), %eax
movzbl %al, %esi
callq 0x5740
movq -0x50(%rbp), %rdi
callq 0x5790
movzbl %al, %esi
leaq 0x6ad4(%rip), %rdi      # 0x9009
movb $0x0, %al
callq 0x2050
movq -0x50(%rbp), %rdi
callq 0x5800
andb $0x1, %al
movzbl %al, %esi
leaq 0x6ac5(%rip), %rdi      # 0x9016
movb $0x0, %al
callq 0x2050
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0xba71(%rip), %rax      # 0xdfd8
movq (%rax), %rdx
xorl %eax, %eax
movl %eax, %edi
leaq 0x6ab4(%rip), %rsi      # 0x9029
callq 0x21c0
movq %rax, -0x70(%rbp)
movl $0x0, -0x7c(%rbp)
cmpl $0x20, -0x7c(%rbp)
jge 0x261e
movl $0x0, -0x80(%rbp)
cmpl $0x20, -0x80(%rbp)
jge 0x260e
jmp 0x259e
leaq -0x73(%rbp), %rdi
movq -0x70(%rbp), %rcx
movl $0x3, %esi
movl $0x1, %edx
callq 0x2250
movl %eax, -0x78(%rbp)
cmpl $0x1, -0x78(%rbp)
jne 0x259e
movq -0x68(%rbp), %rdi
movl -0x80(%rbp), %esi
movl -0x7c(%rbp), %edx
movb -0x73(%rbp), %cl
movb -0x72(%rbp), %r8b
movb -0x71(%rbp), %r9b
movq (%rdi), %rax
movzbl %cl, %ecx
movzbl %r8b, %r8d
movzbl %r9b, %r9d
callq *0x20(%rax)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x2596
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl $0x78, %esi
callq 0x21b0
jmp 0x2637
jmp 0x2610
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x2585
movl -0x44(%rbp), %edi
callq 0x2200
jmp 0x257e
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x22b0