#include "ws2812b.h" #include unsigned char c[3][ledLen]; //declare a global to contain the LED color data //c[0] is blue, c[1] is red, c[2] is green const unsigned char reversed[256] = {0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255}; int lengths[nlines] = {0, len, 2*len, 3*len, 4*len, 5*len, 6*len, 7*len, 8*len, 9*len, 10*len, 11*len, 12*len};//, 13*len, 14*len, 15*len}; //the bitflip function was too slow, so instead we'll use the look up table reversed above. additionally, //we'll add do a lookup array for the multiplication as well. that is what it takes to run fast enough. typedef struct { unsigned char r; unsigned char b; unsigned char g; } ws2812_ptr; int getPixelIndex(int x, int y){ /*this code must take into account the way in which the led strips are connected in the grid in this case, the bottom left is (0,0). the end of a row connects to the end of the row above it, so the ordered list of LEDs sort of snakes up the rows, alternating directions in the x axis.*/ int result = 0; if((y % 2) == 0){ /*in an even row, x is a minimum on the left and a max on the right*/ result = y*width + x; }else{ /*in an odd row x is a minimum on the right and a max on the left*/ result = (y+1)*width - 1 - x; } return result; } void ws2812MultiInit(){ ANSELB &= ~lineMask; TRISB &= ~lineMask; LATB = 0; } void ws2812MultiReset(){ LATB &= ~lineMask; int j = 0; for(j = 0; j < 10000; j++){ Nop();} } unsigned long int bitflip(unsigned char b) { b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; b = (b & 0xCC) >> 2 | (b & 0x33) << 2; b = (b & 0xAA) >> 1 | (b & 0x55) << 1; return (unsigned long int)b; } void ws2812MultiSend(){ unsigned int tempVal = 0; unsigned long val1, val2, val3, val4, val5, val6, val7, val8, val9, val10,val11, val12, val13, val14, val15, val16; int ind = 0; for(ind = 0; ind < len; ind++){ val1 = (reversed[c[0][ind]] << 16) + (reversed[c[1][ind]] << 8) + (reversed[c[2][ind]]); val2 = (reversed[c[0][ind+len]] << 16) + (reversed[c[1][ind+len]] << 8) + (reversed[c[2][ind+len]]); val3 = (reversed[c[0][ind+lengths[2]]] << 16) + (reversed[c[1][ind+lengths[2]]] << 8) + (reversed[c[2][ind+lengths[2]]]); val4 = (reversed[c[0][ind+lengths[3]]] << 16) + (reversed[c[1][ind+lengths[3]]] << 8) + (reversed[c[2][ind+lengths[3]]]); val5 = (reversed[c[0][ind+lengths[4]]] << 16) + (reversed[c[1][ind+lengths[4]]] << 8) + (reversed[c[2][ind+lengths[4]]]); val6 = (reversed[c[0][ind+lengths[5]]] << 16) + (reversed[c[1][ind+lengths[5]]] << 8) + (reversed[c[2][ind+lengths[5]]]); val7 = (reversed[c[0][ind+lengths[6]]] << 16) + (reversed[c[1][ind+lengths[6]]] << 8) + (reversed[c[2][ind+lengths[6]]]); val8 = (reversed[c[0][ind+lengths[7]]] << 16) + (reversed[c[1][ind+lengths[7]]] << 8) + (reversed[c[2][ind+lengths[7]]]); val9 = (reversed[c[0][ind+lengths[8]]] << 16) + (reversed[c[1][ind+lengths[8]]] << 8) + (reversed[c[2][ind+lengths[8]]]); val10 = (reversed[c[0][ind+lengths[9]]] << 16) + (reversed[c[1][ind+lengths[9]]] << 8) + (reversed[c[2][ind+lengths[9]]]); val11 = (reversed[c[0][ind+lengths[10]]] << 16) + (reversed[c[1][ind+lengths[10]]] << 8) + (reversed[c[2][ind+lengths[10]]]); val12 = (reversed[c[0][ind+lengths[11]]] << 16) + (reversed[c[1][ind+lengths[11]]] << 8) + (reversed[c[2][ind+lengths[11]]]); val13 = (reversed[c[0][ind+lengths[12]]] << 16) + (reversed[c[1][ind+lengths[12]]] << 8) + (reversed[c[2][ind+lengths[12]]]); // val14 = (reversed[c[0][ind+lengths[13]]] << 16) + (reversed[c[1][ind+lengths[13]]] << 8) + (reversed[c[2][ind+lengths[13]]]); // val15 = (reversed[c[0][ind+lengths[14]]] << 16) + (reversed[c[1][ind+lengths[14]]] << 8) + (reversed[c[2][ind+lengths[14]]]); // val16 = (reversed[c[0][ind+lengths[15]]] << 16) + (reversed[c[1][ind+lengths[15]]] << 8) + (reversed[c[2][ind+lengths[15]]]); int j = 0; for(j = 0; j < 24; j++){ LATB |= lineMask; //pull data lines high for the LS high time, should be .35uS ~70 Nops Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); /*Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop();*/ //if the bit for which ever dataline is 0, it must be pulled low now, otherwise leave it alone for a bit longer LATB &= (~lineMask)|((val2 & 0x0001)<<1)| (val1 & 0x0001)|((val3 & 0x0001)<<2)|((val4 & 0x0001)<<3)|((val5 & 0x0001)<<4)|((val6 & 0x0001) <<5)|((val7 & 0x0001)<<6)|((val8 & 0x0001)<<7)|((val9 & 0x0001)<<8)|((val10 & 0x0001)<<9)|((val11 & 0x0001)<<10)|((val12 & 0x0001)<<11)|((val13 & 0x0001)<<12);//|(val14 & 0x0001)|(val15 & 0x0001)|(val16 & 0x0001); //now delay the difference in high time between the 0 and 1 states; should be .55uS ~110Nops Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); //now pull all data lines low LATB &= ~lineMask; //delay the high low time; should be 0.35uS ~70 Nops Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); /*Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop();*/ val1 = val1 >> 1; val2 = val2 >> 1; val3 = val3 >> 1; val4 = val4 >> 1; val5 = val5 >> 1; val6 = val6 >> 1; val7 = val7 >> 1; val8 = val8 >> 1; val9 = val9 >> 1; val10 = val10 >> 1; val11 = val11 >> 1; val12 = val12 >> 1; val13 = val13 >> 1; // val14 = val14 >> 1; // val15 = val15 >> 1; // val16 = val16 >> 1; } } } void ws2812_send(ws2812_ptr* led) { //not yet updated, but I don't think I'll ever use it so, you know... int j; long int val; // the WS2812 wants bits in the order of: // GGGGGGGGRRRRRRRRBBBBBBBB // but I want to work in the opposite order. so i'm going to flip // the bits around and do some shifting so my order is // BBBBBBBBRRRRRRRRGGGGGGGG // with the most significant bit on the far right. so the RGB value // of 128 64 32, which normally would be: // R : 0b10000000 // G : 0b01000000 // B : 0b00100000 // will become: // BBBBBBBBRRRRRRRRGGGGGGGG // 000001000000000100000010 val = (bitflip(led->b) << 16) + (bitflip(led->r) << 8) + (bitflip(led->g)); // now begin shifting them over one at a time for(j = 0; j < 24; j++) { // depending on if the currently viewed bit is 1 or 0 // the pin will stay high for different times if (val & 1 == 1) { // if it is a 1, let it stay higher a bit longer PIN = 1; Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); PIN = 0; Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); } else { // but a 0 should go high and then low as fast as possible PIN = 1; Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); PIN = 0; Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); } // and then right shift to get the next bit val = val >> (unsigned char)1; } }