sketch_nov14a.ino


1
#include <Wire.h>  // auch einmal auskommentieren wegen DWARF Error
2
3
const byte BUFLEN = 20;
4
char readData[BUFLEN];
5
6
const char *INIT_COMMAND = "re";
7
const char *TABLE_LAYOUT_COMMAND = "tl";
8
const char *HIGHLIGHT_BOX_COMMAND = "hb";
9
const char *DELIMITER = "|";
10
11
const byte ANZAHL_LEDS = 8;
12
const byte pin_indices[ANZAHL_LEDS] = {9, 8, 7, 6, 5, 4, 3, 2};
13
14
void setup()
15
{
16
  for (byte i = 0; i < ANZAHL_LEDS; i++) {
17
    pinMode(pin_indices[i], OUTPUT);
18
  }
19
20
  TCCR1B = 0;
21
  TCCR1A = 0;
22
  TIMSK1 = 0;
23
  TCCR1B = _BV(CS12) | _BV(CS10);
24
25
  Serial.begin(9600);
26
}
27
28
void loop()
29
{
30
  if (readSerial() ) {
31
    processReadData();
32
  }
33
}
34
35
36
bool readSerial()
37
{
38
  bool state = false;
39
  static byte index = 0;
40
41
  while (Serial.available() )
42
  {
43
    char c = Serial.read();
44
45
    if (c >= 32 && (index < BUFLEN - 1) ) {
46
      readData[index++] = c;
47
    }
48
    else if (c == '\n') {
49
      readData[index] = '\0';   // terminate the string
50
      index = 0;
51
      state = true;
52
    }
53
  }
54
  return state;
55
}
56
57
void resetTimer()
58
{
59
  noInterrupts();
60
  TCNT1 = 18661;
61
  TIMSK1 = _BV(TOIE1);
62
  interrupts();
63
}
64
65
ISR(TIMER1_OVF_vect)
66
{
67
  resetLEDs();
68
  TIMSK1 = 0;
69
}
70
71
void resetLEDs()
72
{
73
  for (byte i = 0; i < ANZAHL_LEDS; i++) {
74
    digitalWrite(pin_indices[i], LOW);
75
  }
76
}
77
78
void setLEDs(const byte input)
79
{
80
  for (byte i = 0; i < ANZAHL_LEDS; i++) {
81
    byte mask = 1 << i;
82
83
    if (mask & input) {
84
      digitalWrite(pin_indices[i], HIGH);
85
    }
86
    else {
87
      digitalWrite(pin_indices[i], LOW);
88
    }
89
  }
90
}
91
92
void processReadData()
93
{
94
  char *token = strtok(readData, DELIMITER);
95
96
  if (token != NULL) {
97
    if (strcmp(token, INIT_COMMAND) == 0) {
98
      Serial.write("bin\n");
99
      return;
100
    }
101
102
    if (strcmp(token, HIGHLIGHT_BOX_COMMAND) == 0) {
103
      token = strtok(NULL, DELIMITER);
104
      setLEDs(atoi(token));
105
      resetTimer();
106
    }
107
  }
108
}