Advertisement
Guest User

DMG LCD Capture w/Teensy

a guest
Apr 17th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1. int vsync = 0;
  2. int clock = 0;
  3. unsigned char data0 = 0;
  4. unsigned char data1 = 0;
  5. int hsync = 0;
  6.  
  7. int vsync_prev = 0;
  8. int clock_prev = 0;
  9. int hsync_prev = 0;
  10. int hsync_read = 0;
  11.  
  12. unsigned char pixel = 0;
  13.  
  14. int line = 0;
  15. int offset = 0;
  16.  
  17. int array_offset = 0;
  18. int byte_offset = 0;
  19. int bit_offset;
  20.  
  21.  
  22. unsigned char frame[5760];
  23.  
  24.  
  25. void setup () {
  26.     Serial.begin(4000000);
  27.    
  28.     for (int i=2; i<7; i++){
  29.         pinMode(i, INPUT);
  30.     }
  31.    
  32.      for (int i=0; i<5760; frame[i++]=0);
  33. }
  34.  
  35.  
  36. void loop() {
  37.     vsync = digitalRead(2);
  38.     clock = digitalRead(3);
  39.     data1 = digitalRead(4);
  40.     data0 = digitalRead(5);
  41.     hsync = digitalRead(6);
  42.    
  43.    
  44.     // Read CLOCK  (falling edge)
  45.     if (clock_prev==HIGH && clock==LOW){
  46.         // Read pixels
  47.         if (offset<160 && line<144){
  48.          
  49.           pixel = 2*data1+data0;
  50.          
  51.           // ************ IMPORTANT ************
  52.           // This part could be implemented as 2-dimensional array, I'm just saving RAM. I tried to make it 2D and the result      
  53.           // was the same.
  54.           // ****************************************
  55.           // Offset in the frame bit map
  56.           bit_offset = (line*320)+offset*2;
  57.  
  58.           // The actual char in the frame array
  59.           array_offset = bit_offset/8;
  60.  
  61.           // The offset within one byte
  62.           byte_offset = bit_offset%8;
  63.  
  64.          // Put the pixel into frame
  65.           frame[array_offset]+=(pixel<<byte_offset);
  66.         }
  67.        
  68.         offset++;
  69.     }
  70.  
  71.     // Read HSYNC (rising edge)
  72.     if (hsync_prev==LOW && hsync==HIGH){
  73.        // Next line
  74.        line++;
  75.        offset = 0;
  76.     }
  77.    
  78.    
  79.     // Read VSYNC (rising edge)
  80.     if (vsync_prev==LOW && vsync==HIGH){
  81.        // Next frame
  82.  
  83.        // Frame divider 0x1E00
  84.        Serial.write(0x1E);
  85.        Serial.write(0x00);
  86.  
  87.        // Send and nullify frame data
  88.        for (int i=0; i<5760; i++){
  89.           Serial.write(frame[i]);
  90.           frame[i] = 0;    
  91.        }
  92.        line = 0;
  93.        offset = 0;
  94.       }
  95.  
  96.     clock_prev = clock;
  97.     vsync_prev = vsync;
  98.     hsync_prev = hsync;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement