Guest User

Untitled

a guest
Mar 13th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include "comm.h"
  2. #include "Rainbow.h"
  3. #include <Wire.h>
  4. #include <EEPROM.h>
  5.  
  6. /* -*- c++ -*-
  7.  
  8. This is firmware for rainbowduinos that are chained together.
  9.  
  10. */
  11.  
  12.  
  13. Comm comm = Comm();
  14. Rainbow rainbow = Rainbow();
  15.  
  16. // color is packed into a 2byte short: 0x0bgr
  17. unsigned short get_short()
  18. {
  19. byte one = comm.read();
  20. byte two = comm.read();
  21. return (one << 8) | two;
  22. }
  23.  
  24. bool handle_packet(int nbytes)
  25. {
  26. unsigned char cmd = comm.read();
  27. Serial.print("Handling ");
  28. Serial.print(nbytes);
  29. Serial.print(" bytes with command ");
  30. Serial.println(cmd);
  31.  
  32. unsigned short color;
  33. unsigned short eight[8];
  34. unsigned short matrix[8][8];
  35.  
  36. unsigned char row, col, pixel, ascii;
  37.  
  38. switch (cmd) {
  39.  
  40. case 'D': // Darken (clear) all LEDs
  41. rainbow.closeAll();
  42. break;
  43.  
  44. case 'L': // Light all LEDs given color
  45. color = get_short();
  46. rainbow.lightAll(color);
  47. break;
  48.  
  49. case 'P': // set one pixel to a color
  50. // format: 0xCR (4 bits for column and 4 bits for row)
  51. pixel = comm.read();
  52. col = pixel >> 4;
  53. row = pixel & 0x0F;
  54.  
  55. // format: 0x0bgr
  56. color = get_short();
  57. rainbow.lightOneDot(col,row,color,OTHERS_ON);
  58. break;
  59.  
  60. case 'R': // Set one row
  61. // format: 0x0R (row number) 0x0bgr * 8 (8 shorts of colors)
  62. row = comm.read();
  63. for (int ind=0; ind<8; ++ind) {
  64. eight[ind] = get_short();
  65. }
  66. rainbow.lightOneLine(row,eight,OTHERS_ON);
  67. break;
  68.  
  69. case 'C': // Set one column
  70. // format: 0x0C (column number) 0x0bgr * 8 (8 shorts of colors)
  71. col = comm.read();
  72. for (int ind=0; ind<8; ++ind) {
  73. eight[ind] = get_short();
  74. }
  75. rainbow.lightOneColumn(col,eight,OTHERS_ON);
  76. break;
  77.  
  78. case 'M': // Set whole matrix
  79. // format: 64 shorts of color, row1's 8 columns first, row8's last
  80. for (int irow = 0; irow < 8; ++irow) {
  81. for (int icol = 0; icol < 8; ++icol) {
  82. matrix[irow][icol] = get_short();
  83. }
  84. }
  85. rainbow.lightAll(matrix);
  86. break;
  87.  
  88. case 'A': // Set an ASCII letter
  89. // format: Letter(byte), color(short), offest(byte)
  90. ascii = comm.read();
  91. color = get_short();
  92. col = comm.read();
  93. rainbow.dispChar(ascii, color, col);
  94. break;
  95.  
  96. default:
  97. Serial.print("Unknown command: ");
  98. Serial.println(cmd);
  99. //comm.drain(nbytes-1);
  100. return false;
  101. break;
  102. }
  103. return true;
  104. }
  105.  
  106.  
  107. void setup ()
  108. {
  109. int addr = EEPROM.read(0);
  110. comm.init(addr);
  111. comm.set_handler(handle_packet);
  112.  
  113. rainbow.init();
  114. rainbow.lightAll(WHITE);
  115. }
  116.  
  117. void loop ()
  118. {
  119. comm.process();
  120. }
  121.  
  122. //= //Timer1 interuption service routine=========================================
  123. ISR(TIMER1_OVF_vect)
  124. {
  125. //sweep 8 lines to make led matrix looks stable
  126. static unsigned char line=0,level=0;
  127.  
  128. flash_line(line,level);
  129.  
  130. line++;
  131. if (line>7) {
  132. line=0;
  133. level++;
  134. if(level>15) {
  135. level=0;
  136. }
  137. }
  138.  
  139. }
Add Comment
Please, Sign In to add comment