summaryrefslogtreecommitdiff
path: root/firmware/blinkyfications.cpp
blob: 2bdea1fccd75a6ced4069114f3c73e7e701139ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * blinkyfication
 *
 *
 * Copyright (C) 2016 Christian Pointner <equinox@spreadspace.org>
 *
 * This file is part of blinkyfication.
 *
 * blinkyfication is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation.
 *
 * blinkyfication 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 blinkyfication. If not, see <http://www.gnu.org/licenses/>.
 */

#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <stdio.h>

#include "util.h"
#include "usbio.h"

#include "Arduino.h"
#include "FastLED.h"


#define NUM_LEDS 8
#define DATA_PIN 1    // PB1 @ Atmega32U2

#define _10MS_ ((uint16_t)625) // 10ms @ 16MHz / Prescaler 1:256

CRGB leds[NUM_LEDS];

void fastled_init()
{
  arduino_init();
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
}

void timer_init()
{
  TCCR1A = 0;
  TCCR1B = 0;
  TCCR1C = 0;

  OCR1A = 0xFFFF;
  TCNT1 = 0;
  TIMSK1 = (1 << OCIE1A);
}

void timer_start(uint16_t timeout)
{
  OCR1A = timeout;
  TCNT1 = 0;
  TCCR1B = (1 << CS12);
}

ISR(TIMER1_COMPA_vect)
{
  TCCR1B = 0;
  fill_solid(leds, NUM_LEDS, CRGB::Black);
  FastLED.show();
}

void handle_cmd(uint8_t cmd)
{
  switch(cmd) {
  case '0': fill_solid(leds, NUM_LEDS, CRGB::Black);       FastLED.show(); break;
  case '1': fill_solid(leds, NUM_LEDS, CRGB::White);       FastLED.show(); break;
  case 'r': fill_solid(leds, NUM_LEDS, CRGB::Red);         FastLED.show(); break;
  case 'g': fill_solid(leds, NUM_LEDS, CRGB::Green);       FastLED.show(); break;
  case 'b': fill_solid(leds, NUM_LEDS, CRGB::Blue);        FastLED.show(); break;
  case 'w': fill_rainbow(leds, NUM_LEDS, 0, 256/NUM_LEDS); FastLED.show(); break;

  case '.': fill_solid(leds, NUM_LEDS, CRGB::White); FastLED.show(); timer_start(10 * _10MS_);  break;
  case 'o': fill_solid(leds, NUM_LEDS, CRGB::White); FastLED.show(); timer_start(30 * _10MS_);  break;
  case 'O': fill_solid(leds, NUM_LEDS, CRGB::White); FastLED.show(); timer_start(100 * _10MS_); break;

  case 'R': fill_solid(leds, NUM_LEDS, CRGB::Red);   FastLED.show(); timer_start(10 * _10MS_); break;
  case 'G': fill_solid(leds, NUM_LEDS, CRGB::Green); FastLED.show(); timer_start(10 * _10MS_); break;
  case 'B': fill_solid(leds, NUM_LEDS, CRGB::Blue);  FastLED.show(); timer_start(10 * _10MS_); break;

  case '!': reset2bootloader(); break;
  }
}

int main(void)
{
  MCUSR &= ~(1 << WDRF);
  wdt_disable();

  cpu_init();
  usbio_init();
  fastled_init();
  sei();

  timer_init();
  fill_solid(leds, NUM_LEDS, CRGB::Black);
  FastLED.show();
  for(;;) {
    int16_t BytesReceived = usbio_bytes_received();
    while(BytesReceived > 0) {
      int ReceivedByte = fgetc(stdin);
      if(ReceivedByte != EOF) {
        handle_cmd(ReceivedByte);
      }
      BytesReceived--;
    }

    usbio_task();
  }
}