// Random Dot Stereoglyph image -- use red/gree-glasses
// Thomas Braunl, Sep. 2019
#include "eyebot.h"
#define LINE 320
#define COL  240

// mix and display red-green image
void mix(BYTE l[], BYTE r[])
{ BYTE img[QVGA_SIZE];
  for (int i=0; i<QVGA_PIXELS; i++)
  { img[3*i]  = l[i] * 255; // left is red
    img[3*i+1]= r[i] * 255; // right is green
    img[3*i+2]= 0;          // zero blue
  }
  LCDImageSize(QVGA); 
  LCDImage(img);
}

// shift square area to left
void shift(BYTE a[], int x, int y, int size, int offset)
{ int pos;
  for (int i=y; i<y+size; i++)
    for (int j=x; j<x+size; j++)
    { pos = i*LINE + j;
      a[pos] = a[pos+offset];
    }
  for (int i=y; i<y+size; i++)
    for (int j=x+size; j<x+size+offset; j++)
    { pos = i*LINE + j;
      a[pos] = rand()%2;  // fill in with random pattern
    }
}


// make a pyramid
void pyramid(BYTE a[], BYTE b[])
{ for (int i=1; i<5; i++)
    shift(a, 20*i,20*i, 240-40*i, i);
}


// int two images
void init(BYTE a[], BYTE b[])
{ for (int i=0; i<QVGA_PIXELS; i++)
  { a[i] = rand()%2;  // 0 or 1
    b[i] = a[i];
  }
}


int main()
{ BYTE left[QVGA_PIXELS], right[QVGA_PIXELS];

  LCDMenu("SHIFT1", "SHIFT2", "PYRAMID", "END");
  LCDArea(  0,250,  50,270, RED,   1); // test pattern
  LCDArea(100,250, 150,270, GREEN, 1); // test pattern
  init(left,right);

  while (1)
  { mix(left,right);  // and display
    switch (KEYGet())
    { case KEY1: shift(left, 50,50, 150, 5); break;
      case KEY2: shift(left, 80,80,  50,10); break;
      case KEY3: pyramid(left,right); break;
      case KEY4: return 0;
    }
  }
}
