Dependencies: EthernetNetIf mbed HTTPClient_ToBeRemoved
main.cpp@0:5461db8e4caa, 2012-06-13 (annotated)
- Committer:
- robertcook
- Date:
- Wed Jun 13 18:28:03 2012 +0000
- Revision:
- 0:5461db8e4caa
Ethernet Video Display - LEDs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robertcook | 0:5461db8e4caa | 1 | #include "mbed.h" |
robertcook | 0:5461db8e4caa | 2 | #include "EthernetNetIf.h" |
robertcook | 0:5461db8e4caa | 3 | #include "HTTPClient.h" |
robertcook | 0:5461db8e4caa | 4 | |
robertcook | 0:5461db8e4caa | 5 | #define ScreenWidth 32 |
robertcook | 0:5461db8e4caa | 6 | #define ScreenHeight 15 |
robertcook | 0:5461db8e4caa | 7 | |
robertcook | 0:5461db8e4caa | 8 | #define HTTPChunkSize 1024 |
robertcook | 0:5461db8e4caa | 9 | |
robertcook | 0:5461db8e4caa | 10 | #define ScreenBufferSize ScreenWidth*ScreenHeight*3 |
robertcook | 0:5461db8e4caa | 11 | |
robertcook | 0:5461db8e4caa | 12 | typedef struct { |
robertcook | 0:5461db8e4caa | 13 | byte blue; |
robertcook | 0:5461db8e4caa | 14 | byte green; |
robertcook | 0:5461db8e4caa | 15 | byte red; |
robertcook | 0:5461db8e4caa | 16 | } color; |
robertcook | 0:5461db8e4caa | 17 | |
robertcook | 0:5461db8e4caa | 18 | unsigned int fileFrameCount; |
robertcook | 0:5461db8e4caa | 19 | |
robertcook | 0:5461db8e4caa | 20 | color disp[ScreenWidth][ScreenHeight]; |
robertcook | 0:5461db8e4caa | 21 | byte screenBuf[ScreenBufferSize]; |
robertcook | 0:5461db8e4caa | 22 | byte readBuf[HTTPChunkSize]; |
robertcook | 0:5461db8e4caa | 23 | |
robertcook | 0:5461db8e4caa | 24 | Serial pc(USBTX, USBRX); |
robertcook | 0:5461db8e4caa | 25 | |
robertcook | 0:5461db8e4caa | 26 | SPI spi(p5, NC, p7); // mosi, miso, sclk |
robertcook | 0:5461db8e4caa | 27 | |
robertcook | 0:5461db8e4caa | 28 | #define LEDCount 110 |
robertcook | 0:5461db8e4caa | 29 | #define channels LEDCount*3 |
robertcook | 0:5461db8e4caa | 30 | |
robertcook | 0:5461db8e4caa | 31 | EthernetNetIf eth; |
robertcook | 0:5461db8e4caa | 32 | HTTPClient http; |
robertcook | 0:5461db8e4caa | 33 | |
robertcook | 0:5461db8e4caa | 34 | HTTPResult result; |
robertcook | 0:5461db8e4caa | 35 | bool completed = false; |
robertcook | 0:5461db8e4caa | 36 | |
robertcook | 0:5461db8e4caa | 37 | void request_callback(HTTPResult r) |
robertcook | 0:5461db8e4caa | 38 | { |
robertcook | 0:5461db8e4caa | 39 | result = r; |
robertcook | 0:5461db8e4caa | 40 | completed = true; |
robertcook | 0:5461db8e4caa | 41 | } |
robertcook | 0:5461db8e4caa | 42 | |
robertcook | 0:5461db8e4caa | 43 | |
robertcook | 0:5461db8e4caa | 44 | void writePixel(int x, int y) { |
robertcook | 0:5461db8e4caa | 45 | |
robertcook | 0:5461db8e4caa | 46 | spi.write(screenBuf[(x*ScreenHeight+y)*3]); |
robertcook | 0:5461db8e4caa | 47 | spi.write(screenBuf[(x*ScreenHeight+y)*3+1]); |
robertcook | 0:5461db8e4caa | 48 | spi.write(screenBuf[(x*ScreenHeight+y)*3+2]); |
robertcook | 0:5461db8e4caa | 49 | |
robertcook | 0:5461db8e4caa | 50 | } |
robertcook | 0:5461db8e4caa | 51 | |
robertcook | 0:5461db8e4caa | 52 | |
robertcook | 0:5461db8e4caa | 53 | void updateDisplay() { |
robertcook | 0:5461db8e4caa | 54 | for (int y = ScreenHeight-1; y >= 0; y--) { |
robertcook | 0:5461db8e4caa | 55 | if (y % 2 == 0) { |
robertcook | 0:5461db8e4caa | 56 | for (int x = 0; x < ScreenWidth; x++) |
robertcook | 0:5461db8e4caa | 57 | writePixel(x,y); |
robertcook | 0:5461db8e4caa | 58 | } |
robertcook | 0:5461db8e4caa | 59 | else { |
robertcook | 0:5461db8e4caa | 60 | for (int x = ScreenWidth-1; x >= 0; x--) |
robertcook | 0:5461db8e4caa | 61 | writePixel(x,y); |
robertcook | 0:5461db8e4caa | 62 | } |
robertcook | 0:5461db8e4caa | 63 | } |
robertcook | 0:5461db8e4caa | 64 | wait_us(800); |
robertcook | 0:5461db8e4caa | 65 | } |
robertcook | 0:5461db8e4caa | 66 | |
robertcook | 0:5461db8e4caa | 67 | |
robertcook | 0:5461db8e4caa | 68 | |
robertcook | 0:5461db8e4caa | 69 | void hexDump(byte *buf, unsigned long len, int wid) { |
robertcook | 0:5461db8e4caa | 70 | |
robertcook | 0:5461db8e4caa | 71 | for (unsigned long i = 0; i < len; i++) { |
robertcook | 0:5461db8e4caa | 72 | if (!(i % wid)) pc.printf("\n"); |
robertcook | 0:5461db8e4caa | 73 | else if (!(i % 4)) pc.printf(" "); |
robertcook | 0:5461db8e4caa | 74 | |
robertcook | 0:5461db8e4caa | 75 | pc.printf("%02x", buf[i]); |
robertcook | 0:5461db8e4caa | 76 | } |
robertcook | 0:5461db8e4caa | 77 | pc.printf("\n"); |
robertcook | 0:5461db8e4caa | 78 | } |
robertcook | 0:5461db8e4caa | 79 | |
robertcook | 0:5461db8e4caa | 80 | |
robertcook | 0:5461db8e4caa | 81 | int setupEthernet() { |
robertcook | 0:5461db8e4caa | 82 | |
robertcook | 0:5461db8e4caa | 83 | pc.printf("Start\n"); |
robertcook | 0:5461db8e4caa | 84 | |
robertcook | 0:5461db8e4caa | 85 | pc.printf("Setting up...\n"); |
robertcook | 0:5461db8e4caa | 86 | EthernetErr ethErr = eth.setup(); |
robertcook | 0:5461db8e4caa | 87 | |
robertcook | 0:5461db8e4caa | 88 | if(ethErr) { |
robertcook | 0:5461db8e4caa | 89 | pc.printf("Error %d in setup.\n", ethErr); |
robertcook | 0:5461db8e4caa | 90 | return -1; |
robertcook | 0:5461db8e4caa | 91 | } |
robertcook | 0:5461db8e4caa | 92 | pc.printf("Setup OK\n"); |
robertcook | 0:5461db8e4caa | 93 | return (0); |
robertcook | 0:5461db8e4caa | 94 | } |
robertcook | 0:5461db8e4caa | 95 | |
robertcook | 0:5461db8e4caa | 96 | |
robertcook | 0:5461db8e4caa | 97 | int main() { |
robertcook | 0:5461db8e4caa | 98 | |
robertcook | 0:5461db8e4caa | 99 | // Setup the spi for 8 bit data, high steady state clock, |
robertcook | 0:5461db8e4caa | 100 | // second edge capture, with a 1MHz clock rate |
robertcook | 0:5461db8e4caa | 101 | spi.format(8,1); |
robertcook | 0:5461db8e4caa | 102 | spi.frequency(1000000); |
robertcook | 0:5461db8e4caa | 103 | |
robertcook | 0:5461db8e4caa | 104 | pc.baud(115200); |
robertcook | 0:5461db8e4caa | 105 | |
robertcook | 0:5461db8e4caa | 106 | setupEthernet(); |
robertcook | 0:5461db8e4caa | 107 | HTTPStream stream; |
robertcook | 0:5461db8e4caa | 108 | unsigned long dataReadRemaining = 6; |
robertcook | 0:5461db8e4caa | 109 | bool headerLoaded = false; |
robertcook | 0:5461db8e4caa | 110 | unsigned int screenBufInd = 0; |
robertcook | 0:5461db8e4caa | 111 | |
robertcook | 0:5461db8e4caa | 112 | stream.readNext((byte *)readBuf, 6); |
robertcook | 0:5461db8e4caa | 113 | |
robertcook | 0:5461db8e4caa | 114 | HTTPResult r = http.get("http://192.168.1.129/traffic.vid", &stream, request_callback); |
robertcook | 0:5461db8e4caa | 115 | |
robertcook | 0:5461db8e4caa | 116 | while(!completed) { |
robertcook | 0:5461db8e4caa | 117 | Net::poll(); //Polls the Networking stack |
robertcook | 0:5461db8e4caa | 118 | |
robertcook | 0:5461db8e4caa | 119 | if (stream.readable()) { |
robertcook | 0:5461db8e4caa | 120 | unsigned int readLength = stream.readLen(); |
robertcook | 0:5461db8e4caa | 121 | |
robertcook | 0:5461db8e4caa | 122 | // Load and process six byte header |
robertcook | 0:5461db8e4caa | 123 | // Three integers -- frame count, height, width |
robertcook | 0:5461db8e4caa | 124 | if (headerLoaded == false) { |
robertcook | 0:5461db8e4caa | 125 | headerLoaded = true; |
robertcook | 0:5461db8e4caa | 126 | fileFrameCount = readBuf[0]+readBuf[1]*256; |
robertcook | 0:5461db8e4caa | 127 | pc.printf("# of frames: %i\n", fileFrameCount); |
robertcook | 0:5461db8e4caa | 128 | pc.printf("height x width: %i x %i\n", readBuf[2]+readBuf[3]*256, readBuf[4]+readBuf[5]*256); |
robertcook | 0:5461db8e4caa | 129 | |
robertcook | 0:5461db8e4caa | 130 | dataReadRemaining = ScreenBufferSize; |
robertcook | 0:5461db8e4caa | 131 | stream.readNext(readBuf, HTTPChunkSize); |
robertcook | 0:5461db8e4caa | 132 | } |
robertcook | 0:5461db8e4caa | 133 | // else load image |
robertcook | 0:5461db8e4caa | 134 | else { |
robertcook | 0:5461db8e4caa | 135 | dataReadRemaining -= readLength; |
robertcook | 0:5461db8e4caa | 136 | //pc.printf("%i", (unsigned int)dataReadRemaining); |
robertcook | 0:5461db8e4caa | 137 | |
robertcook | 0:5461db8e4caa | 138 | memcpy(screenBuf + screenBufInd, readBuf, readLength); |
robertcook | 0:5461db8e4caa | 139 | |
robertcook | 0:5461db8e4caa | 140 | // if any remaining data |
robertcook | 0:5461db8e4caa | 141 | if (dataReadRemaining) { |
robertcook | 0:5461db8e4caa | 142 | //pc.printf(".\n"); |
robertcook | 0:5461db8e4caa | 143 | screenBufInd += readLength; |
robertcook | 0:5461db8e4caa | 144 | stream.readNext((byte*)readBuf, (dataReadRemaining > HTTPChunkSize ? HTTPChunkSize : dataReadRemaining)); |
robertcook | 0:5461db8e4caa | 145 | } |
robertcook | 0:5461db8e4caa | 146 | else { |
robertcook | 0:5461db8e4caa | 147 | //pc.printf("!\n"); |
robertcook | 0:5461db8e4caa | 148 | |
robertcook | 0:5461db8e4caa | 149 | // hexDump(screenBuf,1440,32); |
robertcook | 0:5461db8e4caa | 150 | updateDisplay(); |
robertcook | 0:5461db8e4caa | 151 | //while(1); |
robertcook | 0:5461db8e4caa | 152 | |
robertcook | 0:5461db8e4caa | 153 | dataReadRemaining = ScreenBufferSize; |
robertcook | 0:5461db8e4caa | 154 | screenBufInd = 0; |
robertcook | 0:5461db8e4caa | 155 | stream.readNext((byte*)readBuf, HTTPChunkSize); |
robertcook | 0:5461db8e4caa | 156 | } |
robertcook | 0:5461db8e4caa | 157 | } |
robertcook | 0:5461db8e4caa | 158 | } |
robertcook | 0:5461db8e4caa | 159 | |
robertcook | 0:5461db8e4caa | 160 | } |
robertcook | 0:5461db8e4caa | 161 | pc.printf("\n--------------\n"); |
robertcook | 0:5461db8e4caa | 162 | if (result == HTTP_OK) pc.printf("Read completely\n"); |
robertcook | 0:5461db8e4caa | 163 | else pc.printf("Error %d\n", result); |
robertcook | 0:5461db8e4caa | 164 | |
robertcook | 0:5461db8e4caa | 165 | |
robertcook | 0:5461db8e4caa | 166 | while(1){} |
robertcook | 0:5461db8e4caa | 167 | } |