-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Implement support for APA102 led strips #1941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
30ef58a
esp8266/apa102: Add APA102 support
heyjuvi 1e59bb8
esp8266/apa102: Rework code, especially make use of parts of pull req…
heyjuvi 0c85f93
esp8266/apa102: Add docs
heyjuvi fcb53e8
esp8266/apa102: Rename and fix some things in the apa102 script
heyjuvi 0467fd0
esp8266/apa102: Correct function name to apa102_write in the quickref…
heyjuvi 0fb0446
esp8266/apa102: Remove apa102.py from tests
heyjuvi 76a5bfe
esp8266/apa102: Move things to the correct places
heyjuvi 66dc7b9
esp8266/apa102: Add license information
heyjuvi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ SRC_C = \ | |
esppwm.c \ | ||
esponewire.c \ | ||
espneopixel.c \ | ||
espapa102.c \ | ||
intr.c \ | ||
modpyb.c \ | ||
modpybpin.c \ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Robert Foss, Daniel Busch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include "c_types.h" | ||
#include "eagle_soc.h" | ||
#include "user_interface.h" | ||
#include "espapa102.h" | ||
|
||
#define NOP asm volatile(" nop \n\t") | ||
|
||
static inline void _esp_apa102_send_byte(uint32_t clockPinMask, uint32_t dataPinMask, uint8_t byte) { | ||
for (uint32_t i = 0; i < 8; i++) { | ||
if (byte & 0x80) { | ||
// set data pin high | ||
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, dataPinMask); | ||
} else { | ||
// set data pin low | ||
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, dataPinMask); | ||
} | ||
|
||
// set clock pin high | ||
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask); | ||
byte <<= 1; | ||
NOP; | ||
NOP; | ||
|
||
// set clock pin low | ||
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask); | ||
NOP; | ||
NOP; | ||
} | ||
} | ||
|
||
static inline void _esp_apa102_send_colors(uint32_t clockPinMask, uint32_t dataPinMask, uint8_t *pixels, uint32_t numBytes) { | ||
for (uint32_t i = 0; i < numBytes / 4; i++) { | ||
_esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4 + 3] | 0xE0); | ||
_esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4 + 2]); | ||
_esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4 + 1]); | ||
_esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4]); | ||
} | ||
} | ||
|
||
static inline void _esp_apa102_start_frame(uint32_t clockPinMask, uint32_t dataPinMask) { | ||
for (uint32_t i = 0; i < 4; i++) { | ||
_esp_apa102_send_byte(clockPinMask, dataPinMask, 0x00); | ||
} | ||
} | ||
|
||
static inline void _esp_apa102_append_additionial_cycles(uint32_t clockPinMask, uint32_t dataPinMask, uint32_t numBytes) { | ||
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, dataPinMask); | ||
|
||
// we need to write some more clock cycles, because each led | ||
// delays the data by one edge after inverting the clock | ||
for (uint32_t i = 0; i < numBytes / 8 + ((numBytes / 4) % 2); i++) { | ||
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask); | ||
NOP; | ||
NOP; | ||
|
||
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask); | ||
NOP; | ||
NOP; | ||
} | ||
} | ||
|
||
static inline void _esp_apa102_end_frame(uint32_t clockPinMask, uint32_t dataPinMask) { | ||
for (uint32_t i = 0; i < 4; i++) { | ||
_esp_apa102_send_byte(clockPinMask, dataPinMask, 0xFF); | ||
} | ||
} | ||
|
||
void esp_apa102_write(uint8_t clockPin, uint8_t dataPin, uint8_t *pixels, uint32_t numBytes) { | ||
uint32_t clockPinMask, dataPinMask; | ||
|
||
clockPinMask = 1 << clockPin; | ||
dataPinMask = 1 << dataPin; | ||
|
||
// start the frame | ||
_esp_apa102_start_frame(clockPinMask, dataPinMask); | ||
|
||
// write pixels | ||
_esp_apa102_send_colors(clockPinMask, dataPinMask, pixels, numBytes); | ||
|
||
// end the frame | ||
_esp_apa102_append_additionial_cycles(clockPinMask, dataPinMask, numBytes); | ||
_esp_apa102_end_frame(clockPinMask, dataPinMask); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Robert Foss, Daniel Busch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
void esp_apa102_write(uint8_t clockPin, uint8_t dataPin, uint8_t *pixels, uint32_t numBytes); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# APA102 driver for MicroPython on ESP8266 | ||
# MIT license; Copyright (c) 2016 Robert Foss, Daniel Busch | ||
|
||
from esp import apa102_write | ||
|
||
class APA102: | ||
def __init__(self, clock_pin, data_pin, n): | ||
self.clock_pin = clock_pin | ||
self.data_pin = data_pin | ||
self.n = n | ||
self.buf = bytearray(n * 4) | ||
|
||
self.clock_pin.init(clock_pin.OUT) | ||
self.data_pin.init(data_pin.OUT) | ||
|
||
def __setitem__(self, index, val): | ||
r, g, b, brightness = val | ||
self.buf[index * 4] = r | ||
self.buf[index * 4 + 1] = g | ||
self.buf[index * 4 + 2] = b | ||
self.buf[index * 4 + 3] = brightness | ||
|
||
def __getitem__(self, index): | ||
i = index * 4 | ||
return self.buf[i], self.buf[i + 1], self.buf[i + 2], self.buf[i + 3] | ||
|
||
def write(self): | ||
apa102_write(self.clock_pin, self.data_pin, self.buf) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need to set them as output pins here?
self.clock_pin.init(clock_pin.OUT)
self.data_pin.init(data_pin.OUT)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And you're also right with this one, i think. :)