Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

LED-Steuerung implementiert, Ausgabe aufs richtige UART schicken

  • Loading branch information...
commit 17e1bd4a05c6df44c9add250bb04294eb7dc72d2 1 parent 63ab428
Michael Stapelberg authored

Showing 1 changed file with 34 additions and 13 deletions. Show diff stats Hide diff stats

  1. +34 13 aussen-neu/main.c
47 aussen-neu/main.c
@@ -22,6 +22,8 @@
22 22 /* Nach 40ms erkanntem Druck wird eine Taste als gedrückt erkannt */
23 23 #define DEBOUNCE_MS 40
24 24
  25 +static uint16_t led_state[4] = { 0, 0, 0, 0 };
  26 +
25 27 static struct lookup_entry {
26 28 uint8_t state;
27 29 uint8_t debounce;
@@ -83,6 +85,15 @@ static void uart2_puts(char *str) {
83 85 }
84 86 }
85 87
  88 +static void uart_puts(char *str) {
  89 + char *walk;
  90 + for (walk = str; *walk != '\0'; walk++) {
  91 + while ( !( UCSR0A & (1<<UDRE0)) );
  92 + UDR0 = (unsigned char)*walk;
  93 + }
  94 +}
  95 +
  96 +
86 97 /*
87 98 * Timer-Interrupt. Wird alle 1ms getriggert.
88 99 *
@@ -93,6 +104,12 @@ ISR(TIMER0_OVF_vect) {
93 104 /* Summer deaktivieren */
94 105 PORTB &= ~(1 << PB4);
95 106
  107 + int idx;
  108 + for (idx = 0; idx < 4; idx++) {
  109 + if (led_state[idx] > 0 && --(led_state[idx]) == 0)
  110 + PORTB &= ~(1 << idx);
  111 + }
  112 +
96 113 /* Debouncing */
97 114 uint8_t sample = (PINA | (1 << 3));
98 115 uint8_t c;
@@ -157,22 +174,26 @@ static void handle_command(char *buffer) {
157 174 if (strncmp(buffer, "^PING", strlen("^PING")) == 0) {
158 175 /* Modify the buffer in place and send it back */
159 176 buffer[2] = 'O';
160   - uart2_puts(buffer);
  177 + uart_puts(buffer);
161 178 return;
162 179 }
163 180
  181 + // example:
  182 + // ^LED 0 250 $ (rot oben)
  183 + // ^LED 1 250 $ (grün oben)
  184 + // (beides zusammen ergibt orange)
  185 + // ^LED 2 250 $ (rot unten)
  186 + // ^LED 3 250 $ (grün unten)
  187 + // ^BEEP 1 $
164 188 if (strncmp(buffer, "^LED", strlen("^LED")) == 0) {
165   - //int idx = (buffer[5] == '1' ? 0 : 1);
166   - //int state;
167   - //if (buffer[7] == '1')
168   - // state = 1;
169   - //else if (buffer[7] == '2')
170   - // state = 2;
171   - //else state = 3;
172   - //led_state[idx] = state;
173   -
174   - //DDRC |= (1 << (PC1 + idx));
175   - //PORTC |= (1 << (PC1 + idx));
  189 + int idx = (buffer[5] - '0');
  190 + if (idx < 0 || idx > 3)
  191 + return;
  192 +
  193 + if (sscanf(buffer + strlen("^LED 0 "), "%u", &(led_state[idx])) != 1)
  194 + return;
  195 +
  196 + PORTB |= (1 << idx);
176 197 }
177 198 }
178 199
@@ -253,7 +274,7 @@ int main() {
253 274 lookup_table[c].debounce != DEBOUNCE_MS)
254 275 continue;
255 276 keypress_buffer[5] = lookup_table[c].key;
256   - uart2_puts(keypress_buffer);
  277 + uart_puts(keypress_buffer);
257 278 lookup_table[c].debounce = DEBOUNCE_MS+1;
258 279 }
259 280 }

0 comments on commit 17e1bd4

Please sign in to comment.
Something went wrong with that request. Please try again.