注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题

dcexpert的个人空间 http://home.eeworld.com.cn/space-uid-573537.html [收藏] [复制] [分享] [RSS]

日志

用micropython控制WS2812彩屏显示汉字和小图案

已有 82 次阅读2018-5-8 17:23 |个人分类:MicroPython

在ESP8266/ESP32上,使用MicroPython可以非常容易的控制WS2812彩灯。通过neopixel模块,只要简单的几行代码就可以控制WS2812显示各种颜色。

  1. import neopixel
  2. import machine

  3. np = neopixel.NeoPixel(machine.Pin(2), 256)
  4. for i in range(256):
  5.     np[i] = (i//16, 0, 0)
  6. np.write()
复制代码




WS2812可以做成灯带,也可以做成彩屏。以16x16彩屏为例,它有16 x 16 = 256颗WS2812,这些LED是串联方式连接的,可以看成是一种特殊的灯带。它们的排列方式如下:


知道了排列方式,就可以用它来显示各种内容。16x16的彩屏虽然分辨率不高,但是也可以显示汉字:




  1. from machine import Pin
  2. import time

  3. from neo16x16 import neo16x16

  4. np = neo16x16(Pin(5))

  5. npdat=[
  6. 0x8040,0x0201,0x4042,0x4204,0x1042,0x4230,0x0342,0x7F00,
  7. 0x0342,0x4230,0x1042,0x4204,0x4042,0x0201,0x8040,0x0000,
  8. 0x4080,0x0202,0x4230,0x7842,0x4210,0x0842,0x4210,0xFFFE,
  9. 0x4210,0x0842,0x4210,0x0842,0x4210,0x0802,0x4000,0x0000,
  10. 0x0020,0x2410,0x4824,0x2421,0x7FFE,0xC440,0x4122,0x0402,
  11. 0x2020,0xFFC8,0x0C20,0x4428,0x222C,0x0582,0xF820,0x0000,
  12. 0x0810,0x0820,0x0310,0x0B00,0xFFFF,0x0900,0x0310,0x0000,
  13. 0x0810,0x0820,0x4310,0x0B01,0x7FFF,0x0800,0x0010,0x0000,
  14. 0x0800,0x0061,0x41C0,0x0004,0x3FE0,0x4012,0x4404,0x1842,
  15. 0x4100,0x0302,0x4030,0x301E,0x0040,0x0180,0x0E00,0x0000,
  16. 0x0404,0x2040,0x0104,0x2100,0xFFE4,0x3C90,0x0927,0x2490,
  17. 0x0924,0x2492,0x8924,0x27FE,0x0004,0x2000,0x0004,0x0000,
  18. 0x8000,0x0006,0x1FFE,0x4440,0x0222,0x4440,0x0222,0x7FFE,
  19. 0x0222,0x4440,0x4222,0x4441,0x7FFE,]

  20. n = 0
  21. while True:
  22.     np.show(npdat, n)
  23.     n = (n+16)%112
  24.     time.sleep(500)
复制代码




也可以显示小图案:




  1. from machine import Pin
  2. import time

  3. from neo16x16_img import neo16x16_img

  4. np = neo16x16_img(Pin(2))

  5. npdat=[
  6. 0x2AF3AF, 0x000000, 0x000000, 0x000000,
  7. 0x000000, 0x000000, 0x000000, 0x000000,
  8. 0x000000, 0x000000, 0x000000, 0x000000,
  9. 0x000000, 0x000000, 0x6CF000, 0x39F19F,
  10. 0x06F000, 0x5CF29F, 0x000000, 0x000000,
  11. 0x000000, 0x000000, 0x000000, 0x000000,
  12. 0x000000, 0x000000, 0x000000, 0x000000,
  13. 0x000000, 0x5CF000, 0x07F2AF, 0x00014E,
  14. 0x000000, 0x06F16E, 0x6CF2AF, 0x000000,
  15. 0x000000, 0x000000, 0x000000, 0x000000,
  16. 0x000000, 0x000000, 0x000000, 0x000000,
  17. 0x6DF000, 0x06F29F, 0x00004E, 0x000000,
  18. 0x000000, 0x36E000, 0x07F05F, 0x5DF2AF,
  19. 0x0005EF, 0x000000, 0x000000, 0x000000,
  20. 0x000000, 0x000000, 0x000000, 0x5CF000,
  21. 0x08F29F, 0x14A06F, 0x000000, 0x000000,
  22. 0x000000, 0x000000, 0x26E000, 0x08F05F,
  23. 0x6CF19F, 0x000000, 0x000000, 0x000000,
  24. 0x000000, 0x5B76CA, 0x000000, 0x08F4BC,
  25. 0x25D05F, 0x000000, 0x000000, 0x000000,
  26. 0x000000, 0x000000, 0x000000, 0x26F000,
  27. 0x4B839F, 0x5A45A3, 0x0007C8, 0x000000,
  28. 0x8A7000, 0x5C85A6, 0x7B75B5, 0x000000,
  29. 0x000000, 0x000000, 0x000000, 0x000000,
  30. 0x000000, 0x000000, 0x000000, 0x000000,
  31. 0x000000, 0x6A76B8, 0x6A75B7, 0x8A6695,
  32. 0x000000, 0x5A76C9, 0x8B8594, 0x000000,
  33. 0x000000, 0x000000, 0x000000, 0x000000,
  34. 0x000000, 0x000000, 0x000000, 0x000000,
  35. 0x000000, 0x000000, 0x6DA8CA, 0x000000,
  36. 0x000000, 0x000000, 0x000000, 0x000000,
  37. 0x000000, 0x000000, 0x000000, 0x000000,
  38. 0x000000, 0x000000, 0x000000, 0x000000,
  39. 0x121145, 0x000000, 0x000000, 0x000000,
  40. 0x000000, 0x000000, 0x000000, 0x169156,
  41. 0x000000, 0x000000, 0x000000, 0x000000,
  42. 0x000000, 0x000000, 0x000000, 0x234000,
  43. 0x15818B, 0x000217, 0x000000, 0x000000,
  44. 0x000000, 0x000000, 0x129000, 0x0AE17B,
  45. 0x000169, 0x000000, 0x000000, 0x000000,
  46. 0x000000, 0x000000, 0x000000, 0x19C301,
  47. 0x24709C, 0x00013A, 0x000000, 0x000000,
  48. 0x000000, 0x000000, 0x116000, 0x169237,
  49. 0x24718B, 0x245169, 0x000000, 0x000000,
  50. 0x000000, 0x235000, 0x0CF09D, 0x1590AE,
  51. 0x159159, 0x000000, 0x000000, 0x000000,
  52. 0x000000, 0x000000, 0x000000, 0x17C149,
  53. 0x09D18C, 0x0BF0BE, 0x23519C, 0x000234,
  54. 0x000000, 0x17B000, 0x16B15C, 0x14817C,
  55. 0x000024, 0x000000, 0x000000, 0x000000,
  56. 0x000000, 0x000000, 0x000000, 0x002013,
  57. 0x012000, 0x11A126, 0x000116, 0x000000,
  58. 0x000000, 0x000000, 0x000000, 0x048012,
  59. 0x16B149, 0x12716A, 0x000000, 0x000000,
  60. 0x000000, 0x12811B, 0x147247, 0x09E16A,
  61. 0x15B09D, 0x00010A, 0x000000, 0x000000,
  62. 0x000000, 0x000000, 0x16C127, 0x0BE08D,
  63. 0x17A0BF, 0x18B09C, 0x13A17A, 0x000227,
  64. 0x214000, 0x0AE17A, 0x1680AE, 0x0AD235,
  65. 0x0BE0BF, 0x00009C, 0x000000, 0x000000,
  66. 0x000000, 0x000000, 0x236235, 0x158246,
  67. 0x000245, 0x246312, 0x18B168, 0x200145,
  68. 0x122FFF, 0x000123, 0x000000, 0x000000,
  69. 0x000000, 0x235000, 0x000000, 0x000000,
  70. 0x000000, 0x000000, 0x000000, 0x9FF9FF,
  71. 0x9FF9FF, 0x0009FF, 0x000000, 0x000000,
  72. 0x000000, 0x9FF000, 0x0CF9FF, 0x0CF0CF,
  73. 0x0CF0CF, 0x9FF9FF, 0x000000, 0x000000,
  74. 0x000000, 0x9FF000, 0x0CF0CF, 0x0CF0CF,
  75. 0x0CF0CF, 0x0CF0CF, 0x9FF0CF, 0x000000,
  76. 0x9FF000, 0x0CF0CF, 0x0CF0CF, 0x0CF0CF,
  77. 0x0CF0CF, 0x0CF0CF, 0x9FF0CF, 0x000000,
  78. 0x000000, 0x0CF9FF, 0x0CF0CF, 0x0CF0CF,
  79. 0x0CF0CF, 0x0CF0CF, 0x0CF0CF, 0x0009FF,
  80. 0x0CF9FF, 0x0CF0CF, 0x0CF0CF, 0x0CF0CF,
  81. 0x0CF0CF, 0x0CF0CF, 0x0CF0CF, 0x0009FF,
  82. 0x9FF000, 0x0CF0CF, 0x0CF0CF, 0x0CF0CF,
  83. 0x0CF0CF, 0x0CF0CF, 0x0CF0CF, 0x9FF0CF,
  84. 0x0CF9FF, 0x0CF0CF, 0x0CF0CF, 0x0000FF,
  85. 0x0CF0FF, 0x0CF0CF, 0x0CF0CF, 0x0009FF,
  86. 0x9FF000, 0x0CF0CF, 0x0CF0CF, 0x0FF0CF,
  87. 0x0FF000, 0x0CF0CF, 0x0CF0CF, 0x9FF0CF,
  88. 0x0CF9FF, 0x0CF0CF, 0x0FF0CF, 0x000000,
  89. 0x0FF000, 0x0CF0CF, 0x0CF0CF, 0x0009FF,
  90. 0x000000, 0x0CF9FF, 0x0CF0CF, 0x0000FF,
  91. 0x000000, 0x0CF0FF, 0x0CF000, 0x0009FF,
  92. 0x9FF000, 0x0CF0CF, 0x0FF0CF, 0x000000,
  93. 0x0FF000, 0x0CF0CF, 0x9FF0CF, 0x000000,
  94. 0x000000, 0x9FF000, 0x0CF0CF, 0x0000FF,
  95. 0x000000, 0x0CF0FF, 0x9FF0CF, 0x000000,
  96. 0x000000, 0x9FF000, 0x0009FF, 0x000000,
  97. 0x000000, 0x9FF9FF, 0x000000, 0x000000,
  98. 0x000000, 0x000000, 0x000000, 0x000000,
  99. 0x000000, 0x000000, 0x000000, 0x000000,
  100. 0x000000, 0x000000, 0x000000, 0x000000,
  101. 0x000000, 0x000000, 0x000000, 0x000000,
  102. 0x000000, 0x000000, 0x000000, 0x000000,
  103. 0x000000, 0x000000, 0x000000, 0x000000,
  104. 0x000000, 0x000000, 0xEEE000, 0xFC9FC9,
  105. 0xFC9FC9, 0x000000, 0x000000, 0x000000,
  106. 0x000000, 0x000000, 0xFC9000, 0xFC9FC9,
  107. 0xFC9FC9, 0xEEEFC9, 0x000000, 0x000000,
  108. 0x000000, 0xAAA000, 0x555FC9, 0x000000,
  109. 0x333000, 0xEEEFC9, 0x000000, 0x000000,
  110. 0x000000, 0x000000, 0xFC9FC9, 0x000000,
  111. 0x000F90, 0xFC9000, 0x000FC9, 0x000000,
  112. 0x000000, 0xFC9000, 0x000FC9, 0xF99000,
  113. 0x000000, 0xFC9FC9, 0x000000, 0x000000,
  114. 0x000000, 0x000000, 0xFC9FC9, 0x000000,
  115. 0x000000, 0xFC9000, 0x000FC9, 0x000000,
  116. 0x000000, 0xFC9000, 0x000FC9, 0x000000,
  117. 0x000000, 0xFC9FC9, 0x000000, 0x000000,
  118. 0x000000, 0x000000, 0xFC9FC9, 0x000000,
  119. 0x000000, 0xFC9000, 0x000FC9, 0x000000,
  120. 0x000000, 0xFC9000, 0x000FC9, 0x000000,
  121. 0x000000, 0xFC9FC9, 0x000000, 0x000000,
  122. 0x000000, 0x000000, 0xFC9FC9, 0x000000,
  123. 0x000000, 0xFC9000, 0x000FC9, 0x000000,
  124. 0x000000, 0xFC9000, 0x000FC9, 0xF99000,
  125. 0x000000, 0xFC9FC9, 0x000000, 0x000000,
  126. 0x000000, 0x000000, 0xFC9FC9, 0x000000,
  127. 0x000F90, 0xFC9000, 0x000FC9, 0x000000,
  128. 0x000000, 0xBBB000, 0x333FC9, 0x000000,
  129. 0x111000, 0xEEEFC9, 0x000000, 0x000000,
  130. 0x000000, 0x000000, 0xFC9000, 0xFC9FC9,
  131. 0xFC9FC9, 0xFC9FC9, 0x000000, 0x000000,
  132. 0x000000, 0x000000, 0xFC9000, 0xFC9FC9,
  133. 0xFC9FC9, 0x000000, 0x000000, 0x000000,
  134. ]

  135. n = 0
  136. while 1:
  137.     np.show(npdat, n)
  138.     n = (n+16)%64
  139.     time.sleep_ms(1000)

复制代码


为了方便产生显示需要的数据,还编写了一个小软件,只要输入文字或者选择图片,就可以自动产生显示数据。然后用新的数据替换程序中的,就可以显示新的内容了。



https://github.com/shaoziyang/mi ... ls/microbit_toolbox



此内容由EEWORLD论坛网友dcexpert原创,如需转载或用于商业用途需征得作者同意并注明出处


本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

小黑屋|手机版|Archiver|电子工程世界 ( 京ICP证 060456 )

GMT+8, 2018-10-19 09:05 , Processed in 0.016320 second(s), 10 queries , Gzip On, MemCache On.

Powered by EEWORLD电子工程世界

© 2018 http://bbs.eeworld.com.cn/

返回顶部