Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 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. #define LIGHTS 20 //the number of lights on the strip
  10. #define RGB_MAX 255
  11. #define LED_PORT GPIO_PORT_P2
  12. #define LED_PIN GPIO_PIN3
  13. #define LIGHTS 200
  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.  
  25.  
  26.  
  27.  
  28.  
  29. struct LED {
  30. uint8_t red, green, blue;
  31. };
  32. struct LED led_strip[LIGHTS]; //initialize the struct
  33. uint32_t frame = 0;
  34.  
  35. /**
  36. * main.c
  37. */
  38. void main(void)
  39. {
  40. WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
  41.  
  42. //Initialize ports
  43. MAP_GPIO_setAsOutputPin(LED_PORT, LED_PIN);
  44.  
  45. //struct LED led_strip[LIGHTS]; //initialize the struct
  46. //start the first color as white
  47. set_led_off();
  48. __delay_cycles(4000000);
  49. set_led_white();
  50.  
  51. //set_led_white();
  52. // uint32_t frameRate = BPM/60*20.8*10^9; //20.8 ns for one frame. 1 bpm =
  53. __delay_cycles(200000);
  54. __delay_cycles(4000000);
  55.  
  56. //do all calculations before entering the while loop
  57.  
  58. while(1) {
  59. //do the color set here, since frame rate doesn't matter
  60. /* DO ALL THE CALCULATIONS FOR THE COLOR OF THE LIGHTS */
  61. setGRB();
  62. __delay_cycles(4000000);
  63. set_led_white();
  64. __delay_cycles(4000000);
  65. set_led_off();
  66. __delay_cycles(4000000);
  67.  
  68.  
  69.  
  70.  
  71.  
  72. //SET EACH OF THE LEDS
  73. int i;
  74. int noOps;
  75.  
  76. /* SET ONE FRAME ONE FRAME */
  77.  
  78. // for(i = 0; i < LIGHTS; i++) {
  79. //
  80. // //icicleAnimation();
  81. // setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  82. // //move to next light in strip
  83. // __delay_cycles(1000);
  84. //
  85. //
  86. // }
  87. //go to next frame
  88. __delay_cycles(4000000);
  89. /**
  90. for(i = 0; i < frameRate; i++) {
  91. __no_operation();
  92. }
  93. */
  94.  
  95. }
  96. }
  97.  
  98.  
  99.  
  100. /**
  101. * Cycles through to turn on one GRB light
  102. */
  103.  
  104. void setColor(uint8_t g, uint8_t r, uint8_t b) {
  105. //bit shift down
  106. uint32_t color = (g<<16) | (r<<8) | b;
  107. uint8_t i;
  108. uint8_t ar[24];
  109. for(i = 0; i < 8; i++) {
  110. ar[i] = (g >> (7 - i)) & 0b00000001;
  111. }
  112. for(i = 0; i < 8; i++) {
  113. ar[i+8] = (r >> (7 - i)) & 0b00000001;
  114. }
  115. for(i = 0; i < 8; i++) {
  116. ar[i+16] = (b >> (7 - i)) & 0b00000001;
  117. }
  118.  
  119. uint8_t *p = ar;
  120. for(i = 0; i < 24; i++) {
  121. //bit shift, starting from MSB in g, r, then b
  122.  
  123. if(*(p+i)) {
  124. //Turn high on, so 700 ns or 19 no-op calls
  125. P2->OUT |= 0x08;
  126.  
  127. __no_operation();
  128. __no_operation();
  129. __no_operation();
  130. __no_operation();
  131. __no_operation();
  132. __no_operation();
  133. __no_operation();
  134. __no_operation();
  135. __no_operation();
  136. __no_operation();
  137. __no_operation();
  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.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. //set off, so 600 ns or 15 no-op calls
  161. P2->OUT &= ~0x08;
  162.  
  163. __no_operation();
  164. __no_operation();
  165. __no_operation();
  166. __no_operation();
  167.  
  168.  
  169.  
  170.  
  171.  
  172. }
  173.  
  174. else {
  175.  
  176. //Set high, so 350 ns or 3 no-op calls
  177. P2->OUT |= 0x08;
  178. __no_operation();
  179. __no_operation();
  180. __no_operation();
  181. __no_operation();
  182. __no_operation();
  183. __no_operation();
  184. __no_operation();
  185. __no_operation();
  186. __no_operation();
  187.  
  188. //running at 48MHz, each NOP causes 20.8 ns delay
  189.  
  190.  
  191. //set off, so 600 ns or 15 no-op calls
  192. P2->OUT &= ~0x08;
  193. __no_operation();
  194. __no_operation();
  195. __no_operation();
  196. __no_operation();
  197. __no_operation();
  198. __no_operation();
  199. __no_operation();
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. }
  209. // p++;
  210. }
  211.  
  212. //move to next pixel
  213.  
  214. //send high signal
  215. }
  216.  
  217. void set_led_off() {
  218. int i;
  219. for(i = 0; i < LIGHTS; i++)
  220. {
  221. led_strip[i].red = 0;
  222. led_strip[i].blue = 0;
  223. led_strip[i].green = 0;
  224. }
  225.  
  226. for(i = 0; i < LIGHTS; i++)
  227. {
  228. setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  229. }
  230.  
  231.  
  232.  
  233. }
  234.  
  235. void set_led_white() {
  236. int i;
  237. for(i = 0; i < LIGHTS; i++)
  238. {
  239. led_strip[i].red = RGB_MAX;
  240. led_strip[i].blue = RGB_MAX;
  241. led_strip[i].green = RGB_MAX;
  242. }
  243.  
  244.  
  245. for(i = 0; i < LIGHTS; i++)
  246. {
  247. setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  248. }
  249.  
  250.  
  251.  
  252. }
  253.  
  254.  
  255. void setGRB() {
  256. int j;
  257. for(j=0; j < LIGHTS; j++) {
  258. //set every first light to green
  259. if(j%3 == 0) {
  260. led_strip[j].green =RGB_MAX;
  261. led_strip[j].red = 0;
  262. led_strip[j].blue = 0;
  263. // led_strip[j].green =0;
  264. // led_strip[j].red = 0;
  265. // led_strip[j].blue = 0;
  266. }
  267.  
  268. if(j%3 == 1) {
  269. // led_strip[j].green =RGB_MAX;
  270. // led_strip[j].red = RGB_MAX;
  271. // led_strip[j].blue = RGB_MAX;
  272. led_strip[j].green =0;
  273. led_strip[j].red = RGB_MAX;
  274. led_strip[j].blue = 0;
  275. }
  276.  
  277. if(j%3 == 2) {
  278. // led_strip[j].green =RGB_MAX;
  279. // led_strip[j].red = RGB_MAX;
  280. // led_strip[j].blue = RGB_MAX;
  281. led_strip[j].green =0;
  282. led_strip[j].red = 0;
  283. led_strip[j].blue = RGB_MAX;
  284. }
  285. }
  286. int i;
  287.  
  288. for(i = 0; i < LIGHTS; i++) {
  289. //icicleAnimation();
  290. setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  291. //move to next light in strip
  292. __delay_cycles(1000);
  293. }
  294. }
  295.  
  296.  
  297. void setAnimation() {
  298. static uint8_t frame = 0;
  299. int j;
  300. for(j=0; j < LIGHTS; j++) {
  301. //set every first light to green
  302. if(j%3 == 0) {
  303. led_strip[j].green =0;
  304. led_strip[j].red =0;
  305. led_strip[j].blue = 0;
  306. }
  307.  
  308. if(j%3 == 1) {
  309. led_strip[j].green =0;
  310. led_strip[j].red =0;
  311. led_strip[j].blue = 0;
  312. }
  313.  
  314. if(j%3 == 2) {
  315. // led_strip[j].green =RGB_MAX;
  316. // led_strip[j].red =RGB_MAX;
  317. // led_strip[j].blue = RGB_MAX;
  318. led_strip[j].green =0;
  319. led_strip[j].red =0;
  320. led_strip[j].blue = 0;
  321. }
  322. }
  323. }
  324.  
  325. /**
  326. void icecicleAnimation() {
  327. frame = (frame+1)%LIGHTS;
  328. uint8_t b1 = 255;
  329. uint8_t g1 = 0;
  330. uint8_t r1 = 0;
  331.  
  332. uint8_t b2 = 255;
  333. uint8_t g2 = 153;
  334. uint8_t r2 = 51;
  335.  
  336. uint8_t b3 = 255;
  337. uint8_t g3 = 178;
  338. uint8_t r3 = 102;
  339.  
  340. uint8_t b4 = 255;
  341. uint8_t g4 = 203;
  342. uint8_t r4 = 153;
  343.  
  344. uint8_t b5 = 255;
  345. uint8_t g5 = 229;
  346. uint8_t r5 = 204;
  347.  
  348. led_strip[frame%LIGHTS].blue = b1;
  349. led_strip[frame%LIGHTS].red = r1;
  350. led_strip[frame%LIGHTS].green = g1;
  351.  
  352. led_strip[(frame+1)%LIGHTS].blue = b2;
  353. led_strip[(frame+1)%LIGHTS].red = r2;
  354. led_strip[(frame+1)%LIGHTS].green = g2;
  355.  
  356. led_strip[(frame+2)%LIGHTS].blue = b3;
  357. led_strip[(frame+2)%LIGHTS].red = r3;
  358. led_strip[(frame+2)%LIGHTS].green = g3;
  359.  
  360. led_strip[(frame+3)%LIGHTS].blue = b4;
  361. led_strip[(frame+3)%LIGHTS].red = r4;
  362. led_strip[(frame+3)%LIGHTS].green = g4;
  363.  
  364. led_strip[(frame+4)%LIGHTS].blue = b5;
  365. led_strip[(frame+4)%LIGHTS].red = r5;
  366. led_strip[(frame+4)%LIGHTS].green = g5;
  367. int i;
  368.  
  369. for(i = 0; i < LIGHTS; i++) {
  370. setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
  371. __delay_cycles(1000);
  372. }
  373.  
  374.  
  375. }
  376. */
  377.  
  378. void icicleAnimation() {
  379. return;
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement