123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- /* 7 rows LED bar firmware / main program for Arduino Nano
- * Copyright (C) 2019 asdrea
- *
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "ledbar.h"
- enum {
- CMD_NOSCROLL,
- CMD_HSCROLL,
- CMD_VSCROLL,
- CMD_SPACING
- };
- enum {
- ALIGN_LEFT,
- ALIGN_RIGHT,
- ALIGN_CENTER
- };
- static String text;
- static int16_t text_pos = 0; // q11.5
- static int8_t scroll_speed = 0, scroll_mode = CMD_NOSCROLL, text_align = ALIGN_LEFT;
- static uint8_t text_spacing = 1;
- static uint8_t cmd_mode = 0;
- void setup()
- {
- Serial.begin(9600);
- ledbar_init();
- }
- void loop()
- {
- int16_t p;
- while(Serial.available() > 0) {
- uint8_t c = Serial.read();
- if(cmd_mode) {
- switch(c >> 6) {
- case CMD_NOSCROLL:
- scroll_mode = CMD_NOSCROLL;
- text_align = c & 0x3f;
- text_pos = 0;
- break;
- case CMD_VSCROLL:
- scroll_mode = CMD_VSCROLL;
- scroll_speed = (c & 0x3f) << 2;
- break;
- case CMD_HSCROLL:
- scroll_mode = CMD_HSCROLL;
- scroll_speed = (c & 0x3f) << 2;
- break;
- case CMD_SPACING:
- text_spacing = c & 0x3f;
- break;
- }
- cmd_mode = 0;
- continue;
- }
-
- if(c == '\0') {
- cmd_mode = 1;
- continue;
- }
- //Serial.write(c);
- if(c == '\n' || c == '\r') {
- text = "";
- }
- else if(c == 8 || c == 127) {
- if(text.length() > 0)
- text = text.substring(0, text.length() - 1);
- }
- else
- text += (char)c;
- }
- switch(scroll_mode) {
- default:
- case CMD_NOSCROLL:
- switch(text_align) {
- default:
- case ALIGN_LEFT:
- p = 0;
- break;
- case ALIGN_RIGHT:
- p = (5 + text_spacing) * (LEDBAR_COLS - (int16_t)text.length());
- break;
- case ALIGN_CENTER:
- p = LEDBAR_COLS / 2 - (5 + text_spacing) * ((int16_t)text.length() / 2);
- break;
- }
- break;
- case CMD_HSCROLL:
- text_pos += scroll_speed;
- p = text_pos / 32;
- if(scroll_speed < 0) {
- if(p < (-(5 + text_spacing) * (int16_t)text.length())) {
- p = LEDBAR_COLS;
- text_pos = p * 32;
- }
- }
- else {
- if(p > LEDBAR_COLS) {
- p = -(5 + text_spacing) * (int16_t)text.length();
- text_pos = p * 32;
- }
- }
- break;
- case CMD_VSCROLL:
- // Implement me please :'(
- break;
- }
- ledbar_wait_vsync();
- ledbar_clear(LEDBAR_BACKBUF);
- ledbar_settext(LEDBAR_BACKBUF, p, text.c_str(), text_spacing);
- ledbar_swap_buf();
- }
|