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.
 
 
 

124 lines
3.8 KiB

  1. import epaper
  2. import microbmp
  3. import time
  4. import random
  5. import os
  6. import storage
  7. import gc
  8. epd_colormap = [
  9. [0x00, 0x00, 0x00], # black
  10. [0xff, 0xff, 0xff], # white
  11. [0x00, 0x90, 0x10], # green
  12. [0x00, 0x00, 0xee], # blue
  13. [0xff, 0x00, 0x00], # red
  14. [0xff, 0xdd, 0x00], # yellow
  15. [0xff, 0x77, 0x00], # orange
  16. ]
  17. max_free_height = 200
  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(epd, filename):
  24. global epd_colormap
  25. global free_x, free_y, free_width, free_height
  26. offset_x = 0
  27. offset_y = 0
  28. free_x = 0
  29. free_y = 0
  30. free_width = 0
  31. free_height = 0
  32. color_map = {}
  33. def header_callback(header):
  34. #print("header callback: ", str(header))
  35. #global epd
  36. global offset_x, offset_y
  37. global free_x, free_y, free_width, free_height
  38. global max_free_height
  39. w = header[0]
  40. h = header[1]
  41. rest_x = (epd.width - w)
  42. rest_y = (epd.height - h)
  43. free_x = 0
  44. free_y = max(h, epd.height - max_free_height)
  45. free_width = epd.width
  46. free_height = min(rest_y, max_free_height)
  47. offset_x = rest_x//2
  48. offset_y = max(0, rest_y - max_free_height)//2
  49. def pixel_callback(x, y, color):
  50. #global epd
  51. global epd_colormap
  52. global offset_x, offset_y
  53. # translate color to color index
  54. color_index = 0
  55. color_key = color[0] + color[1] << 8 + color[2] << 16
  56. if color_key in color_map:
  57. color_index = color_map[color_key]
  58. else:
  59. # search for the best color
  60. best_index = 0
  61. best_score = 1000
  62. for index in range(len(epd_colormap)):
  63. c1 = epd_colormap[index]
  64. c2 = color
  65. score = abs(c1[0] - c2[0]) + abs(c1[1] - c2[1]) + abs(c1[2] - c2[2])
  66. if score < best_score:
  67. best_score = score
  68. best_index = index
  69. if score < 10:
  70. break
  71. color_index = best_index
  72. color_map[color_key] = color_index
  73. epd.pixel(offset_x + x, offset_y + y, color_index)
  74. #print(str(time.localtime()), " BMP ", filename, " loading")
  75. time_start = time.ticks_ms()
  76. epd.fill(epd.White)
  77. microbmp.MicroBMP(header_callback=header_callback, data_callback=pixel_callback).load(filename)
  78. time_loaded = time.ticks_ms()
  79. print(" time to load: ", (time_loaded - time_start) / 1000, " s")
  80. #print(str(time.localtime()), " BMP loaded")
  81. #epd.EPD_5IN65F_Display(epd.buffer)
  82. #print(str(time.localtime()), " ePaper printed")
  83. return [free_x, free_y, free_width, free_height]
  84. def draw_pattern(epd):
  85. free_x = 0
  86. free_y = 400
  87. free_width = epd.width
  88. free_height = epd.height - free_y
  89. epd.fill_rect(0, 0, epd.width, free_y, random.randrange(2,7))
  90. return [free_x, free_y, free_width, free_height]
  91. def print_text(epd, text, region, center=False):
  92. fnt = 8
  93. x = region[0]
  94. y = region[1]
  95. w = region[2]
  96. h = region[3]
  97. if center:
  98. x = (w - len(text)*fnt) // 2
  99. epd.text(text, x, y, epd.Black)
  100. def draw_extra(epd, region, caption):
  101. fnt = 8
  102. region[0] += 5
  103. region[1] += 8
  104. if region[2] < fnt or region[3] < fnt:
  105. print("Not enough space for extra: ", str(region))
  106. return
  107. #epd.rect(region[0], region[1], region[2], region[3], epd.Black)
  108. #region[0] += random.randrange(50)
  109. #print_text(str(time.localtime()), region, center=True)
  110. if caption is not None:
  111. #region[1] += fnt * 2
  112. print_text(epd, caption, region)
  113. print("Caption: ", caption)
  114. else:
  115. print("No caption")