Guest User

Untitled

a guest
Dec 11th, 2019
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "msp.h"
  2. #include "stdbool.h"
  3. #include "stdint.h"
  4. #include "stdio.h"
  5. #include <ti/devices/msp432p4xx/driverlib/driverlib.h>
  6.  
  7.  
  8. /* #DEFINES */
  9.  
  10. #define RGB_MAX 255
  11. #define LED_PORT GPIO_PORT_P2
  12. #define LED_PIN GPIO_PIN3
  13. #define LIGHTS 13
  14. enum State{Stopped, Progressing}; //The various blinking states the strip can be in
  15.  
  16.  
  17. /* Prototype function calls */
  18. void setColor(uint8_t g, uint8_t r, uint8_t b);
  19. void setGRB();
  20. void set_led_white();
  21. void set_led_off();
  22. void setAnimation();
  23. void icicleAnimation();
  24. //void hokieStone();
  25. void mardisGras();
  26. void icicleStill();
  27. void icicle2();
  28. void icicleAnimation2();
  29. void crossEndedAnimation();
  30. void rainbowAnimation();
  31.  
  32.  
  33.  
  34. struct LED {
  35. uint8_t red, green, blue;
  36. };
  37. struct LED led_strip[LIGHTS]; //initialize the struct
  38. uint32_t frame = 0;
  39.  
  40. /**
  41. * main.c
  42. */
  43. void main(void)
  44. {
  45. WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
  46.  
  47. //Initialize ports
  48. MAP_GPIO_setAsOutputPin(LED_PORT, LED_PIN);
  49.  
  50. //struct LED led_strip[LIGHTS]; //initialize the struct
  51. //start the first color as white
  52. set_led_off();
  53. __delay_cycles(4000000);
  54. set_led_white();
  55.  
  56. //set_led_white();
  57. // uint32_t frameRate = BPM/60*20.8*10^9; //20.8 ns for one frame. 1 bpm =
  58. __delay_cycles(200000);
  59. __delay_cycles(4000000);
  60.  
  61. //do all calculations before entering the while loop
  62.  
  63.  
  64. while(1) {
  65. //do the color set here, since frame rate doesn't matter
  66. /* DO ALL THE CALCULATIONS FOR THE COLOR OF THE LIGHTS */
  67. // setGRB();
  68. // __delay_cycles(40000000);
  69. // set_led_white();
  70. // __delay_cycles(40000000);
  71. // set_led_off();
  72. // __delay_cycles(40000000);
  73. // hokieStone();
  74. // mardisGras();
  75. // icicleStill();
  76. // icicle2();
  77. // icicleAnimation2();
  78. // icicleAnimation3();
  79.  
  80. // setGRB();
  81. // mardisGras();
  82. int i;
  83. // for(i=0;i<10;i++){
  84. // icicleAnimation();
  85. // }
  86. /**
  87. for(i =0; i<10; i++) {
  88. crossEndedAnimation();
  89. }
  90. */
  91. rainbowAnimation();
  92.  
  93. // for(i=0; i<10; i++) {
  94. // rainbowAnimation();
  95. // }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. //SET EACH OF THE LEDS
  104. int noOps;
  105.  
  106. /* SET ONE FRAME ONE FRAME */
  107.  
  108. // for(i = 0; i < LIGHTS; i++) {
  109. //
  110. // //icicleAnimation();
  111. // setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  112. // //move to next light in strip
  113. // __delay_cycles(1000);
  114. //
  115. //
  116. // }
  117. //go to next frame
  118. //__delay_cycles(4000000);
  119. /**
  120. for(i = 0; i < frameRate; i++) {
  121. __no_operation();
  122. }
  123. */
  124.  
  125. }
  126. }
  127.  
  128.  
  129.  
  130. /**
  131. * Cycles through to turn on one GRB light
  132. */
  133.  
  134. void setColor(uint8_t g, uint8_t r, uint8_t b) {
  135. //bit shift down
  136. uint32_t color = (g<<16) | (r<<8) | b;
  137. uint8_t i;
  138. uint8_t ar[24];
  139. for(i = 0; i < 8; i++) {
  140. ar[i] = (g >> (7 - i)) & 0b00000001;
  141. }
  142. for(i = 0; i < 8; i++) {
  143. ar[i+8] = (r >> (7 - i)) & 0b00000001;
  144. }
  145. for(i = 0; i < 8; i++) {
  146. ar[i+16] = (b >> (7 - i)) & 0b00000001;
  147. }
  148.  
  149. uint8_t *p = ar;
  150. for(i = 0; i < 24; i++) {
  151. //bit shift, starting from MSB in g, r, then b
  152.  
  153. if(*(p+i)) {
  154. //Turn high on, so 700 ns or 19 no-op calls
  155. P2->OUT |= 0x08;
  156.  
  157. __no_operation();
  158. __no_operation();
  159. __no_operation();
  160. __no_operation();
  161. __no_operation();
  162. __no_operation();
  163. __no_operation();
  164. __no_operation();
  165. __no_operation();
  166. __no_operation();
  167. __no_operation();
  168. __no_operation();
  169. __no_operation();
  170. __no_operation();
  171. __no_operation();
  172. __no_operation();
  173. __no_operation();
  174. __no_operation();
  175. __no_operation();
  176. __no_operation();
  177. __no_operation();
  178. __no_operation();
  179. __no_operation();
  180. __no_operation();
  181. __no_operation();
  182. __no_operation();
  183. __no_operation();
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190. //set off, so 600 ns or 15 no-op calls
  191. P2->OUT &= ~0x08;
  192.  
  193. __no_operation();
  194. __no_operation();
  195. __no_operation();
  196. __no_operation();
  197.  
  198.  
  199.  
  200.  
  201.  
  202. }
  203.  
  204. else {
  205.  
  206. //Set high, so 350 ns or 3 no-op calls
  207. P2->OUT |= 0x08;
  208. __no_operation();
  209. __no_operation();
  210. __no_operation();
  211. __no_operation();
  212. __no_operation();
  213. __no_operation();
  214. __no_operation();
  215. __no_operation();
  216. __no_operation();
  217.  
  218. //running at 48MHz, each NOP causes 20.8 ns delay
  219.  
  220.  
  221. //set off, so 600 ns or 15 no-op calls
  222. P2->OUT &= ~0x08;
  223. __no_operation();
  224. __no_operation();
  225. __no_operation();
  226. __no_operation();
  227. __no_operation();
  228. __no_operation();
  229. __no_operation();
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238. }
  239. // p++;
  240. }
  241.  
  242. //move to next pixel
  243.  
  244. //send high signal
  245. }
  246.  
  247. void set_led_off() {
  248. int i;
  249. for(i = 0; i < LIGHTS; i++)
  250. {
  251. led_strip[i].red = 0;
  252. led_strip[i].blue = 0;
  253. led_strip[i].green = 0;
  254. }
  255.  
  256. for(i = 0; i < LIGHTS; i++)
  257. {
  258. setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  259. }
  260.  
  261.  
  262.  
  263. }
  264.  
  265. void set_led_white() {
  266. int i;
  267. for(i = 0; i < LIGHTS; i++)
  268. {
  269. led_strip[i].red = RGB_MAX;
  270. led_strip[i].blue = RGB_MAX;
  271. led_strip[i].green = RGB_MAX;
  272. }
  273.  
  274.  
  275. for(i = 0; i < LIGHTS; i++)
  276. {
  277. setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  278. }
  279.  
  280.  
  281.  
  282. }
  283.  
  284.  
  285. void setGRB() {
  286. int j;
  287. for(j=0; j < LIGHTS; j++) {
  288. /* Set every first light to green */
  289. if(j%3 == 0) {
  290. led_strip[j].green =RGB_MAX;
  291. led_strip[j].red = 0;
  292. led_strip[j].blue = 0;
  293. }
  294.  
  295. /* Set every second light to red*/
  296. if(j%3 == 1) {
  297.  
  298. led_strip[j].green =0;
  299. led_strip[j].red = RGB_MAX;
  300. led_strip[j].blue = 0;
  301. }
  302.  
  303. /* Set every third light to blue*/
  304. if(j%3 == 2) {
  305. led_strip[j].green =0;
  306. led_strip[j].red = 0;
  307. led_strip[j].blue = RGB_MAX;
  308. }
  309. }
  310.  
  311.  
  312. /* Call to set the colors */
  313. int i;
  314. for(i = 0; i < LIGHTS; i++) {
  315. //icicleAnimation();
  316. setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  317. //move to next light in strip
  318. __delay_cycles(1000);
  319. }
  320. }
  321.  
  322. void mardisGras() {
  323. int j;
  324. for(j=0; j < LIGHTS; j++) {
  325. /* Set every first light to green */
  326. if(j%5 == 0) {
  327. led_strip[j].green =255;
  328. led_strip[j].red = 0;
  329. led_strip[j].blue = 0;
  330. }
  331.  
  332. /* Set every second light to red*/
  333. if(j%5 == 1) {
  334.  
  335. led_strip[j].green =69;
  336. led_strip[j].red = 139;
  337. led_strip[j].blue = 19;
  338. }
  339.  
  340. /* Set every third light to blue*/
  341. if(j%5 == 2) {
  342. led_strip[j].green =0;
  343. led_strip[j].red = 255;
  344. led_strip[j].blue = 255;
  345. }
  346.  
  347. if(j%5 == 3) {
  348. led_strip[j].green =100;
  349. led_strip[j].red = 100;
  350. led_strip[j].blue = 0;
  351. }
  352.  
  353. if(j%5 == 4) {
  354. led_strip[j].green =215;
  355. led_strip[j].red = 255;
  356. led_strip[j].blue = 0;
  357. }
  358. }
  359.  
  360.  
  361. /* Call to set the colors */
  362. int i;
  363. for(i = 0; i < LIGHTS; i++) {
  364. //icicleAnimation();
  365. setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  366. //move to next light in strip
  367. __delay_cycles(1000);
  368. }
  369. }
  370.  
  371.  
  372.  
  373.  
  374.  
  375. void icicleAnimation() {
  376. //TODO change icicle colors
  377. /* SET COLOR ARRAY */
  378. int j;
  379.  
  380. led_strip[0].green =255;
  381. led_strip[0].red = 0;
  382. led_strip[0].blue = 0;
  383.  
  384. led_strip[1].green =69;
  385. led_strip[1].red = 139;
  386. led_strip[1].blue = 19;
  387.  
  388. led_strip[2].green =0;
  389. led_strip[2].red = 255;
  390. led_strip[2].blue = 255;
  391.  
  392. led_strip[3].green =255;
  393. led_strip[3].red = 203;
  394. led_strip[3].blue = 153;
  395.  
  396. led_strip[4].green =255;
  397. led_strip[4].red = 229;
  398. led_strip[4].blue = 204;
  399.  
  400. for(j=5; j < LIGHTS; j++) {
  401. /* Set every light off */
  402. led_strip[j].green = 0;
  403. led_strip[j].red = 0;
  404. led_strip[j].blue = 0;
  405. }
  406.  
  407. /* NOW RUN FRAMES*/
  408. uint32_t frame = 0;
  409. uint32_t i;
  410.  
  411. while(frame < LIGHTS) {
  412. for(i = 0; i < LIGHTS; i++) {
  413. setColor(led_strip[(LIGHTS-frame+i)%LIGHTS].green, led_strip[(LIGHTS-frame+i)%LIGHTS].red, led_strip[(LIGHTS-frame+i)%LIGHTS].blue);
  414. }
  415. frame++;
  416. __delay_cycles(4000000);
  417. }
  418. }
  419.  
  420. void crossEndedAnimation() {
  421.  
  422. uint8_t b = 0;
  423. uint8_t g0 = 0;
  424. uint8_t g1 = 50;
  425. uint8_t g2 = 100;
  426. uint8_t g3 = 150;
  427. uint8_t g4 = 200;
  428. uint8_t g5 = 255;
  429. uint8_t r0 = 0;
  430. uint8_t r1 = 50;
  431. uint8_t r2 = 100;
  432. uint8_t r3 = 150;
  433. uint8_t r4 = 200;
  434. uint8_t r5 = 255;
  435.  
  436.  
  437.  
  438.  
  439. /* NOW RUN FRAMES*/
  440. uint32_t frame = 0;
  441. uint32_t i;
  442.  
  443. while(frame < LIGHTS) {
  444. /* Change values of array*/
  445. for(i = 0; i < frame; i++) {
  446. led_strip[i].green = g0;
  447. led_strip[i].red = r0;
  448. led_strip[i].blue = b;
  449. led_strip[LIGHTS-i-1].green = g0;
  450. led_strip[LIGHTS-i-1].red = r0;
  451. led_strip[LIGHTS-i-1].blue = b;
  452. }
  453. //Set green gradient
  454. led_strip[i+1].green = g1;
  455. led_strip[i+1].red = r0;
  456. led_strip[i+2].green = g2;
  457. led_strip[i+2].red = r0;
  458. // led_strip[i+3].green = g3;
  459. // led_strip[i+3].red = r0;
  460. // led_strip[i+4].green = g4;
  461. // led_strip[i+4].red = r0;
  462. // led_strip[i+5].green = g5;
  463. // led_strip[i+5].red = r0;
  464.  
  465. //Set the red gradient
  466. led_strip[LIGHTS-i-2].green = g0;
  467. led_strip[LIGHTS-i-2].red = r1;
  468. led_strip[LIGHTS-i-3].green = g0;
  469. led_strip[LIGHTS-i-3].red = r2;
  470. // led_strip[LIGHTS-i-4].green = g0;
  471. // led_strip[LIGHTS-i-4].red = r3;
  472. // led_strip[LIGHTS-i-5].green = g0;
  473. // led_strip[LIGHTS-i-5].red = r4;
  474. // led_strip[LIGHTS-i-6].green = g0;
  475. // led_strip[LIGHTS-i-6].red = r5;
  476.  
  477. /* Display values of array*/
  478. for(i = 0; i < LIGHTS; i++) {
  479. setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  480. }
  481. frame++;
  482. __delay_cycles(4000000);
  483. }
  484. }
  485.  
  486.  
  487. void rainbowAnimation() {
  488.  
  489. int j;
  490. int i;
  491.  
  492. for(j=0; j < LIGHTS*(3/8); j++){
  493. led_strip[j].green = 128*(16/3)*j/LIGHTS;
  494. led_strip[j].red = 255;
  495. led_strip[j].blue = 0;
  496. }
  497.  
  498. for(j=LIGHTS*(3/8); j < LIGHTS*(4/8); j++){
  499. led_strip[j].green = 255;
  500. led_strip[j].red = 255 - 255*2*j/LIGHTS;
  501. led_strip[j].blue = 0;
  502. }
  503.  
  504. for(j=LIGHTS*(4/8); j < LIGHTS*(5/8); j++){
  505. led_strip[j].green = 255 - 255*(8/5)*j/LIGHTS;
  506. led_strip[j].red = 0;
  507. led_strip[j].blue = 255*(8/5)*j/LIGHTS;
  508. }
  509.  
  510. for(j=LIGHTS*(5/8); j < LIGHTS*(6/8); j++){
  511. led_strip[j].green = 0;
  512. led_strip[j].red = 75*(8/6)*j/LIGHTS;
  513. led_strip[j].blue = 255 - 125*(8/6)*j/LIGHTS;
  514. }
  515.  
  516. for(j=LIGHTS*(6/8); j < LIGHTS*(7/8); j++){
  517. led_strip[j].green = 0;
  518. led_strip[j].red = 75+73*(8/7)*j/LIGHTS;
  519. led_strip[j].blue = 130+81*(8/7)*j/LIGHTS;
  520. }
  521.  
  522. for(j=LIGHTS*(7/8); j < LIGHTS*(8/8); j++){
  523. led_strip[j].green = 0;
  524. led_strip[j].red = 148+107*j/LIGHTS;
  525. led_strip[j].blue = 211-211*j/LIGHTS;
  526. }
  527.  
  528. while(frame < LIGHTS) {
  529. for(i = 0; i < LIGHTS; i++) {
  530. setColor(led_strip[(LIGHTS-frame+i)%LIGHTS].green, led_strip[(LIGHTS-frame+i)%LIGHTS].red, led_strip[(LIGHTS-frame+i)%LIGHTS].blue);
  531. }
  532. frame++;
  533. __delay_cycles(4000000);
  534. }
  535. }
RAW Paste Data