B C¿ªa %ã@s`dZddlZddlmZdddgdddgdddgdddgdddgdddgdœZGdd „d ƒZdS) zX from https://github.com/tinue/APA102_Pi This is the main driver module for APA102 LEDs éN)Úceilééé)ÚrgbZrbgZgrbZgbrZbrgZbgrc@sŽeZdZdZdZdZeddddfdd „Zd d „Zd d „Zdd„Z d"dd„Z d#dd„Z d$dd„Z dd„Z dd„Zedd„ƒZdd„Zdd „Zd!S)%ÚAPA102a! Driver for APA102 LEDS (aka "DotStar"). (c) Martin Erzberger 2016-2017 My very first Python code, so I am sure there is a lot to be optimized ;) Public methods are: - set_pixel - set_pixel_rgb - show - clear_strip - cleanup Helper methods for color manipulation are: - combine_color - wheel The rest of the methods are used internally and should not be used by the user of the library. Very brief overview of APA102: An APA102 LED is addressed with SPI. The bits are shifted in one by one, starting with the least significant bit. An LED usually just forwards everything that is sent to its data-in to data-out. While doing this, it remembers its own color and keeps glowing with that color as long as there is power. An LED can be switched to not forward the data, but instead use the data to change it's own color. This is done by sending (at least) 32 bits of zeroes to data-in. The LED then accepts the next correct 32 bit LED frame (with color information) as its new color setting. After having received the 32 bit color frame, the LED changes color, and then resumes to just copying data-in to data-out. The really clever bit is this: While receiving the 32 bit LED frame, the LED sends zeroes on its data-out line. Because a color frame is 32 bits, the LED sends 32 bits of zeroes to the next LED. As we have seen above, this means that the next LED is now ready to accept a color frame and update its color. So that's really the entire protocol: - Start by sending 32 bits of zeroes. This prepares LED 1 to update its color. - Send color information one by one, starting with the color for LED 1, then LED 2 etc. - Finish off by cycling the clock line a few times to get all data to the very last LED on the strip The last step is necessary, because each LED delays forwarding the data a bit. Imagine ten people in a row. When you yell the last color information, i.e. the one for person ten, to the first person in the line, then you are not finished yet. Person one has to turn around and yell it to person 2, and so on. So it takes ten additional "dummy" cycles until person ten knows the color. When you look closer, you will see that not even person 9 knows its own color yet. This information is still with person 2. Essentially the driver sends additional zeroes to LED 1 as long as it takes for the last color frame to make it down the line to the last LED. ééàrrrizcCsx||_| ¡}t |td¡|_||jkr4|j|_n||_|jdddg|j|_t   ¡|_ |j   ||¡|rt||j _ dS)Nrr)Únum_ledÚlowerÚRGB_MAPÚgetrÚMAX_BRIGHTNESSÚglobal_brightnessÚ LED_STARTÚledsÚspidevÚSpiDevÚspiÚopenÚ max_speed_hz)Úselfr rÚorderZbusÚdevicer©rú0/home/pi/TSA-clean/TSA_voice_assistant/apa102.pyÚ__init__Ms   zAPA102.__init__cCs|j dgd¡dS)z¦Sends a start frame to the LED strip. This method clocks out a start frame, telling the receiving LED that it must update its own color now. réN)rÚxfer2)rrrrÚclock_start_frame_szAPA102.clock_start_framecCs.x(t|jddƒD]}|j dg¡qWdS)aÇSends an end frame to the LED strip. As explained above, dummy data must be sent after the last real colour information so that all of the data can reach its destination down the line. The delay is not as bad as with the human example above. It is only 1/2 bit per LED. This is because the SPI clock line needs to be inverted. Say a bit is ready on the SPI data line. The sender communicates this by toggling the clock line. The bit is read by the LED and immediately forwarded to the output data line. When the clock goes down again on the input side, the LED will toggle the clock up on the output to tell the next LED that the bit is ready. After one LED the clock is inverted, and after two LEDs it is in sync again, but one cycle behind. Therefore, for every two LEDs, one bit of delay gets accumulated. For 300 LEDs, 150 additional bits must be fed to the input of LED one so that the data can reach the last LED. Ultimately, we need to send additional numLEDs/2 arbitrary data bits, in order to trigger numLEDs/2 additional clock changes. This driver sends zeroes, which has the benefit of getting LED one partially or fully ready for the next update to the strip. An optimized version of the driver could omit the "clockStartFrame" method if enough zeroes have been sent as part of "clockEndFrame". éérN)Úranger rr)rÚ_rrrÚclock_end_framehszAPA102.clock_end_framecCs0x"t|jƒD]}| |ddd¡q W| ¡dS)z5 Turns off the strip and shows the result right away.rN)r"r Ú set_pixelÚshow)rÚledrrrÚ clear_stripˆszAPA102.clear_stripédc Cs”|dkr dS||jkrdSt||jdƒ}t|ƒ}|d@|jB}d|}||j|<||j||jd<||j||jd<||j||jd<dS)a Sets the color of one pixel in the LED stripe. The changed pixel is not shown yet on the Stripe, it is only written to the pixel buffer. Colors are passed individually. If brightness is not set the global brightness setting is used. rNgY@rrrr)r rrÚintrrr) rÚled_numÚredÚgreenÚblueÚbright_percentZ brightnessZledstartÚ start_indexrrrr%s  zAPA102.set_pixelcCs*| ||d@d?|d@d?|d@|¡dS)a$Sets the color of one pixel in the LED stripe. The changed pixel is not shown yet on the Stripe, it is only written to the pixel buffer. Colors are passed combined (3 bytes concatenated) If brightness is not set the global brightness setting is used. iÿr!iÿééÿN)r%)rr+Z rgb_colorr/rrrÚ set_pixel_rgb¬szAPA102.set_pixel_rgbcCs0d||j}|j|d…|jd|…|_dS)a  Rotate the LEDs by the specified number of positions. Treating the internal LED array as a circular buffer, rotate it by the specified number of positions. The number could be negative, which means rotating in the opposite direction. rN)r r)rÚ positionsÚcutoffrrrÚrotate¹sz APA102.rotatecCs&| ¡|j t|jƒ¡| ¡dS)z†Sends the content of the pixel buffer to the strip. Todo: More than 1024 LEDs requires more than one xfer operation. N)rrrÚlistrr$)rrrrr&Äsz APA102.showcCs|j ¡dS)z3Release the SPI device; Call this method at the endN)rÚclose)rrrrÚcleanupÐszAPA102.cleanupcCs|d>|d>|S)zMake one 3*8 byte color value.r!r1r)r,r-r.rrrÚ combine_colorÕszAPA102.combine_colorcCsz|dkr d}|dkr.| |dd|dd¡S|dkrX|d8}| d|dd|d¡S|d8}| d|dd|d¡S)z=Get a color from a color wheel; Green -> Red -> Blue -> Greenr2éUrréª)r:)rZ wheel_posrrrÚwheelÜsz APA102.wheelcCst|jƒdS)z8For debug purposes: Dump the LED array onto the console.N)Úprintr)rrrrÚ dump_arrayëszAPA102.dump_arrayN)r))r))r)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrr$r(r%r3r6r&r9Ú staticmethodr:r=r?rrrrr s =      r)rCrÚmathrr rrrrrÚs