ePaper display driven by a Raspberry Pi Pico
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

102 lines
3.1 KiB

  1. import epaper
  2. import microbmp
  3. import time
  4. import random
  5. import gc
  6. epd_resolution = [600, 448]
  7. epd_colormap = [
  8. [0x00, 0x00, 0x00], # black
  9. [0xff, 0xff, 0xff], # white
  10. [0x00, 0xdd, 0x00], # green
  11. [0x00, 0x00, 0xee], # blue
  12. [0x00, 0xdd, 0x00], # red
  13. [0xff, 0xdd, 0x00], # yellow
  14. [0xff, 0x88, 0x00], # orange
  15. ]
  16. images = ["lambo.bmp", "fallout.bmp", "nasa.bmp"]
  17. gc.enable()
  18. def init_display():
  19. #print("ePaper init ", str(time.localtime()))
  20. epd = epaper.EPD_5in65()
  21. epd.fill(epd.White)
  22. return epd
  23. def draw_image(filename):
  24. global epd
  25. global epd_colormap
  26. offset_x = 0
  27. offset_y = 0
  28. color_map = {}
  29. def header_callback(header):
  30. print("header callback: ", str(header))
  31. global epd_resolution
  32. w = header[0]
  33. h = header[1]
  34. offset_x = (epd_resolution[0] - w)//2
  35. offset_y = (epd_resolution[1] - h)//2
  36. print("offset: ", offset_x, offset_y)
  37. def pixel_callback(x, y, color):
  38. global epd
  39. global epd_colormap
  40. # translate color to color index
  41. color_index = 0
  42. color_key = color[0] + color[1] << 8 + color[2] << 16
  43. if color_key in color_map:
  44. color_index = color_map[color_key]
  45. else:
  46. # search for the best color
  47. best_index = 0
  48. best_score = 1000
  49. for index in range(len(epd_colormap)):
  50. c1 = epd_colormap[index]
  51. c2 = color
  52. score = abs(c1[0] - c2[0]) + abs(c1[1] - c2[1]) + abs(c1[2] - c2[2])
  53. if score < best_score:
  54. best_score = score
  55. best_index = index
  56. if score < 20:
  57. break
  58. color_index = best_index
  59. color_map[color_key] = color_index
  60. # hack directly into the 4-bit color buffer instead of:
  61. # epd.pixel(offset_x + x, offset_y + y, color_to_index(color))
  62. buffer_pos = (offset_x + x + (offset_y + y) * epd_resolution[0])
  63. buffer_index = buffer_pos // 2
  64. if buffer_pos % 2 == 1:
  65. buffer_pos = buffer_pos << 4
  66. epd.buffer[buffer_index] = color_index
  67. def pixel_row_callback(x, y, colors):
  68. pass
  69. #global epd
  70. #print("PXL ", str([color, r,g,b, pixel]))
  71. #for i in range(len(colors)):
  72. # epd.pixel(offset_x + x + i, offset_y + y, color_to_index(colors[i]))
  73. print(str(time.localtime()), " BMP ", filename, " loading")
  74. epd.fill(epd.White)
  75. microbmp.MicroBMP(header_callback=header_callback, data_callback=pixel_callback).load(filename)
  76. print(str(time.localtime()), " BMP loaded")
  77. epd.EPD_5IN65F_Display(epd.buffer)
  78. print(str(time.localtime()), " ePaper printed")
  79. # MAIN
  80. epd = init_display()
  81. while True:
  82. for filename in images:
  83. epd.EPD_5IN65F_Init()
  84. print("TV loading image ", filename)
  85. draw_image(filename)
  86. epd.Sleep()
  87. print("TV showing ", filename)
  88. gc.collect()
  89. epd.delay_ms(10000)