Advertisement
Guest User

Untitled

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