Guest User

weird led strip back engineering

a guest
Oct 3rd, 2012
81
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void setup()
  2. {
  3. pinMode(8, OUTPUT);
  4. pinMode(9, OUTPUT);
  5. digitalWrite(8, HIGH);
  6. digitalWrite(9, HIGH);
  7.  
  8. Serial.begin(115200);
  9.  
  10.  
  11. // DDRB |= (1 << DDB0) | (1 << DDB1);
  12. // PORTB |= (1 << PORTB0);
  13. // PORTB |= (1 << PORTB1);
  14.  
  15. /* // Set Timer1 CTC
  16. TCCR1B = (TCCR1B & ~_BV(WGM13)) | _BV(WGM12);
  17. TCCR1A = TCCR1A & ~(_BV(WGM11) | _BV(WGM10));
  18.  
  19. // No prescaler (p.134)
  20. TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
  21.  
  22. OCR1A = 0;
  23. TIMSK1 |= _BV(OCIE1A);*/
  24. }
  25.  
  26. #define nop() __asm volatile ("nop")
  27. #define nop1() nop();
  28. #include <nopnopnop.h>
  29.  
  30. #define onD() PORTB = (1 << PORTB0);
  31. #define onC() PORTB = (1 << PORTB1);
  32. #define onB() PORTB = (1 << PORTB0) | (1 << PORTB1);
  33. #define offB() PORTB = 0;
  34. #define off() nop10(); PORTB = 0;
  35. #define off2() nop10(); PORTB = 0; nop10();
  36.  
  37. #define DH() PORTB |= (1 << PORTB0)
  38. #define CH() PORTB |= (1 << PORTB1)
  39. #define DL() PORTB &= ~(1 << PORTB0)
  40. #define CL() PORTB &= ~(1 << PORTB1)
  41.  
  42. #include <util/delay.h>
  43.  
  44. #define D_ALL 0b01010001
  45. #define C_ALL 0b00001101
  46.  
  47. #define D_PIXEL 0b01100011
  48.  
  49. #define D_GREEN 0b00010000
  50. #define D_RED 0b00001000
  51. #define D_BLUE 0b00000100
  52. #define D_WHITE 0b00011100
  53. #define D_BLACK 0b00000000
  54.  
  55. #define C_PIXEL 0b01000100
  56.  
  57. #define D_P69 0b01000010
  58. #define C_P69 0b00100100
  59.  
  60. #define C_MYSTERY1 0b01000000
  61. #define C_MYSTERY2 0b00011000
  62.  
  63. #define DELAY 50
  64.  
  65. // cccccc-ddddddd
  66. #define CMD_RAINBOW 0b11111000110001 // shifts a pixel for pwm
  67. #define CMD_DOPWM 0b10101000101000 // enables pwm mixing
  68. #define CMD_DOSWIPE 0b10101000100100 // enables backwards shifting?
  69. #define CMD_SHIFT 0b10001000110001 // shifts a pixel into the register
  70. #define CMD_CLS1 0b01001000111000 //?? before foreground paint
  71. #define CMD_SWIPE 0b01001000100001 //?? responsible for program 69
  72. #define CMD_CLS2 0b00101000100000 //?? before background paint
  73. #define CMD_BG 0b01001000110000 // paint background, broken
  74. #define CMD_FG 0b00001000101000 // paint foreground
  75. #define CMD_EMPTY 0b00001000100001 // shifts pixels, removed bg, but not all of it
  76. #define CMD_SETPWM 0b10001000100000 // pwm speed for fading
  77.  
  78. // fade in : CMD_SETPWM + CMD_BG
  79. // fade out: CMD_SETPWM + CMD_BG | BG_OFF
  80. #define PWM_1 0b00100000000000
  81. #define PWM_2 0b00010000000000
  82. #define PWM_3 0b00000000001000
  83. #define PWM_4 0b00000000000100
  84. #define PWM_5 0b00000000000010
  85.  
  86. #define SHIFT_CGREEN 0b01000000000000
  87. #define SHIFT_CRED 0b00100000000000
  88. #define SHIFT_CBLUE 0b00010000000000
  89.  
  90. // bitmap
  91.  
  92. // cccccc-ddddddd
  93. //#define RAINBOW+COL 0b111110 011***1 // shifts a pixel for pwm
  94. //#define SWIPE+M1+M2 0b111110 010***1
  95. //#define SWIPE+MYST1 0b110010 010***1
  96. //#define CMD_SETPWM 0b10**100010***0 // pwm speed for fading
  97. //#define CMD_SHIFT 0b1***10 011***1 // shifts a pixel into the register
  98. //#define SWIPE+MYST2 0b011110 010***1
  99. //#define CMD_CLS1 0b01**10 0111*00 //?? before foreground paint, set background as well
  100. //#define CMD_BG 0b01**10 0110*00 // paint background
  101. //#define CMD_SWIPE 0b010010 010***1 //?? responsible for program 69
  102. //#define CMD_CLS2 0b001010 0100000 //?? before background paint
  103. //#define CMD_FG 0b00**10 0101*00 // paint foreground
  104. //#define CMD_EMPTY 0b000010 0100001 // shifts pixels, removed bg, but not all of it
  105.  
  106. // parameters
  107. #define BG_BLUE 0b00000000000100
  108. #define BG_GREEN 0b00010000000000
  109. #define BG_PERS 0b00100000000000
  110. #define BG_PERS2 0b00000000000010
  111. #define BG_RED 0b00000000001000
  112. #define BG_WHITE 0b00110000000100
  113. #define BG_OFF 0b00100000000000
  114.  
  115. #define FG_RED 0b00000000000100
  116. #define FG_BLUE 0b00010000000000
  117. #define FG_GREEN 0b00100000000000
  118. #define FG_WHITE 0b00110000000100
  119. #define FG_OFF 0b00100000000000
  120.  
  121. #define SHIFT_RED 0b00000000000100
  122. #define SHIFT_BLUE 0b00000000000010
  123. #define SHIFT_GREEN 0b00000000001000
  124. #define SHIFT_WHITE 0b00000000001110
  125.  
  126. #define SWIPE_MYST1 0b10000000000000
  127. #define SWIPE_MYST2 0b00110000000000
  128. #define SWIPE_RED SHIFT_RED
  129. #define SWIPE_BLUE SHIFT_BLUE
  130. #define SWIPE_GREEN SHIFT_GREEN
  131. #define SWIPE_WHITE SHIFT_WHITE
  132.  
  133. #define R CMD_FG | FG_RED
  134. #define B CMD_FG | FG_BLUE
  135. #define G CMD_FG | FG_GREEN
  136. #define W CMD_FG | FG_WHITE
  137.  
  138. uint8_t wait;
  139. int pos = 0, pos2 = 10, pos3 = 16;
  140. int d = 1;
  141. int max = 160;
  142. int first = 0;
  143.  
  144. #define ap(x) abs(159 - x)
  145.  
  146. void pp(int p1, int p2, int p3)
  147. {
  148. for (int i = 0; i < 160; i++)
  149. {
  150. int c = 0;
  151.  
  152. if (i == p1)
  153. c |= SHIFT_CRED;
  154. if (i == p2)
  155. c |= SHIFT_CGREEN;
  156. if (i == p3)
  157. c |= SHIFT_CBLUE;
  158.  
  159. writeOne(CMD_SHIFT | c);
  160. }
  161.  
  162. _delay_ms(10);
  163. }
  164.  
  165. char buffer[160];
  166.  
  167. void loop()
  168. {
  169. while (Serial.read() != 0xFF);
  170.  
  171. for (int i = 0; i < 160; i++)
  172. buffer[i] = Serial.read();
  173.  
  174. noInterrupts();
  175.  
  176. for (int i = 0; i < 160; i++)
  177. writeOne(CMD_SHIFT | buffer[i]);
  178.  
  179. interrupts();
  180. /*
  181.  
  182. while (Serial.available())
  183. {
  184. int c = CMD_SHIFT | (Serial.read() << 10);
  185. writeOne(c);
  186. }7
  187.  
  188. */
  189. return;
  190. writeOne(CMD_SHIFT | SHIFT_CBLUE | SHIFT_BLUE);
  191. writeOne(CMD_SHIFT | SHIFT_BLUE );
  192. writeOne(CMD_SHIFT | SHIFT_CGREEN | SHIFT_GREEN);
  193. writeOne(CMD_SHIFT | SHIFT_CGREEN | SHIFT_GREEN );
  194. writeOne(CMD_SHIFT | SHIFT_CGREEN | SHIFT_GREEN );
  195. writeOne(CMD_SHIFT | SHIFT_CGREEN | SHIFT_GREEN );
  196. writeOne(CMD_SHIFT | SHIFT_GREEN );
  197. writeOne(CMD_SHIFT | SHIFT_CRED | SHIFT_RED );
  198. writeOne(CMD_SHIFT | SHIFT_CRED | SHIFT_RED );
  199. writeOne(CMD_SHIFT | SHIFT_CRED | SHIFT_RED );
  200. writeOne(CMD_SHIFT | SHIFT_CRED | SHIFT_RED );
  201. writeOne(CMD_SHIFT | SHIFT_CRED | SHIFT_RED );
  202. writeOne(CMD_SHIFT | SHIFT_BLUE );
  203. writeOne(CMD_SHIFT | SHIFT_CBLUE | SHIFT_BLUE);
  204. writeOne(CMD_SHIFT | SHIFT_CBLUE | SHIFT_BLUE);
  205. writeOne(CMD_SHIFT | SHIFT_RED );
  206.  
  207. _delay_ms(5000);
  208.  
  209. writeOne(CMD_SHIFT | SHIFT_BLUE);
  210. writeOne(CMD_SHIFT | SHIFT_BLUE);
  211. writeOne(CMD_SHIFT | SHIFT_BLUE);
  212. writeOne(CMD_SHIFT | SHIFT_BLUE);
  213. writeOne(CMD_SHIFT | SHIFT_BLUE);
  214. writeOne(CMD_SHIFT | SHIFT_BLUE);
  215. writeOne(CMD_SHIFT | SHIFT_BLUE);
  216. writeOne(CMD_SHIFT | SHIFT_BLUE);
  217. writeOne(CMD_SHIFT | SHIFT_BLUE);
  218. writeOne(CMD_SHIFT | SHIFT_BLUE);
  219. writeOne(CMD_SHIFT | SHIFT_BLUE);
  220. writeOne(CMD_SHIFT | SHIFT_BLUE);
  221. writeOne(CMD_SHIFT | SHIFT_BLUE);
  222. writeOne(CMD_SHIFT | SHIFT_BLUE);
  223. writeOne(CMD_SHIFT | SHIFT_BLUE);
  224. writeOne(CMD_SHIFT | SHIFT_BLUE);
  225. _delay_ms(5000);
  226. /*
  227. writeOne(CMD_SHIFT | SHIFT_CBLUE);
  228. _delay_ms(500); */
  229. }
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276. // ? ? ? ? ? GG RR BB ?
  277. // DTI + 16 , 16 | 10 , 14 | 26 | 16 , 16 | 18 , 16 | 18 , 18 | 14 , 20 | 12 , 22 | 14 +
  278. // CKI | 16 , 16 , 10 | 14 , 26 , 16 | 16 , 18 | 16 , 18 | 18 , 14 | 20 , 12 | 22 , 14 |
  279. // ? ? ?
  280.  
  281. // ? ? ? ? ? GG RR BB ?
  282. // DTI + 34 | 10 , 14 | 26 | 17 , 17 | 17 , 17 | 17 , 17 | 17 , 17 | 17 , 17 | 17 +
  283. // CKI | 34 , 10 | 14 , 26 , 17 | 17 , 17 | 17 , 17 | 17 , 17 | 17 , 17 | 17 , 17 |
  284. // ? ? ?
  285.  
  286. #define _bit2(r, b, nops) if (C & (1 << b)) \
  287. r##H(); \
  288. \
  289. else \
  290. r##L(); \
  291. \
  292. nop##nops(); \
  293.  
  294. #define _bit(r, b, nops) if (r & (1 << b)) \
  295. r##H(); \
  296. \
  297. else \
  298. r##L(); \
  299. \
  300. nop##nops(); \
  301.  
  302.  
  303. void writeOne(uint16_t C)
  304. {
  305. CL();
  306. nop46();
  307. _bit2(D, 6, 8);
  308. _bit2(C, 13, 18);
  309. _bit2(D, 5, 38);
  310. _bit2(D, 4, 15);
  311. _bit2(C, 12, 21);
  312. _bit2(D, 3, 21);
  313. _bit2(C, 11, 21);
  314. _bit2(D, 2, 21);
  315. _bit2(C, 10, 21);
  316. _bit2(D, 1, 21);
  317. _bit2(C, 9, 21);
  318. _bit2(D, 0, 21);
  319. _bit2(C, 8, 21);
  320. DH();
  321. nop32();
  322. CH();
  323. _delay_us(DELAY);
  324. }
  325.  
  326. inline void writeByte(uint8_t D, uint8_t C)
  327. {
  328. _bit(C, 7, 46);
  329. _bit(D, 7, 8);
  330. _bit(C, 6, 18);
  331. _bit(D, 6, 38);
  332. _bit(D, 5, 15);
  333. _bit(C, 5, 21);
  334. _bit(D, 4, 21);
  335. _bit(C, 4, 21);
  336. _bit(D, 3, 21);
  337. _bit(C, 3, 21);
  338. _bit(D, 2, 21);
  339. _bit(C, 2, 21);
  340. _bit(D, 1, 21);
  341. _bit(C, 1, 21);
  342. DH();
  343. nop32();
  344. CH();
  345. }
  346.  
  347. inline void writeByteCD(uint8_t C, uint8_t D)
  348. {
  349. writeByte(D, C);
  350. _delay_us(DELAY);
  351. }
  352.  
  353.  
  354.  
  355. // volatile uint8_t times[] = { 32, 22, 26, 66, 36, 14, 34, 18, 16, 0xFF, 32, 24, 24, 36, 68, 20, 34, 16, 18, 0xFF }; // * 100.0ns<
  356.  
  357. // volatile uint8_t cmds [] = { CL, DL, DH, DL, DH, DL, CH, CL, DH, CH, W0, CL, DL, DH, DL, DH, DL, CH, CL, DH, CH, W1 };
  358.  
  359.  
  360.  
  361. // volatile uint8_t times[] = { 32, 22, 26, 66, 36, 14, 34, 18, 16, 0xFF, 32, 24, 24, 36, 16, 16, 16, 37, 35, 16, 18, 0xFF }; // * 100.0 ns<
  362.  
  363. // volatile uint8_t cmds [] = { CL, DL, DH, DL, DH, DL, CH, CL, DH, CH, W0, CL, DL, DH, DL, DH, BH, DL, CL, CH, CL, DH, CH, W1 };
  364.  
  365.  
  366.  
  367. // volatile uint8_t times[] = { 32, 22, 26, 66, 36, 14, 34, 18, 16, 0xFF, 32, 24, 24, 36, 34, 18, 70, 18, 16, 0xFF }; // * 100.0 ns<
  368.  
  369. // volatile uint8_t cmds [] = { CL, DL, DH, DL, DH, DL, CH, CL, DH, CH, W0, CL, DL, DH, DL, DH, DL, CH, CL, DH, CH, W1 };
RAW Paste Data