{"id":104,"date":"2019-11-25T13:54:39","date_gmt":"2019-11-25T05:54:39","guid":{"rendered":"http:\/\/cf3b5.com\/blog\/?p=104"},"modified":"2019-11-26T09:35:48","modified_gmt":"2019-11-26T01:35:48","slug":"%e7%94%a8raspberrypi%e6%8e%a7%e5%88%b6ws2812%e5%85%a8%e5%bd%a9%e7%81%af%e5%b8%a6","status":"publish","type":"post","link":"http:\/\/cf3b5.com\/blog\/?p=104","title":{"rendered":"\u7528RaspberryPi\u63a7\u5236WS2812\u5168\u5f69\u706f\u5e26"},"content":{"rendered":"\n<p>WS2812\u611f\u89c9\u662f\u73b0\u5728\u6dd8\u5b9d\u4e0a\u6bd4\u8f83\u5bb9\u6613\u627e\u5230\u7684\u5168\u5f69LED\u706f\u73e0\uff0c\u706f\u73e0\u53ea\u67093\u6839\u7ebf\u63a5\u7ebf\uff0c\u5e76\u4e14\u652f\u6301\u4e32\u8054\uff0c\u5e76\u4e14\u901a\u8fc7\u4fe1\u53f7\u7ebf\uff0c\u53ef\u4ee5\u81ea\u7531\u7684\u63a7\u5236\u4e32\u8054\u7684\u6bcf\u76cf\u706f\u73e0\u7684\u989c\u8272\u548c\u5f00\u5173\uff08\u9ed1\u8272\u5c31\u662f\u5173\uff09\uff0c\u4e0d\u8fc7\u4eae\u5ea6\u4f3c\u4e4e\u6ca1\u6cd5\u72ec\u7acb\u63a7\u5236<\/p>\n\n\n\n<h3>\u7535\u8def\u56fe<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"http:\/\/cf3b5.com\/blog\/wp-content\/uploads\/2019\/11\/image-1024x631.png\" alt=\"\" class=\"wp-image-105\" width=\"402\" height=\"247\" srcset=\"http:\/\/cf3b5.com\/blog\/wp-content\/uploads\/2019\/11\/image-1024x631.png 1024w, http:\/\/cf3b5.com\/blog\/wp-content\/uploads\/2019\/11\/image-300x185.png 300w, http:\/\/cf3b5.com\/blog\/wp-content\/uploads\/2019\/11\/image-768x473.png 768w, http:\/\/cf3b5.com\/blog\/wp-content\/uploads\/2019\/11\/image.png 1224w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/figure><\/div>\n\n\n\n<p>\u84dd\u8272\u662f\u4fe1\u53f7\u7ebf\uff0c\u7ea2\u8272\u662f\u6b63\u6781\uff0c\u9ed1\u8272\u662f\u8d1f\u6781\uff0c\u5982\u679c\u706f\u7c92\u4e0d\u662f\u592a\u591a\u7684\u8bdd\uff0c\u4e0d\u589e\u538b\u76f4\u63a5\u4ece\u4e3b\u677f\u4e0a\u53d6\u7535\u4e5f\u884c<\/p>\n\n\n\n<h3>RaspberryPi\u7684\u51c6\u5907<\/h3>\n\n\n\n<p>\u5b89\u88c5\u4e00\u4e9b\u57fa\u672c\u7684\u5e93<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">sudo apt-get install gcc make build-essential python-dev git scons swig<\/pre>\n\n\n\n<p>\u597d\u50cf\u8bf4\u97f3\u9891\u8f93\u51fa\u5fc5\u987b\u7981\u6b62\u6389\uff0c\u7f16\u8f91\/etc\/modprobe.d\/snd-blacklist.conf\u6587\u4ef6\uff0c\u5728\u672b\u5c3e\u589e\u52a0\u4e0b\u9762\u8fd9\u884c\u5185\u5bb9<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">blacklist snd_bcm2835<\/pre>\n\n\n\n<h3>\u4e0b\u8f7dpython\u7684ws2812\u63a7\u5236\u5e93<\/h3>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">git clone https:\/\/github.com\/jgarff\/rpi_ws281x<\/pre>\n\n\n\n<p>\u7f16\u8bd1&amp;\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">cd rpi_ws281x\/\nsudo scons\ncd python\nsudo python setup.py build\nsudo python setup.py install<\/pre>\n\n\n\n<p>\u5b8c\u6210\u540eneopixel\u5e93\u5c31\u5b89\u88c5\u5b8c\u6210\u4e86\uff0c\u4e0b\u662f\u662f\u6d4b\u8bd5\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">#!\/usr\/bin\/env python3\n# NeoPixel library strandtest example\n# Author: Tony DiCola (tony@tonydicola.com)\n#\n# Direct port of the Arduino NeoPixel library strandtest example.  Showcases\n# various animations on a strip of NeoPixels.\n \nimport time\nfrom neopixel import *\nimport argparse\n \n# LED strip configuration:\nLED_COUNT      = 16      # \u706f\u7684\u6570\u91cf\nLED_PIN        = 18      # \u8d1f\u8d23\u8f93\u51fa\u4fe1\u53f7\u7684\u6811\u8393\u6d3e\u9635\u811a\uff0c18\uff08\u6ce8\u610f\u8fd9\u4e2a\u771f\u5b9e\u7684GPIO\u7684\u987a\u5e8f\n#LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI \/dev\/spidev0.0).\nLED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)\nLED_DMA        = 10      # DMA channel to use for generating signal (try 10)\nLED_BRIGHTNESS = 255     # \u4eae\u5ea6\uff0c255\u6700\u5927\uff0c\u5982\u679c\u706f\u592a\u591a\u53c8\u6ca1\u6709\u5916\u63a5\u6269\u5f20\u4f9b\u7535\u7684\u8bdd\uff0c\u8981\u964d\u4f4e\u4e00\u70b9\uff0c\u5426\u5219\u4f1a\u4f9b\u7535\u4e0d\u8db3\uff0c\u621150\u90fd\u633a\u4eae\u4e86\nLED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)\nLED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53\n \n \n \n# Define functions which animate LEDs in various ways.\ndef colorWipe(strip, color, wait_ms=50):\n    \"\"\"Wipe color across display a pixel at a time.\"\"\"\n    for i in range(strip.numPixels()):\n        strip.setPixelColor(i, color)\n        strip.show()\n        time.sleep(wait_ms\/1000.0)\n \ndef theaterChase(strip, color, wait_ms=50, iterations=10):\n    \"\"\"Movie theater light style chaser animation.\"\"\"\n    for j in range(iterations):\n        for q in range(3):\n            for i in range(0, strip.numPixels(), 3):\n                strip.setPixelColor(i+q, color)\n            strip.show()\n            time.sleep(wait_ms\/1000.0)\n            for i in range(0, strip.numPixels(), 3):\n                strip.setPixelColor(i+q, 0)\n \ndef wheel(pos):\n    \"\"\"Generate rainbow colors across 0-255 positions.\"\"\"\n    if pos &lt; 85:\n        return Color(pos * 3, 255 - pos * 3, 0)\n    elif pos &lt; 170:\n        pos -= 85\n        return Color(255 - pos * 3, 0, pos * 3)\n    else:\n        pos -= 170\n        return Color(0, pos * 3, 255 - pos * 3)\n \ndef rainbow(strip, wait_ms=20, iterations=1):\n    \"\"\"Draw rainbow that fades across all pixels at once.\"\"\"\n    for j in range(256*iterations):\n        for i in range(strip.numPixels()):\n            strip.setPixelColor(i, wheel((i+j) &amp; 255))\n        strip.show()\n        time.sleep(wait_ms\/1000.0)\n \ndef rainbowCycle(strip, wait_ms=20, iterations=5):\n    \"\"\"Draw rainbow that uniformly distributes itself across all pixels.\"\"\"\n    for j in range(256*iterations):\n        for i in range(strip.numPixels()):\n            strip.setPixelColor(i, wheel((int(i * 256 \/ strip.numPixels()) + j) &amp; 255))\n        strip.show()\n        time.sleep(wait_ms\/1000.0)\n \ndef theaterChaseRainbow(strip, wait_ms=50):\n    \"\"\"Rainbow movie theater light style chaser animation.\"\"\"\n    for j in range(256):\n        for q in range(3):\n            for i in range(0, strip.numPixels(), 3):\n                strip.setPixelColor(i+q, wheel((i+j) % 255))\n            strip.show()\n            time.sleep(wait_ms\/1000.0)\n            for i in range(0, strip.numPixels(), 3):\n                strip.setPixelColor(i+q, 0)\n \n# Main program logic follows:\nif __name__ == '__main__':\n    # \u5904\u7406\u7a0b\u5e8f\u8fd0\u884c\u7684\u547d\u4ee4\u884c\u53c2\u6570\n    parser = argparse.ArgumentParser()\n    parser.add_argument('-c', '--clear', action='store_true', help='clear the display on exit')\n    args = parser.parse_args()\n \n    # Create NeoPixel object with appropriate configuration.\n    strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)\n    # Intialize the library (must be called once before other functions).\n    strip.begin()\n \n    print ('Press Ctrl-C to quit.')\n    if not args.clear:\n        print('Use \"-c\" argument to clear LEDs on exit')\n \n    try:\n \n        while True:\n            print ('Color wipe animations.')\n            colorWipe(strip, Color(255, 0, 0))  # Red wipe\n            colorWipe(strip, Color(0, 255, 0))  # Blue wipe\n            colorWipe(strip, Color(0, 0, 255))  # Green wipe\n            print ('Theater chase animations.')\n            theaterChase(strip, Color(127, 127, 127))  # White theater chase\n            theaterChase(strip, Color(127,   0,   0))  # Red theater chase\n            theaterChase(strip, Color(  0,   0, 127))  # Blue theater chase\n            print ('Rainbow animations.')\n            rainbow(strip)\n            rainbowCycle(strip)\n            theaterChaseRainbow(strip)\n \n    except KeyboardInterrupt:\n        if args.clear:\n            colorWipe(strip, Color(0,0,0), 10)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>WS2812\u611f\u89c9\u662f\u73b0\u5728\u6dd8\u5b9d\u4e0a\u6bd4\u8f83\u5bb9\u6613\u627e\u5230\u7684\u5168\u5f69LED\u706f\u73e0\uff0c\u706f\u73e0\u53ea\u67093\u6839\u7ebf\u63a5\u7ebf\uff0c\u5e76\u4e14\u652f\u6301\u4e32\u8054\uff0c\u5e76\u4e14\u901a\u8fc7\u4fe1\u53f7\u7ebf\uff0c\u53ef&hellip; <a href=\"http:\/\/cf3b5.com\/blog\/?p=104\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">\u7528RaspberryPi\u63a7\u5236WS2812\u5168\u5f69\u706f\u5e26<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/104"}],"collection":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=104"}],"version-history":[{"count":5,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":113,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions\/113"}],"wp:attachment":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}