Spidey Wall is the name for a physical wall lit up by multiple addressable LED strips. This program is an LPC1768 web server to control the wall from a browser.
Dependencies: EthernetInterfacePlusHostname RdWebServer mbed-rtos mbed
This project is part of a Light-Wall using addressable LED strips (WS2801). I have published a few posts on my blog about the construction of the wall and building a game to play on it (PacMan). I have also had a guest post from a friend who has set his children the task of producing some interesting animations. The original post is http://robdobson.com/2015/07/spidey-wall/
So far, however, I hadn't fully connected the physical (and electronic) wall with the web-browser creations to drive it. This project is hopefully the final link. A fast and reliable web server using REST commands to drive the 1686 LEDs in the Spidey Wall from code running in a browser (say on an iPad while you are playing a game).
The approach taken here results in the ability to control the RGB values of all 1686 LEDs at a rate of 20 frames per second.
A blog post describing the whole thing is here:
http://robdobson.com/2015/08/a-reliable-mbed-webserver/
Diff: ledstrip.cpp
- Revision:
- 0:887096209439
- Child:
- 1:362331cec9b7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ledstrip.cpp Tue Aug 18 16:03:29 2015 +0000 @@ -0,0 +1,285 @@ +#include "ledstrip.h" +#include "colourconverters.h" +#include "stddef.h" + +#define SPIF 0 // SPI interrupt flag bit +#define SSP_IMSC_TX_RDY 3 +#define SSP_IMSC_BITMASK 0x0f + +volatile int mCurPos0; +int mEndPos0; +volatile int mCurPos1; +int mEndPos1; +volatile bool mShowingLeds0; +volatile bool mShowingLeds1; +unsigned char* pLedValues; +bool inISR; + +extern "C" void spi0_isr() +{ + if (mCurPos0 < mEndPos0) + { + LPC_SSP0->DR = pLedValues[mCurPos0]; // write to FIFO data register + mCurPos0++; + } + else + { + // Turn off interrupts + LPC_SSP0->IMSC = 0; + mShowingLeds0 = false; + } +} + +extern "C" void spi1_isr() +{ + if (mCurPos1 < mEndPos1) + { + LPC_SSP1->DR = pLedValues[mCurPos1]; // write to FIFO data register + mCurPos1++; + } + else + { + // Turn off interrupts + LPC_SSP1->IMSC = 0; + mShowingLeds1 = false; + } +} + +ledstrip::ledstrip(int length, int splitPoint) +{ + mpLedValuesA = 0; + mpLedValuesB = 0; + mpCurLedValues = 0; + + // SPI0 (using SSP 0 in 1768 chip) + mpSPI0 = new SPI(p11, NC, p13); + mpSPI0->format(8,0); + mpSPI0->frequency(500000); + LPC_SSP0->IMSC = 0; // initially no interrupts requested + NVIC_SetVector(SSP0_IRQn,( uint32_t ) spi0_isr); + NVIC_ClearPendingIRQ(SSP0_IRQn); + NVIC_SetPriority(SSP0_IRQn, 2); + NVIC_EnableIRQ(SSP0_IRQn); + + // SPI1 (using SSP 1 in 1768 chip) + mpSPI1 = new SPI(p5, NC, p7); + mpSPI1->format(8,0); + mpSPI1->frequency(500000); + LPC_SSP1->IMSC = 0; // initially no interrupts requested + NVIC_SetVector(SSP1_IRQn,( uint32_t ) spi1_isr); + NVIC_ClearPendingIRQ(SSP1_IRQn); + NVIC_SetPriority(SSP1_IRQn, 2); + NVIC_EnableIRQ(SSP1_IRQn); + + // Enable interrupts + __enable_irq(); + + // Resize the string length + Resize(length, splitPoint); +} + +ledstrip::~ledstrip() +{ + delete mpLedValuesA; + delete mpLedValuesB; +} + +bool ledstrip::Resize(int length, int splitPoint) +{ + if (mShowingLeds0 || mShowingLeds1) + return false; + if (mpLedValuesA != 0) + delete mpLedValuesA; + if (mpLedValuesB != 0) + delete mpLedValuesB; + mLedsBufSize = length*mColoursPerLed; + mpLedValuesA = new unsigned char[mLedsBufSize]; + mpLedValuesB = new unsigned char[mLedsBufSize]; + mpCurLedValues = mpLedValuesA; + mLedsInStrip = length; + mSplitPoint = splitPoint; + Clear(); + return true; +} + +void ledstrip::Clear() +{ +/* Timer timr; + timr.start(); + for (int i = 0; i < mLedsInStrip*mColoursPerLed; i++) + mpCurLedValues[i] = 0; + timr.stop(); + printf("ClearTime loop %d\n", timr.read_us()); // Result is 863uS for 2500 x 3colour LEDS + timr.reset(); + timr.start(); + */ + memset(mpCurLedValues, 0, mLedsBufSize); + /* timr.stop(); + printf("ClearTime memset %d\n", timr.read_us()); // Result is 35uS for 2500 x 3 colour LEDS +*/ +} + +unsigned char* ledstrip::GetBuffer() +{ + return mpCurLedValues; +} + +int ledstrip::GetBufferSizeinBytes() +{ + return mLedsBufSize; +} + +bool ledstrip::IsBusy() +{ + return mShowingLeds0 || mShowingLeds1; +} + +// Fill - solid colour +void ledstrip::Fill(int startLed, int numLeds, + int r1, int g1, int b1) +{ +/* Timer timr; + timr.start(); +*/ + if ((startLed < 0) || (startLed >= mLedsInStrip)) + return; + if (numLeds >= mLedsInStrip - startLed) + numLeds = mLedsInStrip - startLed; + int pos = startLed * mColoursPerLed; + unsigned char* pBuf = GetBuffer(); + for (int i = 0; i < numLeds; i++) + { + pBuf[pos] = (unsigned char) r1; + pBuf[pos+1] = (unsigned char) g1; + pBuf[pos+2] = (unsigned char) b1; + pos += mColoursPerLed; + } +/* timr.stop(); + printf("Fill solid %d\n", timr.read_us()); // Fill 50 LEDS solid colour = 11uS + */ +} + +// Fill - with interpolation of colours using HSV colour space +void ledstrip::Fill(int startLed, int numLeds, + int r1, int g1, int b1, + int r2, int g2, int b2) +{ +/* Timer timr; + timr.start(); + */ + if ((startLed < 0) || (startLed >= mLedsInStrip)) + return; + if (numLeds >= mLedsInStrip - startLed) + numLeds = mLedsInStrip - startLed; + int pos = startLed * mColoursPerLed; + RgbColor startRGB(r1,g1,b1); + HsvColor startHsv = RgbToHsv(startRGB); + RgbColor endRGB(r2,g2,b2); + HsvColor endHsv = RgbToHsv(endRGB); + int curH = startHsv.h << 16; + int curS = startHsv.s << 16; + int curV = startHsv.v << 16; + int interpSteps = numLeds - 1; + if (interpSteps < 1) + interpSteps = 1; + int incH = ((endHsv.h - startHsv.h) << 16) / interpSteps; + int incS = ((endHsv.s - startHsv.s) << 16) / interpSteps; + int incV = ((endHsv.v - startHsv.v) << 16) / interpSteps; + // Since H is a polar value we need to find out if it is best to go clockwise or anti-clockwise + if (endHsv.h > startHsv.h) + { + if (endHsv.h-startHsv.h > 128) + incH = ((startHsv.h-endHsv.h) << 16) / interpSteps; + } + else + { + // Go "round the top" using modulo result + if (startHsv.h-endHsv.h > 128) + incH = ((endHsv.h + 255 - startHsv.h) << 16) / interpSteps; + } + +// printf("StartHSV %d %d %d EndHSV %d %d %d IncHSV %d %d %d\n", startHsv.h, startHsv.s, startHsv.v, endHsv.h, endHsv.s, endHsv.v, incH, incS, incV); + unsigned char* pBuf = GetBuffer(); + for (int i = 0; i < numLeds; i++) + { + RgbColor colrVal = HsvToRgb(HsvColor((curH>>16)&0xff,curS>>16,curV>>16)); + pBuf[pos] = colrVal.r; + pBuf[pos+1] = colrVal.g; + pBuf[pos+2] = colrVal.b; +// printf("HSV %d %d %d RGB %d %d %d\n", curH>>16, curS>>16, curV>>16, colrVal.r, colrVal.g, colrVal.b); + pos += mColoursPerLed; + curH = curH + incH; + curS = curS + incS; + curV = curV + incV; + } + /* + timr.stop(); + printf("Fill gradient %d\n", timr.read_us()); // Fill gradient 50 LEDS = 64uS + */ +} + + +void ledstrip::ShowLeds() +{ + // Check if busy + while (mShowingLeds0 || mShowingLeds1) + ; + wait_us(750); + + // Set up start points + mCurPos0 = 0; + mEndPos0 = mSplitPoint*mColoursPerLed; + mCurPos1 = mSplitPoint*mColoursPerLed; + mEndPos1 = mLedsInStrip*mColoursPerLed; + + // Set the buffer for the ISRs + pLedValues = mpCurLedValues; + + // Flip the current buffer to the alternate one for interleaved writing + if (mpCurLedValues == mpLedValuesA) + mpCurLedValues = mpLedValuesB; + else + mpCurLedValues = mpLedValuesA; + + // Enable interrupts + mShowingLeds0 = true; + LPC_SSP0->IMSC = (1 << SSP_IMSC_TX_RDY) & SSP_IMSC_BITMASK; + + // Check if second strip is used + if (mSplitPoint < mLedsInStrip) + { + LPC_SSP1->IMSC = (1 << SSP_IMSC_TX_RDY) & SSP_IMSC_BITMASK; + mShowingLeds1 = true; + } + // for (int q = 0; q < mLedsInStrip*3; q+=3) + // printf("%d %02x%02x%02x\n", q/3, mpLedValues[q], mpLedValues[q+1], mpLedValues[q+2]); +/* + int pos1 = 0; + int pos2 = mSplitPoint * mColoursPerLed; + for (int j = 0; j < mMaxChainLength; j++) + { + for (int k = 0; k < mColoursPerLed; k++) + { + unsigned char tval1 = 0; + if (pos1 + k < mLedsBufSize) + tval1 = mpLedValues[pos1 + k]; + unsigned char tval2 = 0; + if (pos2 + k < mLedsBufSize) + tval2 = mpLedValues[pos2 + k]; + for (int i = 0; i < 8; i++) + { + dat1 = (tval1 & 0x80) != 0; + tval1 = tval1 << 1; + dat2 = (tval2 & 0x80) != 0; + tval2 = tval2 << 1; + clk = 1; + clk = 0; + wait_us(1); + } + } + pos1 += mColoursPerLed; + pos2 += mColoursPerLed; + } + wait_us(750); +*/ +}