diff options
| author | Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> | 2019-02-15 20:32:31 +0100 |
|---|---|---|
| committer | MechMerlin <30334081+mechmerlin@users.noreply.github.com> | 2019-02-15 11:32:31 -0800 |
| commit | 6f50c7eba19c5b9e60387e70928300a86f0d4816 (patch) | |
| tree | 3797ddf09115c6771937a10484d50b21b32b6cc0 /drivers | |
| parent | 2ba54690e68892fe789a50bb1611af329ec2e438 (diff) | |
| download | qmk_firmware-6f50c7eba19c5b9e60387e70928300a86f0d4816.tar.gz qmk_firmware-6f50c7eba19c5b9e60387e70928300a86f0d4816.zip | |
Added APA102 support (#4940)
* Fixed pin for RGB
* Re-did apa102 stuff
* changed to use pincontrol functions
* Removed a newline?
* Changed to comply with project style
Diffstat (limited to 'drivers')
| -rwxr-xr-x | drivers/avr/apa102.c | 101 | ||||
| -rwxr-xr-x | drivers/avr/apa102.h | 46 |
2 files changed, 147 insertions, 0 deletions
diff --git a/drivers/avr/apa102.c b/drivers/avr/apa102.c new file mode 100755 index 000000000..55a0d5777 --- /dev/null +++ b/drivers/avr/apa102.c | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | /* | ||
| 2 | * APA102 lib V1.0a | ||
| 3 | * | ||
| 4 | * Controls APA102 RGB-LEDs | ||
| 5 | * Author: Mikkel (Duckle29 on github) | ||
| 6 | * | ||
| 7 | * Dec 22th, 2017 v1.0a Initial Version | ||
| 8 | * | ||
| 9 | * This program is free software: you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License as published by | ||
| 11 | * the Free Software Foundation, either version 2 of the License, or | ||
| 12 | * (at your option) any later version. | ||
| 13 | * | ||
| 14 | * This program is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | * GNU General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU General Public License | ||
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | */ | ||
| 22 | |||
| 23 | #include "apa102.h" | ||
| 24 | #include <avr/interrupt.h> | ||
| 25 | #include <avr/io.h> | ||
| 26 | #include <util/delay.h> | ||
| 27 | #include "debug.h" | ||
| 28 | |||
| 29 | // Setleds for standard RGB | ||
| 30 | void inline apa102_setleds(LED_TYPE *ledarray, uint16_t leds){ | ||
| 31 | apa102_setleds_pin(ledarray,leds, _BV(RGB_DI_PIN & 0xF), _BV(RGB_CLK_PIN & 0xF)); | ||
| 32 | } | ||
| 33 | |||
| 34 | void static inline apa102_setleds_pin(LED_TYPE *ledarray, uint16_t leds, uint8_t pinmask_DI, uint8_t pinmask_CLK){ | ||
| 35 | pinMode(RGB_DI_PIN, PinDirectionOutput); | ||
| 36 | pinMode(RGB_CLK_PIN, PinDirectionOutput); | ||
| 37 | |||
| 38 | apa102_send_array((uint8_t*)ledarray,leds) | ||
| 39 | } | ||
| 40 | |||
| 41 | void apa102_send_array(uint8_t *data, uint16_t leds){ // Data is struct of 3 bytes. RGB - leds is number of leds in data | ||
| 42 | apa102_start_frame(); | ||
| 43 | while(leds--){ | ||
| 44 | apa102_send_frame(0xFF000000 | (data->b << 16) | (data->g << 8) | data->r); | ||
| 45 | data++; | ||
| 46 | } | ||
| 47 | apa102_end_frame(leds); | ||
| 48 | } | ||
| 49 | |||
| 50 | void apa102_send_frame(uint32_t frame){ | ||
| 51 | for(uint32_t i=0xFF; i>0;){ | ||
| 52 | apa102_send_byte(frame & i); | ||
| 53 | i = i << 8; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | void apa102_start_frame(){ | ||
| 58 | apa102_send_frame(0); | ||
| 59 | } | ||
| 60 | |||
| 61 | void apa102_end_frame(uint16_t leds) | ||
| 62 | { | ||
| 63 | // This function has been taken from: https://github.com/pololu/apa102-arduino/blob/master/APA102.h | ||
| 64 | // and adapted. The code is MIT licensed. I think thats compatible? | ||
| 65 | |||
| 66 | // We need to send some more bytes to ensure that all the LEDs in the | ||
| 67 | // chain see their new color and start displaying it. | ||
| 68 | // | ||
| 69 | // The data stream seen by the last LED in the chain will be delayed by | ||
| 70 | // (count - 1) clock edges, because each LED before it inverts the clock | ||
| 71 | // line and delays the data by one clock edge. Therefore, to make sure | ||
| 72 | // the last LED actually receives the data we wrote, the number of extra | ||
| 73 | // edges we send at the end of the frame must be at least (count - 1). | ||
| 74 | // For the APA102C, that is sufficient. | ||
| 75 | // | ||
| 76 | // The SK9822 only updates after it sees 32 zero bits followed by one more | ||
| 77 | // rising edge. To avoid having the update time depend on the color of | ||
| 78 | // the last LED, we send a dummy 0xFF byte. (Unfortunately, this means | ||
| 79 | // that partial updates of the beginning of an LED strip are not possible; | ||
| 80 | // the LED after the last one you are trying to update will be black.) | ||
| 81 | // After that, to ensure that the last LED in the chain sees 32 zero bits | ||
| 82 | // and a rising edge, we need to send at least 65 + (count - 1) edges. It | ||
| 83 | // is sufficent and simpler to just send (5 + count/16) bytes of zeros. | ||
| 84 | // | ||
| 85 | // We are ignoring the specification for the end frame in the APA102/SK9822 | ||
| 86 | // datasheets because it does not actually ensure that all the LEDs will | ||
| 87 | // start displaying their new colors right away. | ||
| 88 | |||
| 89 | apa102_send_byte(0xFF); | ||
| 90 | for (uint16_t i = 0; i < 5 + leds / 16; i++){ | ||
| 91 | apa102_send_byte(0); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | void apa102_send_byte(uint8_t byte){ | ||
| 96 | uint8_t i; | ||
| 97 | for (i = 0; i < 8; i++){ | ||
| 98 | digitalWrite(RGB_DI_PIN, !!(byte & (1 << (7-i))); | ||
| 99 | digitalWrite(RGB_CLK_PIN, PinLevelHigh); | ||
| 100 | } | ||
| 101 | } | ||
diff --git a/drivers/avr/apa102.h b/drivers/avr/apa102.h new file mode 100755 index 000000000..e7d7c3684 --- /dev/null +++ b/drivers/avr/apa102.h | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* | ||
| 2 | * light weight WS2812 lib include | ||
| 3 | * | ||
| 4 | * Version 2.3 - Nev 29th 2015 | ||
| 5 | * Author: Tim (cpldcpu@gmail.com) | ||
| 6 | * | ||
| 7 | * Please do not change this file! All configuration is handled in "ws2812_config.h" | ||
| 8 | * | ||
| 9 | * This program is free software: you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License as published by | ||
| 11 | * the Free Software Foundation, either version 2 of the License, or | ||
| 12 | * (at your option) any later version. | ||
| 13 | * | ||
| 14 | * This program is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | * GNU General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU General Public License | ||
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | */ | ||
| 22 | |||
| 23 | #pragma once | ||
| 24 | |||
| 25 | #include <avr/io.h> | ||
| 26 | #include <avr/interrupt.h> | ||
| 27 | |||
| 28 | #include "rgblight_types.h" | ||
| 29 | |||
| 30 | |||
| 31 | /* User Interface | ||
| 32 | * | ||
| 33 | * Input: | ||
| 34 | * ledarray: An array of GRB data describing the LED colors | ||
| 35 | * number_of_leds: The number of LEDs to write | ||
| 36 | * pinmask (optional): Bitmask describing the output bin. e.g. _BV(PB0) | ||
| 37 | * | ||
| 38 | * The functions will perform the following actions: | ||
| 39 | * - Set the data-out pin as output | ||
| 40 | * - Send out the LED data | ||
| 41 | * - Wait 50�s to reset the LEDs | ||
| 42 | */ | ||
| 43 | |||
| 44 | void apa102_setleds (LED_TYPE *ledarray, uint16_t number_of_leds); | ||
| 45 | void apa102_setleds_pin (LED_TYPE *ledarray, uint16_t number_of_leds,uint8_t pinmask); | ||
| 46 | void apa102_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds); | ||
