├── README.md ├── LightAnimation.h ├── LICENSE ├── LPD8806.h ├── main.cpp ├── LightAnimation.cpp └── LPD8806.cpp /README.md: -------------------------------------------------------------------------------- 1 | # LPD8806-microbit 2 | C++ port for bbc micro:bit from adafruit/LPD8806 3 | https://github.com/adafruit/LPD8806 4 | 5 | -------------------------------------------------------------------------------- /LightAnimation.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Port from https://github.com/adafruit/LPD8806 3 | */ 4 | 5 | /* 6 | * File: LightAnimation.h 7 | * Author: taske 8 | * 9 | * Created on 4. November 2016, 22:23 10 | */ 11 | 12 | #ifndef LIGHTANIMATION_H 13 | #define LIGHTANIMATION_H 14 | 15 | #include 16 | #include "MicroBit.h" 17 | 18 | class LightAnimation { 19 | public: 20 | LightAnimation(LPD8806 *strip, MicroBit *uBit); 21 | void colorChase(uint32_t c, uint8_t wait); 22 | void theaterChase(uint32_t c, uint8_t wait); 23 | void colorWipe(uint32_t c, uint8_t wait); 24 | void rainbow(uint8_t wait); 25 | void rainbowCycle(uint8_t wait); 26 | void theaterChaseRainbow(uint8_t wait); 27 | private: 28 | uint32_t Wheel(uint16_t WheelPos); 29 | LPD8806 *strip; 30 | MicroBit *uBit; 31 | }; 32 | 33 | #endif /* LIGHTANIMATION_H */ 34 | 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 newtask 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LPD8806.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Port from https://github.com/adafruit/LPD8806 3 | */ 4 | 5 | 6 | /* 7 | * File: LPD8806.h 8 | * Author: taske 9 | * 10 | * Created on 27. Oktober 2016, 23:06 11 | */ 12 | 13 | #ifndef LPD8806_H 14 | #define LPD8806_H 15 | 16 | #include "MicroBit.h" 17 | 18 | class LPD8806 { 19 | public: 20 | LPD8806(uint16_t n, PinName dpin, PinName cpin); // Configurable pins 21 | LPD8806(uint16_t n); // Use SPI hardware; specific pins only 22 | LPD8806(void); // Empty constructor; init pins & strip length later 23 | 24 | void 25 | begin(void), 26 | setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b), 27 | setPixelColor(uint16_t n, uint32_t c), 28 | show(void), 29 | updatePins(PinName dpin, PinName cpin), // Change pins, configurable 30 | updatePins(void), // Change pins, hardware SPI 31 | updateLength(uint16_t n); // Change strip length 32 | uint16_t 33 | numPixels(void); 34 | uint32_t 35 | Color(size_t r, size_t g, size_t b), 36 | getPixelColor(uint16_t n); 37 | 38 | // test stuff 39 | void draw(u_int color); 40 | private: 41 | void 42 | init(int numLeds), 43 | writeZeroes(), 44 | spi_out(int value), 45 | startSPI(void); 46 | uint16_t 47 | numLEDs, 48 | numBytes; 49 | uint8_t 50 | * pixels; 51 | PinName 52 | datapin = MICROBIT_PIN_P1, 53 | clkpin = MICROBIT_PIN_P0; 54 | 55 | bool begun; 56 | 57 | SPI *spi; 58 | }; 59 | 60 | #endif /* LPD8806_H */ 61 | 62 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "MicroBit.h" 2 | #include 3 | #include 4 | 5 | MicroBit uBit; 6 | LPD8806 strip(6); 7 | LightAnimation animation(&strip, &uBit); 8 | 9 | int main() { 10 | uBit.init(); 11 | strip.begin(); 12 | 13 | int speed = 50; 14 | 15 | while(true) { 16 | 17 | // Send a simple pixel chase in... 18 | animation.colorChase(strip.Color(127, 127, 127), speed); // White 19 | animation.colorChase(strip.Color(127, 0, 0), speed); // Red 20 | animation.colorChase(strip.Color(127, 127, 0), speed); // Yellow 21 | animation.colorChase(strip.Color( 0, 127, 0), speed); // Green 22 | animation.colorChase(strip.Color( 0, 127, 127), speed); // Cyan 23 | animation.colorChase(strip.Color( 0, 0, 127), speed); // Blue 24 | animation.colorChase(strip.Color(127, 0, 127), speed); // Violet 25 | 26 | // Send a theater pixel chase in... 27 | animation.theaterChase(strip.Color(127, 127, 127), speed); // White 28 | animation.theaterChase(strip.Color(127, 0, 0), speed); // Red 29 | animation.theaterChase(strip.Color(127, 127, 0), speed); // Yellow 30 | animation.theaterChase(strip.Color( 0, 127, 0), speed); // Green 31 | animation.theaterChase(strip.Color( 0, 127, 127), speed); // Cyan 32 | animation.theaterChase(strip.Color( 0, 0, 127), speed); // Blue 33 | animation.theaterChase(strip.Color(127, 0, 127), speed); // Violet 34 | 35 | // Fill the entire strip with... 36 | animation.colorWipe(strip.Color(127, 127, 127), speed); // White 37 | animation.colorWipe(strip.Color(127, 0, 0), speed); // Red 38 | animation.colorWipe(strip.Color(127, 127, 0), speed); // Yellow 39 | animation.colorWipe(strip.Color( 0, 127, 0), speed); // Green 40 | animation.colorWipe(strip.Color( 0, 127, 127), speed); // Cyan 41 | animation.colorWipe(strip.Color( 0, 0, 127), speed); // Blue 42 | animation.colorWipe(strip.Color(127, 0, 127), speed); // Violet 43 | 44 | animation.rainbow(10); 45 | 46 | animation.rainbowCycle(0); 47 | animation.theaterChaseRainbow(speed); 48 | } 49 | 50 | release_fiber(); 51 | } -------------------------------------------------------------------------------- /LightAnimation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Port from https://github.com/adafruit/LPD8806 3 | */ 4 | 5 | /* 6 | * File: LightAnimation.cpp 7 | * Author: taske 8 | * 9 | * Created on 4. November 2016, 22:23 10 | */ 11 | 12 | #include "LightAnimation.h" 13 | 14 | LightAnimation::LightAnimation(LPD8806 *strip, MicroBit *uBit) { 15 | this->strip = strip; 16 | this->uBit = uBit; 17 | } 18 | 19 | void LightAnimation::colorChase(uint32_t c, uint8_t wait) { 20 | int i; 21 | 22 | // Start by turning all pixels off: 23 | for(i=0; istrip->numPixels(); i++) 24 | this->strip->setPixelColor(i, 0); 25 | 26 | // Then display one pixel at a time: 27 | for(i=0; i < this->strip->numPixels(); i++) { 28 | this->strip->setPixelColor(i, c); // Set new pixel 'on' 29 | this->strip->show(); // Refresh LED states 30 | this->strip->setPixelColor(i, 0); // Erase pixel, but don't refresh! 31 | this->uBit->sleep(wait); 32 | } 33 | 34 | this->strip->show(); // Refresh to turn off last pixel 35 | } 36 | 37 | //Theatre-style crawling lights. 38 | void LightAnimation::theaterChase(uint32_t c, uint8_t wait) { 39 | for (int j=0; j<10; j++) { //do 10 cycles of chasing 40 | for (int q=0; q < 3; q++) { 41 | for (int i=0; i < this->strip->numPixels(); i=i+3) { 42 | this->strip->setPixelColor(i+q, c); //turn every third pixel on 43 | } 44 | this->strip->show(); 45 | 46 | this->uBit->sleep(wait); 47 | 48 | for (int i=0; i < this->strip->numPixels(); i=i+3) { 49 | this->strip->setPixelColor(i+q, 0); //turn every third pixel off 50 | } 51 | } 52 | } 53 | } 54 | 55 | //Theatre-style crawling lights with rainbow effect 56 | void LightAnimation::theaterChaseRainbow(uint8_t wait) { 57 | for (int j=0; j < 384; j++) { // cycle all 384 colors in the wheel 58 | for (int q=0; q < 3; q++) { 59 | for (int i=0; i < this->strip->numPixels(); i=i+3) { 60 | this->strip->setPixelColor(i+q,this-> Wheel( (i+j) % 384)); //turn every third pixel on 61 | } 62 | this->strip->show(); 63 | 64 | this->uBit->sleep(wait); 65 | 66 | for (int i=0; i < this->strip->numPixels(); i=i+3) { 67 | this->strip->setPixelColor(i+q, 0); //turn every third pixel off 68 | } 69 | } 70 | } 71 | } 72 | 73 | // Fill the dots progressively along the strip. 74 | void LightAnimation::colorWipe(uint32_t c, uint8_t wait) { 75 | int i; 76 | 77 | for (i=0; i < this->strip->numPixels(); i++) { 78 | this->strip->setPixelColor(i, c); 79 | this->strip->show(); 80 | this->uBit->sleep(wait); 81 | } 82 | } 83 | 84 | void LightAnimation::rainbow(uint8_t wait) { 85 | int i, j; 86 | 87 | for (j=0; j < 384; j++) { // 3 cycles of all 384 colors in the wheel 88 | for (i=0; i < this->strip->numPixels(); i++) { 89 | this->strip->setPixelColor(i, this->Wheel( (i + j) % 384)); 90 | } 91 | this->strip->show(); // write all the pixels out 92 | this->uBit->sleep(wait); 93 | } 94 | } 95 | 96 | //Input a value 0 to 384 to get a color value. 97 | //The colours are a transition r - g -b - back to r 98 | 99 | uint32_t LightAnimation::Wheel(uint16_t WheelPos) 100 | { 101 | size_t r, g, b; 102 | switch(WheelPos / 128) 103 | { 104 | case 0: 105 | r = 127 - WheelPos % 128; //Red down 106 | g = WheelPos % 128; // Green up 107 | b = 0; //blue off 108 | break; 109 | case 1: 110 | g = 127 - WheelPos % 128; //green down 111 | b = WheelPos % 128; //blue up 112 | r = 0; //red off 113 | break; 114 | case 2: 115 | b = 127 - WheelPos % 128; //blue down 116 | r = WheelPos % 128; //red up 117 | g = 0; //green off 118 | break; 119 | } 120 | return(this->strip->Color(r,g,b)); 121 | } 122 | 123 | // Slightly different, this one makes the rainbow wheel equally distributed 124 | // along the chain 125 | void LightAnimation::rainbowCycle(uint8_t wait) { 126 | uint16_t i, j; 127 | 128 | for (j=0; j < 384 * 5; j++) { // 5 cycles of all 384 colors in the wheel 129 | for (i=0; i < this->strip->numPixels(); i++) { 130 | // tricky math! we use each pixel as a fraction of the full 384-color wheel 131 | // (thats the i / strip.numPixels() part) 132 | // Then add in j which makes the colors go around per pixel 133 | // the % 384 is to make the wheel cycle around 134 | this->strip->setPixelColor(i, this->Wheel( ((i * 384 / this->strip->numPixels()) + j) % 384) ); 135 | } 136 | this->strip->show(); // write all the pixels out 137 | this->uBit->sleep(wait); 138 | } 139 | } -------------------------------------------------------------------------------- /LPD8806.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Port from https://github.com/adafruit/LPD8806 3 | */ 4 | 5 | /* 6 | * File: LPD8806.cpp 7 | * Author: taske 8 | * 9 | * Created on 27. Oktober 2016, 23:06 10 | */ 11 | 12 | #include "LPD8806.h" 13 | 14 | 15 | LPD8806::LPD8806(uint16_t n, PinName dpin, PinName cpin){ 16 | this->pixels = NULL; 17 | this->begun = false; 18 | this->updateLength(n); 19 | this->updatePins(dpin, cpin); 20 | } 21 | 22 | LPD8806::LPD8806(uint16_t n) { 23 | this->pixels = NULL; 24 | this->begun = false; 25 | this->updateLength(n); 26 | this->updatePins(); 27 | } 28 | 29 | LPD8806::LPD8806() { 30 | this->numLEDs = this->numBytes = 0; 31 | this->pixels = NULL; 32 | this->begun = false; 33 | this->updatePins(); // Must assume hardware SPI until pins are set 34 | } 35 | 36 | void LPD8806::begin(){ 37 | 38 | 39 | this->startSPI(); 40 | 41 | // Do we need this? 42 | for( int i = 0; i < (this->numLEDs * 3 * 2) ; ++i){ 43 | this->spi_out(0); 44 | } 45 | 46 | this->begun = true; 47 | } 48 | 49 | // Change pin assignments post-constructor, switching to hardware SPI: 50 | void LPD8806::updatePins(void) { 51 | this->datapin = MICROBIT_PIN_P1; 52 | this->clkpin = MICROBIT_PIN_P0; 53 | 54 | if(this->begun == true) this->startSPI(); 55 | // Otherwise, SPI is NOT initted until begin() is explicitly called. 56 | } 57 | 58 | // Change pin assignments post-constructor, using arbitrary pins: 59 | void LPD8806::updatePins(PinName dpin, PinName cpin) { 60 | if(this->begun == true) { // If begin() was previously invoked... 61 | // If previously using hardware SPI, turn that off: 62 | //SPI.end(); 63 | } 64 | this->datapin = dpin; 65 | this->clkpin = cpin; 66 | 67 | // If previously begun, enable 'soft' SPI outputs now 68 | // TODO check this if(this->begun == true) startBitbang(); 69 | 70 | } 71 | 72 | // Enable SPI hardware and set up protocol details: 73 | void LPD8806::startSPI(void) { 74 | this->spi = new SPI(this->datapin, MICROBIT_PIN_P14, this->clkpin); // mosi, miso, sclk 75 | this->spi->format(8,0); 76 | this->spi->frequency(10000); 77 | 78 | // Issue initial latch/reset to strip: 79 | for(uint16_t i=((this->numLEDs+31)/32); i>0; i--) this->spi_out(0); 80 | } 81 | 82 | // Change strip length (see notes with empty constructor, above): 83 | void LPD8806::updateLength(uint16_t n) { 84 | uint8_t latchBytes; 85 | uint16_t dataBytes, totalBytes; 86 | 87 | this->numLEDs = this->numBytes = 0; 88 | if(this->pixels) free(pixels); // Free existing data (if any) 89 | 90 | dataBytes = n * 3; 91 | latchBytes = (n + 31) / 32; 92 | totalBytes = dataBytes + latchBytes; 93 | if((this->pixels = (uint8_t *)malloc(totalBytes))) { // Alloc new data 94 | this->numLEDs = n; 95 | this->numBytes = totalBytes; 96 | memset( this->pixels , 0x80, dataBytes); // Init to RGB 'off' state 97 | memset(&this->pixels[dataBytes], 0 , latchBytes); // Clear latch bytes 98 | } 99 | // 'begun' state does not change -- pins retain prior modes 100 | } 101 | 102 | uint16_t LPD8806::numPixels(void) { 103 | return this->numLEDs; 104 | } 105 | 106 | void LPD8806::show(void) { 107 | uint8_t *ptr = this->pixels; 108 | uint16_t i = this->numBytes; 109 | 110 | while(i--) this->spi_out(*ptr++); 111 | 112 | } 113 | 114 | // Convert separate R,G,B into combined 32-bit GRB color: 115 | uint32_t LPD8806::Color(size_t r, size_t g, size_t b) { 116 | return ((uint32_t)(g | 0x80) << 16) | 117 | ((uint32_t)(r | 0x80) << 8) | 118 | b | 0x80 ; 119 | } 120 | 121 | // Set pixel color from separate 7-bit R, G, B components: 122 | void LPD8806::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) { 123 | if(n < this->numLEDs) { // Arrays are 0-indexed, thus NOT '<=' 124 | uint8_t *p = &this->pixels[n * 3]; 125 | *p++ = g | 0x80; // Strip color order is GRB, 126 | *p++ = r | 0x80; // not the more common RGB, 127 | *p++ = b | 0x80; // so the order here is intentional; don't "fix" 128 | } 129 | } 130 | 131 | // Set pixel color from 'packed' 32-bit GRB (not RGB) value: 132 | void LPD8806::setPixelColor(uint16_t n, uint32_t c) { 133 | if(n < this->numLEDs) { // Arrays are 0-indexed, thus NOT '<=' 134 | uint8_t *p = &this->pixels[n * 3]; 135 | *p++ = (c >> 16) | 0x80; 136 | *p++ = (c >> 8) | 0x80; 137 | *p++ = c | 0x80; 138 | } 139 | } 140 | 141 | // Query color from previously-set pixel (returns packed 32-bit GRB value) 142 | uint32_t LPD8806::getPixelColor(uint16_t n) { 143 | if(n < this->numLEDs) { 144 | uint16_t ofs = n * 3; 145 | return ((uint32_t)(this->pixels[ofs ] & 0x7f) << 16) | 146 | ((uint32_t)(this->pixels[ofs + 1] & 0x7f) << 8) | 147 | (uint32_t)(this->pixels[ofs + 2] & 0x7f); 148 | } 149 | 150 | return 0; // Pixel # is out of bounds 151 | } 152 | 153 | 154 | void LPD8806::spi_out(int value){ 155 | this->spi->write(value); 156 | } 157 | 158 | void LPD8806::writeZeroes(){ 159 | for( int i = 0; i < 3 * this->numLEDs; ++i){ 160 | spi->write(0); 161 | } 162 | } 163 | 164 | void LPD8806::draw(u_int color){ 165 | for( int i = 0; i < this->numLEDs * 3 ; ++i){ 166 | spi->write(0x80| color); 167 | } 168 | 169 | this->writeZeroes(); 170 | } 171 | 172 | --------------------------------------------------------------------------------