ledbar.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* 7 rows LED bar firmware / main program for Arduino Nano
  2. * Copyright (C) 2019 asdrea
  3. *
  4. * This program is free software: you can redistribute it and/or modify it under
  5. * the terms of the GNU General Public License as published by the Free Software
  6. * Foundation, either version 3 of the License, or (at your option) any later
  7. * version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  12. * details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "ledbar.h"
  18. enum {
  19. CMD_NOSCROLL,
  20. CMD_HSCROLL,
  21. CMD_VSCROLL,
  22. CMD_SPACING
  23. };
  24. enum {
  25. ALIGN_LEFT,
  26. ALIGN_RIGHT,
  27. ALIGN_CENTER
  28. };
  29. static String text;
  30. static int16_t text_pos = 0; // q11.5
  31. static int8_t scroll_speed = 0, scroll_mode = CMD_NOSCROLL, text_align = ALIGN_LEFT;
  32. static uint8_t text_spacing = 1;
  33. static uint8_t cmd_mode = 0;
  34. void setup()
  35. {
  36. Serial.begin(9600);
  37. ledbar_init();
  38. }
  39. void loop()
  40. {
  41. int16_t p;
  42. while(Serial.available() > 0) {
  43. uint8_t c = Serial.read();
  44. if(cmd_mode) {
  45. switch(c >> 6) {
  46. case CMD_NOSCROLL:
  47. scroll_mode = CMD_NOSCROLL;
  48. text_align = c & 0x3f;
  49. text_pos = 0;
  50. break;
  51. case CMD_VSCROLL:
  52. scroll_mode = CMD_VSCROLL;
  53. scroll_speed = (c & 0x3f) << 2;
  54. break;
  55. case CMD_HSCROLL:
  56. scroll_mode = CMD_HSCROLL;
  57. scroll_speed = (c & 0x3f) << 2;
  58. break;
  59. case CMD_SPACING:
  60. text_spacing = c & 0x3f;
  61. break;
  62. }
  63. cmd_mode = 0;
  64. continue;
  65. }
  66. if(c == '\0') {
  67. cmd_mode = 1;
  68. continue;
  69. }
  70. //Serial.write(c);
  71. if(c == '\n' || c == '\r') {
  72. text = "";
  73. }
  74. else if(c == 8 || c == 127) {
  75. if(text.length() > 0)
  76. text = text.substring(0, text.length() - 1);
  77. }
  78. else
  79. text += (char)c;
  80. }
  81. switch(scroll_mode) {
  82. default:
  83. case CMD_NOSCROLL:
  84. switch(text_align) {
  85. default:
  86. case ALIGN_LEFT:
  87. p = 0;
  88. break;
  89. case ALIGN_RIGHT:
  90. p = (5 + text_spacing) * (LEDBAR_COLS - (int16_t)text.length());
  91. break;
  92. case ALIGN_CENTER:
  93. p = LEDBAR_COLS / 2 - (5 + text_spacing) * ((int16_t)text.length() / 2);
  94. break;
  95. }
  96. break;
  97. case CMD_HSCROLL:
  98. text_pos += scroll_speed;
  99. p = text_pos / 32;
  100. if(scroll_speed < 0) {
  101. if(p < (-(5 + text_spacing) * (int16_t)text.length())) {
  102. p = LEDBAR_COLS;
  103. text_pos = p * 32;
  104. }
  105. }
  106. else {
  107. if(p > LEDBAR_COLS) {
  108. p = -(5 + text_spacing) * (int16_t)text.length();
  109. text_pos = p * 32;
  110. }
  111. }
  112. break;
  113. case CMD_VSCROLL:
  114. // Implement me please :'(
  115. break;
  116. }
  117. ledbar_wait_vsync();
  118. ledbar_clear(LEDBAR_BACKBUF);
  119. ledbar_settext(LEDBAR_BACKBUF, p, text.c_str(), text_spacing);
  120. ledbar_swap_buf();
  121. }