- add LED control with PWM
[moodlight.git] / console.c
1 /* $Id: console.c,v 1.2 2010/06/26 19:08:18 simimeie Exp $
2  * Functions for a serial console.
3  */
4
5 #include <avr/io.h>
6 #include <avr/interrupt.h>
7 #include <avr/pgmspace.h>
8 #include <avr/version.h>
9 #include <util/delay.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "console.h"
13 #include "ledpwm.h"
14
15 /* PD0 is RXD, PD1 is TXD, but we don't need to address them manually */
16
17 /* Formula for calculating the value of UBRR from baudrate and cpufreq */
18 #define UBRRCALC ((CPUFREQ / (16 * BAUDRATE)) - 1)
19
20 #define INPUTBUFSIZE 30
21 static uint8_t inputbuf[INPUTBUFSIZE];
22 static uint8_t inputpos = 0;
23 #define OUTPUTBUFSIZE 400
24 static uint8_t outputbuf[OUTPUTBUFSIZE];
25 static uint16_t outputhead = 0; /* WARNING cannot be modified atomically */
26 static uint16_t outputtail = 0;
27 static uint8_t opinprog = 0;
28 static uint8_t escstatus = 0;
29 static prog_uint8_t CRLF[] = "\r\n";
30 static prog_uint8_t WELCOMEMSG[] = "\r\n"\
31                                    "\r\n ******************************************"\
32                                    "\r\n * HaWo Moodlight v0 (prototype)          *"\
33                                    "\r\n * (C) Michael 'PoempelFox' Meier 06/2010 *"\
34                                    "\r\n ******************************************"\
35                                    "\r\n"\
36                                    "\r\nProcessor: atmega328"\
37                                    "\r\nAVR-libc: " __AVR_LIBC_VERSION_STRING__ " (" __AVR_LIBC_DATE_STRING__ ")"\
38                                    "\r\nSoftware Version 0.1, Compiled " __DATE__ " " __TIME__;
39 static prog_uint8_t PROMPT[] = "\r\nroot@moodlight# ";
40
41 /* Handler for TXC (TX Complete) IRQ */
42 ISR(USART_TX_vect) {
43         if (outputhead == outputtail) { /* Nothing more to send! */
44                 opinprog = 0;
45         } else {
46                 UDR0 = outputbuf[outputhead];
47                 outputhead++;
48                 if (outputhead >= OUTPUTBUFSIZE) {
49                         outputhead = 0;
50                 }
51         }
52 }
53
54 #if defined __GNUC__
55 /* Unfortunately, gcc is very very dumb, and despite -Os tries to inline this
56  * function even though it is called about a thousand times, when only
57  * NETCONSOLE is defined, costing us more than 2.5 KiloBytes of Flash.
58  * Way to go, gcc "optimization". Though NETCONSOLE does not exist
59  * in this copy of the console code, it certainly can't hurt. */
60 static void appendchar(uint8_t what) __attribute__((noinline));
61 #endif /* __GNUC__ */
62 /* This can only be called safely with interrupts disabled - remember that! */
63 static void appendchar(uint8_t what) {
64   uint16_t newpos;
65   newpos = (outputtail + 1);
66   if (newpos >= OUTPUTBUFSIZE) {
67     newpos = 0;
68   }
69   if (newpos != outputhead) {
70     outputbuf[outputtail] = what;
71     outputtail = newpos;
72   }
73   if (!opinprog) {
74     /* Send the byte */
75     UDR0 = what;
76     outputhead++;
77     if (outputhead >= OUTPUTBUFSIZE) {
78       outputhead = 0;
79     }
80     opinprog = 1;
81   }
82 }
83
84 void console_printchar_noirq(uint8_t what) {
85   appendchar(what);
86 }
87
88 /* This can only be called safely with interrupts disabled - remember that! */
89 void console_printhex8_noirq(uint8_t what) {
90         uint8_t buf;
91         uint8_t i;
92         for (i=0; i<2; i++) {
93                 buf = (uint8_t)(what & (uint8_t)0xF0) >> 4;
94                 if (buf <= 9) {
95                         buf += '0';
96                 } else {
97                         buf += 'A' - 10;
98                 }
99                 appendchar(buf);
100                 what <<= 4;
101         }
102 }
103
104 /* This can only be called safely with interrupts disabled - remember that! */
105 void console_printdec_noirq(uint8_t what) {
106   uint8_t buf;
107   buf = what / 100;
108   appendchar(buf + '0');
109   what %= 100;
110   buf = what / 10;
111   appendchar(buf + '0');
112   buf = what % 10;
113   appendchar(buf + '0');
114 }
115
116 /* This can only be called safely with interrupts disabled - remember that! */
117 void console_printbin8_noirq(uint8_t what) {
118         uint8_t i;
119         for (i = 0; i < 8; i++) {
120                 if (what & 0x80) {
121                         appendchar('1');
122                 } else {
123                         appendchar('0');
124                 }
125                 what <<= 1;
126         }
127 }
128
129 /* This can only be called safely with interrupts disabled - remember that! */
130 static void console_printtext_noirq(const uint8_t * what) {
131         while (*what) {
132                 appendchar(*what);
133                 what++;
134         }
135 }
136
137 /* This can only be called safely with interrupts disabled - remember that! */
138 static void console_printpgm_noirq_P(PGM_P what) {
139         uint8_t t;
140         while ((t = pgm_read_byte(what++))) {
141                 appendchar(t);
142         }
143 }
144
145 /* These are wrappers for our internal functions, disabling IRQs before
146  * calling them. */
147 void console_printtext(const uint8_t * what) {
148         cli();
149         console_printtext_noirq(what);
150         sei();
151 }
152
153 void console_printpgm_P(PGM_P what) {
154         cli();
155         console_printpgm_noirq_P(what);
156         sei();
157 }
158
159 void console_printhex8(uint8_t what) {
160         cli();
161         console_printhex8_noirq(what);
162         sei();
163 }
164
165 void console_printdec(uint8_t what) {
166         cli();
167         console_printdec_noirq(what);
168         sei();
169 }
170
171 void console_init(void) {
172         /* Set Baud Rate */
173         UBRR0H = (uint8_t)(UBRRCALC >> 8);
174         UBRR0L = (uint8_t)(UBRRCALC);
175         /* Set 8 Bit mode - how braindead is it to default to 5 bit?! */
176         UCSR0C = /* _BV(URSEL) | */ _BV(UCSZ00) | _BV(UCSZ01);
177         /* Enable Send and Receive and IRQs */
178         UCSR0B = _BV(TXEN0) | _BV(RXEN0) | _BV(TXCIE0) | _BV(RXCIE0);
179         console_printpgm_noirq_P(WELCOMEMSG);
180         console_printpgm_noirq_P(PROMPT);
181         return;
182 }
183
184 /* Handler for RXC (RX Complete) IRQ. */
185 /* We do all query processing here. */
186 ISR(USART_RX_vect) {
187         uint8_t inpb;
188         
189         inpb = UDR0;
190         if (escstatus == 1) {
191                 if (inpb == '[') {
192                         escstatus = 2;
193                 } else {
194                         escstatus = 0;
195                         appendchar(7); /* Bell */
196                 }
197                 return;
198         }
199         if (escstatus == 2) {
200                 switch (inpb) {
201                 case 'A': /* Up */
202                         /* Try to restore the last comnmand */
203                         for (; inputpos > 0; inputpos--) {
204                                 appendchar(8);
205                         }
206                         while (inputbuf[inputpos]) {
207                                 appendchar(inputbuf[inputpos++]);
208                         }
209                         break;
210                 case 'B': /* Down */
211                         /* New empty command line */
212                         for (; inputpos > 0; inputpos--) {
213                                 appendchar(8);
214                                 appendchar(' ');
215                                 appendchar(8);
216                         }
217                         break;
218                 case 'C': /* Left */
219                 case 'D': /* Right */
220                 default:
221                         appendchar(7); /* Bell */
222                         break;
223                 };
224                 escstatus = 0;
225                 return;
226         }
227         /* escstatus == 0, i.e. not in an escape */
228         switch (inpb) {
229         case 0 ... 7: /* Nonprinting characters. Ignore. */
230         case 11 ... 12: /* Nonprinting characters. Ignore. */
231         case 14 ... 26: /* Nonprinting characters. Ignore. */
232         case 28 ... 31: /* Nonprinting characters. Ignore. */
233         case 0x7f ... 0xff: /* Nonprinting characters. Ignore. */
234                 console_printhex8_noirq(inpb);
235                 appendchar(7); /* Bell */
236                 break;
237         case 9: /* tab. Should be implemented some day? */
238                 appendchar(7); /* Bell */
239                 break;
240         case 27: /* Escape */
241                 escstatus = 1;
242                 break;
243         case 8: /* Backspace */
244                 if (inputpos > 0) {
245                         inputpos--;
246                         appendchar(inpb);
247                         appendchar(' ');
248                         appendchar(inpb);
249                 }
250                 break;
251         case '\r': /* 13 */
252         case '\n': /* 10 */
253                 if (inputpos == 0) {
254                         console_printpgm_noirq_P(PROMPT);
255                         break;
256                 }
257                 inputbuf[inputpos] = 0; /* Null-terminate the string */
258                 console_printpgm_noirq_P(CRLF);
259                 /* now lets see what it is */
260                 if        (strcmp_P(inputbuf, PSTR("help")) == 0) {
261                         console_printpgm_noirq_P(PSTR("Available commands:"));
262                         console_printpgm_noirq_P(PSTR("\r\n motd             repeat welcome message"));
263                         console_printpgm_noirq_P(PSTR("\r\n showpins [x]     shows the avrs inputpins"));
264                         console_printpgm_noirq_P(PSTR("\r\n status           show status / counters"));
265                         console_printpgm_noirq_P(PSTR("\r\n [rgb] n          sets the brightness of the red / green / blue LED to n"));
266                         /* console_printpgm_noirq_P(PSTR("\r\n save             saves settings to EEPROM")); */
267                 } else if (strcmp_P(inputbuf, PSTR("motd")) == 0) {
268                         console_printpgm_noirq_P(WELCOMEMSG);
269                 } else if (strncmp_P(inputbuf, PSTR("showpins"), 8) == 0) {
270                         uint8_t which = 0;
271                         uint8_t stal = 1; uint8_t endl = 4;
272                         if (inputpos == 10) {
273                                 switch (inputbuf[9]) {
274                                 case 'a':
275                                 case 'A':
276                                         which = 1; break;
277                                 case 'b':
278                                 case 'B':
279                                         which = 2; break;
280                                 case 'c':
281                                 case 'C':
282                                         which = 3; break;
283                                 case 'd':
284                                 case 'D':
285                                         which = 4; break;
286                                 };
287                         }
288                         if (which) {
289                                 stal = which;
290                                 endl = which;
291                         }
292                         for (which = stal; which <= endl; which++) {
293                                 uint8_t pstatus;
294                                 switch (which) {
295 /*                              case 1: pstatus = PINA;
296                                         console_printpgm_noirq_P(PSTR("PINA: 0x"));
297                                         break;*/
298                                 case 2: pstatus = PINB;
299                                         console_printpgm_noirq_P(PSTR("PINB: 0x"));
300                                         break;
301                                 case 3: pstatus = PINC;
302                                         console_printpgm_noirq_P(PSTR("PINC: 0x"));
303                                         break;
304                                 case 4: pstatus = PIND;
305                                         console_printpgm_noirq_P(PSTR("PIND: 0x"));
306                                         break;
307                                 default:
308                                         pstatus = 0;
309                                         console_printpgm_noirq_P(PSTR("NOPIN: 0x"));
310                                         break;
311                                 };
312                                 console_printhex8_noirq(pstatus);
313                                 appendchar(' ');
314                                 console_printbin8_noirq(pstatus);
315                                 if (which < endl) {
316                                         appendchar('\r'); appendchar('\n');
317                                 }
318                         }
319 /*              } else if (strcmp_P(inputbuf, PSTR("save")) == 0) {
320                         saveeepromsettings();
321                         console_printpgm_noirq_P(PSTR("Settings written to EEPROM."));*/
322                 } else if (strcmp_P(inputbuf, PSTR("status")) == 0) {
323                         console_printpgm_noirq_P(PSTR("Current status:\r\n"));
324                         console_printpgm_noirq_P(PSTR("none. nothing implemented yet."));
325                 } else if (strncmp_P(inputbuf, PSTR("r "), 2) == 0) {
326                         uint8_t v;
327                         v = strtoul(&inputbuf[2], NULL, 0);
328                         ledpwm_setled(LEDPWM_REDLED, v);
329                         console_printpgm_P(PSTR("brightness of RED LED set to 0x"));
330                         console_printhex8(v);
331                 } else if (strncmp_P(inputbuf, PSTR("g "), 2) == 0) {
332                         uint8_t v;
333                         v = strtoul(&inputbuf[2], NULL, 0);
334                         ledpwm_setled(LEDPWM_GREENLED, v);
335                         console_printpgm_P(PSTR("brightness of GREEN LED set to 0x"));
336                         console_printhex8(v);
337                 } else if (strncmp_P(inputbuf, PSTR("b "), 2) == 0) {
338                         uint8_t v;
339                         v = strtoul(&inputbuf[2], NULL, 0);
340                         ledpwm_setled(LEDPWM_BLUELED, v);
341                         console_printpgm_P(PSTR("brightness of BLUE LED set to 0x"));
342                         console_printhex8(v);
343 #ifdef JOKECMDS
344                 } else if (strncmp_P(inputbuf, PSTR("ls"), 2) == 0) {
345                         console_printpgm_noirq_P(PSTR("Total 4711\r\n"));
346                         console_printpgm_noirq_P(PSTR("----r-xr--  1 root root 42234223666 Dec 18  2004 duschcam1.avi"));
347                 } else if (strncmp_P(inputbuf, PSTR("cat"), 3) == 0) {
348                         if (inputpos > 4) {
349                                 console_printtext_noirq(&inputbuf[4]);
350                                 console_printpgm_noirq_P(PSTR(": Permission denied"));
351                         } else {
352                                 console_printpgm_noirq_P(PSTR("Usage: cat filename"));
353                         }
354 #endif /* JOKECMDS */
355                 } else {
356 #ifdef JOKECMDS
357                         console_printpgm_noirq_P(PSTR("bash: "));
358                         console_printtext_noirq(inputbuf);
359                         console_printpgm_noirq_P(PSTR(": command not found."));
360 #else /* JOKECMDS */
361                         console_printpgm_noirq_P(PSTR("Unknown command: "));
362                         console_printtext_noirq(inputbuf);
363 #endif /* JOKECMDS */
364                 }
365                 /* show PROMPT and go back to start. */
366                 console_printpgm_noirq_P(PROMPT);
367                 inputpos = 0;
368                 break;
369         default:
370                 if (inputpos < (INPUTBUFSIZE - 1)) { /* -1 for terminating \0 */
371                         inputbuf[inputpos++] = inpb;
372                         /* Echo the character */
373                         appendchar(inpb);
374                 } else {
375                         appendchar(7); /* Bell */
376                 }
377         };
378 }
This page took 0.065596 seconds and 3 git commands to generate.