ser2neo: a serial port to NeoPixel ring bridge.

ser2neo.cc 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Takes commands over serial and updates a NeoPixel LED ring.
  2. //
  3. // Copyright 2015 Google Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. #include <avr/interrupt.h>
  18. #include <string.h>
  19. #include "neopixel.h"
  20. #include "serial.h"
  21. /// Main application class.
  22. ///
  23. /// Reads commands from the serial port, processes them, and
  24. /// responds.
  25. ///
  26. class Ser2Neo {
  27. public:
  28. void init();
  29. void run();
  30. private:
  31. static const uint8_t EOF = '\r';
  32. static const uint8_t ESC = '+';
  33. int read_escaped();
  34. void read_leds();
  35. void wait_eol();
  36. void send_ok(const char* msg=nullptr);
  37. NeoPixel leds;
  38. Serial serial;
  39. };
  40. /// Initialise the hardware.
  41. void Ser2Neo::init() {
  42. leds.init();
  43. serial.init();
  44. }
  45. /// Read a character, de-escaping if required.
  46. int Ser2Neo::read_escaped() {
  47. auto got = serial.getch();
  48. if (got == EOF) {
  49. return -1;
  50. }
  51. if (got == ESC) {
  52. got = serial.getch();
  53. if (got == EOF) {
  54. return -1;
  55. }
  56. got ^= ESC;
  57. }
  58. return got;
  59. }
  60. /// Read the (potentially escaped) LED levels.
  61. void Ser2Neo::read_leds() {
  62. leds.clear();
  63. while (true) {
  64. int got = read_escaped();
  65. if (got < 0) {
  66. return;
  67. }
  68. leds.append(got);
  69. }
  70. }
  71. /// Wait until the end-of-line character is received.
  72. void Ser2Neo::wait_eol() {
  73. while (serial.getch() != EOF) {
  74. }
  75. }
  76. /// Send OK with an optional message.
  77. void Ser2Neo::send_ok(const char* msg) {
  78. wait_eol();
  79. serial.putstr("OK");
  80. if (msg != nullptr) {
  81. serial.putstr(" ");
  82. serial.putstr(msg);
  83. }
  84. serial.putstr("\n");
  85. }
  86. /// Run the main loop. Process and run commands.
  87. void Ser2Neo::run() {
  88. sei();
  89. serial.putstr("READY\n");
  90. for (;;) {
  91. switch (serial.getch()) {
  92. case '?':
  93. send_ok("ser2neo 1 neopixel 16 grb");
  94. break;
  95. case '!':
  96. send_ok("sync");
  97. break;
  98. case 'l':
  99. read_leds();
  100. leds.write();
  101. serial.putstr("OK\n");
  102. break;
  103. case EOF:
  104. break;
  105. default:
  106. wait_eol();
  107. serial.putstr("ERROR\n");
  108. break;
  109. }
  110. }
  111. }
  112. static Ser2Neo ser2led;
  113. int main() {
  114. ser2led.init();
  115. ser2led.run();
  116. return 0;
  117. }