SHARE
TWEET

Untitled

a guest Apr 26th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import board
  2. import digitalio
  3. import time
  4. import busio
  5. from adafruit_bus_device.spi_device import SPIDevice
  6.  
  7. EPD_WIDTH   =   200
  8. EPD_HEIGHT  =   200
  9.  
  10. PANEL_SETTING                               = 0x00
  11. POWER_SETTING                               = 0x01
  12. POWER_OFF                                   = 0x02
  13. POWER_OFF_SEQUENCE_SETTING                  = 0x03
  14. POWER_ON                                    = 0x04
  15. POWER_ON_MEASURE                            = 0x05
  16. BOOSTER_SOFT_START                          = 0x06
  17. DEEP_SLEEP                                  = 0x07
  18. DATA_START_TRANSMISSION_1                   = 0x10
  19. DATA_STOP                                   = 0x11
  20. DISPLAY_REFRESH                             = 0x12
  21. DATA_START_TRANSMISSION_2                   = 0x13
  22. PLL_CONTROL                                 = 0x30
  23. TEMPERATURE_SENSOR_COMMAND                  = 0x40
  24. TEMPERATURE_SENSOR_CALIBRATION              = 0x41
  25. TEMPERATURE_SENSOR_WRITE                    = 0x42
  26. TEMPERATURE_SENSOR_READ                     = 0x43
  27. VCOM_AND_DATA_INTERVAL_SETTING              = 0x50
  28. LOW_POWER_DETECTION                         = 0x51
  29. TCON_SETTING                                = 0x60
  30. TCON_RESOLUTION                             = 0x61
  31. SOURCE_AND_GATE_START_SETTING               = 0x62
  32. GET_STATUS                                  = 0x71
  33. AUTO_MEASURE_VCOM                           = 0x80
  34. VCOM_VALUE                                  = 0x81
  35. VCM_DC_SETTING_REGISTER                     = 0x82
  36. PROGRAM_MODE                                = 0xA0
  37. ACTIVE_PROGRAM                              = 0xA1
  38. READ_OTP_DATA                               = 0xA2
  39.  
  40. lut_vcom0 = bytearray([
  41.   0x0E, 0x14, 0x01, 0x0A, 0x06, 0x04, 0x0A, 0x0A,
  42.   0x0F, 0x03, 0x03, 0x0C, 0x06, 0x0A, 0x00
  43. ])
  44.  
  45. lut_w = bytearray([
  46.   0x0E, 0x14, 0x01, 0x0A, 0x46, 0x04, 0x8A, 0x4A,
  47.   0x0F, 0x83, 0x43, 0x0C, 0x86, 0x0A, 0x04
  48. ])
  49.  
  50. lut_b =  bytearray([
  51.   0x0E, 0x14, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A,
  52.   0x0F, 0x83, 0x43, 0x0C, 0x06, 0x4A, 0x04
  53. ])
  54.  
  55. lut_g1 =  bytearray([
  56.   0x8E, 0x94, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A,
  57.   0x0F, 0x83, 0x43, 0x0C, 0x06, 0x0A, 0x04
  58. ])
  59.  
  60. lut_g2 =  bytearray([
  61.   0x8E, 0x94, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A,
  62.   0x0F, 0x83, 0x43, 0x0C, 0x06, 0x0A, 0x04
  63. ])
  64.  
  65. lut_vcom1 =  bytearray([
  66.   0x03, 0x1D, 0x01, 0x01, 0x08, 0x23, 0x37, 0x37,
  67.   0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  68. ])
  69.  
  70. lut_red0 =  bytearray([
  71.   0x83, 0x5D, 0x01, 0x81, 0x48, 0x23, 0x77, 0x77,
  72.   0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  73. ])
  74.  
  75. lut_red1 =  bytearray([
  76.   0x03, 0x1D, 0x01, 0x01, 0x08, 0x23, 0x37, 0x37,
  77.   0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  78. ])
  79.  
  80. led = digitalio.DigitalInOut(board.L)
  81. led.direction = digitalio.Direction.OUTPUT
  82.  
  83. busy = digitalio.DigitalInOut(board.BUSY)
  84. res = digitalio.DigitalInOut(board.RES)
  85. dc = digitalio.DigitalInOut(board.DC)
  86. eink_en = digitalio.DigitalInOut(board.EINK_EN)
  87.  
  88. cs = digitalio.DigitalInOut(board.CS)
  89. comm_port = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
  90. device = SPIDevice(comm_port, cs)
  91.  
  92. # for framebuffer
  93. frame_black = bytearray(round(EPD_WIDTH * EPD_HEIGHT / 8))
  94. frame_red = bytearray(round(EPD_WIDTH * EPD_HEIGHT / 8))
  95.  
  96. def epd_send_cmd(cmd):
  97.     dc.value = 0
  98.     with device as bus_device:
  99.         bus_device.write(bytes([cmd]))
  100.  
  101. def epd_send_data(data):
  102.     dc.value = 1
  103.     with device as bus_device:
  104.         bus_device.write(bytes([data]))
  105.  
  106. def epd_wait_until_idle():
  107.     while True:
  108.         if busy.value != 0:
  109.             break
  110.         time.sleep(0.1)
  111.         led.value = ~led.value
  112.  
  113. def epd_set_lut_bw():
  114.     epd_send_cmd(0x20)         #g vcom
  115.     for count in range(15):
  116.         epd_send_data(lut_vcom0[count])
  117.  
  118.     epd_send_cmd(0x21)        #g ww --
  119.     for count in range(15):
  120.         epd_send_data(lut_w[count])
  121.  
  122.     epd_send_cmd(0x22)         #g bw r
  123.     for count in range(15):
  124.         epd_send_data(lut_b[count])
  125.  
  126.     epd_send_cmd(0x23)         #g wb w
  127.     for count in range(15):
  128.         epd_send_data(lut_g1[count])
  129.    
  130.     epd_send_cmd(0x24)         #g bb b
  131.     for count in range(15):
  132.         epd_send_data(lut_g2[count])
  133.  
  134. def epd_set_lut_red():
  135.     epd_send_cmd(0x25)
  136.     for count in range(15):
  137.         epd_send_data(lut_vcom1[count])
  138.  
  139.     epd_send_cmd(0x26)
  140.     for count in range(15):
  141.         epd_send_data(lut_red0[count])
  142.  
  143.     epd_send_cmd(0x27)
  144.     for count in range(15):
  145.         epd_send_data(lut_red1[count])
  146.  
  147. def epd_reset():
  148.     res.value = 0
  149.     time.sleep(0.2)
  150.     res.value = 1
  151.     time.sleep(0.2)
  152.  
  153. def epd_display_frame():
  154.     epd_send_cmd(DATA_START_TRANSMISSION_1)
  155.     time.sleep(0.2)
  156.     for i in range(EPD_HEIGHT * EPD_WIDTH / 8):
  157.         temp = 0x00
  158.         for bit in range(4):
  159.             if ((frame_black[i] & (0x80 >> bit)) != 0):
  160.                 temp |= 0xC0 >> (bit * 2)
  161.         epd_send_data(temp)
  162.        
  163.         temp = 0x00
  164.         for bit in range(4, 8):
  165.             if ((frame_black[i] & (0x80 >> bit)) != 0):
  166.                 temp |= 0xC0 >> ((bit - 4) * 2)
  167.         epd_send_data(temp)
  168.     time.sleep(0.2)
  169.  
  170.     epd_send_cmd(DATA_START_TRANSMISSION_2)
  171.     time.sleep(0.2)
  172.     for i in range(EPD_HEIGHT * EPD_WIDTH / 8):
  173.         epd_send_data(frame_red[i])
  174.     time.sleep(0.2)
  175.  
  176.     epd_send_cmd(DISPLAY_REFRESH)
  177.     epd_wait_until_idle()
  178.  
  179. def epd_init():
  180.     epd_reset()
  181.     epd_send_cmd(POWER_SETTING)
  182.     epd_send_data(0x07)
  183.     epd_send_data(0x00)
  184.     epd_send_data(0x08)
  185.     epd_send_data(0x00)
  186.     epd_send_cmd(BOOSTER_SOFT_START)
  187.     epd_send_data(0x07)
  188.     epd_send_data(0x07)
  189.     epd_send_data(0x07)
  190.     epd_send_cmd(POWER_ON)
  191.     epd_wait_until_idle()
  192.  
  193.     epd_send_cmd(PANEL_SETTING)
  194.     epd_send_data(0xcf)
  195.     epd_send_cmd(VCOM_AND_DATA_INTERVAL_SETTING)
  196.     epd_send_data(0x17)
  197.     epd_send_cmd(PLL_CONTROL)
  198.     epd_send_data(0x39)
  199.     epd_send_cmd(TCON_RESOLUTION)
  200.     epd_send_data(0xC8)
  201.     epd_send_data(0x00)
  202.     epd_send_data(0xC8)
  203.     epd_send_cmd(VCM_DC_SETTING_REGISTER)
  204.     epd_send_data(0x30)
  205.  
  206.     epd_set_lut_bw()
  207.     epd_set_lut_red()
  208.  
  209.     time.sleep(0.2)
  210.  
  211. def paint_clear(fb, colored):
  212.     if colored:
  213.         val = 0x00
  214.     else:
  215.         val = 0xff
  216.     for i in range(len(fb)):
  217.         fb[i] = val
  218.  
  219. def epd_sleep():
  220.     epd_send_cmd(VCOM_AND_DATA_INTERVAL_SETTING)
  221.     epd_send_data(0x17)
  222.     epd_send_cmd(VCM_DC_SETTING_REGISTER)         #to solve Vcom drop
  223.     epd_send_data(0x00)
  224.     epd_send_cmd(POWER_SETTING)         #power setting
  225.     epd_send_data(0x02)        #gate switch to external
  226.     epd_send_data(0x00)
  227.     epd_send_data(0x00)
  228.     epd_send_data(0x00)
  229.     epd_wait_until_idle()
  230.     epd_send_cmd(POWER_OFF)
  231.  
  232. def epaper_pins_off():
  233.     busy.direction = digitalio.Direction.OUTPUT
  234.     res.direction = digitalio.Direction.OUTPUT
  235.     dc.direction = digitalio.Direction.OUTPUT
  236.     cs.direction = digitalio.Direction.OUTPUT
  237.     eink_en.direction = digitalio.Direction.OUTPUT
  238.     res.value = 0
  239.     dc.value = 0
  240.     cs.value = 0
  241.     eink_en.value = 1
  242.  
  243. def epaper_pins_init():
  244.     busy.direction = digitalio.Direction.INPUT
  245.     res.direction = digitalio.Direction.OUTPUT
  246.     res.value = 0
  247.     dc.direction = digitalio.Direction.OUTPUT
  248.     dc.value = 0
  249.     cs.direction = digitalio.Direction.OUTPUT
  250.     cs.value = 0
  251.  
  252.     time.sleep(0.1)
  253.  
  254.     eink_en.direction = digitalio.Direction.OUTPUT
  255.     eink_en.value = 1
  256.  
  257. def epaper_on():
  258.     time.sleep(0.5)
  259.     eink_en.value = 0
  260.     time.sleep(0.5)
  261.  
  262. def epaper_off():
  263.     time.sleep(0.5)
  264.     eink_en.value = 1
  265.  
  266. def paint_draw_pixel(fb, x, y, colored):
  267.     if (x < 0 or x >= EPD_WIDTH or y < 0 or y >= EPD_HEIGHT):
  268.         return
  269.  
  270.     if (colored):
  271.         fb[int((x + y * EPD_WIDTH) / 8)] &= ~(0x80 >> (x % 8))
  272.     else:
  273.         fb[int((x + y * EPD_WIDTH) / 8)] |= 0x80 >> (x % 8)
  274.  
  275. def paint_circle(fb, x, y, radius, colored):
  276.     # Bresenham algorithm
  277.     x_pos = -radius
  278.     y_pos = 0
  279.     err = 2 - 2 * radius
  280.     e2 = 0
  281.  
  282.     while True:
  283.         paint_draw_pixel(fb, x - x_pos, y + y_pos, colored)
  284.         paint_draw_pixel(fb, x + x_pos, y + y_pos, colored)
  285.         paint_draw_pixel(fb, x + x_pos, y - y_pos, colored)
  286.         paint_draw_pixel(fb, x - x_pos, y - y_pos, colored)
  287.         e2 = err
  288.         if (e2 <= y_pos):
  289.             y_pos = y_pos + 1
  290.             err = err + y_pos * 2 + 1
  291.             if(-x_pos == y_pos and e2 <= x_pos):
  292.               e2 = 0
  293.  
  294.         if (e2 > x_pos):
  295.             x_pos = x_pos + 1
  296.             err = err + x_pos * 2 + 1
  297.        
  298.         if x_pos > 0:
  299.             break
  300.  
  301. epaper_pins_init()
  302. epaper_on()
  303. epd_init()
  304.  
  305. paint_clear(frame_black, False)
  306. paint_clear(frame_red, False)
  307.  
  308. for i in range(0, 100, 8):
  309.     paint_circle(frame_black, 100, 100, i, True)
  310.     paint_circle(frame_red, 100, 100, i+4, True)
  311.  
  312. epd_display_frame()
  313. epd_sleep()
  314.  
  315. while True:
  316.     led.value = True
  317.     time.sleep(0.5)
  318.     led.value = False
  319.     time.sleep(0.01)
  320.     print("Hello, world!")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top