i2c_vga_ddc.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # Copyright (C) 2024 Victor Suarez Rovere <suarezvictor@gmail.com>
  2. # SPDX-License-Identifier: AGPL-3.0-only
  3. from umachine import SoftI2C
  4. #in the CPU board, SCL is PE12, SDA is PE13 (maps respectively to VGA pins 15 and 12)
  5. i2c = SoftI2C(scl=12, sda=13, freq=100000)
  6. scan = i2c.scan() #VGA DDC returns 0x37, 0x49, 0x50, 0x59
  7. assert(0x50 in scan)
  8. bytes = i2c.readfrom(0x50, 128)
  9. assert(len(bytes) == 128 and sum(bytes) % 256 == 0) #CRC check
  10. for i in range(38, 54, 2):
  11. if i == 38: print("\nStandard resolutions:")
  12. if bytes[i] != 1 or bytes[i+1] != 1:
  13. xres = (bytes[i]+31)*8
  14. aspect = [(16, 10), (4, 3), (5, 4), (16, 9)][bytes[i+1] >> 6]
  15. yres = xres * aspect[1] / aspect[0]
  16. vfreq = (bytes[i+1] & 63) + 60
  17. #print(' X res: %d, aspect %d:%d, Y res (derived): %d), vertical frequency: %d' % (xres, aspect[0], aspect[1], yres, vfreq))
  18. print('\t%4d x %4d\t@ %d Hz refresh' % (xres, yres, vfreq))
  19. for n in range(1, 5):
  20. if n == 1: print("\nNative resolutions:")
  21. i = n * 18 + 36
  22. if bytes[i] != 0 or bytes[i+1] != 0:
  23. b = bytes[i:i+18]
  24. h = b[2] | ((b[4]&0xF0) << 4)
  25. v = b[5] | ((b[7]&0xF0) << 4)
  26. clk = (b[0] | (b[1]<<8)) / 100.0
  27. print('\t%4d x %4d\t%3.1f MHz pixel clock' % (h, v, clk))
  28. print('\nScreen size: %d x %dcm' % (bytes[21], bytes[22]))